Clean up genetic algorithm
This commit is contained in:
parent
924e4c9638
commit
e20e16d476
|
@ -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,
|
|
||||||
)
|
|
||||||
|
|
Loading…
Reference in New Issue