MPC-Tuner
A Python package for the optimization of NMPC implementation options
MPC_tuner
A python package for the optimization of NMPC implementation options
Citation
A full description of the principles and a detailed illustrative example are given in the paper below:
Mazen Alamir. A framework and a
python-package for real-time NMPC parameter settings. arXiv:2309.17238, October, 2023. (in a second reviewing stage for the Journal IEEE Transactions on Control Systems Technologies).
Mazen Alamir. (2023). mazenalamir/MPC_tuner: MPC_tuner (1.0). Zenodo. https://doi.org/10.5281/zenodo.8409251
Recall on Nonlinear Model Predictive Control
Nonlinear Model Predictive Control (NMPC) is the most advanced control design. It enables to take into account nonlinear dynamics, non conventional control objective through the definition of a cost function and the presence of input (control) and state constraints. It is generally based on the repetitive solution of optimal control problems of the following form (soft constraints are used except for the input control saturations):
\[ \min_{\mathbf u} J(\mathbf u\ \vert x_0,p):=\rho_f\Psi(x_N)+\sum_{k=1}^{N_\text{pred}} \ell(x_{k},u_{k-1})+ \rho_\text{cstr} \max_{i=1}^{n_c}\lfloor c_i(x_k,u_{k-1})\rfloor_+^2 \]
where
- \(\mathbf u:=(u_0,\dots,u_{N_\text{pred}-1})\in \mathbb [R^{n_u}]^{N_\text{pred}}\) is the sequence of control that minimizes the above cost function
- \(x_k\) are the next states starting from the initial state \(x_0\) and given the dynamics:
\[ \dot x = f(x,u,p) \]
in which \(p\) is a vector of model’s parameters. Once this problem is solved the first action \(u_0\) is applied to the system and the process is repeated in the next updating instants.
The raison d’être of this package is that the above equation and principle still leave many undecided choices that might be mandatory to derive a real-time implementatable algorithm. The next section describes the implementation parameters that the package helps tuning.
The implementation parameters that are tuned by the MPC_tuner package
While the theoretical assessment of NMPC are now clear and freely available solvers are widely used, it remains true that the final implementation of NMPC involves the choice of the following parameters (see below for some illustrations):
- The prediction horizon’s length
- The control updating period \(\tau_u=\kappa \tau\) (\(\tau\) is the largest sampling period making a Runge kutta integration scheme sufficiently consistent).
- The penalties on the soft constraints \(\rho_\text{cstr}\) and on the final cost \(\rho_f\),
- The control parameterization (number of d.o.f) leading to a number
n_ctrof decision instants over the prediction horizon, - The sampling period \(\tau_p=\dfrac{\tau_u}{\texttt{nstep}}\) for the integration inside the solver. More precisely the tuned parameter is \(\mu_d\in [0,1]\) that appears in the following expression
n_steps\(=\lceil 1+\mu_d(\kappa-1)\rceil\) - The maximum number of iterations
iter_maxallowed for the solver at each updating compuation.
To summarize, the algorithm provided by the package enables to tune the following vector of implementation parameters:
\[ \pi := \begin{bmatrix} \kappa\cr \mu_d\cr N_\text{pred}\cr n_\text{ctr}\cr \rho_f\cr \rho_\text{cstr}\cr \texttt{max-iter}\end{bmatrix} \]
| Illustration | Parameter to tune |
|---|---|
![]() |
\(\tau_u=\kappa\tau\) The control updating period |
![]() |
The control horizon defining the number of decision instants n_ctr |
![]() |
The sampling period for the integration inside the solver \(\tau_p=\frac{\tau_u}{\lceil 1+\mu_d(\kappa-1)\rceil}\) |
The guiding principles
The idea is to choose the implementation parameters such that some admissibility conditions are satisfied over a set \(\mathcal A\) of cerifying scenarios that is randomly sampled. The cardinalty of the set is chosen based on the probabilistic certification rules. The admissibility conditions are described in the following section.
The admissibility conditions
The NMPC implementation parameters are chosen such that the following requirements are satisfied:
The real-time compatibility meaning that the computation time is lower than the available time. The latter is computed on a targeted implementation framework that might be different from the one on which the package is built (Casadi, simple-shooting IPOPT). This can be done by using the following threshold on the computation time:
imp_acc\(\times \tau_u\). More precisely, when the time for a single compuation is lower thanimp_acc\(\times \tau_u\) and this for all the updating instants during the closed-loop simulation then real-time compatibility is conformed for the simualted scenario. This enables the package to be used to assess the successful implementation of any solver on any device provided that the parameterimp_acccan be computed and certifiedThe contraction of the cost function at the end of a predefined period of closed-loop simulation
The constraints satisfaction at each instants over the closed-loop simulation.
Description of the package
The figure below shows the main utilities provided by the package as well as the necessary user-defined elements that need to be provided:
Summary of the main classes and functions exported by the package and the user-defined object to prepare for the specific control problem.
Content of the repository
In the list of files, you can find a complete example illustrating the NMPC design for the control of a Planar Vertical Take Of and Landing (PVTOL) nonlinear system. More precisely, the repository contains two files:
- The python file
MPC_tuner.pycontaining the package - The python file
user_defined_pvtol.pywhich contains the PVTOL related files to be used by the package utilities - The jupyter notebook
test_MPC_tuner.ipynbwhich uses the previous files to find the admissible MPC settings.
An example of user-defined python file
| user-defined PVTOL-related file | NOTA |
|---|---|
![]() |
Please note the use of the casadi-vertcat when it comes to define a vector so that the package processing can be correctly done. This is generally source of error. Doe not use neither lists nor numpy.array(list) syntax. |
A jupyter notebook cell that runs the main MPC_Design utility
import numpy as np
from MPC_tuner import Design_MPC, Sigma, generate_A, OptimPar
from user_defined_pvtol import pvtol
# Generate the set of scenarios
#--------------------------------------------------
# Here, a small number of batch of small size is
# used for the illustration.
nb, nsb = 30, 10
A = generate_A(pvtol, nb, nsb)
# Generate the set of candidate sigma's
#--------------------------------------------------
N_trials = 100
S = [Sigma() for _ in range(N_trials)]
# Set the Design meta-parameters and run the Design
#--------------------------------------------------
optim_par = OptimPar(gam=0.98, c_max=0.1,
imp_acc=1.0, T=0.5)
R_design_log = Design_MPC(pvtol, S, A, optim_par)If you are using
Jupyter notebook, please notice that the intermediate computation results will be shown in the terminal window in order to avoid long jupyter output cell. ## Typical results
![]() |
Dataframe showing the set of admissible NMPC settings meeting the convergence, constaints satisfation and real-time implementability on the target device. |
![]() |
Evolution of the number of discarded configurations among the 100 initially sampled when the sub batches of certification scenarios are visited |
Possible future extensions
I tried to include other solvers like Acado, ForcePro but this seems quite complicated. Because the first is not really Python-friendly and the second is a commercial solver. This maybe explains why the Casadi framework promised access to Acado but it is still not done. The whole framework would have been possible to implement via the frameworks Pyomo or Gekko which both have in common the Ipopt solver. Fast gradient could have been added also as an option for the solver. This might be done in near future depending on the success of the current version.
The major issue by now is the computation time, so any suggested heuristics (other than Bayesian appraoch or Reinforcement Learning, which I believe is not appropriate) are welcome!





