Clean up genetic algorithm

This commit is contained in:
coolneng 2021-06-21 03:47:05 +02:00
parent 924e4c9638
commit e20e16d476
Signed by: coolneng
GPG Key ID: 9893DA236405AF57
1 changed files with 2 additions and 15 deletions

View File

@ -6,8 +6,6 @@ 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(
@ -178,7 +176,7 @@ def select_new_gene(individual, n):
return new_gene
def mutate(offspring, data, probability=0.001):
def mutate(offspring, n, data, probability=0.001):
expected_mutations = len(offspring) * n * probability
individuals = []
genes = []
@ -297,19 +295,8 @@ 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, data)
offspring = mutate(offspring, n, 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,
)