Compare commits
No commits in common. "1cf8a2696a31f428c5c70a59a70a637da543d445" and "da234aae96e15ea2cbfbfb93cafdfc4511420edf" have entirely different histories.
1cf8a2696a
...
da234aae96
@ -65,39 +65,31 @@ def get_first_random_solution(m, data):
|
|||||||
return data.iloc[random_indexes]
|
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):
|
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:
|
while (
|
||||||
if random_candidate["distance"] not in solution["distance"].values:
|
solution.loc[worst_index, "distance"] <= previous.loc[worst_index, "distance"]
|
||||||
solution.loc[worst_index] = random_candidate
|
):
|
||||||
else:
|
solution.loc[worst_index] = random_candidate
|
||||||
return solution, True
|
return solution
|
||||||
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):
|
def execute_algorithm(choice, n, m, data):
|
||||||
if choice == "greedy":
|
if choice == "greedy":
|
||||||
return greedy_algorithm(n, m, data)
|
return greedy_algorithm(n, m, data)
|
||||||
elif choice == "local":
|
elif choice == "local":
|
||||||
return local_search(m, data)
|
return local_search(n, m, data)
|
||||||
else:
|
else:
|
||||||
print("The valid algorithm choices are 'greedy' and 'local'")
|
print("The valid algorithm choices are 'greedy' and 'local'")
|
||||||
exit(1)
|
exit(1)
|
||||||
|
Loading…
Reference in New Issue
Block a user