Sensitivity methods for CasADi and CVODES

703 views
Skip to first unread message

Dino

unread,
Apr 10, 2013, 5:04:25 AM4/10/13
to casadi...@googlegroups.com
Hi

When using CVodesIntegrator in CasADi to solve optimal control problems, is it possible to specify which type of sensitivity method to be used (forward/adjoint sensitivity)? If not specified, what is the default mode for this?
For instance, consider the VDP single shooting example https://github.com/casadi/casadi/blob/develop/examples/python/vdp_single_shooting.py . Which sensitivity method is used here and how can one change between forward/adjoint sensitivity?

Best regards,
Dino

Joel Andersson

unread,
Apr 10, 2013, 5:59:12 AM4/10/13
to casadi...@googlegroups.com
Hello!

If you create a CVodesIntegrator instance, or a function which contains calls to a CVodesIntegrator instance, you can calculate both forward and adjoint sensitivities by using the "setFwdSeed/setAdjSeed" functions (see user guide for details). I guess you refer to the question of how Jacobians and Hessians are calculated.

In CasADi, Hessians are calculated by using the adjoint mode to calculate the gradient of the Lagrangian. Here, this will result in a backwards integration problem being formulated internally in a new CVodes instance. The Hessian is then calculated by applying the forward mode algorithmic differentiation to the gradient of the Lagrangian, which will internally result in forward sensitivity analysis applied to the forward and backward problems (so-called forward-over-adjoint sensitivity analysis). This essentially gives a way to calculate Hessian-times-vector product. The complete Hessian is then assembled using a graph coloring algorithm (starcoloring).

For Jacobians, the situation is a bit different. Here the heuristic is that first the sparsity pattern of the Jacobian is calculated and then graph coloring techniques are applied to determine the smallest number of forward or adjoint derivatives needed to calculate the Jacobian. If the forward mode requires less than twice the number of directions compared to the adjoint mode, the forward mode is selected, otherwise the adjoint mode. You can override this my setting the option "ad_mode" to "forward" or "reverse" for the constraint function (before the Jacobian of the constraint function is formed).

The same approach is used both for the Jacobian of the (NLP) constraints and for the Jacobian of the ODE right-hand-side. The latter is passed to CVodes to use during the integration (CVodes is using an implicit method).

In the vdp_single_shooting.py example, the Jacobian of the constraints is dense and has two rows and 20 columns and therefore the adjoint mode is selected (2 directions is less then half of 20). I want to stress that vdp_single_shooting.py is mainly a demonstration on how integrators can be embedded into optimization problems in CasADi. For solving practical optimal control problem, we recommend looking at the other examples, in particular the ones involving collocation. 

I hope this makes things more clear!
Joel

Dino

unread,
Apr 10, 2013, 7:54:28 AM4/10/13
to casadi...@googlegroups.com
Hi, and thanks for the answer.

Just to clarify.
For solving a single shooting OCP, the NLP with only controls u as decision variables need gradients of objective and constraint functions wrt. u. Are these gradients automatically provided by forward or adjoint sensitivity methods in CasADi when using the example code? In this case, is there an option for deciding which of these to use?

Joel Andersson

unread,
Apr 10, 2013, 8:54:30 AM4/10/13
to casadi...@googlegroups.com
Hello,

See the answer above. It will use a combination of forward and adjoint sensitivity methods that can be overridden in the case of calculating the gradient of the constraint functions (i.e. the transpose of the jacobian of the constraint function). All of it is provided automatically.

Joel

Lukas Hebing

unread,
Oct 25, 2019, 8:38:38 AM10/25/19
to CasADi
Hi Joel,

the "setFwdSeed/setAdjSeed" are not mentioned in the guide or in the help of the "integrator" function. Only "SIMULTANEOUS|staggered" are listed as "sensitivity_method".

Joel Andersson

unread,
Oct 25, 2019, 10:00:46 AM10/25/19
to CasADi
The post you answered to is from 2013. It doesn't apply to current CasADi.

Joel
Reply all
Reply to author
Forward
0 new messages