Compare commits

..

No commits in common. "b63b5b08b6ff62b5ce0451dfa6be27a6510fc803" and "f534521410bf318e56ef495e9058273558c7e9ba" have entirely different histories.

1 changed files with 16 additions and 56 deletions

View File

@ -1,8 +1,6 @@
from preprocessing import parse_file
from pandas import DataFrame, Series
from pandas import DataFrame
from sys import argv
from random import seed, randint
from time import time
def get_first_solution(n, data):
@ -13,7 +11,7 @@ def get_first_solution(n, data):
distance_sum = distance_sum.append(
{"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"] = 0
return furthest_row
@ -27,14 +25,11 @@ def get_different_element(original, row):
def get_furthest_element(element, data):
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_point = get_different_element(original=element, row=furthest_row)
return Series(data={"point": furthest_point, "distance": furthest_row["distance"]})
def remove_solution_dataset(data, solution):
return data.query(f"source != {solution} and destination != {solution}")
furthest_element = {"point": furthest_point, "distance": furthest_row["distance"]}
return furthest_element, furthest_index
def greedy_algorithm(n, m, data):
@ -42,64 +37,29 @@ 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)
last_solution = solutions["point"].tail(n=1)
centroid, index = get_furthest_element(element=int(last_solution), data=data)
solutions = solutions.append(dict(centroid), ignore_index=True)
data = data.drop(index)
return solutions
def get_pseudorandom_solution(n, data):
seed(42)
solution = data.iloc[randint(a=0, b=n)]
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))
# NOTE In each step, switch to the element that gives the least amount
def local_search():
pass
def usage(argv):
print(f"Usage: python {argv[0]} <file> <algorithm choice>")
print("algorithm choices:")
print("greedy: greedy algorithm")
print("local: local search algorithm")
print(f"Usage: python {argv[0]} <file>")
exit(1)
def main():
if len(argv) != 3:
if len(argv) != 2:
usage(argv)
n, m, data = parse_file(argv[1])
start_time = time()
solutions = execute_algorithm(choice=argv[2], n=n, m=m, data=data)
end_time = time()
show_results(solutions, time_delta=end_time - start_time)
solutions = greedy_algorithm(n, m, data)
print(solutions)
if __name__ == "__main__":