我刚开始使用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 fitness (individual):
sum = reduce(add,individual,0)
return abs(target - sum)
'计算平均人口适应度'
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)
'适应度函数计算个体中元素的总和,并查看它远离目标的距离(全局声明)'
'成绩计算平均适合度'
请帮助我!提前谢谢