what might lead to "constraint function returned Nan"?
Show older comments
Hi everyone,
My problem is that during an optimization problem ,in which I use fmincon, the solver does the iteration #0 , it does also the first and after it doesn't stops anymore to evaluate the gradient. Indeed it continue trying to find a minimum but it doesn't stop until I got this sentence in the editor
User constraint function returned NaN; trying a new point...
2 192 9.556505e-01 0.000e+00 5.412e-06 6.386e-06 1.614e+02
Then it continues telling me that the constraint function returned NaN for the 3 succeeding iteration and after it stops.
The problem is that my constrants (that are what my constraint function returns) are not NaN. These constraints are actually good, I have 3 inequality constraints and they are satisfied ( I know this because I save the constraints at each iteration ).
When it stops it says
Local minimum possible. Constraints satisfied
However the minimization is not finished.
Then what might lead me to "constraint function returned Nan"? Thank you all
EDIT SOLUTION
My solution is to impose invalid constraints when a NaN is found. In this way fmincon will not accept that value and it will just discard the guess. Thank you all
1 Comment
dpb
on 22 Dec 2015
Well, w/o the functions themselves it's hard to say but it's likely over the search range the evaluation is attempted beyond the range of the constraint such that the function does do something like divide 0/0 or the like. It's not just dividing by zero as that would return Inf, not NaN, nor is it an exponential w/ a large argument as that would be a zero.
Answers (2)
Alan Weiss
on 22 Dec 2015
0 votes
fmincon tried to take a step, and when it evaluated your nonlinear constraint functions at the new point, at least one of them returned NaN. You say that "...my constrants (that are what my constraint function returns) are not NaN." However, did you try to evaluate them where fmincon tried to step? I guarantee that at least one nonlinear constraint function returns a NaN there.
Check your nonlinear constraint function definitions and see if it is possible that they can return NaN at some locations. Sometimes you can tighten bounds so that they cannot. Sometimes it is unavoidable that they return NaN. In that case, perhaps you can start fmincon at a different initial point x0.
Good luck,
Alan Weiss
MATLAB mathematical toolbox documentation
Marco Palermo
on 22 Dec 2015
Edited: Marco Palermo
on 22 Dec 2015
3 Comments
dpb
on 22 Dec 2015
Again, w/o the constraint functions we can't tell much about where they would return NaN nor what would be a reasonable value to return in the cases that are causing it; generally one can find a limit although that isn't necessarily so depending on the function.
Marco Palermo
on 22 Dec 2015
dpb
on 22 Dec 2015
"without" We can't say anything about your constraint functions when we don't know what they are...
Categories
Find more on Choose a Solver in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!