From 46b4dc749d560a51ea3c64f187130a044565f1ed Mon Sep 17 00:00:00 2001 From: SimpleArt <71458112+SimpleArt@users.noreply.github.com> Date: Thu, 19 Nov 2020 21:55:12 -0500 Subject: [PATCH] Added function decorator --- src/crossover/crossover_methods.py | 47 ++++++++++++++++-------------- 1 file changed, 25 insertions(+), 22 deletions(-) diff --git a/src/crossover/crossover_methods.py b/src/crossover/crossover_methods.py index 599338f..5eb10dd 100644 --- a/src/crossover/crossover_methods.py +++ b/src/crossover/crossover_methods.py @@ -5,39 +5,42 @@ class Crossover_Methods: class Population: """Methods for selecting chromosomes to crossover.""" - def sequential_selection(ga): + def __append_children_from_mating_pool(crossover_method): + def helper(ga): + mating_pool = ga.population.mating_pool + ga.population.append_children([ + [chromosome for cromosome in crossover_method(ga, mating_pool)] + ) + return helper + + + @__append_children_from_mating_pool + def sequential_selection(ga, mating_pool): """Select sequential pairs from the mating pool. Every parent is paired with the previous parent. The first parent is paired with the last parent. """ - mating_pool = ga.population.get_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 - ) - ) + for index in range(len(mating_pool)): # for each parent in the mating pool + yield ga.crossover_individual_impl( # apply crossover to + ga, # + mating_pool[index], # the parent and + mating_pool[index-1] # the previous parent + ) - def random_selection(ga): + @__append_children_from_mating_pool + def random_selection(ga, mating_pool): """Select random pairs from the mating pool. Every parent is paired with a random parent. """ - mating_pool = ga.population.get_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 - ) - ) + for parent in mating_pool: # for each parent in the mating pool + yield ga.crossover_individual_impl( # apply crossover to + ga, # + parent, # the parent and + random.choice(mating_pool) # a random parent + ) class Individual: