Population.next_population and better comments
This commit is contained in:
@ -12,7 +12,15 @@ class Crossover_Methods:
|
||||
"""
|
||||
|
||||
mating_pool = ga.population.get_mating_pool()
|
||||
return ga.make_population([ga.crossover_individual_impl(ga, mating_pool[index], mating_pool[index-1]) for index in range(len(mating_pool))])
|
||||
|
||||
for index in range(len(mating_pool)): # for each parent in the mating pool
|
||||
ga.population.add_child( # add a child
|
||||
ga.crossover_individual_impl( # by crossing
|
||||
ga, #
|
||||
mating_pool[index], # the parent and
|
||||
mating_pool[index-1] # the previous parent
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
def random_selection(ga):
|
||||
@ -21,7 +29,15 @@ class Crossover_Methods:
|
||||
"""
|
||||
|
||||
mating_pool = ga.population.get_mating_pool()
|
||||
return ga.make_population([ga.crossover_individual_impl(ga, parent, random.choice(mating_pool)) for parent in mating_pool])
|
||||
|
||||
for parent in mating_pool: # for each parent in the mating pool
|
||||
ga.population.add_child( # add a child
|
||||
ga.crossover_individual_impl( # by crossing
|
||||
ga, #
|
||||
parent, # the parent and
|
||||
random.choice(mating_pool) # a random parent
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
class Individual:
|
||||
@ -41,18 +57,27 @@ class Crossover_Methods:
|
||||
|
||||
def uniform(ga, parent_one, parent_two):
|
||||
"""Cross two parents by swapping all genes randomly"""
|
||||
return ga.make_chromosome([
|
||||
random.choice([parent_one.get_gene(i), parent_two.get_gene(i)])
|
||||
for i in range(parent_one.size())])
|
||||
|
||||
return ga.make_chromosome([ # Make a new chromosome
|
||||
random.choice([ # by selecting random genes from
|
||||
parent_one.get_gene(i), # each parent
|
||||
parent_two.get_gene(i) #
|
||||
]) #
|
||||
for i in range(parent_one.size())]) # for each gene
|
||||
|
||||
class Arithmetic:
|
||||
"""Crossover methods for numerical genes"""
|
||||
|
||||
def int_random(ga, parent_one, parent_two):
|
||||
"""Cross two parents by taking a random integer value between each of the genes"""
|
||||
return ga.make_chromosome([
|
||||
ga.make_gene(random.randint(*sorted([parent_one.get_gene(i).get_value(), parent_two.get_gene(i).get_value()])))
|
||||
for i in range(parent_one.size())])
|
||||
|
||||
return ga.make_chromosome([ # Make a new chromosome
|
||||
ga.make_gene( # filled with new genes
|
||||
random.randint(*sorted([ # by choosing random integers between
|
||||
parent_one.get_gene(i).get_value(), # the parents' genes
|
||||
parent_two.get_gene(i).get_value() #
|
||||
]))) #
|
||||
for i in range(parent_one.size())]) # for each gene
|
||||
|
||||
|
||||
def int_weighted(ga, parent_one, parent_two):
|
||||
@ -60,16 +85,26 @@ class Crossover_Methods:
|
||||
|
||||
# the percentage of genes taken from the first gene
|
||||
weight = 0.25
|
||||
return ga.make_chromosome([
|
||||
ga.make_gene(int(weight*parent_one.get_gene(i).get_value()+(1-weight)*parent_two.get_gene(i).get_value()))
|
||||
for i in range(parent_one.size())])
|
||||
|
||||
return ga.make_chromosome([ # Make a new chromosome
|
||||
ga.make_gene(int( # filled with new integer genes
|
||||
weight*parent_one.get_gene(i).get_value()+ # with weight% from parent one and
|
||||
(1-weight)*parent_two.get_gene(i).get_value() # (100-weight)% from parent two
|
||||
)) #
|
||||
for i in range(parent_one.size())]) # for each gene
|
||||
|
||||
|
||||
def float_random(ga, parent_one, parent_two):
|
||||
"""Cross two parents by taking a random numeric value between each of the genes"""
|
||||
return ga.make_chromosome([
|
||||
ga.make_gene(random.uniform(parent_one.get_gene(i).get_value(), parent_two.get_gene(i).get_value()))
|
||||
for i in range(parent_one.size())])
|
||||
|
||||
return ga.make_chromosome([ # Make a new chromosome
|
||||
ga.make_gene( # filled with new genes
|
||||
random.uniform( # by taking a random float between
|
||||
parent_one.get_gene(i).get_value(), # the parents' genes
|
||||
parent_two.get_gene(i).get_value() #
|
||||
) #
|
||||
) #
|
||||
for i in range(parent_one.size())]) # for each gene
|
||||
|
||||
|
||||
def float_weighted(ga, parent_one, parent_two):
|
||||
@ -77,6 +112,10 @@ class Crossover_Methods:
|
||||
|
||||
# the percentage of genes taken from the first gene
|
||||
weight = 0.25
|
||||
return ga.make_chromosome([
|
||||
ga.make_gene(weight*parent_one.get_gene(i).get_value()+(1-weight)*parent_two.get_gene(i).get_value())
|
||||
for i in range(parent_one.size())])
|
||||
|
||||
return ga.make_chromosome([ # Make a new chromosome
|
||||
ga.make_gene( # filled with new float genes
|
||||
weight*parent_one.get_gene(i).get_value()+ # with weight% from parent one and
|
||||
(1-weight)*parent_two.get_gene(i).get_value() # (100-weight)% from parent two
|
||||
) #
|
||||
for i in range(parent_one.size())]) # for each gene
|
||||
|
||||
Reference in New Issue
Block a user