From 028db4ba91fb66fe828691c41007b082f189ed65 Mon Sep 17 00:00:00 2001 From: coolneng Date: Fri, 16 Apr 2021 20:06:13 +0200 Subject: [PATCH] Return best solution or the same if there's none --- src/local_search.py | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/src/local_search.py b/src/local_search.py index a30f5e8..66e7ae9 100644 --- a/src/local_search.py +++ b/src/local_search.py @@ -15,11 +15,16 @@ def replace_worst_element(previous, data): def get_random_solution(previous, data): - solution = replace_worst_element(previous, data) - while solution["distance"].sum() <= previous["distance"].sum(): - if solution.equals(previous): - break - solution = replace_worst_element(previous=solution, data=data) + candidates = [] + candidates.append(previous) + solution, worst_index = replace_worst_element(previous, data) + previous_worst_distance = previous["distance"].loc[worst_index] + while candidates[-1].distance.loc[worst_index] <= previous_worst_distance: + last_solution = candidates[-1] + solution, _ = replace_worst_element(previous=solution, data=data) + if solution.equals(last_solution): + return last_solution + candidates.append(solution) return solution