← Notes · 21 November 2026 · 5 min read

Letting a learning controller explore without crashing

Reinforcement learning learns by trying things, including dangerous ones. A safety filter learned from data lets it try everything except what would crash the machine. On two benchmarks: 81–318 crashes per training run without the filter, none with it.

Reinforcement learning is good at finding controllers nobody would have designed by hand. It gets there by trial and error, and some of the errors are crashes. In a simulator that’s fine. On a car or a drone it is the reason learned controllers rarely leave the lab.

In 2024 I designed a way around this, and this year I built it: a small component that sits between any controller and the machine, passes every safe command unchanged, and replaces only the unsafe ones. The learning algorithm doesn’t need to know it is there.

What “unsafe” means without a model

The hard part is deciding which commands are unsafe when you don’t have the equations of the machine. Here I use my toolbox ddma. From logged transitions (state, command, next state) and a few honest bounds on how fast the dynamics can change, it computes a model with guaranteed error bounds. On that model it finds the states from which the limits can be kept forever, and for each of those states the commands that do it.

That is the filter. At run time it is a table lookup: is the proposed command among the safe ones for the current state? If yes, it passes. If not, the filter sends the nearest safe command instead. One call takes 0.1–0.2 ms, about as long as evaluating the neural network that proposed the command.

Two benchmarks

I tried it on two problems. In lane keeping, a car at 15–30 m/s on roads with unknown curves must stay in its lane. In the quadrotor benchmark, a drone in a corridor 2 m wide follows a target that sometimes flies beyond the walls, so a controller that follows it blindly hits a wall.

On both, two standard learning algorithms (SAC and PPO) were trained five times alone and five times behind the filter, with the same random numbers.

A learning agent, alone and behind the filter

Alone, the agents crashed 81 to 318 times per training run. Behind the filter, none of the 40 training runs had a single crash. On held-out test episodes the picture is the same: no crash with the filter, for every controller.

Does the filter get in the way?

A filter that makes everything safe by refusing to move would be useless, so the cost matters as much as the crash count. Two results answer it.

On the quadrotor, agents trained alone still hit a wall in 10–32 % of the test episodes. Behind the filter they follow the target right up to the wall and reach 98–99 % of the return of an MPC that knows the true model. Here the filter doesn’t just prevent crashes; it is what makes the learned controller usable.

For a controller that is already safe, the filter has almost nothing to do. The reference MPC respects the walls on its own; the filter changes 9 % of its commands slightly and costs 0.1 % of its return.

The same holds beyond reinforcement learning. DeePC, a controller that predicts from a short recorded trajectory, crashed in 5 of 20 episodes when given 6 s of data instead of 20 s. Behind the filter it never crashed and recovered most of its performance.

What I learned building it

The filter is only as good as its grid. The drone’s sideways motion is a chain of integrators (position, speed, acceleration), and with fewer than about 120 speed cells the filter certified nothing at all. Coarse cells create loops that don’t exist in the real system, and the filter then sees no way to stay safe.

And the filter needs no changes to the learning code: it is a wrapper around the environment, so the same training script runs with and without it. A safety layer that requires rewriting the controller is much harder to adopt.

The lesson

Safety and learning don’t have to be traded against each other. Let the learner handle performance, give safety to a small component you can prove things about, and keep the interface between them as thin as one function call.

Code, all results and the scripts that produce them: github.com/anas-makdesi/safety-filter.