← Projects · Research

Safety filter for learning-based control

A learning controller that cannot crash, even while it learns. A filter learned from data sits between the controller (reinforcement learning, DeePC or MPC) and the machine, passes every safe command and corrects only the unsafe ones. On a lane-keeping car and a quadrotor in a corridor: 81–318 crashes per training run without it, none with it.

Safety filter · Reinforcement learning · DeePC · MPC · Python · PyTorch

Code on GitHub →Stack: Python · PyTorch · Stable-Baselines3 · Gymnasium · CVXPY · OSQP · ddma · pytest · GitHub Actions · matplotlib

0crashes in 40 training runs behind the filter (81–318 per run without it)
0crashes in 480 test episodes with the filter, for every controller
0.1 %performance cost for controllers that are already safe
0.1–0.2 msper filter call

What it does

Learned controllers perform well, but nothing in them rules out a dangerous command, least of all while they are still learning. This project puts a small, proven component between any controller and the machine:

  1. Logged dataSampled transitions of the machine (state, command → next state). The equations are never given.
  2. Certified safe setWith my toolbox ddma: a model with guaranteed error bounds, then the states from which the limits can be kept forever, and the commands that do it.
  3. Filter in the loopAt every step: a safe command passes unchanged, an unsafe one is replaced by the nearest safe command.
  4. Any controller on topReinforcement learning (SAC, PPO), DeePC, MPC. The controller is in charge of performance, the filter of safety.

The filter is built in a few seconds from the data and is a table lookup at run time. It needs no change to the controller or to the learning algorithm.

Demo: a learning agent, with and without the filter

Two benchmarks. Lane keeping: a car at 15–30 m/s on roads with unknown curves must stay in its lane. Quadrotor: a drone in a corridor 2 m wide follows a target that sometimes flies beyond the walls; as in real autopilots, the learned controller sends acceleration setpoints.

Below, the same agent early in its training, with the same random numbers, alone (orange) and behind the filter (teal). Alone it leaves the lane after 2.5 s and the corridor after 3.1 s.

Lane keeping: a learning agent without and with the safety filter

Quadrotor: a learning agent without and with the safety filter

Results

While learning. Each agent was trained five times without the filter and five times behind it.

Benchmark Agent Crashes per training run, no filter With the filter
Lane keeping SAC 85 (81–89) 0
Lane keeping PPO 203 (183–226) 0
Quadrotor SAC 115 (93–136) 0
Quadrotor PPO 290 (271–318) 0

After training, on 20 test episodes of 30 s that were never used in training (a return of 600 is perfect):

Benchmark Controller Alone: crashes Alone: return With filter: crashes With filter: return
Quadrotor SAC 32 of 100 388 0 of 100 574
Quadrotor PPO 10 of 100 517 0 of 100 578
Quadrotor MPC with the true model 0 of 20 581.8 0 of 20 581.2
Lane keeping SAC 0 of 100 585 0 of 100 599
Lane keeping PPO 0 of 100 600 0 of 100 582
  • The filter makes the learned controllers competitive. On the quadrotor, agents trained without it still hit a wall in 10–32 % of the test episodes. Behind the filter they follow the target up to the wall and reach 98–99 % of the return of an MPC that knows the true model.
  • It costs almost nothing when it is not needed. The MPC already respects the walls; the filter changes 9 % of its commands slightly and costs 0.1 % of the return. On lane keeping it never touches a trained controller.
  • It is fast. About 0.1–0.2 ms per call, comparable to evaluating the neural-network policy, against about 5 ms for the MPC and up to 50 ms for DeePC.

DeePC with less favourable data or tuning. DeePC predicts from a short recorded trajectory, so its safety depends on that data and on its tuning. Tuned with 20 s of data and a 10 cm margin to the walls, DeePC never crashed on the quadrotor (return 579), so the filter had nothing to do. Two less favourable set-ups, 20 test episodes each:

DeePC variant Alone: crashes Alone: return With filter: crashes With filter: return
6 s of data instead of 20 s 5 of 20 421.9 0 of 20 546.7
No safety margin to the walls 2 of 20 554.6 0 of 20 581.0

With too little data DeePC hits a wall in a quarter of the episodes; behind the filter it never does and recovers most of its performance. Without a safety margin, the filter takes over the margin’s job: no crash, and a higher return than the carefully tuned version (579), because the controller no longer has to stay 10 cm away from the walls.

Crashes and return during training

Engineering

  • One interface for every filter and every controller. A filter has two methods, filter(x, u) and contains(x); a Gymnasium wrapper applies it to any environment, so the RL training code is the same with and without it. Filters of decoupled channels (the drone’s lateral and vertical motion) are combined side by side.
  • Three kinds of controller, written from scratch on standard libraries. RL with Stable-Baselines3 and PyTorch; DeePC in CVXPY with regularisation and an SVD-reduced data matrix; a reference MPC with the true model.
  • Honest evaluation. Five seeds per condition, fixed held-out test episodes, the same random numbers with and without the filter, and every number in the tables generated by a script from one results file.
  • Tests on the guarantees. The simulators are checked against the assumptions the filter relies on; crude agents (random, full lock, full thrust) never crash behind the filter and always crash without it; safe commands pass unchanged. CI on GitHub Actions.

Next

Gridless safe envelopes for these systems (a research question: they need commands that depend on the state), and the filter’s interface inside ddma.


For researchers: the method

The filter is the maximal controlled invariant set of a symbolic abstraction, computed by ddma. The system is written as x⁺ = x + f(x, u) + w. The method is given which coordinates each component of f depends on, bounds on its partial derivatives and on w, and sampled transitions; from these, the growth-cone model of my thesis (Chapter 3) gives, for every cell of a grid on states and inputs, a box that contains the successor. The safety fixed point on the resulting abstraction yields the set of cells from which the specification can be kept, with the admissible input cells of each; the filter returns the admissible input closest to the proposed one.

Lane keeping Quadrotor
State lateral offset, heading error, speed lateral: position, speed, acceleration; vertical: height, speed
Data 2·10⁶ transitions 2·10⁶ transitions
Grid 160 × 120 × 24 cells, 21 inputs 120 × 240 × 40 and 120 × 80 cells, 21 inputs each
Certified share of the specification 46 % (same grid from the exact model: 54 %; true maximum about 60 %) lateral 43 %, vertical 80 %
Build time about 4 s about 9 s

Two observations from building it. Integrator chains need fine grids in velocity: with fewer than about 120 velocity cells the drone’s lateral abstraction certifies nothing, because coarse cells create self-loops that do not exist in the system. And the gridless envelopes of the invariance project do not fit these systems as they stand: they certify sets kept invariant by a constant range of inputs, which an integrator chain does not have.

RL settings: SAC for 60,000 steps and PPO for 300,000, library-default hyperparameters, five seeds; episodes of 30 s at 20 Hz; a crash ends the episode with a penalty of 50. The budgets are enough for the comparison and are not tuned for the best return.