DaeBuilder + Opti Stack

279 views
Skip to first unread message

Sushil Cherian

unread,
Jun 7, 2021, 12:58:18 AM6/7/21
to CasADi
Is it possible to create a DaeBuilder object to represent system dynamics, and use this as constraints in an optimal control problem using Opti stack? For example, as an argument to opti.subject_to()? Any example would be awesome.Thanks. 
Sushil Cherian.

joaosa...@gmail.com

unread,
Jun 14, 2021, 10:11:12 AM6/14/21
to CasADi
double L = 0.8;
//auto L = dae.add_p("L");
//auto ti = dae.add_p("ti");
auto u_v = dae.add_u("u_v");
auto u_w = dae.add_u("u_w");
auto x = dae.add_x("x");
auto y = dae.add_x("y");
auto o = dae.add_x("o");

//ODE
auto x_dot = u_v * cos(o);
auto y_dot = u_v * sin(o);
auto o_dot = (1 / L) * u_w;
dae.add_ode("x_dot", x_dot);
dae.add_ode("y_dot", y_dot);
dae.add_ode("o_dot", o_dot);
//dae.add_lc("gamma", {"ode"});
auto ode = dae.create("f", {"x", "u", "p"}, {"ode"});
auto jac_ode_x = dae.create("ode", {"x", "u", "p"}, {"jac_ode_x"});
auto jac_ode_u = dae.create("ode", {"x", "u", "p"}, {"jac_ode_u"});
//auto hess_ode_x_x = dae.create("ode", {"x", "u", "p"}, {"hess_gamma_x_x"});

std::cout << ode <<std::endl;;
//Integrator
// Time length
double T = 10.0;

// Shooting length
int nu = 20; // Number of control segments

// Time horizon for integrator
double t0 = 0;
double tf = T/nu;

casadi::Dict opts;
// opts["expand_f"] = true;
// opts["interpolation_order"] = 1;
// opts["number_of_finite_elements"] = 1000;
opts["t0"] = t0;
opts["tf"] = tf;

//SXDict dae = {{"x", x}, {"p", U_ode}, {"t", t}, {"ode", ode}};dict
casadi::Function F = casadi::integrator("integrator", "rk", ode, opts);

joaosa...@gmail.com

unread,
Jun 14, 2021, 10:12:32 AM6/14/21
to CasADi
dae is to build your ode
Then you use it to create an integrator
Then integrate equations (E.g. and use shooting method)

Sushil Cherian

unread,
Jun 15, 2021, 4:41:11 AM6/15/21
to CasADi
Thanks for your response. I understand how daebuilder is used normally. My question was whether there is any way it can be used with CasADi Opti stack, which seems to be a good way to abstract away lot of the underlying implementation details:
All the examples given deal with algebraic constraints, not differential equations. Hence the question.
Any feedback is welcome. Thanks.

joaosa...@gmail.com

unread,
Jun 15, 2021, 7:31:53 AM6/15/21
to CasADi
Once you have the constraints from integrating your Dae
you can add them on Opti stack in the following way
e.g. 
Opti ocp;
for ( int k = 0; k < N; ++k){
ocp.subject_to( integrator(x(k),u, dt) - x(k+1) ); // multiple shooting
}

Adding the  dae directly to the Opti stack I never tried it, so maybe someone else could help you.

joaosa...@gmail.com

unread,
Jun 15, 2021, 7:35:26 AM6/15/21
to CasADi
Note you cannot use differential equations directly. You need to do transcription, meaning discretize your ode's.
Some examples are multiple shooting, single shooting, collocation ... there are examples on the example pack folder.
The transcription method implies some form of integration and the resulting equations you add them as constraints in the Opti stack.
Reply all
Reply to author
Forward
0 new messages