← Projects · Research

Safe learning-based MPC: learn while you drive, never leave the safe region

A predictive controller that gets its performance from a model learned from data and its safety from a second, guaranteed model built from the same data. Both keep learning during operation. A robot trained only below 1 m/s learns to lap at 1.9 m/s without ever leaving its certified safe region; without the certificate, the same controller crashed in one of five runs.

MPC · Safe learning · Online learning · Python · numba

Code on GitHub →Stack: Python · NumPy · SciPy · numba · ddma · pytest · GitHub Actions · matplotlib

0collisions and 0 steps outside the certified safe set, in 50 laps (5 runs)
1.1 → 1.9 m/smean lap speed, after training only below 1 m/s
14.4 → 9 slap time, from the first lap to the later ones
< 0.1 sto compute the safe set over 100,000 states and 1,600 inputs
~1.5 msto learn from each new measurement, both models included

The same run with and without certified inputs

Same planner, learned model, data and disturbances (run 3 of 5): with the certified inputs the robot stays safe; with the learned model alone it crashes after 12.8 s.

What it does

Learned models make a controller fast and accurate, but a neural network or a regression fit comes with no guarantee: one bad prediction and the robot hits a wall. Purely robust controllers are safe, but so cautious that they leave most of the performance on the table. This project gets both, from the same data:

  1. Logged dataMeasured transitions (state, input → next state). One long recording is enough; independent samples are not needed.
  2. Guaranteed model → safe actionsA range the true next state is proven to fall in. From it, for every state, the commands that keep the system inside its limits forever, whatever the disturbance does.
  3. Learned modelA smooth best-fit model of the dynamics, constrained to stay inside the guaranteed one. This is what makes the scheme work.
  4. Predictive controllerMPC plans with the learned model for performance, but may only pick from the safe actions. Always feasible, always safe.

And it keeps learning: every new measurement tightens the guaranteed model and refines the learned one, in about 1.5 ms, while the system runs.

Because the learned model is forced to lie inside the guaranteed one, the planner can never rely on a trajectory that the guarantee rules out. No linear model is assumed; the only prior knowledge is a bound on how fast the dynamics can change.

Demo: a robot that learns to go faster, safely

A car-like robot must visit four zones in a 5 × 5 m room, in order and as fast as possible, without touching the walls or the two blocks. Its dynamics are not given to the controller: it gets 10,000 logged measurements, all below 1 m/s, and may command up to 2 m/s.

At first the learned model is poor above 1 m/s, so the planner stays near 1 m/s even where the guarantee would allow more (top figure, orange line). Each lap adds high-speed data. From the second lap on, the robot averages about 1.9 m/s and laps in about 9 s instead of 14 s, with zero collisions and without ever leaving the certified safe set.

Guaranteed turn-rate bounds before and after online learning

The uncertainty of the guaranteed model at high speed shrinks threefold (11.9 → 3.9 rad/s on the turn rate). The error of the learned model there drops from 5.3 to 1.4 rad/s.

What happens without the guarantee? I ran the same planner, learned model and online learning without the safe-action constraint, over 5 runs of 10 laps. The planner then trusts a model that is still wrong at high speed: in one run it drove into a block 12.8 s after the start, and in the others it came within 4 cm of a wall. The certified controller never collided and kept at least 14 cm, for about 2.5 % more lap time.

The same run with and without certified inputs

With and without the certified input set, over five runs

Algorithms and performance

I redesigned the algorithms so that learning, safety and planning all run in real time:

  • Guaranteed model from growth cones, with ddma. Each measurement bounds the unknown dynamics by a cone. The model is the growth-cone model of my toolbox ddma (GrowthMap): because the cone is a sum of one-dimensional terms, a new measurement is evaluated on the whole grid with a few vectorized operations, so every cell is tightened, in about 0.1 ms.
  • Safe set without storing transitions. For a planar vehicle, the cells a state can move to are the current cell shifted by offsets that depend only on its heading and the input. The fixed point tests these boxes against 3-D summed-area tables, with the heading axis doubled for wrap-around. It keeps one witness input per state, in a compiled numba kernel.
  • Learned model as box-constrained least squares. The learned model is piecewise multi-affine on the same grid. Staying inside the guaranteed model becomes simple bounds on its corner values. Offline it is a sparse bounded least-squares fit; online, a normalized gradient step followed by a projection.
  • MPC by the cross-entropy method. The safe commands form a non-convex, often disconnected set of cells, which suits sampling better than gradients. Each step, 2,000 candidate plans are evaluated in batch, the sampling distribution is refitted to the best 10 % five times, and the first command is always projected onto a safe cell. A shortest-path cost-to-go keeps the short horizon from getting stuck behind obstacles. About 20 ms per step at a 5 Hz control rate. I benchmarked it against random shooting and a gradient method per safe cell: it finds plans 1.4 % from the best known (2.0–2.3 % for the others) and gives the fastest laps.
