Codegen for MPC

289 views
Skip to first unread message

Akash Garg

unread,
Jun 28, 2021, 9:09:02 AM6/28/21
to CasADi
Hello,

I have written my optimization problem as a  multiple shooting MPC problem. It is very much inspired by the "Sim_2_MPC_R_PS_mul_shooting_RK.m" given in the example pack of CasADi. I'm getting reasonably good solution in MATLAB but the computation time is very large, as expected. I'm searching for ways to generate standalone C code for the same. The examples I found on https://web.casadi.org/docs/#syntax-for-generating-code are for functions. I do't know how to use codegen for entire MPC problem. Also I'm using sqpmethod as the solver.

Any help will be appreciated :)

Thanks in advance,
Akash 

Akash Garg

unread,
Jun 28, 2021, 11:07:37 AM6/28/21
to CasADi
I discovered the following commands can be useful:

solver.generate('cg_demo',struct('mex',true));
mex cg_demo.c -DMATLAB_MEX_FILE
cg_demo('solver')

Where originally in the MATLAB code, the solver is defined as:

nlp_prob = struct('f', obj, 'x', OPT_variables, 'g', g, 'p', P);
solver = nlpsol('solver', 'sqpmethod', nlp_prob);

In the given scenario, I get an error if I try to pass upper and lower bounds for constraint vector 'g' and parameters p. cg_demo('solver') expects an argument of size OPT_variables.
Could you suggest me a way out?

Joris Gillis

unread,
Jun 29, 2021, 4:29:39 PM6/29/21
to CasADi
Dear Akash,

Since you work with sqpmethod, your approach will succeed.
I don't think we implemented a key-value interface for the mex entry point (makes sense ro add it though),
so you'll have to use an 'ordered argument' approach: in your cg_demo call, add an extra argument  for each input listed by disp(solver)

Best regards,
  Joris

Hoomaan Moradi

unread,
Dec 1, 2021, 12:17:25 PM12/1/21
to CasADi

Dear Joris and Akash, 

Is there any example for solving an MPC problem in Simulink which has the capability of codegen like what has been explained here:

https://web.casadi.org/blog/s-function/

Thank you in advance,

Hoomaan

Reply all
Reply to author
Forward
0 new messages