MH-P2/src/main.py

81 lines
2.5 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 sys import argv
from time import time
2021-05-19 20:03:04 +02:00
from itertools import combinations
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(
n, m, data, select_mode=args.selection, crossover_mode=args.crossover
)
2021-04-29 12:33:46 +02:00
else:
2021-06-21 03:46:35 +02:00
return memetic_algorithm(n, m, data, hybridation=args.hybridation)
2021-04-29 12:33:46 +02:00
2021-05-19 20:03:04 +02:00
def get_row_distance(source, destination, data):
row = data.query(
"""(source == @source and destination == @destination) or \
(source == @destination and destination == @source)"""
)
return row["distance"].values[0]
def get_fitness(solutions, data):
counter = 0
comb = combinations(solutions.index, r=2)
for index in list(comb):
elements = solutions.loc[index, :]
counter += get_row_distance(
source=elements["point"].head(n=1).values[0],
destination=elements["point"].tail(n=1).values[0],
data=data,
)
return counter
def show_results(solutions, fitness, time_delta):
2021-04-29 12:33:46 +02:00
duplicates = solutions.duplicated().any()
print(solutions)
2021-05-19 20:03:04 +02:00
print(f"Total distance: {fitness}")
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
def usage(argv):
2021-06-21 03:46:35 +02:00
print(f"Usage: python {argv[0]} <file> <algorithm choice> <")
2021-04-29 12:33:46 +02:00
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)
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-05-19 20:03:04 +02:00
fitness = get_fitness(solutions, data)
show_results(solutions, fitness, time_delta=end_time - start_time)
2021-04-29 12:33:46 +02:00
if __name__ == "__main__":
main()