通过EASBest选择从DEAP中选择eaSimple算法的个人数量

时间:2019-11-05 15:58:03

标签: python-3.x deap

我正在尝试从DEAP module运行eaSimple算法。我想指定每一代要选择的个人数量。但是,如果我为selection function指定一个k参数,则会收到错误消息。

from deap import base, tools, creator, algorithms
import random
import numpy as np

def fitness(individual):
    x = individual[0]
    y = individual[1]
    return np.exp(-9*x*y) * np.sin(3*np.pi*x)**2 * np.sin(3*np.pi*y)**2,

toolbox = base.Toolbox()

creator.create("FitnessMax", base.Fitness, weights=(1.0,))
creator.create("Individual", list, fitness=creator.FitnessMax)
toolbox.register("individual", tools.initRepeat, creator.Individual, random.random, n=2)

toolbox.register("population", tools.initRepeat, list, toolbox.individual)

toolbox.register('evaluate', fitness)
toolbox.register('mutate', tools.mutPolynomialBounded, eta=.6, low=[0,0], up=[1,1], indpb=0.1)
toolbox.register('mate', tools.cxUniform, indpb=0.5)
toolbox.register('select', tools.selBest, k=50)

pop = toolbox.population(n=100)

pop, logbook = algorithms.eaSimple(pop, toolbox, cxpb=0.5, mutpb=0.1, ngen=100)

此示例中的最后一行引发错误

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-2-5ee0faee2c49> in <module>
----> 1 pop, logbook = algorithms.eaSimple(pop, toolbox, cxpb=0.5, mutpb=0.1, ngen=100)

/usr/local/lib64/python3.7/site-packages/deap/algorithms.py in eaSimple(population, toolbox, cxpb, mutpb, ngen, stats, halloffame, verbose)
    163     for gen in range(1, ngen + 1):
    164         # Select the next generation individuals
--> 165         offspring = toolbox.select(population, len(population))
    166 
    167         # Vary the pool of individuals

TypeError: selBest() got multiple values for argument 'k'

请注意,将行toolbox.register('select', tools.selBest, k=50)替换为toolbox.register('select', tools.selBest)可消除该错误。


此处k的默认值是什么,如何为k指定我自己的值?

1 个答案:

答案 0 :(得分:0)

eaSimple算法中,实际上没有选择。在the documentation中,伪代码描述了引擎盖状态下发生的事情

population = select(population, len(population))

这意味着选择初始种群大小的一部分种群,即整个种群进行突变,交配和评估。

然后算法执行

population = offspring

因此,由变异和交配产生的所有个体都替换了所有父母。


这(有点)也回答了问题的第二部分:eaSimple不可能在k方法中为selBest指定自定义值。