Change CLI using argparse
This commit is contained in:
parent
f4dd4700c7
commit
924e4c9638
35
src/main.py
35
src/main.py
|
@ -4,16 +4,16 @@ from memetic_algorithm import memetic_algorithm
|
||||||
from sys import argv
|
from sys import argv
|
||||||
from time import time
|
from time import time
|
||||||
from itertools import combinations
|
from itertools import combinations
|
||||||
|
from argparse import ArgumentParser
|
||||||
|
|
||||||
|
|
||||||
def execute_algorithm(choice, n, m, data):
|
def execute_algorithm(args, n, m, data):
|
||||||
if choice == "genetic":
|
if args.algorithm == "genetic":
|
||||||
return genetic_algorithm(n, m, data)
|
return genetic_algorithm(
|
||||||
elif choice == "memetic":
|
n, m, data, select_mode=args.selection, crossover_mode=args.crossover
|
||||||
return memetic_algorithm(m, data)
|
)
|
||||||
else:
|
else:
|
||||||
print("The valid algorithm choices are 'genetic' and 'memetic'")
|
return memetic_algorithm(n, m, data, hybridation=args.hybridation)
|
||||||
exit(1)
|
|
||||||
|
|
||||||
|
|
||||||
def get_row_distance(source, destination, data):
|
def get_row_distance(source, destination, data):
|
||||||
|
@ -47,19 +47,30 @@ def show_results(solutions, fitness, time_delta):
|
||||||
|
|
||||||
|
|
||||||
def usage(argv):
|
def usage(argv):
|
||||||
print(f"Usage: python {argv[0]} <file> <algorithm choice>")
|
print(f"Usage: python {argv[0]} <file> <algorithm choice> <")
|
||||||
print("algorithm choices:")
|
print("algorithm choices:")
|
||||||
print("genetic: genetic algorithm")
|
print("genetic: genetic algorithm")
|
||||||
print("memetic: memetic algorithm")
|
print("memetic: memetic algorithm")
|
||||||
exit(1)
|
exit(1)
|
||||||
|
|
||||||
|
|
||||||
|
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():
|
def main():
|
||||||
if len(argv) != 3:
|
args = parse_arguments()
|
||||||
usage(argv)
|
n, m, data = parse_file(args.file)
|
||||||
n, m, data = parse_file(argv[1])
|
|
||||||
start_time = time()
|
start_time = time()
|
||||||
solutions = execute_algorithm(choice=argv[2], n=n, m=m, data=data)
|
solutions = execute_algorithm(args, n, m, data)
|
||||||
end_time = time()
|
end_time = time()
|
||||||
fitness = get_fitness(solutions, data)
|
fitness = get_fitness(solutions, data)
|
||||||
show_results(solutions, fitness, time_delta=end_time - start_time)
|
show_results(solutions, fitness, time_delta=end_time - start_time)
|
||||||
|
|
Loading…
Reference in New Issue