简体   繁体   English

Scipy.optimize.minimize method ='SLSQP'忽略约束

[英]Scipy.optimize.minimize method='SLSQP' ignores constraint

I'm using SciPy for optimization and the method SLSQP seems to ignore my constraints. 我正在使用SciPy进行优化,SLSQP方法似乎忽略了我的约束。

Specifically, I want x[3] and x[4] to be in the range [0-1] 具体来说,我希望x [3]和x [4]在[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

Here is my code: 这是我的代码:

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. 编辑 - 即使删除第一个约束,问题仍然存在。

The problem persists when I try to use the bounds variables. 当我尝试使用bounds变量时问题仍然存在。 ie,

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. 我知道这是一个非常古老的问题,但我很感兴趣。

When does it happen? 它什么时候发生?

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)

The problem goes away. 问题消失了。

How do I impose hard constraints? 我如何施加硬约束?

Note that none of the constrained algorithms in scipy.minimize have guarantees that the function will never be evaluated outside the constraints. 请注意, scipy.minimize中的约束算法都不能保证函数永远不会在约束之外进行求值。 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] . 因此,例如,为了确保不使用x [3]的负值,可以使用变换x3_real = 10^x[3] This way x[3] can be any value but the variable you use will never be negative. 这样x [3]可以是任何值,但你使用的变量永远不会是负数。

Deeper analysis 更深入的分析

Investigating the Fortran code for slsqp yields the following insights into when this error occurs. 调查slsqp的Fortran代码可以获得有关何时发生此错误的以下见解。 The routine returns a MODE variable, which can take on these values: 例程返回一个MODE变量,该变量可以采用以下值:

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               *

The part which assigns mode 4 (which is the error you are getting) is as follows: 分配模式4的部分(这是你得到的错误)如下:

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

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 ). 所以基本上你可以看到它试图找到一个下降方向,如果约束是活动的,它会尝试沿约束进行衍生评估,并在lsq子问题( mode = 6 )中使用奇异矩阵失败,那么它会导致所有约束方程如果进行评估并且没有一个成功下降方向,这必然是一组相互矛盾的约束( mode = 4 )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 scipy.optimize.minimize忽略约束 - scipy.optimize.minimize ignores constraint scipy.optimize.minimize - slsqp - 黑森矩阵 - scipy.optimize.minimize - slsqp - hessian matrix scipy.optimize.minimize(COBYLA和SLSQP)忽略for循环内启动的约束 - scipy.optimize.minimize (COBYLA and SLSQP) ignores constraints initiated within for loop python scipy.optimize.minimize “SLSQP求解器”在xo之间添加约束 - python scipy.optimize.minimize “SLSQP solver” adding constraint between xo 超出范围时scipy.optimize.minimize(method ='SLSQP')内存问题 - scipy.optimize.minimize(method=‘SLSQP’) memory issues when outside the bounds 具有线性约束的Scipy.optimize.minimize SLSQP失败 - Scipy.optimize.minimize SLSQP with linear constraints fails 打印选择的 scipy.optimize.minimize 方法 - print chosen method of scipy.optimize.minimize Scipy 中的共享目标和约束计算优化 Minimize() SLSQP - Sharing objective and constraint calculation within Scipy Optimize Minimize() SLSQP 为什么 scipy.optimize.minimize 不适用于约束和初始值 0 - Why scipy.optimize.minimize does not work with a constraint and initial value 0 scipy.optimize.minimize('SLSQP') 在给定 2000 暗变量时太慢 - scipy.optimize.minimize('SLSQP') too slow when given 2000 dim variable
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM