Return the best solution when you can't explore

This commit is contained in:
coolneng 2021-04-19 15:51:39 +02:00
parent 2eb84a6883
commit 8b5029645f
Signed by: coolneng
GPG Key ID: 9893DA236405AF57
1 changed files with 16 additions and 7 deletions

View File

@ -17,26 +17,35 @@ def replace_worst_element(previous, data):
return solution, worst_index
def choose_best_solution(previous, current, index):
if previous.loc[index].distance >= current.loc[index].distance:
return previous
return current
def get_random_solution(previous, 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]
last_solution = candidates[-1]
while last_solution.distance.loc[worst_index] <= previous_worst_distance:
solution, _ = replace_worst_element(previous=solution, data=data)
if solution.equals(last_solution):
return last_solution
best_solution = choose_best_solution(
previous=previous, current=solution, index=worst_index
)
return best_solution
candidates.append(solution)
return solution
last_solution = candidates[-1]
return last_solution
def explore_neighbourhood(element, data, max_iterations=100000):
neighbourhood = []
neighbourhood.append(element)
for _ in range(max_iterations):
for i in range(max_iterations):
print(f"Iteration {i}")
previous_solution = neighbourhood[-1]
neighbour = get_random_solution(previous=previous_solution, data=data)
if neighbour.equals(previous_solution):