Compare commits
2 Commits
da234aae96
...
1cf8a2696a
Author | SHA1 | Date |
---|---|---|
coolneng | 1cf8a2696a | |
coolneng | 33a9cf323a |
|
@ -65,31 +65,39 @@ def get_first_random_solution(m, data):
|
|||
return data.iloc[random_indexes]
|
||||
|
||||
|
||||
def local_search(n, m, data):
|
||||
solutions = DataFrame(columns=["point", "distance"])
|
||||
first_solution = get_pseudorandom_solution(n=n, data=data)
|
||||
solutions = solutions.append(first_solution, ignore_index=True)
|
||||
for _ in range(m):
|
||||
pass
|
||||
return solutions
|
||||
def get_random_solution(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.loc[worst_index, "distance"] <= previous.loc[worst_index, "distance"]
|
||||
):
|
||||
solution.loc[worst_index] = random_candidate
|
||||
return solution
|
||||
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
|
||||
|
||||
|
||||
def explore_neighbourhood(element, data, max_iterations=100000):
|
||||
neighbour = DataFrame()
|
||||
for _ in range(max_iterations):
|
||||
neighbour, stop_condition = get_random_solution(element, data)
|
||||
if stop_condition:
|
||||
break
|
||||
return neighbour
|
||||
|
||||
|
||||
def local_search(m, data):
|
||||
first_solution = get_first_random_solution(m=m, data=data)
|
||||
best_solution = explore_neighbourhood(element=first_solution, data=data)
|
||||
return best_solution
|
||||
|
||||
|
||||
def execute_algorithm(choice, n, m, data):
|
||||
if choice == "greedy":
|
||||
return greedy_algorithm(n, m, data)
|
||||
elif choice == "local":
|
||||
return local_search(n, m, data)
|
||||
return local_search(m, data)
|
||||
else:
|
||||
print("The valid algorithm choices are 'greedy' and 'local'")
|
||||
exit(1)
|
||||
|
|
Loading…
Reference in New Issue