From 097ed9b52a1396e021abf3090de78afbc9915372 Mon Sep 17 00:00:00 2001 From: coolneng Date: Mon, 19 Apr 2021 18:25:53 +0200 Subject: [PATCH] Remove redundant previous solution checks --- src/local_search.py | 26 +++++--------------------- 1 file changed, 5 insertions(+), 21 deletions(-) diff --git a/src/local_search.py b/src/local_search.py index cd64486..bf11982 100644 --- a/src/local_search.py +++ b/src/local_search.py @@ -17,28 +17,12 @@ 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] - last_solution = candidates[-1] - while last_solution.distance.loc[worst_index] <= previous_worst_distance: + while solution.distance.loc[worst_index] <= previous_worst_distance: solution, _ = replace_worst_element(previous=solution, data=data) - if solution.equals(last_solution): - best_solution = choose_best_solution( - previous=previous, current=solution, index=worst_index - ) - return best_solution - candidates.append(solution) - last_solution = candidates[-1] - return last_solution + return solution def explore_neighbourhood(element, data, max_iterations=100000): @@ -48,13 +32,13 @@ def explore_neighbourhood(element, data, max_iterations=100000): print(f"Iteration {i}") previous_solution = neighbourhood[-1] neighbour = get_random_solution(previous=previous_solution, data=data) - if neighbour.equals(previous_solution): - break neighbourhood.append(neighbour) return neighbour def local_search(m, data): first_solution = get_first_random_solution(m=m, data=data) - best_solution = explore_neighbourhood(element=first_solution, data=data) + best_solution = explore_neighbourhood( + element=first_solution, data=data, max_iterations=100 + ) return best_solution