微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

ap /进化算法/

如何解决ap /进化算法/

我是遗传算法的新手,现在我想从一个名为jcvi的开放项目中使用定义的遗传算法“ eaSimpleConverge()”。但它总是显示错误

UnboundLocalError: local variable 'updated' referenced before assignment

然后我尝试通过在执行“ while循环”之前设置“ updated = 0”来进行修复,尽管在此之后代码显示错误,结果一直为空,并且最大分数始终为(0,)。因此,任何人都可以提出解决问题的提示,我非常感激。我感到困惑,为什么在将update设置为0后没有结果,这是错误的吗?这是原始代码

def eaSimpleConverge(population,toolBox,cxpb,mutpb,ngen,stats=None,halloffame=None,callback=None,verbose=True):

 invalid_ind = [ind for ind in population if not ind.fitness.valid]
 fitnesses = toolBox.map(toolBox.evaluate,invalid_ind)
 for ind,fit in zip(invalid_ind,fitnesses):
    ind.fitness.values = fit

 if halloffame is not None:
    halloffame.update(population)

 record = stats.compile(population) if stats else {}

# Begin the generational process
 gen = 1
 updated =0
 best = (0,)
 while True:
    # Select the next generation individuals
    offspring = toolBox.select(population,len(population))

    # vary the pool of individuals
    offspring = varand(offspring,mutpb)

    # Evaluate the individuals with an invalid fitness
    invalid_ind = [ind for ind in offspring if not ind.fitness.valid]
    fitnesses = toolBox.map(toolBox.evaluate,invalid_ind)
    for ind,fitnesses):
        ind.fitness.values = fit

    # Update the hall of fame with the generated individuals
    if halloffame is not None:
        halloffame.update(offspring)

    if callback is not None:
        callback(halloffame[0],gen)

    # Replace the current population by the offspring
    population[:] = offspring

    # Append the current generation statistics to the logbook
    record = stats.compile(population) if stats else {}
    current_best = record["max"]
    if gen % 20 == 0 and verbose:
        print(
            "Current iteration {0}: max_score={1}".format(gen,current_best),file=sys.stderr,)

    if current_best > best:
        best = current_best
        updated = gen
    gen += 1
    
    if gen - updated > ngen:
        break
        
 return population

感谢您的时间,并再次提供帮助!

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。