Additional error checking for permutation methods

This commit is contained in:
SimpleArt
2020-12-20 14:00:52 -05:00
parent ef3be9d5d1
commit 91da18cf54

View File

@ -228,6 +228,14 @@ class Crossover_Methods:
"""Cross two parents by slicing out a random part of one parent
and then filling in the rest of the genes from the second parent."""
# Too small to cross
if len(parent_1) < 2:
return parent_1.gene_list
# Unequal parent lengths
if len(parent_1) != len(parent_2):
raise ValueError("Parents do not have the same lengths.")
# Swap with weighted probability so that most of the genes
# are taken directly from parent 1.
if random.choices([0, 1], cum_weights = [weight, 1]) == 1:
@ -267,6 +275,14 @@ class Crossover_Methods:
and then filling in the rest of the genes from the second parent,
preserving the ordering of genes wherever possible."""
# Too small to cross
if len(parent_1) < 2:
return parent_1.gene_list
# Unequal parent lengths
if len(parent_1) != len(parent_2):
raise ValueError("Parents do not have the same lengths.")
# Swap with weighted probability so that most of the genes
# are taken directly from parent 1.
if random.choices([0, 1], cum_weights = [weight, 1]) == 1: