Compare commits
2 Commits
b63b5b08b6
...
bf7ca7f520
Author | SHA1 | Date |
---|---|---|
coolneng | bf7ca7f520 | |
coolneng | 75c3a94fbe |
|
@ -25,16 +25,25 @@ def get_different_element(original, row):
|
|||
return row.source
|
||||
|
||||
|
||||
def get_furthest_element(element, data):
|
||||
def get_closest_element(element, data):
|
||||
element_df = data.query(f"source == {element} or destination == {element}")
|
||||
furthest_index = element_df["distance"].astype(float).idxmax()
|
||||
furthest_row = data.iloc[furthest_index]
|
||||
furthest_point = get_different_element(original=element, row=furthest_row)
|
||||
return Series(data={"point": furthest_point, "distance": furthest_row["distance"]})
|
||||
closest_index = element_df["distance"].astype(float).idxmin()
|
||||
closest_row = data.loc[closest_index]
|
||||
closest_point = get_different_element(original=element, row=closest_row)
|
||||
return Series(data={"point": closest_point, "distance": closest_row["distance"]})
|
||||
|
||||
|
||||
def remove_solution_dataset(data, solution):
|
||||
return data.query(f"source != {solution} and destination != {solution}")
|
||||
def explore_solutions(solutions, data):
|
||||
closest_elements = solutions["point"].apply(func=get_closest_element, data=data)
|
||||
furthest_index = closest_elements["distance"].astype(float).idxmax()
|
||||
return closest_elements.iloc[furthest_index]
|
||||
|
||||
|
||||
def remove_duplicates(current, previous, data):
|
||||
data = data.query(
|
||||
f"(source != {current} or destination not in @previous) and (source not in @previous or destination != {current})"
|
||||
)
|
||||
return data
|
||||
|
||||
|
||||
def greedy_algorithm(n, m, data):
|
||||
|
@ -42,10 +51,11 @@ def greedy_algorithm(n, m, data):
|
|||
first_solution = get_first_solution(n, data)
|
||||
solutions = solutions.append(first_solution, ignore_index=True)
|
||||
for _ in range(m):
|
||||
last_solution = int(solutions["point"].tail(n=1))
|
||||
centroid = get_furthest_element(element=last_solution, data=data)
|
||||
solutions = solutions.append(centroid, ignore_index=True)
|
||||
data = remove_solution_dataset(data=data, solution=last_solution)
|
||||
element = explore_solutions(solutions, data)
|
||||
solutions = solutions.append(element)
|
||||
data = remove_duplicates(
|
||||
current=element["point"], previous=solutions["point"], data=data
|
||||
)
|
||||
return solutions
|
||||
|
||||
|
||||
|
|
Loading…
Reference in New Issue