[英]Python minimize function: passing additional arguments to constraint dictionary
I don't know how to pass additional arguments through the minimize function to the constraint dictionary. 我不知道如何通过最小化函数将其他参数传递给约束字典。 I can successfully pass additional arguments to the objective function.
我可以成功地将其他参数传递给目标函数。
Documentation on minimize function is here: http://docs.scipy.org/doc/scipy/reference/generated/scipy.optimize.minimize.html#scipy.optimize.minimize 关于最小化功能的文档在这里: http : //docs.scipy.org/doc/scipy/reference/generated/scipy.optimize.minimize.html#scipy.optimize.minimize
The constraints argument is a dict that has a field 'args', where args is a sequence. constraints参数是一个包含字段'args'的字典,其中args是一个序列。 I'm sure this is where I need to pass in the additional arguments but I don't know the syntax.
我确定这是我需要传递其他参数但我不知道语法的地方。 The closest I have got is below:
我最接近的是:
from scipy.optimize import minimize
def f_to_min (x, p):
return (p[0]*x[0]*x[0]+p[1]*x[1]*x[1]+p[2])
f_to_min([1,2],[1,1,1]) # test function to minimize
p=[] # define additional args to be passed to objective function
f_to_min_cons=({'type': 'ineq', 'fun': lambda x, p : x[0]+p[0], 'args': (p,)}) # define constraint
p0=np.array([1,1,1])
minimize(f_to_min, [1,2], args=(p0,), method='SLSQP', constraints=f_to_min_cons)
I get the following error 我收到以下错误
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
<ipython-input-19-571500063c9e> in <module>()
1 p0=np.array([1,1,1])
----> 2 minimize(f_to_min, [1,2], args=(p0,), method='SLSQP', constraints=f_to_min_cons)
C:\Python27\lib\site-packages\scipy\optimize\_minimize.pyc in minimize(fun, x0, args, method, jac, hess, hessp, bounds, constraints, tol, callback, options)
356 elif meth == 'slsqp':
357 return _minimize_slsqp(fun, x0, args, jac, bounds,
--> 358 constraints, **options)
359 else:
360 raise ValueError('Unknown solver %s' % method)
C:\Python27\lib\site-packages\scipy\optimize\slsqp.pyc in _minimize_slsqp(func, x0, args, jac, bounds, constraints, maxiter, ftol, iprint, disp, eps, **unknown_options)
298 # meq, mieq: number of equality and inequality constraints
299 meq = sum(map(len, [atleast_1d(c['fun'](x, *c['args'])) for c in cons['eq']]))
--> 300 mieq = sum(map(len, [atleast_1d(c['fun'](x, *c['args'])) for c in cons['ineq']]))
301 # m = The total number of constraints
302 m = meq + mieq
<ipython-input-18-163ef1a4f6fb> in <lambda>(x, p)
----> 1 f_to_min_cons=({'type': 'ineq', 'fun': lambda x, p : x[0]+p[0], 'args': (p,)})
IndexError: list index out of range
I'm accessing the first element of the additional parameter so I shouldn't have an out of range error. 我正在访问附加参数的第一个元素,所以我不应该超出范围错误。
If you remove the constraints=f_to_min_cons argument from the minimize function then the code above works. 如果从最小化函数中删除constraints = f_to_min_cons参数,则上面的代码可以正常工作。
The answer simply, is that p = [] has no elements and no length, and so p[0] is out of bounds. 答案很简单,就是p = []没有元素而没有长度,因此p [0]超出范围。
The following, where we set p = [0], runs without error. 以下,我们设置p = [0],运行没有错误。 What p should actually hold is of course, not something that we can answer with the information given.
实际应该持有什么,当然不是我们可以用给出的信息来回答的。
import numpy as np
from scipy.optimize import minimize
def f_to_min (x, p):
return (p[0]*x[0]*x[0]+p[1]*x[1]*x[1]+p[2])
f_to_min([1,2],[1,1,1]) # test function to minimize
p=[0] # define additional args to be passed to objective function
f_to_min_cons=({'type': 'ineq', 'fun': lambda x, p : x[0]+p[0], 'args': (p,)},) # define constraint
p0=np.array([1,1,1])
minimize(f_to_min, [1,2], args=(p0,), method='SLSQP', constraints=f_to_min_cons)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.