scipy: SLSQP optimizer doesn't handle non-linear/linear constraints correctly

<<Please describe the issue in detail here, and for bug reports fill in the fields below.>>

I am trying to solve an engineering problem where I have a quadratic cost function and non linear equality and inequality constraints. I am using scipy SLSQP optimizer to get an optimum solution. The optimizer returns a solution saying the optimization terminated successfully. When I check the value of constraint equations at the optimum solution, I can clearly see that the constraints have not been satisfied; equality constraints are not equal to 0 (off by oder of integer) and some of the inequality constraints are less than zero (should have been greater than 0, again off by order of integer).

This led me to test the SLSQP optimization with a simple linear cost function (doesn’t matter even if I take a quadratic function) and linear inequality constraints. The SLSQP still returns a solution even though I made sure that the inequality constraints render the problem space infeasible i.e there is no common feasible region (values of the two variables that I used) for both the constraints. If the values of the inequality constraints are checked, one is ~0 and the other is ~ -1 (which is what should not have been if the optimizer is reporting an optimum solution successfully).

Just to test the same toy problem with other scipy libraries, I used ‘linprog’ to optimize the same cost function and same constraints. The ‘linprog’ reports an error: “Optimization failed. Unable to find a feasible starting point.

I wasn’t able to see a similar issue reported here and therefore I am reporting it.

It would be really appreciated if someone could help me understand what is happening here.

Reproducing code example:

<<A short code example that reproduces the problem. It should be self-contained, i.e., possible to run as-is via 'python myproblem.py'>>

from __future__ import division
import numpy as np
import math
from scipy.optimize import minimize


def cost(x):
    return -1 * x[0]  + 4 * x[1]

def ineqcons1(x):
    return x[1] - x[0] -1

def ineqcons2(x):
    return  x[0] - x[1]

x0 = (1,5)
bnds = ((-5,5), (-5,5))
con = ({'type':'ineq', 'fun': ineqcons1},{'type':'ineq', 'fun': ineqcons2})
res = minimize(cost, x0, method='SLSQP',  bounds = bnds, constraints=con, options={'maxiter' : 10000, 'ftol':0.0001 })
print "optimization result",res

for cons in con:
    print "constraints values for optimized variables", cons['fun'], cons['fun'](res.x), cons['type']

Error message:

<<Full error message, if any (starting from line Traceback: ...)>>

optimization result      fun: -11.000000016144568
     jac: array([-1.,  4.,  0.])
 message: 'Optimization terminated successfully.'
    nfev: 16
     nit: 8
    njev: 4
  status: 0
 success: True
       x: array([-5., -4.])
constraints values for optimized variables <function ineqcons1 at 0x112825938> -4.03614208722e-09 ineq
constraints values for optimized variables <function ineqcons2 at 0x1128259b0> -0.999999995964 ineq

Scipy/Numpy/Python version information:

<<Output from 'import sys, scipy, numpy; print(scipy.__version__, numpy.__version__, sys.version_info)'>>
('0.18.1', '1.12.0', sys.version_info(major=2, minor=7, micro=13, releaselevel='final', serial=0))

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Comments: 19 (11 by maintainers)

Most upvoted comments

This sounds similar to gh-6859, where SLSQP produces garbage with initial guess outside bounds. It is as if the algorithm assumes the initial guess is feasible, and returns garbage otherwise. . The FORTRAN code of the optimizer however supposedly has return codes for infeasibility, and does not mention such issue: . https://github.com/scipy/scipy/blob/master/scipy/optimize/slsqp/slsqp_optmz.f . The solution to this probably requires someone to audit what goes wrong in there.