Compare commits
No commits in common. "b63b5b08b6ff62b5ce0451dfa6be27a6510fc803" and "f534521410bf318e56ef495e9058273558c7e9ba" have entirely different histories.
b63b5b08b6
...
f534521410
@ -1,8 +1,6 @@
|
|||||||
from preprocessing import parse_file
|
from preprocessing import parse_file
|
||||||
from pandas import DataFrame, Series
|
from pandas import DataFrame
|
||||||
from sys import argv
|
from sys import argv
|
||||||
from random import seed, randint
|
|
||||||
from time import time
|
|
||||||
|
|
||||||
|
|
||||||
def get_first_solution(n, data):
|
def get_first_solution(n, data):
|
||||||
@ -13,7 +11,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"].astype(float).idxmax()
|
furthest_index = distance_sum["distance"].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
|
||||||
@ -27,14 +25,11 @@ 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"].astype(float).idxmax()
|
furthest_index = element_df["distance"].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)
|
||||||
return Series(data={"point": furthest_point, "distance": furthest_row["distance"]})
|
furthest_element = {"point": furthest_point, "distance": furthest_row["distance"]}
|
||||||
|
return furthest_element, furthest_index
|
||||||
|
|
||||||
def remove_solution_dataset(data, solution):
|
|
||||||
return data.query(f"source != {solution} and destination != {solution}")
|
|
||||||
|
|
||||||
|
|
||||||
def greedy_algorithm(n, m, data):
|
def greedy_algorithm(n, m, data):
|
||||||
@ -42,64 +37,29 @@ def greedy_algorithm(n, m, data):
|
|||||||
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):
|
for _ in range(m):
|
||||||
last_solution = int(solutions["point"].tail(n=1))
|
last_solution = solutions["point"].tail(n=1)
|
||||||
centroid = get_furthest_element(element=last_solution, data=data)
|
centroid, index = get_furthest_element(element=int(last_solution), data=data)
|
||||||
solutions = solutions.append(centroid, ignore_index=True)
|
solutions = solutions.append(dict(centroid), ignore_index=True)
|
||||||
data = remove_solution_dataset(data=data, solution=last_solution)
|
data = data.drop(index)
|
||||||
return solutions
|
return solutions
|
||||||
|
|
||||||
|
|
||||||
def get_pseudorandom_solution(n, data):
|
# NOTE In each step, switch to the element that gives the least amount
|
||||||
seed(42)
|
def local_search():
|
||||||
solution = data.iloc[randint(a=0, b=n)]
|
pass
|
||||||
return Series(data={"point": solution["destination"], "distance": 0})
|
|
||||||
|
|
||||||
|
|
||||||
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 execute_algorithm(choice, n, m, data):
|
|
||||||
if choice == "greedy":
|
|
||||||
return greedy_algorithm(n, m, data)
|
|
||||||
elif choice == "local":
|
|
||||||
return local_search(n, m, data)
|
|
||||||
else:
|
|
||||||
print("The valid algorithm choices are 'greedy' and 'local'")
|
|
||||||
exit(1)
|
|
||||||
|
|
||||||
|
|
||||||
def show_results(solutions, time_delta):
|
|
||||||
distance_sum = solutions["distance"].sum()
|
|
||||||
duplicates = solutions.duplicated()
|
|
||||||
print(solutions)
|
|
||||||
print("Total distance: " + str(distance_sum))
|
|
||||||
if solutions[duplicates].empty:
|
|
||||||
print("No duplicates found")
|
|
||||||
print("Execution time: " + str(time_delta))
|
|
||||||
|
|
||||||
|
|
||||||
def usage(argv):
|
def usage(argv):
|
||||||
print(f"Usage: python {argv[0]} <file> <algorithm choice>")
|
print(f"Usage: python {argv[0]} <file>")
|
||||||
print("algorithm choices:")
|
|
||||||
print("greedy: greedy algorithm")
|
|
||||||
print("local: local search algorithm")
|
|
||||||
exit(1)
|
exit(1)
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
if len(argv) != 3:
|
if len(argv) != 2:
|
||||||
usage(argv)
|
usage(argv)
|
||||||
n, m, data = parse_file(argv[1])
|
n, m, data = parse_file(argv[1])
|
||||||
start_time = time()
|
solutions = greedy_algorithm(n, m, data)
|
||||||
solutions = execute_algorithm(choice=argv[2], n=n, m=m, data=data)
|
print(solutions)
|
||||||
end_time = time()
|
|
||||||
show_results(solutions, time_delta=end_time - start_time)
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
Loading…
Reference in New Issue
Block a user