Additional error checking for permutation methods
This commit is contained in:
@ -228,6 +228,14 @@ class Crossover_Methods:
|
|||||||
"""Cross two parents by slicing out a random part of one parent
|
"""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."""
|
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
|
# Swap with weighted probability so that most of the genes
|
||||||
# are taken directly from parent 1.
|
# are taken directly from parent 1.
|
||||||
if random.choices([0, 1], cum_weights = [weight, 1]) == 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,
|
and then filling in the rest of the genes from the second parent,
|
||||||
preserving the ordering of genes wherever possible."""
|
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
|
# Swap with weighted probability so that most of the genes
|
||||||
# are taken directly from parent 1.
|
# are taken directly from parent 1.
|
||||||
if random.choices([0, 1], cum_weights = [weight, 1]) == 1:
|
if random.choices([0, 1], cum_weights = [weight, 1]) == 1:
|
||||||
|
|||||||
Reference in New Issue
Block a user