MH-P2/src/main.py

59 lines
1.7 KiB
Python
Raw Normal View History

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 time import time
2021-06-21 03:46:35 +02:00
from argparse import ArgumentParser
2021-04-29 12:33:46 +02:00
2021-06-21 03:46:35 +02:00
def execute_algorithm(args, n, m, data):
if args.algorithm == "genetic":
return genetic_algorithm(
2021-06-21 03:50:14 +02:00
n,
m,
data,
select_mode=args.selection,
crossover_mode=args.crossover,
max_iterations=100,
2021-06-21 03:46:35 +02:00
)
2021-06-21 07:39:39 +02:00
return memetic_algorithm(
n,
m,
data,
hybridation=args.hybridation,
max_iterations=100,
2021-05-19 20:03:04 +02:00
)
2021-06-21 07:39:39 +02:00
def show_results(solution, time_delta):
duplicates = solution.duplicated().any()
print(solution)
print(f"Total distance: {solution.fitness.values[0]}")
2021-04-29 12:33:46 +02:00
if not duplicates:
print("No duplicates found")
2021-05-19 20:03:04 +02:00
print(f"Execution time: {time_delta}")
2021-04-29 12:33:46 +02:00
2021-06-21 03:46:35 +02:00
def parse_arguments():
parser = ArgumentParser()
parser.add_argument("file", help="dataset of choice")
subparsers = parser.add_subparsers(dest="algorithm")
parser_genetic = subparsers.add_parser("genetic")
parser_memetic = subparsers.add_parser("memetic")
parser_genetic.add_argument("crossover", choices=["uniform", "position"])
parser_genetic.add_argument("selection", choices=["generational", "stationary"])
parser_memetic.add_argument("hybridation", choices=["all", "random", "best"])
return parser.parse_args()
2021-04-29 12:33:46 +02:00
def main():
2021-06-21 03:46:35 +02:00
args = parse_arguments()
n, m, data = parse_file(args.file)
2021-04-29 12:33:46 +02:00
start_time = time()
2021-06-21 03:46:35 +02:00
solutions = execute_algorithm(args, n, m, data)
2021-04-29 12:33:46 +02:00
end_time = time()
2021-06-21 07:39:39 +02:00
show_results(solutions, time_delta=end_time - start_time)
2021-04-29 12:33:46 +02:00
if __name__ == "__main__":
main()