Identifying the the exact constraint with NaN when solving with Ipopt

3,160 views
Skip to first unread message

NE

unread,
Jan 26, 2018, 4:00:27 AM1/26/18
to CasADi
Dear all,

First of all, I would like to thank the CasADi developers for your great work with CasADi.

I am currently trying to solve a dynamic optimization problem using direct collocation in CasADi version 3.3.0, MatlabR2014b- version. While using IPOPT to solve the resulting NLP, I get the following message after 40 iterations:

  39 -1.3410974e+00 3.37e+01 2.89e+08   2.8 1.24e+05    -  1.82e-04 3.29e-05h  1
iter    objective    inf_pr   inf_du lg(mu)  ||d||  lg(rg) alpha_du alpha_pr  ls
  40r-1.3410974e+00 3.37e+01 1.00e+03   2.8 0.00e+00    -  0.00e+00 1.66e-07R  2
CasADi - 2018-01-19 10:57:59 WARNING("solver:nlp_g failed: NaN detected for output g, at (row 1006, col 0).") [.../casadi/core/oracle_function.cpp:249]
Warning: Cutting back alpha due to evaluation error
CasADi - 2018-01-19 10:57:59 WARNING("solver:nlp_g failed: NaN detected for output g, at (row 1006, col 0).") [.../casadi/core/oracle_function.cpp:249]

This warning message persists until IPOPT stops and says it could not find a solution.

Now, I am trying to figure out which constraint is actually causing the problem by looking at (row 1006, col 0). I have the following questions:

1. Which object's row and column is CasADi referring to? Is it the vector of the constraints g (i.e. collocation and continuity constraints)? Is it the jacobian matrix?

2. How is the indexing done on the rows of this object (constraint g or jacobian) in CasADi's matlab version? Does the row indexing start from 0 or 1? That is, is it [0 1 2 ... 1006] or [1 2 ... 1006]?

I would be very grateful if you can help me. Thanks!

Joris Gillis

unread,
Jan 26, 2018, 4:38:43 AM1/26/18
to CasADi
Hi!

It's the vector of constraints. ('output g')
The reported indexing is 0-based, even in matlab.

Best regards,
  Joris
Message has been deleted

NE

unread,
Jan 26, 2018, 5:07:46 AM1/26/18
to CasADi
Thanks a lot for the fast response and help Joris. This has clarified a lot for me.

NE

unread,
Jan 26, 2018, 5:12:37 AM1/26/18
to CasADi
Hello Joris,

It happens that this constraint has a square root term in it. Is there I can track or find out the exact numerical evaluation of this constraint in CasADi after iteration 40? 

Do you have some tips on how to know what is actually going on during a specific iterate in CasADi when using iPOPT? Thanks.

Joris Gillis

unread,
Jan 26, 2018, 5:26:46 AM1/26/18
to CasADi
Hi!

You may pass the monitor options to nlpsol:
opts.monitor = char('nlp_g');

NE

unread,
Jan 26, 2018, 5:27:46 AM1/26/18
to CasADi
Super. Thanks.

NE

unread,
Feb 9, 2018, 6:52:50 AM2/9/18
to CasADi
I'm still working on fixing this problem and I have another question. I found out that this constraint has a square root function in it.

What might be the reason why solver:nlp_g is failing and a NaN is detected for output g?

Could this have anything to do with derivative calculations by casADi or just simply a function evaluation?

Thanks for your reply.

Mikhail Katliar

unread,
Feb 13, 2018, 9:30:26 AM2/13/18
to CasADi
Hello NE,

the squared roots are nasty because their derivative goes to infinity as the argument approaches 0. Ipopt uses the gradient of the constraints, hence this might be your case.

пятница, 9 февраля 2018 г., 12:52:50 UTC+1 пользователь NE написал:

NE

unread,
Feb 21, 2018, 7:29:50 AM2/21/18
to CasADi
Thanks a lot for the clarification Mikhail.

OJ adukwu

unread,
Feb 10, 2020, 2:16:12 PM2/10/20
to CasADi
What do one do in this case now? I have similar problem. When I multiply the term inside the root by certain factor, the error message no longer appears. But this will change my model hence my result is not correct.

Thanks

Bruno M L

unread,
Feb 10, 2020, 2:43:53 PM2/10/20
to CasADi
Hi!

you could try sqrt(max(eps, x)), with eps being a small positive number. Then, inside will always be a positive number and the optimization wont crash.

After the problems converges, check if x is negative. If it is, then it has converged to a weird solution.

I hope this helps.

Nathan Lazarus

unread,
Jul 23, 2020, 12:52:30 AM7/23/20
to CasADi
I came across this searching for the same error, and thought I should post a great trick I was taught: use a second degree taylor polynomial to approximate the square root function at some small value epsilon > 0, making your function defined globally but fall off very fast for negative values.
Reply all
Reply to author
Forward
0 new messages