scipy: minimize returns success with a result full of nans
scipy.minimize(method='SLSQP')
returns success even though the result is nan
. Note that the constraints are not satisfiable.
Reproducing code example:
from scipy.optimize import minimize
import numpy as np
if __name__ == "__main__":
np.random.seed(10)
cons = ({'type': 'ineq', 'fun': lambda x: -x[0] - x[1] - 3},
{'type': 'ineq', 'fun': lambda x: x[1] + x[2] - 2})
bnds = ((-2, 2), (-2, 2), (-2, 2))
target = lambda x: 1
res = minimize(target, [-1.8869783504471584, -0.640096352696244, -0.8174212253407696], method='SLSQP', bounds=bnds, constraints=cons, options={'disp':False, 'maxiter':10000})
for i, c in enumerate(cons):
print("the value of constraint function {} is {}".format(i, c['fun'](res.x)))
print(res)
Error message:
the value of constraint function 0 is nan
the value of constraint function 1 is nan
fun: 1.0
jac: array([0., 0., 0.])
message: 'Optimization terminated successfully.'
nfev: 106
nit: 10
njev: 9
status: 0
success: True
x: array([nan, nan, nan])
Scipy/Numpy/Python version information:
scipy 1.1.0
numpy 1.15.4
python 3.6.6
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Comments: 21 (15 by maintainers)
We put an explicit bound, equality, and inequality constraint check at the end of
linprog
. If anything does not satisfy the tolerance,linprog
reports failure. That might be useful forminimize
.