Cast distance to float to get the maximum value

This commit is contained in:
coolneng 2021-04-12 12:22:26 +02:00
parent f73e28fb8a
commit 04c92add44
Signed by: coolneng
GPG Key ID: 9893DA236405AF57
1 changed files with 4 additions and 3 deletions

View File

@ -12,7 +12,7 @@ def get_first_solution(n, data):
distance_sum = distance_sum.append( distance_sum = distance_sum.append(
{"point": element, "distance": distance}, ignore_index=True {"point": element, "distance": distance}, ignore_index=True
) )
furthest_index = distance_sum["distance"].idxmax() furthest_index = distance_sum["distance"].astype(float).idxmax()
furthest_row = distance_sum.iloc[furthest_index] furthest_row = distance_sum.iloc[furthest_index]
furthest_row["distance"] = 0 furthest_row["distance"] = 0
return furthest_row return furthest_row
@ -26,13 +26,14 @@ def get_different_element(original, row):
def get_furthest_element(element, data): def get_furthest_element(element, data):
element_df = data.query(f"source == {element} or destination == {element}") element_df = data.query(f"source == {element} or destination == {element}")
furthest_index = element_df["distance"].idxmax() furthest_index = element_df["distance"].astype(float).idxmax()
furthest_row = data.iloc[furthest_index] furthest_row = data.iloc[furthest_index]
furthest_point = get_different_element(original=element, row=furthest_row) furthest_point = get_different_element(original=element, row=furthest_row)
furthest_element = {"point": furthest_point, "distance": furthest_row["distance"]} furthest_element = {"point": furthest_point, "distance": furthest_row["distance"]}
return furthest_element, furthest_index return furthest_element, furthest_index
# FIXME Remove duplicated elements properly
def greedy_algorithm(n, m, data): 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)
@ -50,7 +51,6 @@ def get_pseudorandom_solution(n, data):
return data.iloc[randint(a=0, b=n)] return data.iloc[randint(a=0, b=n)]
# NOTE In each step, switch to the element that gives the least amount
def local_search(n, m, data): def local_search(n, m, data):
solutions = DataFrame(columns=["point", "distance"]) solutions = DataFrame(columns=["point", "distance"])
first_solution = get_pseudorandom_solution(n=n, data=data) first_solution = get_pseudorandom_solution(n=n, data=data)
@ -74,6 +74,7 @@ def show_results(solutions):
distance_sum = solutions["distance"].sum() distance_sum = solutions["distance"].sum()
print(solutions) print(solutions)
print("Total distance: " + str(distance_sum)) print("Total distance: " + str(distance_sum))
print(solutions.duplicated())
def usage(argv): def usage(argv):