brocksam / pycollo

General-purpose optimal control, trajectory optimisation and parameter optimisation using direct collocation
https://brocksam.github.io/pycollo/
MIT License
8 stars 3 forks source link

Update cart-pole swing-up example with explicit and implicit dynamics formulations #70

Closed brocksam closed 1 year ago

brocksam commented 1 year ago

This PR updates the cart-pole swing-up example OCP in examples/cart_pole_swing_up/ to now contain two example OCP files: one based on an explicit formulation of the dynamics, and another based on an implicit formulation of the dynamics.

In an explicit formulation there are ODEs available for all of the state variables. This is the most intuitive formulation as each state variable maps directly to an ODE that describes its rate of change.

When modelling multibody systems however it often occurs that a mass matrix and forcing vector are derived to describe the system. In order to obtain the ODEs for an explicit formulation this mass-matrix-forcing-vector linear system needs to be solved using linear algebra, typically an LU solve. This gives explicit ODEs for the state equations, but they are very complex symbolic equations that grow in complexity with the size of the linear system. These need to be differentiated through which is slow, inefficient and undesirable.

Instead, an implicit formulation can be used which avoids the need to invert the mass matrix or solve the mass-matrix-forcing-vector system. An additional control variable is introduced for each velocity state variable with the velocity state variable being mapped to the introduced control variable via its state equation. Acceleration equations are then enforced in DAE form as equality path constraints.

An implicit formulation will contain more NLP decision variables and NLP constraints. However, counterintuitively they can often result in lower-cost NLP function evaluations, faster NLP iterations, better convergence properties, faster solve times, and better solution accuracy for a given transcription mesh. Therefore, using an implicit formulation should often be preferred, especially in situations where it would be required to solve a linear matrix system to obtain the ODEs for an explicit formulation.