casadi / casadi

CasADi is a symbolic framework for numeric optimization implementing automatic differentiation in forward and reverse modes on sparse matrix-valued computational graphs. It supports self-contained C-code generation and interfaces state-of-the-art codes such as SUNDIALS, IPOPT etc. It can be used from C++, Python or Matlab/Octave.
http://casadi.org
GNU Lesser General Public License v3.0
1.65k stars 368 forks source link

Acessing the adjoint state #3583

Open gabrieldlm opened 6 months ago

gabrieldlm commented 6 months ago

Hi, is there anyway of accessing the adjoint state after solving a optimal control problem?

jaeandersson commented 6 months ago

If you solve the optimal control problem by writing it as an NLP, you can get the Lagrange multipliers from the solver. These can be used to construct costate trajectories.

gabrieldlm commented 6 months ago

Thanks , by "writeing it as an NLP" you mean as in the rocket.py example ? I´m using the api calls used in race_car.py.

jaeandersson commented 5 months ago

It's not clear to me what you exactly mean by the "adjoint state" here. Do you define the necessary conditions for optimality for the continuous time system using (Pontryagin's)? If you write the problem as an (finite dimensional) NLP, the interface will give you Lagrange multipliers corresponding to simple bounds and nonlinear bounds (lam_x and lam_g respectively). In many cases, you can relate these Lagrange multipliers to costate trajectories for the continuous time problem. The mapping will depend on how you do your discretization.