A python package for the optimization of NMPC implementation options
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
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):
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:
in which
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.
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_ctr
of 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 expressionn_steps
$=\lceil 1+\mu_d(\kappa-1)\rceil$ - The maximum number of iterations
iter_max
allowed for the solver at each updating compuation.
To summarize, the algorithm provided by the package enables to tune the following vector of implementation parameters:
The idea is to choose the implementation parameters such that some admissibility conditions are satisfied over a set
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_acc
can be computed and certified -
The 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.
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.
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.py
containing the package - The python file
user_defined_pvtol.py
which contains the PVTOL related files to be used by the package utilities - The jupyter notebook
test_MPC_tuner.ipynb
which uses the previous files to find the admissible MPC settings.
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.
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!