Can I send arguments to a constraint function using scipy.optimize.NonlinearConstraint?

1.6k views Asked by At

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.

1

There are 1 answers

1
Erwin Kalvelagen On BEST ANSWER

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.

from scipy.optimize import (BFGS, SR1, Bounds, NonlinearConstraint, minimize)

class problem:
  arg1 = 1
  arg2 = 2
  arg3 = 3

  def f(self,x):
    return -x[0]*self.arg1-x[1]*self.arg2

  def g(self,x):
    return x[0]-self.arg3*x[1]

p = problem()


bounds = Bounds([0,0], [2,3])
nonlinear_constraint = NonlinearConstraint(p.g, 0.0, 0.0, jac='2-point', hess=BFGS())
res = minimize(p.f,
                x0=[0,0],
                method='trust-constr',
                jac="2-point",
                hess=SR1(),
                constraints=[nonlinear_constraint],
                options={'verbose': 1},
                bounds=bounds)
res