Scipy 最小化.. 'Inequality constraints incompatible'

Scipy minimize .. 'Inequality constraints incompatible'

嗨,我正在尝试最小化一个简单的 3 变量函数,在 x 变量中有一些范围约束..但我得到'不平等约束不兼容 - 知道吗?谢谢!!

from scipy.optimize import minimize

def f(x):
    return (int(558*x[0]*x[1]*x[2])-(x[2]*(558-int(558*x[0])))-(x[2]*558))

x0 = [0.4, 1.0, 2.0]

#real data Ranges 
#x[0] 0..1    
#x[1] 1..3
#x[2] 5..50

cons=( 
        {'type': 'ineq','fun': lambda x: x[0]},
        {'type': 'ineq','fun': lambda x: 1-x[0]},
        {'type': 'ineq','fun': lambda x: x[1]-1},
        {'type': 'ineq','fun': lambda x: 3-x[1]},
        {'type': 'ineq','fun': lambda x: x[2]-5},
        {'type': 'ineq','fun': lambda x: 50-x[2]}
)

res = minimize(f, x0, constraints=cons)
print(res)

完整结果是

    fun: -33490.99993615066
     jac: array([ 6.7108864e+07,  6.7108864e+07, -8.9300000e+02])
 message: 'Inequality constraints incompatible'
    nfev: 8
     nit: 2
    njev: 2
  status: 4
 success: False
       x: array([ 0.4       ,  1.        , 49.99999993])

你好,我怀疑问题出在使用的数值方法上。

默认情况下使用约束,minimize 使用梯度最小二乘法 (SLSQP)。因此,要最小化的函数必须是规则的,但考虑到您对 int 的使用,它不是。

使用另一种方法:约束优化线性逼近 (COBYLA),这是一种不同的性质,我得到以下结果

from scipy.optimize import minimize

def f(x):
    return (int(558*x[0]*x[1]*x[2])-(x[2]*(558-int(558*x[0])))-(x[2]*558))

x0 = [0.4, 1.0, 2.0]

#real data Ranges 
#x[0] 0..1    
#x[1] 1..3
#x[2] 5..50

cons=( 
        {'type': 'ineq','fun': lambda x: x[0]},
        {'type': 'ineq','fun': lambda x: 1-x[0]},
        {'type': 'ineq','fun': lambda x: x[1]-1},
        {'type': 'ineq','fun': lambda x: 3-x[1]},
        {'type': 'ineq','fun': lambda x: x[2]-5},
        {'type': 'ineq','fun': lambda x: 50-x[2]}
)

res = minimize(f, x0, constraints=cons, method="cobyla")
print(res)

与显示器

     fun: -55800.0
   maxcv: 7.395570986446986e-32
 message: 'Optimization terminated successfully.'
    nfev: 82
  status: 1
 success: True
       x: array([-7.39557099e-32,  1.93750000e+00,  5.00000000e+01])