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): def get_random_solution(previous, data):
solution = replace_worst_element(previous, data) candidates = []
while solution["distance"].sum() <= previous["distance"].sum(): candidates.append(previous)
if solution.equals(previous): solution, worst_index = replace_worst_element(previous, data)
break previous_worst_distance = previous["distance"].loc[worst_index]
solution = replace_worst_element(previous=solution, data=data) 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 return solution