python-3.x - 使用 selBest 选择的 DEAP 的 eaSimple 算法选择的个体数量
问题描述
我正在尝试从DEAP 模块eaSimple
运行算法。我想指定每一代要选择的个体数量。但是,如果我为选择函数指定一个参数,则会出现错误。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
?
解决方案
在eaSimple
算法中,实际上没有进行选择。从文档中,描述引擎盖下发生的事情的伪代码说明了
population = select(population, len(population))
这意味着选择初始种群大小的种群子集,即整个种群进行突变、交配和评估。
然后算法做
population = offspring
因此,突变和交配产生的所有个体都取代了所有父母。
这也(某种程度上)回答了问题的第二部分:不可能在方法eaSimple
中指定自定义值。k
selBest
推荐阅读
- mysql - 在 1B 记录上执行 %term% 之类的 mysql(带有索引字段)
- python - LocustIO:用户群
- python - 如何将 Pandas `GROUPBY` 函数的结果转换为原始数据框
- arrays - 算法逻辑,拆分数组
- python - DataFrame属性和列有什么区别
- powershell - 用于选项卡完成的 PowerShell 脚本
- javascript - 使用 Firebase 中的对象设置状态
- typescript - 从接口解构值,否则解构未定义
- android - Kotlin 结构化并发 [协程] 模型是否适用于适用于数据库写入的 UI?
- swift - 如何在swift的if语句中使用两个let变量