From dfdd142fdb02b6ec77b8d0b8ae6e810b92cf26ad Mon Sep 17 00:00:00 2001 From: coolneng Date: Wed, 19 May 2021 19:20:27 +0200 Subject: [PATCH] Add correct index to the solutions list --- src/greedy.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/greedy.py b/src/greedy.py index a779c35..2c74a82 100644 --- a/src/greedy.py +++ b/src/greedy.py @@ -29,10 +29,12 @@ def get_closest_element(element, data): return Series(data={"point": closest_point, "distance": closest_row["distance"]}) -def explore_solutions(solutions, data): +def explore_solutions(solutions, data, index): 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] + solution = closest_elements.iloc[furthest_index] + solution.name = index + return solution def remove_duplicates(current, previous, data): @@ -46,8 +48,8 @@ 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 - 1): - element = explore_solutions(solutions, data) + for iteration in range(m - 1): + element = explore_solutions(solutions, data, index=iteration + 1) solutions = solutions.append(element) data = remove_duplicates( current=element["point"], previous=solutions["point"], data=data