神经进化环出指数问题

2024-09-28 05:22:28 发布

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

我试图在没有整洁库的情况下制作一个简单的神经进化算法(因为我刚刚开始,想很好地理解它是如何工作的),但我似乎无法修复我一直存在的这个错误

注意:第一个总体是随机生成的

代码所做的是生成随机字母,直到它匹配用户给出的驼峰大小写字符串。我有两门课,一门是DNA课,一门是人口课。DNA类具有繁殖、变异和计算能力等功能。每个人都在做标题上写的事

我的问题是我创造的交配池。我这样做的方式是让它计算每个DNA对象的适合度,并将其添加到交配池中,使适合度%等于它添加到交配池中的次数

例如:如果某个DNA对象的适应度得分为5%,则该对象将被添加到交配池中5次

下面是loop()代码

def loop():
    global population
    global found
    while not found:
        next_population = False
        mating_pool = []
        for x in range(0, population.__len__()):
            if population[x].fitness == 1:
                found = True
            population[x].CalculateFitness()
            if population[x].fitness > 0:
                mating_strength = int(population[x].fitness * 100)
            else:
                mating_strength = 0
            for count in range(0, mating_strength):
                mating_pool.append(population[x])

        next_population = []
        while not next_population:
            parentA = mating_pool[random.randint(0, mating_pool.__len__())]
            parentB = mating_pool[random.randint(0, mating_pool.__len__())]
            child = DNA()
            child.char_list = parentA.Breed(parentB)
            child.Mutation()
            next_population.append(child)
            if next_population.__len__() == population.__len__():
                population = next_population
                next_population = True

This is the error, sometimes it shows up with parentA and sometimes with parentB:

    parentB = mating_pool[random.randint(0, mating_pool.__len__())]
IndexError: list index out of range

Tags: 对象childlenifrangestrengthdnanext

热门问题