From 3e08b67371f01947f73a630e15bc50801936d163 Mon Sep 17 00:00:00 2001 From: coolneng Date: Fri, 16 Apr 2021 20:05:45 +0200 Subject: [PATCH] Simplify worst element replacement --- src/local_search.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/local_search.py b/src/local_search.py index 001ceb6..a30f5e8 100644 --- a/src/local_search.py +++ b/src/local_search.py @@ -1,4 +1,4 @@ -from numpy.random import choice, randint, seed +from numpy.random import choice, seed def get_first_random_solution(m, data): @@ -9,10 +9,9 @@ def get_first_random_solution(m, data): def replace_worst_element(previous, data): solution = previous.copy() - worst_index = previous["distance"].astype(float).idxmin() - random_candidate = data.loc[randint(low=0, high=len(data.index))] - solution.loc[worst_index] = random_candidate - return solution + worst_index = solution["distance"].astype(float).idxmin() + solution.loc[worst_index] = data.sample(random_state=42).squeeze() + return solution, worst_index def get_random_solution(previous, data):