Compare commits

...

2 Commits

Author SHA1 Message Date
coolneng bf7ca7f520
Remove duplicates in an efficient way 2021-04-13 22:44:31 +02:00
coolneng 75c3a94fbe
Change metric in Greedy algorithm 2021-04-13 22:44:17 +02:00
1 changed files with 21 additions and 11 deletions

View File

@ -25,16 +25,25 @@ def get_different_element(original, row):
return row.source 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}") element_df = data.query(f"source == {element} or destination == {element}")
furthest_index = element_df["distance"].astype(float).idxmax() closest_index = element_df["distance"].astype(float).idxmin()
furthest_row = data.iloc[furthest_index] closest_row = data.loc[closest_index]
furthest_point = get_different_element(original=element, row=furthest_row) closest_point = get_different_element(original=element, row=closest_row)
return Series(data={"point": furthest_point, "distance": furthest_row["distance"]}) return Series(data={"point": closest_point, "distance": closest_row["distance"]})
def remove_solution_dataset(data, solution): def explore_solutions(solutions, data):
return data.query(f"source != {solution} and destination != {solution}") 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): def greedy_algorithm(n, m, data):
@ -42,10 +51,11 @@ def greedy_algorithm(n, m, data):
first_solution = get_first_solution(n, data) first_solution = get_first_solution(n, data)
solutions = solutions.append(first_solution, ignore_index=True) solutions = solutions.append(first_solution, ignore_index=True)
for _ in range(m): for _ in range(m):
last_solution = int(solutions["point"].tail(n=1)) element = explore_solutions(solutions, data)
centroid = get_furthest_element(element=last_solution, data=data) solutions = solutions.append(element)
solutions = solutions.append(centroid, ignore_index=True) data = remove_duplicates(
data = remove_solution_dataset(data=data, solution=last_solution) current=element["point"], previous=solutions["point"], data=data
)
return solutions return solutions