Compare commits

..

No commits in common. "c2cc3c716dd127a87f39422b579de1bad050a7d8" and "f4dd4700c7e3ae36a76644c87aca92a19b657013" have entirely different histories.

2 changed files with 27 additions and 36 deletions

View File

@ -6,6 +6,8 @@ from functools import partial
from multiprocessing import Pool
from copy import deepcopy
from preprocessing import parse_file
def get_row_distance(source, destination, data):
row = data.query(
@ -176,7 +178,7 @@ def select_new_gene(individual, n):
return new_gene
def mutate(offspring, n, data, probability=0.001):
def mutate(offspring, data, probability=0.001):
expected_mutations = len(offspring) * n * probability
individuals = []
genes = []
@ -295,8 +297,19 @@ def genetic_algorithm(n, m, data, select_mode, crossover_mode, max_iterations=10
for _ in range(max_iterations):
parents = select_parents(population, n, select_mode)
offspring = crossover(crossover_mode, parents, m)
offspring = mutate(offspring, n, data)
offspring = mutate(offspring, data)
population = replace_population(population, offspring, select_mode)
population = evaluate_population(population, data)
best_index, _ = get_best_elements(population)
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,27 +4,16 @@ from memetic_algorithm import memetic_algorithm
from sys import argv
from time import time
from itertools import combinations
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,
)
def execute_algorithm(choice, n, m, data):
if choice == "genetic":
return genetic_algorithm(n, m, data)
elif choice == "memetic":
return memetic_algorithm(m, data)
else:
return memetic_algorithm(
n,
m,
data,
hybridation=args.hybridation,
max_iterations=100,
)
print("The valid algorithm choices are 'genetic' and 'memetic'")
exit(1)
def get_row_distance(source, destination, data):
@ -58,30 +47,19 @@ def show_results(solutions, fitness, time_delta):
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("genetic: genetic algorithm")
print("memetic: memetic algorithm")
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():
args = parse_arguments()
n, m, data = parse_file(args.file)
if len(argv) != 3:
usage(argv)
n, m, data = parse_file(argv[1])
start_time = time()
solutions = execute_algorithm(args, n, m, data)
solutions = execute_algorithm(choice=argv[2], n=n, m=m, data=data)
end_time = time()
fitness = get_fitness(solutions, data)
show_results(solutions, fitness, time_delta=end_time - start_time)