MH-P2/src/main.py

59 lines
1.7 KiB
Python
Executable File

from preprocessing import parse_file
from genetic_algorithm import genetic_algorithm
from memetic_algorithm import memetic_algorithm
from time import time
from argparse import ArgumentParser
def execute_algorithm(args, n, m, data):
if args.algorithm == "genetic":
return genetic_algorithm(
n,
m,
data,
select_mode=args.selection,
crossover_mode=args.crossover,
max_iterations=100,
)
return memetic_algorithm(
n,
m,
data,
hybridation=args.hybridation,
max_iterations=100,
)
def show_results(solution, time_delta):
duplicates = solution.duplicated().any()
print(solution)
print(f"Total distance: {solution.fitness.values[0]}")
if not duplicates:
print("No duplicates found")
print(f"Execution time: {time_delta}")
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()
def main():
args = parse_arguments()
n, m, data = parse_file(args.file)
start_time = time()
solutions = execute_algorithm(args, n, m, data)
end_time = time()
show_results(solutions, time_delta=end_time - start_time)
if __name__ == "__main__":
main()