I am trying to use SciPy to minimize a function:
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
.
Also I clarified that both Jacobi and Hess matrices are optionas as arguments of 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. Please, help.
The code for rosen
(in the example you are working from):
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
. Internally rosen
treats x
as an array.
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. minimize
does not vary those; they are just 'settings'.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.