Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 2.7 使用DEAP编写我的第一个遗传算法-遇到类型错误_Python 2.7_Deap - Fatal编程技术网

Python 2.7 使用DEAP编写我的第一个遗传算法-遇到类型错误

Python 2.7 使用DEAP编写我的第一个遗传算法-遇到类型错误,python-2.7,deap,Python 2.7,Deap,我刚开始使用DEAP的第一个GA算法。作为一个初学者,我只是尝试去激活一个我已经用python编写的算法。 当我试图通过在工具箱容器中注册来评估我的适应度函数时,我得到以下错误 !![错误列表][1] Traceback (most recent call last): fitnesses = toolbox.map(toolbox.evaluate, invalid_ind) File "number_optimize_DEAP.py", line 38, in gra

我刚开始使用DEAP的第一个GA算法。作为一个初学者,我只是尝试去激活一个我已经用python编写的算法。 当我试图通过在工具箱容器中注册来评估我的适应度函数时,我得到以下错误

!![错误列表][1]

    Traceback (most recent call last):
    fitnesses = toolbox.map(toolbox.evaluate, invalid_ind)
    File "number_optimize_DEAP.py", line 38, in grade
        summed = reduce(add, (fitness(x) for x in population),0)
    File "number_optimize_DEAP.py", line 38, in <genexpr>
        summed = reduce(add, (fitness(x) for x in population),0)
    File "number_optimize_DEAP.py", line 29, in fitness
        sum = reduce(add,individual,0)
    TypeError: reduce() arg 2 must support iteration'
“计算平均人口适应度”

def grade(population):

    summed = reduce(add, (fitness(x) for x in population),0)
    return summed / (len(population) * 1.0)

toolbox.register("evaluate",grade)
toolbox.register("Crossover",tools.cxOnePoint)
toolbox.register("Mutate",tools.mutUniformInt,indpb = 0.01)
toolbox.register("Selection",tools.selBest)

def evolution():
    pop = toolbox.Population(n = 100)
    hof = tools.HallOfFame(1)
    stats = tools.Statistics(lambda ind: ind.fitness.values)
    stats.register("avg", numpy.mean)
    stats.register("min",numpy.min)
    stats.register("max", numpy.max)

pop,logbook = algorithms.eaSimple(pop,toolbox, cxpb = 0.2, mutpb = 0.01, ngen = 10, stats         = stats, halloffame = hof, verbose = True)
'适应度函数计算个体中元素的总和,并查看其与目标的距离'

“等级计算平均适合度”


请帮我摆脱困境!!提前感谢

错误消息很清楚:fitness正在尝试使用reduce对个人进行迭代,但无法对任何个人进行迭代。@jonrsharpe感谢您的帮助。但“个人”在这里是一个列表,我如何解决“不能迭代的问题”?你确定吗?如果您之前将print typeindividual放在行上会发生什么?您是如何定义toolbox.Population的?您能在创建pop的地方添加代码段吗?另外,reduce函数不是内置的。它有什么作用?
def grade(population):

    summed = reduce(add, (fitness(x) for x in population),0)
    return summed / (len(population) * 1.0)

toolbox.register("evaluate",grade)
toolbox.register("Crossover",tools.cxOnePoint)
toolbox.register("Mutate",tools.mutUniformInt,indpb = 0.01)
toolbox.register("Selection",tools.selBest)

def evolution():
    pop = toolbox.Population(n = 100)
    hof = tools.HallOfFame(1)
    stats = tools.Statistics(lambda ind: ind.fitness.values)
    stats.register("avg", numpy.mean)
    stats.register("min",numpy.min)
    stats.register("max", numpy.max)

pop,logbook = algorithms.eaSimple(pop,toolbox, cxpb = 0.2, mutpb = 0.01, ngen = 10, stats         = stats, halloffame = hof, verbose = True)