3

Tried solving an NLP using the scipy.optimize SLSQP. The problem is clearly infeasible but the minimize function in scipy.optimize seems to disagree.

minimize X^2 + Y^2 
subject to 
X + Y = 11
X, Y >= 6

The code:

from scipy.optimize import minimize

def obj(varx):
    return varx[1]**2 + varx[0]**2

def constr1(varx):
    constr1 = -varx[0]-varx[1]+11
    return constr1


bnds = [(6,float('Inf')),(6,float('Inf'))]
ops = ({'maxiter':100000, 'disp':'bool'})
cons = ({'type':'eq', 'fun':constr1})       
res = minimize(obj, x0=[7,7], method='SLSQP', constraints = cons, bounds = bnds, options = ops)

print res.x
print res.success

The output:

Optimization terminated successfully.    (Exit mode 0)
            Current function value: 72.0
            Iterations: 6
            Function evaluations: 8
            Gradient evaluations: 2
[ 6.  6.]
True

Am I missing something?

  • I have seen this bug before. Don't know how to fix this (apart from using a different solver). – Erwin Kalvelagen Mar 03 '17 at 12:27
  • Know any other reliable non linear solver? – FireyDonuts Mar 03 '17 at 16:52
  • See [here](http://scicomp.stackexchange.com/questions/83/is-there-a-high-quality-nonlinear-programming-solver-for-python) for a discussion about this. I am mainly doing large scale modeling, and their my main general purpose NLP solvers are CONOPT and IPOPT (among others). – Erwin Kalvelagen Mar 03 '17 at 19:15

1 Answers1

0

You can try mystic. It fails to solve the problem for infeasible solutions to the constraints. While not "obvious" (maybe), it returns an inf for infeasible solutions... I guess the behavior could be improved upon (I'm the author) to make it more obvious that only infeasible solutions are found.

>>> def objective(x):
...   return x[0]**2 + x[1]**2
... 
>>> equations = """
... x0 + x1 = 11
... """
>>> bounds = [(6,None),(6,None)]
>>> 
>>> from mystic.solvers import fmin_powell, diffev2
>>> from mystic.symbolic import generate_constraint, generate_solvers, simplify
>>>
>>> cf = generate_constraint(generate_solvers(simplify(equations)))
>>>
>>> result = fmin_powell(objective, x0=[10,10], bounds=bounds, constraints=cf, gtol=50, disp=True, full_output=True)
Warning: Maximum number of iterations has been exceeded
>>> result[1]
array(inf)
>>>
>>> result = diffev2(objective, x0=bounds, bounds=bounds, constraints=cf, npop=40, gtol=50, disp=True, full_output=True)
Warning: Maximum number of iterations has been exceeded
>>> result[1]
inf 
Mike McKerns
  • 33,715
  • 8
  • 119
  • 139