Compare commits

..

No commits in common. "bf7ca7f520a4ac1ec09d331e40332f2d21eebd0f" and "b63b5b08b6ff62b5ce0451dfa6be27a6510fc803" have entirely different histories.

1 changed files with 11 additions and 21 deletions

View File

@ -25,25 +25,16 @@ def get_different_element(original, row):
return row.source
def get_closest_element(element, data):
def get_furthest_element(element, data):
element_df = data.query(f"source == {element} or destination == {element}")
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"]})
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"]})
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 remove_solution_dataset(data, solution):
return data.query(f"source != {solution} and destination != {solution}")
def greedy_algorithm(n, m, data):
@ -51,11 +42,10 @@ 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):
element = explore_solutions(solutions, data)
solutions = solutions.append(element)
data = remove_duplicates(
current=element["point"], previous=solutions["point"], data=data
)
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)
return solutions