0

I'm having a constrained optimization problem, which i want to solve using the scipy.optimize package.

from scipy import optimize as opt
import numpy as np


def f(x):
    return (x[0]-5)**2 + (x[1]-6)**2

#Bounds and Linear Constraints 
bounds = opt.Bounds([0,0],[np.inf, np.inf])
lin_const = opt.LinearConstraint([[1,2],[0,0]], [-np.inf, 0], [4,0])

#Nonlinear Constraints, Jacobian and Hessian 
def cons_f(x):
    return [x[0]**2 - 4, np.exp(-x[0]) - 1]

def cons_J(x):
    return [[2*x[0], 0], [-np.exp(-x[0])]]

def cons_H(x, v):
    return v[0]*np.array([[2,0], [0,0]]) + v[1]*np.array([[np.exp(-x[0]), 0], [0,0]])

nonlin_const = opt.NonlinearConstraint(cons_f, -np.inf, 1, jac=cons_J, hess=cons_H)

#Solving the optimization problem
x0 = np.array([0.50, 0.75])
res = opt.minimize(f, x0, method='trust-constr',jac="2-point", hess=opt.SR1(), bounds=bounds, constraints=[lin_const, nonlin_const], options={'verbose': 1})
print(res.x)

I followed the Scipy doc closely but I get the typical numpy boolean index did not match indexed array along dimension 0; dimension is 1 but corresponding boolean dimension is 2 error message, what am I missing? Thanks for your help!

Here is the full Error Message:

IndexError                                Traceback (most recent call last)
Input In [32], in <cell line: 2>()
      1 x0 = np.array([0.50, 0.75])
----> 2 res = opt.minimize(f, x0, method='trust-constr',jac=cons_J, hess=opt.SR1(), bounds=bounds, constraints=[lin_const, nonlin_const], options={'verbose': 1})
      3 print(res.x)

File ~\anaconda3\envs\choquetclassifier\lib\site-packages\scipy\optimize\_minimize.py:634, in minimize(fun, x0, args, method, jac, hess, hessp, bounds, constraints, tol, callback, options)
    631     return _minimize_slsqp(fun, x0, args, jac, bounds,
    632                            constraints, callback=callback, **options)
    633 elif meth == 'trust-constr':
--> 634     return _minimize_trustregion_constr(fun, x0, args, jac, hess, hessp,
    635                                         bounds, constraints,
    636                                         callback=callback, **options)
    637 elif meth == 'dogleg':
    638     return _minimize_dogleg(fun, x0, args, jac, hess,
    639                             callback=callback, **options)

File ~\anaconda3\envs\choquetclassifier\lib\site-packages\scipy\optimize\_trustregion_constr\minimize_trustregion_constr.py:361, in _minimize_trustregion_constr(fun, x0, args, grad, hess, hessp, bounds, constraints, xtol, gtol, barrier_tol, sparse_jacobian, callback, maxiter, verbose, finite_diff_rel_step, initial_constr_penalty, initial_tr_radius, initial_barrier_parameter, initial_barrier_tolerance, factorization_method, disp)
    357     prepared_constraints.append(PreparedConstraint(bounds, x0,
    358                                                    sparse_jacobian))
    360 # Concatenate initial constraints to the canonical form.
--> 361 c_eq0, c_ineq0, J_eq0, J_ineq0 = initial_constraints_as_canonical(
    362     n_vars, prepared_constraints, sparse_jacobian)
    364 # Prepare all canonical constraints and concatenate it into one.
    365 canonical_all = [CanonicalConstraint.from_PreparedConstraint(c)
    366                  for c in prepared_constraints]

File ~\anaconda3\envs\choquetclassifier\lib\site-packages\scipy\optimize\_trustregion_constr\canonical_constraint.py:352, in initial_constraints_as_canonical(n, prepared_constraints, sparse_jacobian)
    350     finite_ub = ub < np.inf
    351     c_ineq.append(f[finite_ub] - ub[finite_ub])
--> 352     J_ineq.append(J[finite_ub])
    353 elif np.all(ub == np.inf):
    354     finite_lb = lb > -np.inf

IndexError: boolean index did not match indexed array along dimension 0; dimension is 1 but corresponding boolean dimension is 2
  • I think the error message should already give you a hint, together with the traceback: Boolean index doesn't match: that means a boolean index is used whose size does not match the array it is indexing. From the traceback, we see the failure is happening in `J[finite_ub]`, the jacobian. So check the dimensionality of what your Jacobian returns. It seems to me that you return something like `[[x,y],z]`, which doesn't even have a shape... I haven't looked into this deeply enough to know what dimensionality would be correct (the docs should tell you), but your Jacobian is clearly wrong. – burnpanck Jul 13 '22 at 14:05
  • Thanks @burnpanck , I am literally seeing it now! – Glenbreaks Jul 13 '22 at 14:12

0 Answers0