C Code NLP

1,642 views
Skip to first unread message

Pedro Roque

unread,
Oct 4, 2017, 4:28:55 PM10/4/17
to CasADi
Hello everyone!

I am trying to use CasADi in the following way: I have to solv an NMPC problem on Matlab, but the computer where I am deploying it cannot have other libs installed. So, my approach would be: have a compiled C code that would take as arguments the Desired and Current positions, Sampling time and other params, then over MEX pass this params to the compiled code and extract the solution using, again, MEX. 

So far, I tried to compile the C code examples for nlp_generate , but that did not work (missing functions, which I found that don't exist on the libraries). What would you suggest me to do?

All the best!
Pedro,

Joel Andersson

unread,
Oct 4, 2017, 7:21:21 PM10/4/17
to CasADi
Hi Pedro,

I don't think I'm able to follow exactly what you are trying to do. What are you trying to generate C code for? The QP matrices? Or the whole step? Which solver (QP or NLP?) are you trying to use to solve the QP/NLP? Which example did you try?

Best regards,
Joel

Pedro Roque

unread,
Oct 4, 2017, 7:50:13 PM10/4/17
to CasADi
Hi Joel,

Thank you for your reply! Here is the example I tried to compile: https://github.com/casadi/casadi/blob/master/docs/examples/cplusplus/nlp_codegen.cpp , and the errors I got: https://github.com/casadi/casadi/issues/2092 .

Regarding what I am trying to accomplish:

On the system on which I want to solve the NMPC (and so, NLP solver is what I will use), I only have access to a Matlab front-end on which I cannot install CasADi. However, I can use a binary file (a compiled code) that solves the nonlinear problem (the optimization step), and call this from a matlab script (if, for instance, I compile it with mex and pass/receive parameters with the mexFunction).

So, what I want to do is, on a very simple matlab script, call the binary file with (created with mex) and pass as arguments a desired and current state, as well as other parameters for the solver. The output of this function would be a control signal that then would be used. This binary with me statically compiled, so that it does not have external dependecies.

Tell me if it is not clear, or if there is anything else you need to know!

Best regards,
Pedro Roque

Joel Andersson

unread,
Oct 5, 2017, 6:25:30 PM10/5/17
to CasADi
Hi Pedro,

So your controller (in MATLAB) cannot be using CasADi, only generated MEX code from CasADi?

In the nlp_codegen example, C code for the NLP, including derivative information, is generated and then read in and solved in a separate location. But you need to use CasADi in the controller since only the NLP is c code generated, not the NLP solver (IPOPT). CasADi has no way of including IPOPT into generated C code. So you would at least compile the C++ version of CasADi to be able to call the optimizer this way. We hope to eventually C code generate entire solvers for QP and NLP, but we're not there yet.

One way forward, is to write your own simple NLP solver using a Gauss-Newton method. You can find an example of how to do it here: https://gist.github.com/jaeandersson/f8293aceb5d3ce465e97fe09456ab4ed. It's part of an exercise for the upcoming new edition of the MPC book by Rawlings et al. This code can be modified so that the optimizer does not have any dependency on CasADi. You would need to rewrite the function "sqpstep" so that it calls generated MEX functions instead of self.fun.g, self.fun.J and self.fun.H and you'd need to use a different QP solver, for example quadprog in MATLAB.

Best regards,
Joel



Pedro Roque

unread,
Oct 5, 2017, 8:01:19 PM10/5/17
to CasADi
Hi Joel,

What I wanted to do was to compile it using static links. I would hope that, by doing so, all the needed files from IPOPT and CasADi libs would be incorporated on such binary. Is this not possible?

Due to time constraints I am not sure if there is a viable way to implement such algorithm, but I will give it a look! Is there any other suggestion (or other framework) I could use that I would allow me to do this?

Best regards,
Pedro

Joel Andersson

unread,
Oct 6, 2017, 10:43:56 AM10/6/17
to CasADi
It is certainly possible to compile CasADi using static linkage and link with your binary - IPOPT (and it's linear solver) might be much tricker however. Anyway, I'm not sure if I'd go down that rabbit hole.. For code generating an NLP solver for NMPC, why don't you have a look at ACADOS: https://github.com/acados/acados. It uses CasADi for the model equations and to calculate derivatives, but then code generates its own NLP solver.

Best regards,
Joel

Pedro Roque

unread,
Oct 6, 2017, 2:07:01 PM10/6/17
to CasADi
Thank you a lot for the suggestion Joel!

Regarding the using the CasADi such in the example of nlp_generate, I am not able to compile and hence the first question - can you tell me why am I getting those errors? Is there any misisng lib or version that I should have?

Best regards,
Pedro Roque

Joel Andersson

unread,
Oct 6, 2017, 5:47:12 PM10/6/17
to CasADi
Which errors? You need to be more specific. Joel

Pedro Roque

unread,
Oct 6, 2017, 5:51:45 PM10/6/17
to CasADi

Joel Andersson

unread,
Oct 8, 2017, 5:53:53 PM10/8/17
to CasADi
OK, I've answered there. You probably aren't compiling the example correctly.

Pedro Roque

unread,
Oct 10, 2017, 3:22:51 PM10/10/17
to CasADi
Answered there, that might be true! I also sent you an email explaning some stuff. Thank you a lot for your help!

Best,
Pedro

Pedro Roque

unread,
Oct 10, 2017, 5:07:11 PM10/10/17
to CasADi
Updated on https://github.com/casadi/casadi/issues/2092#issuecomment-335600300

Managed to compile it, but cannot run it, wrong exec type... 

Joel Andersson

unread,
Oct 10, 2017, 5:18:26 PM10/10/17
to CasADi
Please do not cross-post. Joel
Message has been deleted

Pedro Roque

unread,
Oct 10, 2017, 8:06:31 PM10/10/17
to CasADi
Thank you Joel

I managed to run the example, but now I cannot statically link it - as I am using .so which are dynamic links. You have any idea of how to do this?
Also, I realized that on Windows we have .dll.a for libcasadi , and so that might solve my problem - I just need to point the compiler (I will most likely use MinGW for this) to the casadi binaries folder, from the Python package. What do you think of this? (ultimately, I will have to run things on Windows...)

Best regards,

terça-feira, 10 de Outubro de 2017 às 17:18:26 UTC-4, Joel Andersson escreveu:
Please do not cross-post. Joel

Pedro Roque

unread,
Oct 11, 2017, 10:42:20 AM10/11/17
to CasADi
So, I managed to compile it on windows with:
g++ -c -m32 -std=c++11 -I"C:\MIT_Casadi\casadi\include" nlp_codegen.cpp -o nlp

and linking with
g++ -m32 nlp -static -L"C:\MIT_Casadi\casadi" -l"casadi.dll" -o test.exe

But even after defining casadi as static, it complained about not finding casadi lib. Do you have any suggestion why it is so?

Best,
Pedro

Pedro Roque

unread,
Oct 11, 2017, 7:26:34 PM10/11/17
to CasADi
Updates:  I can now run the example in a somewhat isolated form, by putting the needed libs on the same folder as the generated .exe .

However, on starting to code the NMPC problem, I have some doubts:
1) On the nlp_codegen example, "f" is the function ot minimize (the cost function), but in other examples (nmpc_ipopt.py) F is used as the dynamics function - which is right?
2) I want to use a system dynamics in the DAE form, therefore I should use the DaeBuilder, can you confirm this?
3) I shall define as SX::sym my state and control input, or only the control input? 
4) How shall I build the graph for the dynamics over an horizon NU? I was trying to do it as suggested on the rocket_ipopt example, but I did not understand how the whole dynamics are simulated for all the iterations, or how they are not all used (also they are put in the G function and not in F)

