[英]Scipy minimize constrained function
I am solving the following optimization problem: 我正在解决以下优化问题:
with this Python code: 使用此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
I am getting an error 我收到了一个错误
message: 'Inequality constraints incompatible'
消息:'不平等约束不兼容'
What can cause this error? 什么可能导致此错误?
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, Dec 6 2015, 01:54:25) [MSC v.1900 64 bit (AMD64)] on win32
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
Scipy's optimize module has lots of options. Scipy的优化模块有很多选项。 See the documentation or this tutorial .
请参阅文档或本教程 。 Since you didn't specify the method here, it will use Sequential Least SQuares Programming (
SLSQP
). 由于您未在此处指定方法,因此将使用Sequential Least SQuares Programming(
SLSQP
)。 Alternatively, you could use the Trust-Region Constrained Algorithm ( trust-const
). 或者,您可以使用Trust-Region约束算法(
trust-const
)。
For this problem, I found that trust-const
seemed much more robust to starting values than SLSQP
, handling starting values from [-2,-2,-2]
to [10,10,10]
, although negative initial values resulted in increased iterations, as you'd expect. 对于这个问题,我发现
trust-const
对于起始值似乎比SLSQP
更稳健,处理从[-2,-2,-2]
到[10,10,10]
,尽管负初始值导致增加迭代,正如您所期望的那样。 Negative values below -2
exceeded the max iterations, although I suspect might still converge if you increased max iterations, although specifying negative values at all for x1
and x3
is kind of silly, of course, I just did it to get a sense of how robust it was to a range of starting values. 低于
-2
负值超过了最大迭代次数,尽管我怀疑如果你增加了最大迭代次数可能仍会收敛,虽然为x1
和x3
指定负值是有点傻,当然,我只是去了解它是如何理解的强大的是一系列起始值。
The specifications for SLSQP
and trust-const
are conceptually the same, but the syntax is a little different (in particular, note the use of NonlinearConstraint
). SLSQP
和trust-const
的规范在概念上是相同的,但语法略有不同(特别注意使用NonlinearConstraint
)。
from scipy.optimize import minimize, NonlinearConstraint, SR1
def f(x):
return math.log(x[0]**2 + 1) + x[1]**4 + x[0]*x[2]
constr_func = lambda x: np.array( [ x[0]**3 - x[1]**2 - 1,
x[0],
x[2] ] )
x0=[0.,0.,0.]
nonlin_con = NonlinearConstraint( constr_func, 0., np.inf )
res = minimize( f, x0, method='trust-constr',
jac='2-point', hess=SR1(),
constraints = nonlin_con )
Here are the results, edited for conciseness: 以下是结果,为简洁而编辑:
fun: 0.6931502233468916
message: '`gtol` termination condition is satisfied.'
x: array([1.00000063e+00, 8.21427026e-09, 2.40956900e-06])
Note that the function value and x values are the same as in @CoryKramer's answer. 请注意,函数值和x值与@ CoryKramer的答案相同。 The x array may look superficially different at first glance, but both answers round to
[1, 0, 0]
. x数组乍一看可能看起来不同,但两个答案都是
[1, 0, 0]
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.