您好,我正在尝试最小化一个简单的三变量函数,其中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])发布于 2020-12-20 08:42:49
你好,我怀疑问题来自于所使用的数值方法。
默认情况下,带有约束的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])https://stackoverflow.com/questions/65375813
复制相似问题