def cut_and_splice(parent1, parent2, loci=None):
"""Return a new chromosome created with cut and splice crossover.
This is suitable for use with list or value encoding, and will work with
chromosomes of heterogeneous lengths.
Args:
parent1 (List): A parent chromosome.
parent2 (List): A parent chromosome.
loci (Tuple[int, int]): A crossover locus for each parent.
Returns:
List[List]: Two new chromosomes descended from the given parents.
"""
if loci is None:
loci = []
loci[0] = int(random.triangular(1, len(parent1) / 2, len(parent1) - 2))
loci[1] = int(random.triangular(1, len(parent2) / 2, len(parent2) - 2))
child1 = parent1[0:loci[0]] + parent2[loci[0]:]
child2 = parent2[0:loci[1]] + parent1[loci[1]:]
return [child1, child2]
评论列表
文章目录