From 27f3baca07ce2df2e5169799a5b73b7a9290e76b Mon Sep 17 00:00:00 2001 From: coolneng Date: Mon, 22 Mar 2021 19:36:47 +0100 Subject: [PATCH] Compute the first element for the greedy algorithm --- src/processing.py | 29 +++++++++++++++++++++++++---- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/src/processing.py b/src/processing.py index 4336bf1..277173b 100644 --- a/src/processing.py +++ b/src/processing.py @@ -1,6 +1,5 @@ from preprocessing import parse_file -from pandas import DataFrame -from secrets import randbelow +from pandas import DataFrame, Series from sys import argv @@ -8,16 +7,37 @@ 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): + element_df = data.query(f"source == {element} or destination == {element}") + distance = element_df["distance"].sum() + distance_sum = distance_sum.append( + {"point": element, "distance": distance}, ignore_index=True + ) + furthest_index = distance_sum["distance"].idxmax() + furthest_row = distance_sum.iloc[furthest_index] return furthest_row def greedy_algorithm(n, m, data): - solutions = DataFrame(columns=["source", "destination", "distance"]) + 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 = get_furthest_element(element=randbelow(n), data=data) + centroid = solutions.apply(get_furthest_element, 1, data) solutions = solutions.append(centroid) +# NOTE In each step, switch the element that gives the least amount +def local_search(): + pass + + def usage(argv): print(f"Usage: python {argv[0]} ") exit(1) @@ -27,6 +47,7 @@ def main(): if len(argv) != 2: usage(argv) n, m, data = parse_file(argv[1]) + greedy_algorithm(n, m, data) if __name__ == "__main__":