Scipy 最小化约束函数 [英] Scipy minimize constrained function
本文介绍了Scipy 最小化约束函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在解决以下优化问题:
I am solving the following optimization problem:
使用此 Python 代码:
with this Python code:
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
我收到一个错误
消息:'不等式约束不兼容'
message: 'Inequality constraints incompatible'
什么会导致这个错误?
推荐答案
问题似乎与您最初的猜测有关.如果我将您的起始值更改为
The issue seems to be with your initial guess. If I change your starting values to
x0 = [1.0, 1.0, 1.0]
然后你的代码会执行得很好(至少在我的机器上)
Then your code will execute fine (at least on my machine)
Python 3.5.1 (v3.5.1:37a07cee5969, 2015 年 12 月 6 日,01:54:25) [MSC v.1900 64 位 (AMD64)] 在 win32 上
Python 3.5.1 (v3.5.1:37a07cee5969, Dec 6 2015, 01:54:25) [MSC v.1900 64 bit (AMD64)] on win32
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
这篇关于Scipy 最小化约束函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文