Compare commits

..

3 Commits

Author SHA1 Message Date
coolneng c2cc3c716d
Limit number of iterations to 100 2021-06-21 03:50:14 +02:00
coolneng e20e16d476
Clean up genetic algorithm 2021-06-21 03:48:50 +02:00
coolneng 924e4c9638
Change CLI using argparse 2021-06-21 03:46:35 +02:00
2 changed files with 36 additions and 27 deletions

View File

@ -6,8 +6,6 @@ from functools import partial
from multiprocessing import Pool from multiprocessing import Pool
from copy import deepcopy from copy import deepcopy
from preprocessing import parse_file
def get_row_distance(source, destination, data): def get_row_distance(source, destination, data):
row = data.query( row = data.query(
@ -178,7 +176,7 @@ def select_new_gene(individual, n):
return new_gene return new_gene
def mutate(offspring, data, probability=0.001): def mutate(offspring, n, data, probability=0.001):
expected_mutations = len(offspring) * n * probability expected_mutations = len(offspring) * n * probability
individuals = [] individuals = []
genes = [] genes = []
@ -297,19 +295,8 @@ def genetic_algorithm(n, m, data, select_mode, crossover_mode, max_iterations=10
for _ in range(max_iterations): for _ in range(max_iterations):
parents = select_parents(population, n, select_mode) parents = select_parents(population, n, select_mode)
offspring = crossover(crossover_mode, parents, m) offspring = crossover(crossover_mode, parents, m)
offspring = mutate(offspring, data) offspring = mutate(offspring, n, data)
population = replace_population(population, offspring, select_mode) population = replace_population(population, offspring, select_mode)
population = evaluate_population(population, data) population = evaluate_population(population, data)
best_index, _ = get_best_elements(population) best_index, _ = get_best_elements(population)
return population[best_index] return population[best_index]
n, m, data = parse_file("data/GKD-c_11_n500_m50.txt")
genetic_algorithm(
n=10,
m=4,
data=data,
select_mode="generational",
crossover_mode="uniform",
max_iterations=10,
)

View File

@ -4,16 +4,27 @@ 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,
return memetic_algorithm(m, data) m,
data,
select_mode=args.selection,
crossover_mode=args.crossover,
max_iterations=100,
)
else: else:
print("The valid algorithm choices are 'genetic' and 'memetic'") return memetic_algorithm(
exit(1) n,
m,
data,
hybridation=args.hybridation,
max_iterations=100,
)
def get_row_distance(source, destination, data): def get_row_distance(source, destination, data):
@ -47,19 +58,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)