leap-c or (learning predictive control) provides a simple way how to wrap optimal-control solvers into modern deep learning pipelines like
PyTorch.
This repository provides a differentiable layer based on the very fast acados framework. Higher-level planners, environments, and RL training utilities live in downstream projects such as leap-c-lab and mpc-sac.
Note
The repository recently moved toward a smaller core interface. The previous broader interface is
available in v0.2.0-alpha.
- A simple
torchinterface foracadoscalledAcadosDiffMpcTorch. - Solve optimal control problems in parallel using multithreading.
- Backpropagate exact solution sensitivities through the MPC layer.
- Retrieve sensitivities of the optimal cost and the optimal control sequence with respect to problem parameters.
- Details as warm-starting, initialization and more are conveniently handled by the
AcadosDiffMpcTorchinterface.
Follow the installation guide.
import torch
from leap_c.torch import AcadosDiffMpcTorch
# Build these with acados/CasADi and leap-c's parameter manager.
ocp = build_acados_ocp(...)
parameter_manager = build_parameter_manager(...)
diff_mpc = AcadosDiffMpcTorch(ocp, parameter_manager)
x0 = torch.tensor([[1.0, 0.0]], dtype=torch.float64)
Q = torch.tensor([[1.0, 1.0]], dtype=torch.float64, requires_grad=True)
ctx, u0, x, u, value = diff_mpc(x0, params={"Q": Q})
value.sum().backward()
print(u0) # first optimal action
print(Q.grad) # gradient through the MPC solveThe snippet omits the acados OCP construction. For a full runnable example, see Define a differentiable MPC.
- leap-c-lab: example environments, OCP definitions, and planners that show how to build on the core layer.
- mpc-sac: SAC/CrossQ-style RL training utilities for
learning MPC controllers with
leap-candleap-c-lab.
Open a new thread or browse existing ones on the GitHub discussions page.
If you use code from this repository in your work, please cite:
@misc{fichtner_leapc_2025,
title = {Leap-c/Leap-c: V0.1.0-Alpha},
author = {Fichtner, Leonard and Reinhardt, Dirk and Hoffmann, Jasper and Airaldi, Filippo and Frey, Jonathan and Kir Hromatko, Josip and Baumgaertner, Katrin and Amria, Mazen and Reiter, Rudolf and Sawant, Shambhuraj},
year = 2025,
month = oct,
howpublished = {Zenodo}
}The following projects follow similar ideas and might be interesting:
- mpc.pytorch: Early work on embedding MPC in PyTorch for end-to-end learning, with a more restricted class of MPC problems
- mpcrl: A simpler codebase for using RL with MPC as function approximator
- Neuromancer: A differentiable programming library that allows to include parametric optimization layers (including MPC) in PyTorch computational graphs
- ntnu-itk-autonomous-ship-lab/rlmpc: A codebase tailored for marine vessel control using RL and MPC