Update crossover_methods.py

- Avoid bugging if the mating pool only has one parent.
- Shortened decorator name.
This commit is contained in:
SimpleArt
2020-11-27 16:03:34 -05:00
parent fa832c1059
commit bf148222eb

View File

@ -1,6 +1,6 @@
import random import random
def append_children_to_next_population(population_method): def append_to_next_population(population_method):
"""Appends the new chromosomes to the next population. """Appends the new chromosomes to the next population.
Also modifies the input to include the mating pool. Also modifies the input to include the mating pool.
""" """
@ -12,13 +12,13 @@ def append_children_to_next_population(population_method):
def weak_check_exceptions(population_method): def weak_check_exceptions(population_method):
"""Checks if the first two chromosomes can be crossed.""" """Checks if the first and last chromosomes can be crossed."""
def new_method(ga, mating_pool): def new_method(ga, mating_pool):
# check if any genes are an Exception from # check if any genes are an Exception from
# crossing just the first two parents. # crossing just the first and last parents.
for gene in ga.crossover_individual_impl(ga, mating_pool[0], mating_pool[1]): for gene in ga.crossover_individual_impl(ga, mating_pool[0], mating_pool[-1]):
if isinstance(gene.value, Exception): if isinstance(gene.value, Exception):
raise gene.value raise gene.value
@ -81,7 +81,7 @@ def values_to_genes(individual_method):
class Crossover_Methods: class Crossover_Methods:
# Private method decorators, see above. # Private method decorators, see above.
_append_children_to_next_population = append_children_to_next_population _append_to_next_population = append_to_next_population
_weak_check_exceptions = weak_check_exceptions _weak_check_exceptions = weak_check_exceptions
_strong_check_exceptions = strong_check_exceptions _strong_check_exceptions = strong_check_exceptions
_genes_to_chromosome = genes_to_chromosome _genes_to_chromosome = genes_to_chromosome
@ -92,7 +92,7 @@ class Crossover_Methods:
"""Methods for selecting chromosomes to crossover.""" """Methods for selecting chromosomes to crossover."""
@append_children_to_next_population @append_to_next_population
@weak_check_exceptions @weak_check_exceptions
def sequential_selection(ga, mating_pool): def sequential_selection(ga, mating_pool):
"""Select sequential pairs from the mating pool. """Select sequential pairs from the mating pool.
@ -108,7 +108,7 @@ class Crossover_Methods:
) )
@append_children_to_next_population @append_to_next_population
@weak_check_exceptions @weak_check_exceptions
def random_selection(ga, mating_pool): def random_selection(ga, mating_pool):
"""Select random pairs from the mating pool. """Select random pairs from the mating pool.