使用Deap Fram在Mate函数上创建新个体

2024-06-26 13:33:53 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试创建一个个性化的伴侣功能。以这样一种方式从群体中的两个个体中创造出一个新个体。下面是我的伙伴函数:

def BREED(parent1, parent2):
    child = []
    childP1 = []
    childP2 = []

    geneA = int(random.random() * len(parent1))
    geneB = int(random.random() * len(parent1))

    startGene = min(geneA, geneB)
    endGene = max(geneA, geneB)

    for i in range(startGene, endGene):
        childP1.append(parent1[i])

    childP2 = [item for item in parent2 if item not in childP1]

    child = childP1 + childP2
    #print(parent1)
    parent2=array.array("i",child)
    return parent1, parent2

这将返回一个错误:'array.array' object has no attribute 'fitness', because parent2 is no more an Individual Object.如何创建新的个人


Tags: inchildlenrandomitemarrayint个体