48 lines
1.2 KiB
Python
48 lines
1.2 KiB
Python
|
from preprocessing import parse_file
|
||
|
from greedy import greedy_algorithm
|
||
|
from local_search import local_search
|
||
|
from sys import argv
|
||
|
from time import time
|
||
|
|
||
|
|
||
|
def execute_algorithm(choice, n, m, data):
|
||
|
if choice == "greedy":
|
||
|
return greedy_algorithm(n, m, data)
|
||
|
elif choice == "local":
|
||
|
return local_search(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().any()
|
||
|
print(solutions)
|
||
|
print("Total distance: " + str(distance_sum))
|
||
|
if not duplicates:
|
||
|
print("No duplicates found")
|
||
|
print("Execution time: " + str(time_delta))
|
||
|
|
||
|
|
||
|
def usage(argv):
|
||
|
print(f"Usage: python {argv[0]} <file> <algorithm choice>")
|
||
|
print("algorithm choices:")
|
||
|
print("greedy: greedy algorithm")
|
||
|
print("local: local search algorithm")
|
||
|
exit(1)
|
||
|
|
||
|
|
||
|
def main():
|
||
|
if len(argv) != 3:
|
||
|
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)
|
||
|
|
||
|
|
||
|
if __name__ == "__main__":
|
||
|
main()
|