From e3c55ca89f4059516d8464785f5cf1feacab0b77 Mon Sep 17 00:00:00 2001 From: coolneng Date: Thu, 15 Apr 2021 20:05:15 +0200 Subject: [PATCH] Refactor random solution generation --- src/processing.py | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/src/processing.py b/src/processing.py index 0539c7a..1b69943 100644 --- a/src/processing.py +++ b/src/processing.py @@ -64,17 +64,21 @@ def get_first_random_solution(m, data): return data.iloc[random_indexes] -def get_random_solution(previous, data): +def replace_worst_element(previous, data): solution = previous.copy() worst_index = previous["distance"].astype(float).idxmin() - worst_element = previous["distance"].loc[worst_index] random_candidate = data.loc[randint(low=0, high=len(data.index))] - while solution["distance"].loc[worst_index] <= worst_element: - if random_candidate["distance"] not in solution["distance"].values: - solution.loc[worst_index] = random_candidate - else: - return solution, True - return solution, False + solution.loc[worst_index] = random_candidate + return solution + + +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) + return solution def explore_neighbourhood(element, data, max_iterations=100000):