Add correct index to the solutions list

This commit is contained in:
coolneng 2021-05-19 19:20:27 +02:00
parent 38585aa16b
commit dfdd142fdb
Signed by: coolneng
GPG Key ID: 9893DA236405AF57
1 changed files with 6 additions and 4 deletions

View File

@ -29,10 +29,12 @@ def get_closest_element(element, data):
return Series(data={"point": closest_point, "distance": closest_row["distance"]}) return Series(data={"point": closest_point, "distance": closest_row["distance"]})
def explore_solutions(solutions, data): def explore_solutions(solutions, data, index):
closest_elements = solutions["point"].apply(func=get_closest_element, data=data) closest_elements = solutions["point"].apply(func=get_closest_element, data=data)
furthest_index = closest_elements["distance"].astype(float).idxmax() furthest_index = closest_elements["distance"].astype(float).idxmax()
return closest_elements.iloc[furthest_index] solution = closest_elements.iloc[furthest_index]
solution.name = index
return solution
def remove_duplicates(current, previous, data): def remove_duplicates(current, previous, data):
@ -46,8 +48,8 @@ def greedy_algorithm(n, m, data):
solutions = DataFrame(columns=["point", "distance"]) solutions = DataFrame(columns=["point", "distance"])
first_solution = get_first_solution(n, data) first_solution = get_first_solution(n, data)
solutions = solutions.append(first_solution, ignore_index=True) solutions = solutions.append(first_solution, ignore_index=True)
for _ in range(m - 1): for iteration in range(m - 1):
element = explore_solutions(solutions, data) element = explore_solutions(solutions, data, index=iteration + 1)
solutions = solutions.append(element) solutions = solutions.append(element)
data = remove_duplicates( data = remove_duplicates(
current=element["point"], previous=solutions["point"], data=data current=element["point"], previous=solutions["point"], data=data