From bf7ca7f520a4ac1ec09d331e40332f2d21eebd0f Mon Sep 17 00:00:00 2001 From: coolneng Date: Tue, 13 Apr 2021 22:44:31 +0200 Subject: [PATCH] Remove duplicates in an efficient way --- src/processing.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/processing.py b/src/processing.py index f9a311c..013fcb1 100644 --- a/src/processing.py +++ b/src/processing.py @@ -39,6 +39,13 @@ def explore_solutions(solutions, data): 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): solutions = DataFrame(columns=["point", "distance"]) first_solution = get_first_solution(n, data) @@ -46,6 +53,9 @@ def greedy_algorithm(n, m, data): for _ in range(m): element = explore_solutions(solutions, data) solutions = solutions.append(element) + data = remove_duplicates( + current=element["point"], previous=solutions["point"], data=data + ) return solutions