diff --git a/src/survivor_selection/survivor_selection_methods.py b/src/survivor_selection/survivor_selection_methods.py index 78f5420..48d9128 100644 --- a/src/survivor_selection/survivor_selection_methods.py +++ b/src/survivor_selection/survivor_selection_methods.py @@ -3,41 +3,32 @@ import random class Survivor_Selection: """Survivor selection determines which individuals should be brought to the next generation""" + def __append_to_next_population(survivor_method): + return lambda ga: ga.population.append_children(survivor_method(ga)) + + + @__append_to_next_population def fill_in_best(ga): """Fills in the next population with the best chromosomes from the last population""" needed_amount = len(ga.population) - ga.population.total_children - - ga.population.append_children( - ga.population[:needed_amount] - ) + return ga.population[:needed_amount] + @__append_to_next_population def fill_in_random(ga): """Fills in the next population with random chromosomes from the last population""" needed_amount = len(ga.population) - ga.population.total_children - - ga.population.append_children([ # add in chromosomes - random.choice( # randomly - ga.population # from the population - ) # - for n in range(needed_amount)]) # until the next population is full + return [random.choice(ga.population) for n in range(needed_amount)] + @__append_to_next_population def fill_in_parents_then_random(ga): """Fills in the next population with all parents followed by random chromosomes from the last population""" needed_amount = len(ga.population) - ga.population.total_children - parent_amount = max(len(ga.population.total_parents), needed_amount) + parent_amount = min(ga.population.total_parents, 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 - random.choice( # randomly - ga.population # from the population - ) # - for n in range(random_amount)]) # until the next population is full + return ga.population.get_mating_pool()[:parent_amount] + [random.choice(ga.population) for n in range(random_amount)]