def single_point(parent1, parent2, locus=None):
"""Return a new chromosome created with single-point crossover.
This is suitable for use with list or value encoding, and will work with
chromosomes of heterogenous lengths.
Args:
parent1 (List): A parent chromosome.
parent2 (List): A parent chromosome.
locus (int): The locus at which to crossover or ``None`` for a randomly
selected locus.
Returns:
List[List]: Two new chromosomes descended from the given parents.
"""
if len(parent1) > len(parent2):
parent1, parent2 = parent2, parent1
if locus is None:
locus = int(random.triangular(1, len(parent1) / 2, len(parent1) - 2))
child1 = parent1[0:locus] + parent2[locus:]
child2 = parent2[0:locus] + parent1[locus:]
return [child1, child2]
评论列表
文章目录