Thank you in advance for any info!

Best regards,
Pedro

Joel Andersson

unread,
Oct 12, 2017, 1:19:16 PM10/12/17
to CasADi

On Wednesday, October 11, 2017 at 6:26:34 PM UTC-5, Pedro Roque wrote:
Updates:  I can now run the example in a somewhat isolated form, by putting the needed libs on the same folder as the generated .exe .

However, on starting to code the NMPC problem, I have some doubts:
1) On the nlp_codegen example, "f" is the function ot minimize (the cost function), but in other examples (nmpc_ipopt.py) F is used as the dynamics function - which is right?

It should be clear from the context.
 
2) I want to use a system dynamics in the DAE form, therefore I should use the DaeBuilder, can you confirm this?

No, just using a DAE integrator, or implement a direct collocation method with support for algebraic states should be fine.
 
3) I shall define as SX::sym my state and control input, or only the control input? 

It depends on your optimal control problem transcription strategy. You'll find some information about it in the chapter called "Optimal control with CasADi" in the user guide. In general I advise you to get a good book on the subject and try to read up on it before trying to implement it.

4) How shall I build the graph for the dynamics over an horizon NU? I was trying to do it as suggested on the rocket_ipopt example, but I did not understand how the whole dynamics are simulated for all the iterations, or how they are not all used (also they are put in the G function and not in F)

Doing optimal control with CasADi assumes that you have an understanding of the transcription methods. Please start by reading up on the subject.

Joel
Reply all
Reply to author
Forward
0 new messages