Fixed crossover methods

This commit is contained in:
SimpleArt
2020-10-12 22:58:26 -04:00
parent 72ae3ec1e7
commit b8f1de9b52

View File

@ -1,6 +1,4 @@
import random import random
from initialization.chromosome_structure.chromosome import Chromosome
from initialization.population_structure.population import Population
class Crossover_Methods: class Crossover_Methods:
@ -11,26 +9,26 @@ class Crossover_Methods:
"""Select sequential pairs from the mating pool""" """Select sequential pairs from the mating pool"""
mating_pool = ga.population.mating_pool mating_pool = ga.population.mating_pool
return Population([ga.crossover_individual_impl(mating_pool[index], mating_pool[index+1]) for index in range(len(mating_pool)-1)]) return ga.make_population([ga.crossover_individual_impl(ga, mating_pool[index], mating_pool[index+1]) for index in range(len(mating_pool)-1)])
def random_selection(ga): def random_selection(ga):
"""Select random pairs from the mating pool""" """Select random pairs from the mating pool"""
mating_pool = ga.population.mating_pool mating_pool = ga.population.mating_pool
return Population([ga.crossover_individual_impl(random.choice(mating_pool), random.choice(mating_pool)) for n in mating_pool]) return ga.make_population([ga.crossover_individual_impl(ga, random.choice(mating_pool), random.choice(mating_pool)) for n in mating_pool])
class Individual: class Individual:
"""Methods for crossing parents""" """Methods for crossing parents"""
def single_point_crossover(parent_one, parent_two): def single_point_crossover(ga, parent_one, parent_two):
"""Cross two parents by swapping genes at one random point""" """Cross two parents by swapping genes at one random point"""
index = random.randint(0, parent_one.size()-1) index = random.randint(0, parent_one.size()-1)
return Chromosome(parent_one.get_gene_list()[:index] + parent_two.get_gene_list()[index:]) return ga.make_chromosome(parent_one.get_gene_list()[:index] + parent_two.get_gene_list()[index:])
def multi_point_crossover(parent_one, parent_two): def multi_point_crossover(ga, parent_one, parent_two):
"""Cross two parents by swapping genes at multiple points""" """Cross two parents by swapping genes at multiple points"""
pass pass