crossover.py 文件源码

python
阅读 31 收藏 0 点赞 0 评论 0

项目:levis 作者: rawg 项目源码 文件源码
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]
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号