Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

NonlinearConstraints in SciPy Optimize

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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'

>Solution :

There are a several things going wrong here:

  1. By setting jac=cons_J and hess=cons_H you are using the derivatives of the constraint function as objective derivatives, which probably is not what you want to do.
  2. The constraint hessian cons_H is wrong.
  3. Your constraint function is a function of three variables but your initial guess x0 lets minimize think you have an optimization problem of two variables.

After fixing all problems, your code could look like this:

import numpy as np
from scipy.optimize import Bounds, minimize, NonlinearConstraint

# objective and derivatives
def fun(x):
    return x[0]**2+x[1]**2-8*x[1]+16

def grad(x):
    return np.array([2*x[0], 2*x[1]-8, 0])

def hess(x):
    return np.array([[2, 0, 0], [0, 2, 0], [0, 0, 0]])

# constraint function and derivatives
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]*np.array([[2, 0, 0], [0, 2, 0], [0, 0, 0]])


# variable bounds
bounds = Bounds([0,0,0],[np.inf,np.inf,np.inf])

# constraint
con = NonlinearConstraint(cons_f, -np.inf, 6, jac=cons_J, hess=cons_H)

# initial guess
x0=[1,1,1]

res = minimize(fun, x0, method='trust-constr', jac=grad, hess=hess,
               constraints=[con], bounds=bounds)
1 comments
  1. What is the logic in the construction of cons_H?
    I well understand the construction of cons_J as gradient but why does cons_H depend on x and v?
    x makes complete sense to me, but why v? What is the v supposed to mean?

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading