From 5e6a6d00e9ba28cff6663ced1e9537ed580dbe5b Mon Sep 17 00:00:00 2001 From: coolneng Date: Fri, 16 Apr 2021 20:07:31 +0200 Subject: [PATCH] Remove duplicates in local search --- src/local_search.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/local_search.py b/src/local_search.py index 66e7ae9..8377654 100644 --- a/src/local_search.py +++ b/src/local_search.py @@ -28,12 +28,20 @@ def get_random_solution(previous, data): return solution +def remove_duplicates(element, data): + duplicate_free_df = data.query( + "(source not in @element.source) or (destination not in @element.destination)" + ) + return duplicate_free_df + + def explore_neighbourhood(element, data, max_iterations=100000): neighbourhood = [] neighbourhood.append(element) for _ in range(max_iterations): previous_solution = neighbourhood[-1] neighbour = get_random_solution(previous=previous_solution, data=data) + data = remove_duplicates(element=neighbour, data=data) if neighbour.equals(previous_solution): break neighbourhood.append(neighbour)