I have a bounded optimization problem with nonlinear constraints that I am trying to solve. The nonlinear constraint function requires arguments and this is where I fail to make it work. Below is the structure I follow. How do I send the arguments arg3, arg4, arg5
to the constraint function cons
?
from scipy.optimize import (BFGS, SR1, Bounds, NonlinearConstraint, minimize)
def objfcn(x, arg1, arg2):
...
return out1
def cons(x, arg3, arg4, arg5):
...
return out2
bounds = Bounds([-d, -d, -d, -d], [d, d, d, d])
nonlinear_constraint = NonlinearConstraint(cons, 0.0, 0.0, jac='2-point', hess=BFGS())
res = minimize(objfcn,
x0,
args=(arg1, arg2),
method='trust-constr',
jac="2-point",
hess=SR1(),
constraints=[nonlinear_constraint]
options={'verbose': 1},
bounds=bounds)
EDIT: The current not so nice solution is that I pass the arguments to the constraint function cons() via global variables.
trust-constr
has a bit of a different approach than the other constrained solvers. I did not see direct way to pass args to the constraints. Of course, we can always try to package things in a class.