gpt4 book ai didi

python-2.7 - DEAP 工具箱 : to consider different types and ranges of genes in mutation and crossover operators

转载 作者:行者123 更新时间:2023-12-04 00:01:42 24 4
gpt4 key购买 nike

我正在研究遗传算法实现,并且正在使用 DEAP 工具箱。
我写了一个初始化染色体的代码,它们的第一个基因是 [0.01, 2048] 范围内的浮点数,他们的第二个基因再次在 [0.0001, 10] 范围内 float ,最后三个基因是 bool 值。这是我的代码:

toolbox.register("attr_flt1", random.uniform, 0.01, 2048)
toolbox.register("attr_flt2", random.uniform, 0.0001, 10)
toolbox.register("attr_bool", random.randint, 0, 1)
enter ctoolbox.register("individual", tools.initCycle, creator.Individual,
(toolbox.attr_flt1, toolbox.attr_flt2, toolbox.attr_bool, toolbox.attr_bool, toolbox.attr_bool),
n=1)

有一个创建的人口样本:
[1817.2852738610263, 6.184224906600851, 0, 0, 1], [1145.7253307024512, 8.618185266721435, 1, 0, 1], ...

现在,我想通过考虑基因类型和范围的差异对我的染色体进行突变和交叉。
目前我有一个错误,因为在应用交叉和变异算子后,染色体的第一个基因产生了 0 值,这与我的评估函数有问题。
任何人都可以使用 DEAP 工具箱帮助我进行代码选择、变异和交叉,这些工具箱会在最初定义的范围内产生新的种群吗?

最佳答案

如果使用变异算子 mutPolynomialBounded (记录 here ),然后您可以指定每个基因的间隔。

根据您指出的界限,也许使用诸如

eta = 0.5 #indicates degree of ressemblance of mutated individual
indpb = 0.1 #probability of individual to be mutated
low = [0.01, 0.0001, 0, 0, 0] #lower bound for each gene
up = [2048, 10, 1, 1, 1] #upper bound for each gene
toolbox.register('mutate', mutPolynomialBounded(individual, eta, low, up, indpb))

因为突变函数将解决您的错误。
这样,第一个基因在区间 [0.01, 2048] ,第二个基因在区间 [0.0001, 10]最后三个基因在区间 [0, 1] .

如果您还希望最后三个基因为 01 (但不是两者之间的浮点数),那么您可能必须实现自己的变异功能。例如,以下函数将根据您的要求为每个基因选择随机值
def mutRandom(individual, indpb):
if random.random() < indpb:
individual[0] = toolbox.attr_flt1()
individual[1] = toolbox.attr_flt2()
for i in range(2, 5):
individual[i] = toolbox.attr_bool()
return individual,

关于python-2.7 - DEAP 工具箱 : to consider different types and ranges of genes in mutation and crossover operators,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47720921/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com