This repository provides a set of Python scripts and Jupyter notebooks solving toy optimal control problems (simple pendulum, cartpole, etc.). The goal is to familiarize students with the core concepts and methods from Optimal Control theory (continuous and discrete time).
In particular, the following methods are implemented:
- Dynamic Programming (Value Iteration)
- HJB PDE numerical resolution
- Pontryagin Maximum Principle (a.k.a. indirect optimal control)
- Direct Optimal Control (using SQP or DDP, based on Crocoddyl API)
You can run the notebooks directly in your web browser without installing anything locally:
Open the notebooks directly in Google Colab using the badges below.
Important
Google Colab uses condacolab to install compiled robotics dependencies.
Run the setup cell at the top of the notebook. The first time, Colab may automatically restart the runtime after installing Conda. This is expected. After the restart, run the same setup cell again, then continue with the notebook.
Click the badge below to start a full JupyterLab environment in Binder with all dependencies pre-installed:
We support two ways to install dependencies locally: Pixi (recommended, modern and fast) and Conda.
Pixi is a modern package creator and manager. It installs all dependencies (including Python and system libraries) locally inside a .pixi/ folder without touching your global system environment.
-
Install Pixi (if you don't have it):
curl -fsSL https://pixi.sh/install.sh | bash(Restart your terminal after installation)
-
Run a script directly: Pixi will automatically install everything needed on the first run:
pixi run pendulum-ocp
Other tasks:
pendulum-bellman,cartpole-ocp,kuka-ocp,kuka-mpc. -
Start Jupyter Lab:
pixi run lab
If you prefer traditional Conda/Mamba:
-
Create the environment:
conda env create -f environment.yaml
-
Activate the environment:
conda activate mpc_tutorial
-
Install the package and mim_robots:
pip install git+https://github.com/machines-in-motion/mim_robots.git --no-deps
-
Run a script:
python pendulum/pendulum_ocp.py
The conda environment was created using the following command:
conda create -n mpc_tutorial -c conda-forge --override-channels \
python=3.12 pip matplotlib \
mim-solvers=0.2.0 \
crocoddyl=3.2.0 \
pinocchio=3.8.0 \
pinocchio-python=3.8.0 \
libpinocchio=3.8.0 \
coal=3.0.1 \
coal-python=3.0.1 \
libcoal=3.0.1 \
ipython \
matplotlib \
numpy<2.0.0The main dependencies installed automatically by the setup tools are:
- Crocoddyl (Contact-Consistent Robot Co-dynamics)
- Pinocchio (Rigid Body Dynamics)
- mim_solvers (Fast solvers for optimal control)
- mim_robots (Robot descriptions and wrappers)
pybullet,osqp,numpy,scipy,matplotlib,jupyterlab
Some KUKA examples in this repository are freely adapted from my companion repository minimal_examples_crocoddyl, which provides compact examples for using Crocoddyl.
This tutorial is also inspired by standard references in numerical optimization and numerical optimal control, in particular:
-
S. Gros and M. Diehl, Numerical Optimal Control, 2022.
https://www.syscop.de/files/2020ss/NOC/book-NOCSE.pdf -
J. Nocedal and S. J. Wright, Numerical Optimization, Springer.
https://www.ime.unicamp.br/~pulino/MT404/TextosOnline/NocedalJ.pdf -
A. Jordana, S. Kleff, A. Meduri, J. Carpentier, N. Mansard, and L. Righetti, Structure-Exploiting Sequential Quadratic Programming for Model-Predictive Control.
https://laas.hal.science/hal-04330251