0

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

![错误列表][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)

'健身函数计算个体中元素的总和,并查看它与目标的距离(全局声明)'

'等级计算平均适应度'

请帮我解决这个问题!提前致谢

4

0 回答 0