Refactor random solution generation

This commit is contained in:
coolneng 2021-04-15 20:05:15 +02:00
parent 98a86a97c0
commit e3c55ca89f
Signed by: coolneng
GPG Key ID: 9893DA236405AF57
1 changed files with 12 additions and 8 deletions

View File

@ -64,17 +64,21 @@ def get_first_random_solution(m, data):
return data.iloc[random_indexes] return data.iloc[random_indexes]
def get_random_solution(previous, data): def replace_worst_element(previous, data):
solution = previous.copy() solution = previous.copy()
worst_index = previous["distance"].astype(float).idxmin() 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))] random_candidate = data.loc[randint(low=0, high=len(data.index))]
while solution["distance"].loc[worst_index] <= worst_element: solution.loc[worst_index] = random_candidate
if random_candidate["distance"] not in solution["distance"].values: return solution
solution.loc[worst_index] = random_candidate
else:
return solution, True def get_random_solution(previous, data):
return solution, False 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): def explore_neighbourhood(element, data, max_iterations=100000):