Return best solution or the same if there's none

This commit is contained in:
coolneng 2021-04-16 20:06:13 +02:00
parent 3e08b67371
commit 028db4ba91
Signed by: coolneng
GPG Key ID: 9893DA236405AF57
1 changed files with 10 additions and 5 deletions

View File

@ -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