I'm trying to use the optimization module in SciPy, just writing short trial programs. I can get solutions when there are linear constraints, but the Hessian definition just doesnt work. I've used the example on this site but I get an error when try not to use the built-in Rosenberg function and its hessian.
Also tried with a simple problem found online, my code being:
import numpy as np
from scipy import optimize
from scipy.optimize import NonlinearConstraint
def fun(x):
return x[0]**2+x[1]**2-8*x[1]+16
bounds = optimize.Bounds([0,0,0],[np.inf,np.inf,np.inf])
def cons_f(x):
return x[0]**2+x[1]**2+x[2]
def cons_J(x):
return [2*x[0],2*x[1],1]
def cons_H(x,v):
return v[0]*[2,2,0]
nonlinear_constraint = optimize.NonlinearConstraint(cons_f, -np.inf, 6, jac=cons_J, hess=cons_H)
x0=[1,1]
res = optimize.minimize(fun, x0, method='trust-constr', jac=cons_J, hess=cons_H,
constraints=[nonlinear_constraint],
options={'verbose': 1}, bounds=bounds)
print(res.x)
I get the following error for both cases:
Traceback (most recent call last):
File "C:\Users\user\OneDrive - EOP\Escritorio\Test.py", line 19, in <module>
res = optimize.minimize(fun, x0, method='trust-constr', jac=cons_J, hess=cons_H,
File "C:\Users\user\AppData\Local\Programs\Python\Python39\lib\site-packages\scipy\optimize\_minimize.py", line 634, in minimize
return _minimize_trustregion_constr(fun, x0, args, jac, hess, hessp,
File "C:\Users\user\AppData\Local\Programs\Python\Python39\lib\site-packages\scipy\optimize\_trustregion_constr\minimize_trustregion_constr.py", line 332, in _minimize_trustregion_constr
objective = ScalarFunction(fun, x0, args, grad, hess,
File "C:\Users\user\AppData\Local\Programs\Python\Python39\lib\site-packages\scipy\optimize\_differentiable_functions.py", line 163, in __init__
self.H = hess(np.copy(x0), *args)
TypeError: cons_H() missing 1 required positional argument: 'v'