2021-04-29 12:33:46 +02:00
|
|
|
from preprocessing import parse_file
|
2021-05-10 19:25:06 +02:00
|
|
|
from genetic_algorithm import genetic_algorithm
|
|
|
|
from memetic_algorithm import memetic_algorithm
|
2021-04-29 12:33:46 +02:00
|
|
|
from sys import argv
|
|
|
|
from time import time
|
|
|
|
|
|
|
|
|
|
|
|
def execute_algorithm(choice, n, m, data):
|
2021-05-10 19:25:06 +02:00
|
|
|
if choice == "genetic":
|
|
|
|
return genetic_algorithm(n, m, data)
|
|
|
|
elif choice == "memetic":
|
|
|
|
return memetic_algorithm(m, data)
|
2021-04-29 12:33:46 +02:00
|
|
|
else:
|
2021-05-10 19:25:06 +02:00
|
|
|
print("The valid algorithm choices are 'genetic' and 'memetic'")
|
2021-04-29 12:33:46 +02:00
|
|
|
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:")
|
2021-05-10 19:25:06 +02:00
|
|
|
print("genetic: genetic algorithm")
|
|
|
|
print("memetic: memetic algorithm")
|
2021-04-29 12:33:46 +02:00
|
|
|
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()
|