From 85e6b072c64b793763048dedcb8665bbd6f9388a Mon Sep 17 00:00:00 2001 From: coolneng Date: Sun, 11 Apr 2021 22:07:57 +0200 Subject: [PATCH] Implement furthest element computation --- src/processing.py | 33 +++++++++++++++++++++------------ 1 file changed, 21 insertions(+), 12 deletions(-) diff --git a/src/processing.py b/src/processing.py index 277173b..dd62d74 100644 --- a/src/processing.py +++ b/src/processing.py @@ -1,16 +1,8 @@ from preprocessing import parse_file -from pandas import DataFrame, Series +from pandas import DataFrame from sys import argv -def get_furthest_element(element, data): - element_df = data.query(f"source == {element} or destination == {element}") - furthest_index = element_df["distance"].idxmax() - furthest_row = data.iloc[furthest_index] - print(furthest_row) - return furthest_row - - def get_first_solution(n, data): distance_sum = DataFrame(columns=["point", "distance"]) for element in range(n): @@ -24,16 +16,33 @@ def get_first_solution(n, data): return furthest_row +def get_different_element(original, row): + if row.source == original: + return row.destination + return row.source + + +def get_furthest_element(element, data): + element_df = data.query(f"source == {element} or destination == {element}") + furthest_index = element_df["distance"].idxmax() + furthest_row = data.iloc[furthest_index] + furthest_point = get_different_element(original=element, row=furthest_row) + return {"point": furthest_point, "distance": furthest_row["distance"]} + + def greedy_algorithm(n, m, data): solutions = DataFrame(columns=["point", "distance"]) first_solution = get_first_solution(n, data) solutions = solutions.append(first_solution, ignore_index=True) for _ in range(m): - centroid = solutions.apply(get_furthest_element, 1, data) - solutions = solutions.append(centroid) + last_solution = solutions["point"].tail(n=1) + centroid = get_furthest_element(element=int(last_solution), data=data) + solutions = solutions.append(dict(centroid), ignore_index=True) + data = data.drop(centroid["point"], columns=["source", "destination"]) + print(solutions) -# NOTE In each step, switch the element that gives the least amount +# NOTE In each step, switch to the element that gives the least amount def local_search(): pass