[英]SciPy TypeError in a constrained minimization problem: 'missing 5 required positional arguments:'
I am trying to use SciPy to minimize a function:我正在尝试使用 SciPy 来最小化一个函数:
def effort(e,v1,v2,v3,v4,v5):
return -e
with respect to constraints (the part of the code in which constraints are defined compiles successfully).关于约束(定义约束的代码部分编译成功)。
Minimization itself:最小化本身:
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)
I used the following code as a sample:我使用以下代码作为示例:
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)
I am getting the following error:我收到以下错误:
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'
I clarified that effort
should be written without its arguments as an argument of minimize
.我澄清说
effort
应该在没有参数的情况下编写effort
作为minimize
的参数。
Also I clarified that both Jacobi and Hess matrices are optionas as arguments of minimize
.我还澄清说,雅可比矩阵和赫斯矩阵都是可选的,作为
minimize
参数。
I have searched for the error of type a function missing required positional arguments
, but I did not manage to find out why compilation fails in this case.我已经搜索了类型为
a function missing required positional arguments
的错误,但我没有设法找出在这种情况下编译失败的原因。 Please, help.请帮忙。
The code for rosen
(in the example you are working from): rosen
的代码(在您正在使用的示例中):
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
So while x0 = np.array([0.5, 0])
has two values, the function has only one positional argument, x
.因此,虽然
x0 = np.array([0.5, 0])
有两个值,但该函数只有一个位置参数x
。 Internally rosen
treats x
as an array.在内部,
rosen
将x
视为一个数组。
In a function like effort(e,v1,v2,v3,v4,v5)
, e
is the variable that minimize
changes, and v1,v2,...
are extra variables that are passed via the args
parameter.在类似
effort(e,v1,v2,v3,v4,v5)
的函数中, e
是minimize
变化的变量, v1,v2,...
是通过args
参数传递的额外变量。 minimize
does not vary those; minimize
不会改变那些; they are just 'settings'.它们只是“设置”。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.