Compare commits
2 Commits
98a86a97c0
...
d5f9ed4ffb
Author | SHA1 | Date |
---|---|---|
coolneng | d5f9ed4ffb | |
coolneng | e3c55ca89f |
|
@ -64,25 +64,32 @@ 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
|
||||
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):
|
||||
neighbour = DataFrame()
|
||||
neighbourhood = []
|
||||
neighbourhood.append(element)
|
||||
for _ in range(max_iterations):
|
||||
neighbour, stop_condition = get_random_solution(element, data)
|
||||
if stop_condition:
|
||||
previous_solution = neighbourhood[-1]
|
||||
neighbour = get_random_solution(previous=previous_solution, data=data)
|
||||
if neighbour.equals(previous_solution):
|
||||
break
|
||||
neighbourhood.append(neighbour)
|
||||
return neighbour
|
||||
|
||||
|
||||
|
|
Loading…
Reference in New Issue