Compare commits

..

No commits in common. "d5f9ed4ffb3b798c6706b7b38ce5867950cd8440" and "98a86a97c04caf4b2cb292d5fe98653ba954c0ce" have entirely different histories.

1 changed files with 11 additions and 18 deletions

View File

@ -64,32 +64,25 @@ def get_first_random_solution(m, data):
return data.iloc[random_indexes] return data.iloc[random_indexes]
def replace_worst_element(previous, data): def get_random_solution(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:
if random_candidate["distance"] not in solution["distance"].values:
solution.loc[worst_index] = random_candidate solution.loc[worst_index] = random_candidate
return solution else:
return solution, True
return solution, False
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): def explore_neighbourhood(element, data, max_iterations=100000):
neighbourhood = [] neighbour = DataFrame()
neighbourhood.append(element)
for _ in range(max_iterations): for _ in range(max_iterations):
previous_solution = neighbourhood[-1] neighbour, stop_condition = get_random_solution(element, data)
neighbour = get_random_solution(previous=previous_solution, data=data) if stop_condition:
if neighbour.equals(previous_solution):
break break
neighbourhood.append(neighbour)
return neighbour return neighbour