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 (将#修改为@)