Integrate learning-based Python models into acados for real-time model predictive control.
-
Define your
AcadosOcp
, including the nominal dynamics modelfrom acados_template import AcadosOcp ocp = AcadosOcp() # ...
-
Define the residual model using the
L4acados
ResidualModel
(here as a PyTorchResidualModel example):import l4acados as l4a residual_model = l4a.PyTorchResidualModel(your_pytorch_model)
Other models can be straightforwardly implemented using as a
ResidualModel
instance; see here for already available residual models. -
Generate the
L4acados
solver objectl4acados_solver = l4a.ResidualLearningMPC( ocp=ocp, residual_model=residual_model, use_cython=True, # accelerate get/set operations by using the acados Cython interface )
-
Done! The
ResidualLearningMPC
object can be interfaced like theAcadosOcpSolver
:l4acados_solver.set(...)
l4acados_solver.solve()
l4acados_solver.get(...)
Not all solver interface functions are mapped by the
ResidualLearningMPC
. You can still access the underlyingAcadosOcpSolver
object through thel4acados_solver.ocp_solver
property. Besides the dynamics model and parameter definition, thel4acados_solver.ocp_solver
is equivalent to theacados_solver.ocp_solver
generated from the originalocp
.
-
Clone
git submodule update --recursive --init external/acados
-
Build the submodule
acados
according to the installation instructions:mkdir -p external/acados/build cd external/acados/build cmake -DACADOS_PYTHON=ON .. # do not forget the ".." make install -j4
You can also install
acados
separately; in this case, refer to the acados version of the submodule in case you encounter errors with a newer version ofacados
. -
Install acados Python interface
pip install -e external/acados/interfaces/acados_template
-
Set environment variables (where
$PROJECT_DIR
is the directory you cloned the repository into in Step 1):export ACADOS_SOURCE_DIR=$PROJECT_DIR/external/acados export LD_LIBRARY_PATH=$ACADOS_SOURCE_DIR/lib
Install L4acados
with optional dependencies of your choice
pip install -e .[<optional-dependencies>]
Available options:
- (without optional dependencies): Basic ResidualLearningMPC for custom implementations (e.g. Jacobian approximations, finite-difference approximations for e.g. models without sensitivity information)
[pytorch]
: PyTorch models.[gpytorch]
: GPyTorch models.[gpytorch-exo]
: GpyTorch models with online-learning improvements.- not supported yet: JAX, TensorFlow
If you would like to contribute features to L4acados
, please follow the development installation instructions below to set up your working environment.
-
Install
L4acados
with development dependencies (in addition to the used learning framework, see above):pip install -e .[dev]
-
Sync notebooks with jupytext:
jupytext --set-formats ipynb,py examples/*/*.ipynb
-
Add pre-commit hooks
pre-commit install
If you use this software, please cite our corresponding articles as written below.
@online{lahr_l4acados_2024,
title = {L4acados: {{Learning-based}} Models for Acados, Applied to {{Gaussian}} Process-Based Predictive Control},
shorttitle = {L4acados},
author = {Lahr, Amon and Näf, Joshua and Wabersich, Kim P. and Frey, Jonathan and Siehl, Pascal and Carron, Andrea and Diehl, Moritz and Zeilinger, Melanie N.},
date = {2024-11-28},
eprint = {2411.19258},
eprinttype = {arXiv},
doi = {10.48550/arXiv.2411.19258},
pubstate = {prepublished}
}
@article{lahr_zero-order_2023,
title = {Zero-Order optimization for {{Gaussian}} process-based model predictive control},
author = {Lahr, Amon and Zanelli, Andrea and Carron, Andrea and Zeilinger, Melanie N.},
year = {2023},
journal = {European Journal of Control},
pages = {100862},
issn = {0947-3580},
doi = {10.1016/j.ejcon.2023.100862}
}