Python 约束最小化问题中的SciPy类型错误:';缺少5个必需的位置参数:';

Python 约束最小化问题中的SciPy类型错误:';缺少5个必需的位置参数:';,python,optimization,scipy,typeerror,nonlinear-optimization,Python,Optimization,Scipy,Typeerror,Nonlinear Optimization,我正在尝试使用SciPy最小化函数: def effort(e,v1,v2,v3,v4,v5): return -e 关于约束(定义约束的代码部分成功编译) 最小化本身: from scipy.optimize import minimize x0 = np.array([0.5, 1,0,0,0,0]) res = minimize(effort, x0, method='trust-constr', constraints=[linear_cons

我正在尝试使用SciPy最小化函数:

def effort(e,v1,v2,v3,v4,v5):
    return -e
关于约束(定义约束的代码部分成功编译)

最小化本身:

from scipy.optimize import minimize

x0 = np.array([0.5, 1,0,0,0,0])
res = minimize(effort, x0, method='trust-constr',
                constraints=[linear_constraint, nonlinear_constraint],
                options={'verbose': 1}, bounds=bounds)
print(res.x)
我使用以下代码作为示例:

from scipy.optimize import minimize
from scipy.optimize import rosen, rosen_der, rosen_hess, rosen_hess_prod

x0 = np.array([0.5, 0])
res = minimize(rosen, x0, method='trust-constr', jac=rosen_der, hess=rosen_hess,
                constraints=[linear_constraint, nonlinear_constraint],
                options={'verbose': 1}, bounds=bounds)
print(res.x)
我得到以下错误:

TypeError                                 Traceback (most recent call last)
<ipython-input-3-9ebbcf16d7f7> in <module>
      5 res = minimize(effort, x0, method='trust-constr',
      6                 constraints=[linear_constraint, nonlinear_constraint],
----> 7                 options={'verbose': 1}, bounds=bounds)
      8 print(res.x)

/opt/conda/lib/python3.6/site-packages/scipy/optimize/_minimize.py in minimize(fun, x0, args, method, jac, hess, hessp, bounds, constraints, tol, callback, options)
    620         return _minimize_trustregion_constr(fun, x0, args, jac, hess, hessp,
    621                                             bounds, constraints,
--> 622                                             callback=callback, **options)
    623     elif meth == 'dogleg':
    624         return _minimize_dogleg(fun, x0, args, jac, hess,

/opt/conda/lib/python3.6/site-packages/scipy/optimize/_trustregion_constr/minimize_trustregion_constr.py in _minimize_trustregion_constr(fun, x0, args, grad, hess, hessp, bounds, constraints, xtol, gtol, barrier_tol, sparse_jacobian, callback, maxiter, verbose, finite_diff_rel_step, initial_constr_penalty, initial_tr_radius, initial_barrier_parameter, initial_barrier_tolerance, factorization_method, disp)
    330     # Define Objective Function
    331     objective = ScalarFunction(fun, x0, args, grad, hess,
--> 332                                finite_diff_rel_step, finite_diff_bounds)
    333 
    334     # Put constraints in list format when needed

/opt/conda/lib/python3.6/site-packages/scipy/optimize/_differentiable_functions.py in __init__(self, fun, x0, args, grad, hess, finite_diff_rel_step, finite_diff_bounds)
     73 
     74         self._update_fun_impl = update_fun
---> 75         self._update_fun()
     76 
     77         # Gradient evaluation

/opt/conda/lib/python3.6/site-packages/scipy/optimize/_differentiable_functions.py in _update_fun(self)
    162     def _update_fun(self):
    163         if not self.f_updated:
--> 164             self._update_fun_impl()
    165             self.f_updated = True
    166 

/opt/conda/lib/python3.6/site-packages/scipy/optimize/_differentiable_functions.py in update_fun()
     70 
     71         def update_fun():
---> 72             self.f = fun_wrapped(self.x)
     73 
     74         self._update_fun_impl = update_fun

/opt/conda/lib/python3.6/site-packages/scipy/optimize/_differentiable_functions.py in fun_wrapped(x)
     67         def fun_wrapped(x):
     68             self.nfev += 1
---> 69             return fun(x, *args)
     70 
     71         def update_fun():

TypeError: effort() missing 5 required positional arguments: 'v1', 'v2', 'v3', 'v4', and 'v5'
TypeError回溯(最近一次调用)
在里面
5 res=最小化(工作量,x0,方法='trust-constr',
6约束=[线性约束,非线性约束],
---->7选项={'verbose':1},界限=界限)
8印刷品(res.x)
/最小化中的opt/conda/lib/python3.6/site-packages/scipy/optimize//u minimize.py(fun、x0、args、method、jac、hess、hessp、bounds、constraints、tol、callback、options)
620返回最小信任区域施工(fun、x0、args、jac、hess、hessp、,
621边界、约束条件,
-->622回调=回调,**选项)
623 elif meth==“狗腿”:
624返回最小狗腿(趣味、x0、args、jac、hess、,
/opt/conda/lib/python3.6/site-packages/scipy/optimize//u trustregion\u constr/minimize\u trustregion\u constr.py in\u minimize\u trustregion\u constr(fun、x0、args、grad、hess、hessp、bounds、constraints、xtol、gtol、barrier、sparse\u jacobian、回调、maxiter、verbose、有限差分相关步长、初始构造惩罚、初始tr\u半径、初始barrier\u参数、初始barrier\u容差、因子分解法、disp)
330#定义目标函数
331目标=ScalarFunction(趣味、x0、args、grad、hess、,
-->332有限差分相对步,有限差分界)
333
334#需要时以列表格式放置约束
/opt/conda/lib/python3.6/site-packages/scipy/optimize//u differentiable\u functions.py in_uuu_u_uinit_uu(self、fun、x0、args、grad、hess、finite_diff\u rel\u step、finite_diff\u bounds)
73
74 self.\u update\u fun\u impl=update\u fun
--->75自我更新乐趣()
76
77#梯度评估
/opt/conda/lib/python3.6/site-packages/scipy/optimize//\u differentiable\u functions.py in\u update\u fun(self)
162定义更新乐趣(自我):
163如果未更新self.f_:
-->164 self.\u update\u fun\u impl()
165 self.f_updated=真
166
/更新中的opt/conda/lib/python3.6/site-packages/scipy/optimize//\u differentiable\u functions.py
70
71 def update_fun():
--->72 self.f=fun_包装(self.x)
73
74 self.\u update\u fun\u impl=update\u fun
/opt/conda/lib/python3.6/site-packages/scipy/optimize//\u differentiable\u functions.py in fun\u wrapped(x)
67 def fun_包装(x):
68.nfev+=1
--->69返回乐趣(x,*args)
70
71 def update_fun():
TypeError:efforce()缺少5个必需的位置参数:“v1”、“v2”、“v3”、“v4”和“v5”
我澄清了
efforce
应该在编写时不将其参数作为
minimize
的参数

我还澄清了Jacobi矩阵和Hess矩阵都是
最小化
的可选参数


我已经搜索了类型为
的错误,该类型的函数缺少必需的位置参数,但我没有找到在这种情况下编译失败的原因。请提供帮助。

rosen的代码(在您使用的示例中):

因此,虽然
x0=np.array([0.5,0])
有两个值,但函数只有一个位置参数,
x
。内部
rosen
x
视为一个数组


在类似
的函数中(e、v1、v2、v3、v4、v5)
e
最小化
更改的变量,
v1、v2、
是通过
args
参数传递的额外变量。
最小化
不会改变这些变量;它们只是“设置”。

您只将一个参数传递给努力函数,即6个元素的数组
def rosen(x):
    """
    The Rosenbrock function.

    The function computed is::

        sum(100.0*(x[1:] - x[:-1]**2.0)**2.0 + (1 - x[:-1])**2.0)

    Parameters
    ----------
    x : array_like
        1-D array of points at which the Rosenbrock function is to be computed.

    ....
    """
    x = asarray(x)
    r = numpy.sum(100.0 * (x[1:] - x[:-1]**2.0)**2.0 + (1 - x[:-1])**2.0,
                  axis=0)
    return r