Step Time
Guaranteed-model update, per measurement ~0.1 ms (whole grid)
Learning update, per measurement ~1.5 ms (all outputs, both models)
Safe set, 100,000 states × 1,600 inputs < 0.1 s
MPC step ~20 ms

Engineering

  • Tests that check the guarantees. The guaranteed model contains the true function plus every disturbance, on random functions in one to three dimensions. The learned model stays inside it after the offline fit and after every online step (20,000 random points per check). The safe set is checked to be invariant and maximal, independently of the fast kernel. From random certified states, a safe input keeps the true robot, with random disturbance, inside the safe set (3,000 trials). In closed loop, every MPC command is certified.
  • Built on ddma. Both guaranteed models come from ddma: the growth-cone model (ADHS paper) and the two monotone problems of the EJC paper (minimal simulating maps); they can be intersected. This project adds the learned model, the safety controller for vehicles and the MPC.
  • Reproducible. One script runs the ten-lap demo, one the comparison with and without the guarantee, one the optimizer benchmark, one makes every figure. CI on GitHub Actions for Python 3.10 to 3.13 runs lint, tests and a two-lap demo.

Next

  • The offline obstacle-avoidance study of the EJC paper as a second example.
  • Exploration that is aware of the guarantee: collecting data where it grows the safe set fastest.
  • A generic (non-planar) safety backend through ddma, and a Gymnasium wrapper for reinforcement-learning agents.

For researchers: the compatible models approach

The scheme decouples safety and performance with two models of an unknown nonlinear system x⁺ = f(x, u) + w with bounded derivatives and bounded disturbance. A set-valued over-approximation F with f(x, u) + W ⊆ F(x, u) gives, through a symbolic abstraction and a fixed point, a safety controller C_F. A single-valued estimate f̂ is compatible if f̂(x, u) + W ⊆ F(x, u). Then an MPC that predicts with f̂, keeps the predicted states in the safe set and restricts the first input to C_F(x) is recursively feasible, and the closed loop never leaves the safe set (EJC 2023, Theorem 1).

  • EJC 2023 (offline). F comes from the monotone transformation of bounded-derivative dynamics and the minimal simulating maps of our TAC paper. f̂ is a continuous piecewise multi-affine function on the same partition, fitted by a sparse box-constrained least-squares problem; compatibility follows from the vertex property of multi-affine functions (Belta and Habets, 2006).
  • ADHS 2024 (online). F is the interval hull of growth cones built from each data point and the derivative bounds, evaluated at cell vertices; f̂ is updated by projected stochastic gradient.

Unlike the two-model scheme of Aswani et al. (2013), no linear structure is assumed, and unlike PAC or scenario-based data-driven abstractions, the guarantee is deterministic.

The offline case study of the EJC paper (figures from the paper):

The certified safe region for three headings: the robot cannot stop, so it must always have a way around the obstacle

Closed-loop trajectory around the obstacle to the target

Methods: A. Makdesi, A. Girard, L. Fribourg. Implementation: Anas Makdesi.

Related papers

  1. IFAC ADHS

    Online Learning for Safe Model Predictive Control with the Compatible Models Approach

    Anas Makdesi, Antoine Girard, Laurent Fribourg · IFAC Conference on Analysis and Design of Hybrid Systems, 2024

  2. EJC

    Safe Learning-Based Model Predictive Control Using the Compatible Models Approach

    Anas Makdesi, Antoine Girard, Laurent Fribourg · European Journal of Control, 2023