A Thousand Futures: MPPI Control, a Drone, and the Temperature Trap
I like control algorithms that I can explain on a napkin, and MPPI is one of them. MPPI stands for Model Predictive Path Integral control. You take a plan, shake it a thousand ways, roll each version through a model, score the futures, and average them with weights. No gradients, no solver, just samples. There is one catch everybody mentions: the temperature, λ (lambda). Set it too high and the average flies into the obstacle. I wanted to see that catch with my own eyes, so I built a small browser demo of a drone in a forest of pillars, with a slider for λ.
You can fly it right now in your browser: live demo. The code is on GitHub: mraad/drone-mppi, and the technical documentation is at mraad.github.io/drone-mppi/docs.
The algorithm in seven steps
The drone is a point mass with position and velocity. The control is acceleration, clamped to ±10 m/s². Every 50 ms, the controller does this:
- Start with a plan: 30 accelerations, which is 1.5 s at 0.05 s per step.
- Add random noise to the plan, K times (K = 1000 by default).
- Simulate each noisy plan through the model. This gives K futures.
- Score each future. Distance to the goal costs a little at each step. Each step inside a pillar or off the map costs 1000.
- Turn each cost into a weight,
exp(-(cost - min_cost) / λ), and normalize. - Make the new plan the weighted average of the noisy plans.
- Fly the first step, slide the plan forward by one step, and repeat.
The min_cost subtraction looks like a detail, but it is the line that keeps the demo alive. At λ = 0.01 and a best cost of 244, exp(-24400) is zero in floating point. Every weight becomes zero, the normalization divides zero by zero, and the plan turns into NaN. Subtracting the minimum keeps the best weight at exactly 1. There is a test that fails if anyone removes it.
Here is the heart of it, from mppi.js:
let cmin = Infinity, best = 0;
for (let k = 0; k < K; k++) if (costs[k] < cmin) { cmin = costs[k]; best = k; }
let sum = 0;
for (let k = 0; k < K; k++) sum += (w[k] = Math.exp(-(costs[k] - cmin) / lambda));
for (let k = 0; k < K; k++) w[k] /= sum;
U.fill(0);
for (let k = 0, p = 0; k < K; k++)
for (let j = 0; j < n; j++, p++) U[j] += w[k] * V[p];
Setting up the trap
To show the λ story honestly, the scenario has to force a choice. In my first version, the drone started at 4 m/s, and at high λ it did not crash. It just braked, because braking is the average of "go left" and "go right". So I made it faster. The drone now starts at 12 m/s, aimed at a pillar on the line to the goal. A stop at 10 m/s² needs 12² / (2 · 10) = 7.2 m, and the pillar edge is only 6.5 m away. It has to swerve or crash.
The second surprise was the noise. With white noise (an independent random value at each step), not one of 1000 futures missed the pillar at 12 m/s. The random pushes inside one future cancel each other. I switched to smooth noise, where each step keeps 90% of the previous step's noise. Now about 10% of the futures miss the pillar, split evenly between left and right.
What λ does
I ran the dead-ahead scenario with K = 1000 and five random seeds for each λ value. One step is 0.05 s, and a run stops after 15 s.
| λ | Result |
|---|---|
| 0.01 to 1 | Goal in about 1.5 s, all seeds |
| 10 to 30 | Goal in 1.7 to 2.3 s |
| 100 to 1000 | No crash, but no goal in 15 s |
| 3000 | Crash in 4 of 5 seeds |
| 10 000 and more | Crash at about 0.55 s, all seeds |
Now the best part. At λ = 300, the first planning step looks exactly like the textbook warning. About 50 safe futures pass left, about 50 pass right, and their average points straight at the pillar.
And guess what? The drone did not crash at λ = 100 to 1000, in any seed. The split is never exactly 50/50. The plan moves a little toward the bigger group. The next iteration starts from that plan (a warm start), so more of the new samples go that way, and the imbalance grows. With about 10 iterations before impact, it wins in time. The price is that the goal pull also averages away, so the drone survives but drifts.
The crash at high λ has a different cause. Once λ is bigger than a few collision steps, futures that hit the pillar get almost the same vote as the safe ones. The average is then close to "do nothing", and the drone coasts in at 12 m/s.
So the practical rule holds, with a twist. A low λ commits to the best future at once. A medium λ wastes time on a soft average and is only rescued by the warm start. A high λ flies into the pillar, because the crashers vote too.
WebAssembly, and why I skipped the GPU
The demo is vanilla JavaScript, and it runs from the local disk with no server. I also wrote the rollout loop (steps 3 and 4) by hand in WebAssembly text. The 376-byte binary is embedded in a script tag as base64, because browsers block fetch() of a .wasm file on file://. The kernel uses the same f64 operations in the same order as the JavaScript, so the costs match bit for bit. A test checks that on 2000 random sequences.
Measured in Node 26 on an Apple arm64 Mac, with 28 pillars:
| K | Rollout, JS | Rollout, WASM | Full iteration, JS |
|---|---|---|---|
| 1 000 | 0.81 ms | 0.42 ms | 2.4 ms |
| 5 000 | 4.23 ms | 1.96 ms | 14.0 ms |
| 20 000 | 16.8 ms | 8.0 ms | 57 ms |
WebAssembly makes the rollout about 2 times faster. But the rollout is only about a third of each iteration. Making the Gaussian noise costs more, because Box-Muller needs log, sqrt, sin and cos. At the default K = 1000, everything fits easily in the 50 ms control period.
That is also why I did not move it to the GPU. At 2.4 ms per iteration, the read-back from WebGPU would make the loop asynchronous for little gain, and WGSL has no f64, so the bit-exact check would go away. The GPU starts to make sense around K = 20 000, and only if the noise is made on the GPU too.
Try it
Open the live demo, select "Pillar dead ahead", and move the λ slider. To run it from your own disk:
- Clone the repo:
git clone https://github.com/mraad/drone-mppi.git - Open
index.htmlin a browser. No server is needed. - Run
npm testto check the six tests.
Click to add or remove a pillar, and shift-click to move the goal. The forest scenario adds 28 random pillars.
Limits and next steps
This is a teaching demo, not a flight controller. The drone is a point mass with no drag or attitude. The cost has no control-effort term, and the collision test is a circle check at each step, so a fast drone could pass through a thin pillar between two steps. The λ table comes from five seeds, not a large study.
Next, I want to write the noise straight into WebAssembly memory instead of copying it, try a faster normal-random method, and see how far K can go in the browser before the GPU is worth it. More to come.
Comments