Scipy.optimize.minimize method ='SLSQP'忽略约束 [英] Scipy.optimize.minimize method='SLSQP' ignores constraint
问题描述
我正在使用SciPy进行优化,而SLSQP方法似乎忽略了我的约束.
I'm using SciPy for optimization and the method SLSQP seems to ignore my constraints.
具体来说,我希望x [3]和x [4]在[0-1]范围内
Specifically, I want x[3] and x[4] to be in the range [0-1]
我收到消息:不平等约束不兼容"
I'm getting the message: 'Inequality constraints incompatible'
这是执行结果,后跟示例代码(使用伪函数):
Here is the results of the execution followed by an example code (uses a dummy function):
status: 4
success: False
njev: 2
nfev: 24
fun: 0.11923608071680103
x: array([-10993.4278558 , -19570.77080806, -23495.15914299, -26531.4862831 ,
4679.97660534])
message: 'Inequality constraints incompatible'
jac: array([ 12548372.4766904 , 12967696.88362279, 39928956.72239509,
-9224613.99092537, 3954696.30747453, 0. ])
nit: 2
这是我的代码:
from random import random
from scipy.optimize import minimize
def func(x):
""" dummy function to optimize """
print 'x'+str(x)
return random()
my_constraints = ({'type':'ineq', 'fun':lambda(x):1-x[3]-x[4]},
{'type':'ineq', 'fun':lambda(x):x[3]},
{'type':'ineq', 'fun':lambda(x):x[4]},
{'type':'ineq', 'fun':lambda(x):1-x[4]},
{'type':'ineq', 'fun':lambda(x):1-x[3]})
minimize(func, [57.9499 ,-18.2736,1.1664,0.0000,0.0765],
method='SLSQP',constraints=my_constraints)
编辑- 即使删除第一个约束,问题仍然存在.
EDIT - The problem persists when even when removing the first constraint.
当我尝试使用 bounds 变量时,问题仍然存在. 即
The problem persists when I try to use the bounds variables. i.e.,
bounds_pairs = [(None,None),(None,None),(None,None),(0,1),(0,1)]
minimize(f,initial_guess,method=method_name,bounds=bounds_pairs,constraints=non_negative_prob)
推荐答案
我知道这是一个非常老的问题,但我对此很感兴趣.
I know this is a very old question, but I was intrigued.
当优化函数不能可靠地微分时,会出现此问题.如果您使用像这样的漂亮平滑函数:
This problem occurs when the optimisation function is not reliably differentiable. If you use a nice smooth function like this:
opt = numpy.array([2, 2, 2, 2, 2])
def func(x):
return sum((x - opt)**2)
问题消失了.
请注意,scipy.minimize
中的任何约束算法都不能保证永远不会在约束之外对函数进行求值.如果这是您的要求,则应使用转换.因此,例如,要确保从未使用过x [3]的负值,可以使用转换x3_real = 10^x[3]
.这样x [3]可以是任何值,但您使用的变量永远不会为负.
Note that none of the constrained algorithms in scipy.minimize
have guarantees that the function will never be evaluated outside the constraints. If this is a requirement for you, you should rather use transformations. So for instance to ensure that no negative values for x[3] are ever used, you can use a transformation x3_real = 10^x[3]
. This way x[3] can be any value but the variable you use will never be negative.
研究slsqp的Fortran代码可对发生此错误的时间产生以下见解.该例程返回一个MODE
变量,该变量可以采用以下值:
Investigating the Fortran code for slsqp yields the following insights into when this error occurs. The routine returns a MODE
variable, which can take on these values:
C* MODE = -1: GRADIENT EVALUATION, (G&A) *
C* 0: ON ENTRY: INITIALIZATION, (F,G,C&A) *
C* ON EXIT : REQUIRED ACCURACY FOR SOLUTION OBTAINED *
C* 1: FUNCTION EVALUATION, (F&C) *
C* *
C* FAILURE MODES: *
C* 2: NUMBER OF EQUALITY CONTRAINTS LARGER THAN N *
C* 3: MORE THAN 3*N ITERATIONS IN LSQ SUBPROBLEM *
C* 4: INEQUALITY CONSTRAINTS INCOMPATIBLE *
C* 5: SINGULAR MATRIX E IN LSQ SUBPROBLEM *
C* 6: SINGULAR MATRIX C IN LSQ SUBPROBLEM *
分配模式4(这是您遇到的错误)的部分如下:
The part which assigns mode 4 (which is the error you are getting) is as follows:
C SEARCH DIRECTION AS SOLUTION OF QP - SUBPROBLEM
CALL dcopy_(n, xl, 1, u, 1)
CALL dcopy_(n, xu, 1, v, 1)
CALL daxpy_sl(n, -one, x, 1, u, 1)
CALL daxpy_sl(n, -one, x, 1, v, 1)
h4 = one
CALL lsq (m, meq, n , n3, la, l, g, a, c, u, v, s, r, w, iw, mode)
C AUGMENTED PROBLEM FOR INCONSISTENT LINEARIZATION
IF (mode.EQ.6) THEN
IF (n.EQ.meq) THEN
mode = 4
ENDIF
ENDIF
因此,基本上您可以看到它尝试寻找下降方向,如果约束处于活动状态,它将尝试沿约束进行导数求值,并在lsq子问题(mode = 6
)中使用奇异矩阵失败,那么它就说明了如果所有对约束方程进行了评估,但均未得出成功的下降方向,这必须是一组矛盾的约束(mode = 4
).
So basically you can see it attempts to find a descent direction, if the constraints are active it attempts derivative evaluation along the constraint and fails with a singular matrix in the lsq subproblem (mode = 6
), then it reasons that if all the constraint equations were evaluated and none yielded successful descent directions, this must be a contradictory set of constraints (mode = 4
).
这篇关于Scipy.optimize.minimize method ='SLSQP'忽略约束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!