Change metric in Greedy algorithm
This commit is contained in:
parent
b63b5b08b6
commit
75c3a94fbe
|
@ -25,16 +25,18 @@ def get_different_element(original, row):
|
|||
return row.source
|
||||
|
||||
|
||||
def get_furthest_element(element, data):
|
||||
def get_closest_element(element, data):
|
||||
element_df = data.query(f"source == {element} or destination == {element}")
|
||||
furthest_index = element_df["distance"].astype(float).idxmax()
|
||||
furthest_row = data.iloc[furthest_index]
|
||||
furthest_point = get_different_element(original=element, row=furthest_row)
|
||||
return Series(data={"point": furthest_point, "distance": furthest_row["distance"]})
|
||||
closest_index = element_df["distance"].astype(float).idxmin()
|
||||
closest_row = data.loc[closest_index]
|
||||
closest_point = get_different_element(original=element, row=closest_row)
|
||||
return Series(data={"point": closest_point, "distance": closest_row["distance"]})
|
||||
|
||||
|
||||
def remove_solution_dataset(data, solution):
|
||||
return data.query(f"source != {solution} and destination != {solution}")
|
||||
def explore_solutions(solutions, data):
|
||||
closest_elements = solutions["point"].apply(func=get_closest_element, data=data)
|
||||
furthest_index = closest_elements["distance"].astype(float).idxmax()
|
||||
return closest_elements.iloc[furthest_index]
|
||||
|
||||
|
||||
def greedy_algorithm(n, m, data):
|
||||
|
@ -42,10 +44,8 @@ def greedy_algorithm(n, m, data):
|
|||
first_solution = get_first_solution(n, data)
|
||||
solutions = solutions.append(first_solution, ignore_index=True)
|
||||
for _ in range(m):
|
||||
last_solution = int(solutions["point"].tail(n=1))
|
||||
centroid = get_furthest_element(element=last_solution, data=data)
|
||||
solutions = solutions.append(centroid, ignore_index=True)
|
||||
data = remove_solution_dataset(data=data, solution=last_solution)
|
||||
element = explore_solutions(solutions, data)
|
||||
solutions = solutions.append(element)
|
||||
return solutions
|
||||
|
||||
|
||||
|
|
Loading…
Reference in New Issue