From 6b67e6db342e8f75b58315b248e5ccc9a65bb080 Mon Sep 17 00:00:00 2001 From: coolneng Date: Tue, 20 Apr 2021 18:14:41 +0200 Subject: [PATCH] Craft duplicate check before insert --- src/local_search.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/local_search.py b/src/local_search.py index 8cff412..3567ba6 100644 --- a/src/local_search.py +++ b/src/local_search.py @@ -7,11 +7,18 @@ def get_first_random_solution(m, data): return data.loc[random_indexes] +def element_in_dataframe(solution, element): + duplicates = solution.query( + f"(source == {element.source} and destination == {element.destination}) or (source == {element.destination} and destination == {element.source})" + ) + return not duplicates.empty + + def replace_worst_element(previous, data): solution = previous.copy() worst_index = solution["distance"].astype(float).idxmin() random_element = data.sample().squeeze() - while solution.isin(random_element.values.ravel()).any().any(): + while element_in_dataframe(solution=solution, element=random_element): random_element = data.sample().squeeze() solution.loc[worst_index] = random_element return solution, worst_index