Compare commits

...

2 Commits

Author SHA1 Message Date
coolneng 1cf8a2696a
Implement local search algorithm 2021-04-14 19:26:13 +02:00
coolneng 33a9cf323a
Check if the random candidate is a duplicate 2021-04-14 19:25:25 +02:00
1 changed files with 21 additions and 13 deletions

View File

@ -65,31 +65,39 @@ 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 ( while solution["distance"].loc[worst_index] <= worst_element:
solution.loc[worst_index, "distance"] <= previous.loc[worst_index, "distance"] 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 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(n, m, data) return local_search(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)