generating dependencies for combined (integrator and solver)

405 views
Skip to first unread message

Akash Garg

unread,
Aug 9, 2021, 5:32:00 PM8/9/21
to CasADi
Hi everyone,

Firstly thank you to CasADi developers for providing to us this amazing library.

I'm  solved an MPC problem using CasADI with RK4 integrator in MATLAB. Now I want to generate C code for my MPC problem. I have generated c file for the solver and then shared lib file using:
solver = nlpsol('solver', 'ipopt', nlp_prob,opts);
solver.generate_dependencies('nlpN3.c') 
gcc -shared -o nlpN3.so -fPIC nlpN3.c

I  have a horizon of length N for MPC. So I construct the cost and equality constraint on state (using RK4 integrator.....that I call function f......for each horizon) in a for loop similarly as given in the examples. 
My question is after I export the c file for solver, does it capture the f function inherently as well (RK4 integrator)?
I assume no, so I export c file for function f separately and load it in my C code using external command. But how can I combine the two shared libraries (solver and f function) in C code? The problem in combining the two .so files is because solver library takes arg_x, arg_p, arg_lbx, arg_ubx, arg_lbg, arg_ubg and gives the solution then where in between can I insert the .so file for RK4 integrator.

Thanks in advance
Akash

Joris Gillis

unread,
Aug 9, 2021, 6:13:19 PM8/9/21
to CasADi
Dear Akash,

To use RK4 with codegen, add a 'simplify' true option at constructor time. The c file will then by compilable and contain the RK4 integrator within.

Best regards,
  Joris

Akash Garg

unread,
Aug 10, 2021, 6:00:33 AM8/10/21
to CasADi
Dear Joris, 

Thank you for your reply.

But I write rhs of ode and call the ode four times inside the for loop. I do this because I also pass the parameters related to dynamics (which are dependent on state and therefore kept as symbolic) of my robot. Where in this, can I add the 'simplify' option? 

Regards,
Akash

Joris Gillis

unread,
Aug 10, 2021, 4:08:40 PM8/10/21
to CasADi
I assumed you where using the builtin RK4 integrator, for which options can be added at construction cfr https://github.com/casadi/casadi/blob/3.5.5/docs/examples/matlab/direct_multiple_shooting.m#L51-L52 ('cvodes' can be replaced by 'rk').

you are already code-exporting that without extra work

  Joris

Akash Garg

unread,
Aug 10, 2021, 6:05:17 PM8/10/21
to CasADi
Hi Joris,

Thank you clarifying the doubt. 

However I am facing a strange issue. I call the solver in C++ using the following commands & record it's output ((MPC_HORIZON+1)*states + MPC_HORIZON*controls)
casadi::Function solv = casadi::nlpsol("solv", "ipopt", path + "/nlpN3.so");
res = solv(arg);
sol_cas = res.at(0);

I then provide the same arguments (args_x, args_p, args_lbx, args_ubx, args_lbg, args_ubg) to solver in MATLAB, but the solver returns output which has some of the control outputs different from the C++ ones. I rechecked the arguments I am providing to MATLAB and C++ and they are exactly same. 

Can the functionality of the IPOPT solver be different in the two cases. It will be very helpful if you can suggest to me some of the areas where I might be making some silly mistakes.

Thanks in advance,
Akash
Reply all
Reply to author
Forward
0 new messages