Rewrote for clarity and potential bug

This commit is contained in:
SimpleArt
2020-11-12 18:00:22 -05:00
parent ef5f87cbc6
commit aa677a1d81

View File

@ -6,33 +6,37 @@ class Survivor_Selection:
def fill_in_best(ga): def fill_in_best(ga):
"""Fills in the next population with the best chromosomes from the last population""" """Fills in the next population with the best chromosomes from the last population"""
# add in chromosomes starting from needed_amount = ga.population.size()-len(ga.population.next_population)
# the first chromosome in the population
# until the next population is full
ga.population.append_children( ga.population.append_children(
ga.population.get_chromosome_list()[:ga.population.size()-len(ga.population.next_population)] ga.population.get_chromosome_list()[:needed_amount]
) )
def fill_in_random(ga): def fill_in_random(ga):
"""Fills in the next population with random chromosomes from the last population""" """Fills in the next population with random chromosomes from the last population"""
needed_amount = ga.population.size()-ga.population.total_children()
ga.population.append_children([ # add in chromosomes ga.population.append_children([ # add in chromosomes
random.choice( # randomly random.choice( # randomly
ga.population.get_chromosome_list() # from the population ga.population.get_chromosome_list() # from the population
) # until the next population is full ) #
for n in range(ga.population.size()-ga.population.total_children())]) for n in range(needed_amount)]) # until the next population is full
def fill_in_parents_then_random(ga): def fill_in_parents_then_random(ga):
"""Fills in the next population with all parents followed by random chromosomes from the last population""" """Fills in the next population with all parents followed by random chromosomes from the last population"""
ga.population.append_children( # add in chromosomes needed_amount = ga.population.size()-ga.population.total_children()
ga.population.get_mating_pool() # from the mating pool parent_amount = max(len(ga.population.get_mating_pool()), needed_amount)
) # random_amount = needed_amount - parent_amount
ga.population.append_children( # add in chromosomes
ga.population.get_mating_pool()[:parent_amount] # from the mating pool
) #
ga.population.append_children([ # add in chromosomes ga.population.append_children([ # add in chromosomes
random.choice( # randomly random.choice( # randomly
ga.population.get_chromosome_list() # from the population ga.population.get_chromosome_list() # from the population
) # until the next population is full ) #
for n in range(ga.population.size()-ga.population.total_children())]) for n in range(random_amount)]) # until the next population is full