what might lead to "constraint function returned Nan"?

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

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.

Sign in to comment.

Answers (2)

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
Ok, I have putted that if it found a NaN all the inequality constraints are positive then fmincon should see a point in which it can't go.
May this be a solution for the problem in your opinion? However during the optimization process I have found other NaN from the constraint function but it didn't lead me to the "Trying new point...". Ideas about that?
Thank you all
EDIT
User constraint function returned NaN; trying a new point...
2 143 9.801074e-01 0.000e+00 3.120e-07 1.468e-06 1.452e+06
Local minimum possible. Constraints satisfied.
Just got this. The optimization process is not finished. It should take about 6 or 7 iteration to be completed. However now I'm going to rerun it with the above "solution" and I hope it will work. Tell me if you have any idea about this "result".

3 Comments

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.
"without" We can't say anything about your constraint functions when we don't know what they are...

Sign in to comment.

Asked:

on 22 Dec 2015

Edited:

on 23 Dec 2015

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!