crossover.py 文件源码

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

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


问题


面经


文章

微信
公众号

扫码关注公众号