整数的Scipy微分进化 [英] Scipy Differential Evolution with integers
问题描述
我正在尝试使用scipy.optimize.differential_evolution进行优化.该代码要求x中每个变量的范围.但是我想要一个解决方案,其中x的部分必须是整数,而其他部分则可以自由浮动.我的代码的相关部分看起来像
I'm trying to run an optimization with scipy.optimize.differential_evolution. The code calls for bounds for each variable in x. But I want to a solution where parts of x must be integers, while others can range freely as floats. The relevant part of my code looks like
bounds = [(0,3),(0,3),(0,3),???,???]
result = differential_evolution(func, bounds)
我该如何替换???,以强制这些变量在给定范围内为整数?
What do I replace the ???'s with to force those variables to be ints in a given range?
推荐答案
如注释中所述,不直接支持整数约束".
As noted in the comments there isn't direct support for a "integer constraint".
不过,您可以最小化修改后的目标函数,例如:
You could however minimize a modified objective function, e.g.:
def func1(x):
return func(x) + K * (x[3] - round(x[3]))**2
,这将迫使x[3]
接近整数值(不幸的是,您必须调整K
参数).
and this will force x[3]
towards an integer value (unfortunately you have to tune the K
parameter).
另一种方法是在评估目标函数之前对(某些)实值参数进行四舍五入:
An alternative is to round (some of) the real-valued parameters before evaluating the objective function:
def func1(x):
z = x;
z[3] = round(z[3])
return func(z)
这两种都是使用差分进化方法解决离散优化问题的常用技术,并且效果很好.
Both are common techniques to approach a discrete optimization problem using Differential Evolution and they work quite well.
这篇关于整数的Scipy微分进化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!