Scipy: How can I use Bounds with trust-constr?

Viewed 4164

for my constrained Problem I want to use the Scipy-Trusted-Constr algorithm as I have a multivariable, constraint problem. I dont want /can't calculate the Jacobi/Hessian analytically, and compute it. However, when setting the bounds, the computation of the Jacobian crashes:

  File "C:\Python27\lib\site-packages\scipy\optimize\_trustregion_constr\tr_interior_point.py", line 56, in __init__
    self.jac0 = self._compute_jacobian(jac_eq0, jac_ineq0, s0)
  File "C:\Python27\lib\site-packages\scipy\optimize\_trustregion_constr\tr_interior_point.py", line 164, in _compute_jacobian
    [J_ineq, S]]))
  File "C:\Python27\lib\site-packages\numpy\matrixlib\defmatrix.py", line 1237, in bmat
    arr_rows.append(concatenate(row, axis=-1))
ValueError: all the input array dimensions except for the concatenation axis must match exactly

The error occurs both when using old style bounds and the newest Bounds object. I could reproduce the error with this code:

import numpy as np
import scipy.optimize as scopt

def RosenbrockN(x):
    result = 0
    for i in range(len(x)-1):
        result += 100*(x[i+1]-x[i]**2)**2+(1-x[i])**2
    return result
x0 = [0.0, 0.0, 0.0]
#bounds = scopt.Bounds([-2.0,-0.5,-2.0],[2.0,0.8,0.7])
bounds = [(-2.0,2.0),(-0.5,0.8),(-2.0,0.7)] 
Res = scopt.minimize(RosenbrockN, x0, \
                    method = 'trust-constr', bounds = bounds, \
                    jac = '2-point', hess = scopt.SR1())

I take it that I just misunderstand how bounds are set, but cant find my mistake. Advice is appreciated.

EDIT: I also tried the code example from the documentation which gave the same result. Other methods as SLSQP work well with bounds.

SciPy Version 1.1.0, Python Version 2.7.4, OS Win 7 Ent.

1 Answers

I removed your jac and hess arguments and got it to work; perhaps the problem lies there?

import numpy as np
import scipy.optimize as scopt

def RosenbrockN(x):
    result = 0
    for i in range(len(x)-1):
        result += 100*(x[i+1]-x[i]**2)**2+(1-x[i])**2
    return result

x0 = [0.0, 0.0, 0.0]
#bounds = scopt.Bounds([-2.0,-0.5,-2.0],[2.0,0.8,0.7])
bounds = [(-2.0,2.0),(-0.5,0.8),(-2.0,0.7)] 
Res = scopt.minimize(RosenbrockN, x0, \
                    method = 'SLSQP', bounds = bounds)
print(Res)

Result is

     fun: 0.051111012543332675
     jac: array([-0.00297706, -0.50601892, -0.00621008])
 message: 'Optimization terminated successfully.'
    nfev: 95
     nit: 18
    njev: 18
  status: 0
 success: True
       x: array([0.89475126, 0.8       , 0.63996894])
Related