Max*_*Max 6 python optimization mathematical-optimization scipy
我正在解决以下优化问题:
使用此Python代码:
from scipy.optimize import minimize
import math
def f(x):
return math.log(x[0]**2 + 1) + x[1]**4 + x[0]*x[2]
x0 = [0, 0, 0]
cons=({'type': 'ineq',
'fun': lambda x: x[0]**3 - x[1]**2 - 1},
{'type': 'ineq',
'fun': lambda x: x[0]},
{'type': 'ineq',
'fun': lambda x: x[2]})
res = minimize(f, x0, constraints=cons)
print res
Run Code Online (Sandbox Code Playgroud)
我收到了一个错误
消息:'不平等约束不兼容'
什么可能导致此错误?
问题似乎与您最初的猜测有关.如果我将您的起始值更改为
x0 = [1.0, 1.0, 1.0]
Run Code Online (Sandbox Code Playgroud)
然后你的代码将执行正常(至少在我的机器上)
win32上的Python 3.5.1(v3.5.1:37a07cee5969,2015年12月6日,01:54:25)[MSC v.1900 64位(AMD64)]
message: 'Optimization terminated successfully.'
njev: 10
jac: array([ 1., 0., 1., 0.])
fun: 0.6931471805582502
nit: 10
status: 0
x: array([ 1.00000000e+00, -1.39724765e-06, 1.07686548e-14])
success: True
nfev: 51
Run Code Online (Sandbox Code Playgroud)