Stochastic MPC

Planning against sampled futures — and a precise answer to when the uncertainty actually changes the decision.

What you'll learn
  • Build a scenario-based stochastic MPC
  • Prove when averaging over scenarios cannot change the optimal input
  • Design a fair comparison between MPC and SMPC

Real cars are pushed around by wind, slope and sensor noise. The stochastic MPC example models this with small random disturbances on position and velocity:

\[\mathbf{x}_{k+1} = A\mathbf{x}_k + Bu_k + \mathbf{w}_k, \qquad w_{\text{pos}} \sim \mathcal{N}(0, 0.01^2),\;\; w_{\text{vel}} \sim \mathcal{N}(0, 0.05^2)\]

Scenarios

The script handles the uncertainty by sampling. At every step it draws five disturbance sequences over the horizon, predicts the car under each, and minimises the average cost — with one shared input plan for all scenarios:

for s in range(num_scenarios):
    for t in range(N):
        constraints.append(x_scenarios[s][t+1] == A @ x_scenarios[s][t] + B @ u[t] + w[s, t])
        scenario_cost += cp.quad_form(x_scenarios[s][t] - ref_horizon[t], Q) + cp.quad_form(u[t], R)
    cost += (1.0 / num_scenarios) * scenario_cost

This is still a QP, just a larger one: five copies of the state, one copy of the input.

Violet: each sampled scenario's predicted velocity under the shared plan. Dashed blue: their mean. The read-out compares the input SMPC applies with what plain MPC would apply from the same state — raise the number of scenarios and the two converge.

When does the spread matter?

There is a clean result hiding here. Because the dynamics are linear, each scenario’s predicted state is the nominal prediction plus a disturbance term that does not depend on \(u\):

\[\mathbf{x}^{(s)}_t = \bar{\mathbf{x}}_t(u) + \mathbf{d}^{(s)}_t\]

Average a quadratic over the scenarios and it splits exactly into the cost at the mean disturbance plus a spread term:

\[\frac1S \sum_s \|\bar{\mathbf{x}}_t(u) + \mathbf{d}^{(s)}_t - \mathbf{r}_t\|_Q^2 = \|\bar{\mathbf{x}}_t(u) + \bar{\mathbf{d}}_t - \mathbf{r}_t\|_Q^2 + \underbrace{\frac1S \sum_s \|\mathbf{d}^{(s)}_t - \bar{\mathbf{d}}_t\|_Q^2}_{\text{independent of } u}\]

The spread term cannot be changed by any input, so the optimiser ignores it. The scenario SMPC is exactly nominal MPC tracking a reference shifted by the sample mean of the disturbances. Checked numerically against the script’s formulation: across 200 random problems the two optimal inputs agree to \(4 \times 10^{-8}\).

Two consequences follow:

  • With many scenarios the sample mean tends to zero and SMPC becomes MPC.
  • With few scenarios, SMPC is MPC plus noise from that sample mean — which shrinks like \(1/\sqrt{S}\):
How far SMPC's first input strays from plain MPC's, by number of scenarios. The measured curve follows the 1/√S prediction closely.

Comparing MPC and SMPC fairly

The comparison script reports SMPC with noticeably larger tracking error:

Five stacked plots comparing deterministic and stochastic MPC over ten seconds: position, velocity, position error, velocity error and acceleration. The stochastic trace is noisier and has larger position error.
From compare_mpc_vs_smpc.py.

But the two runs face different cars: run_deterministic_mpc updates the state with car_dynamics, which has no disturbance, while SMPC uses car_dynamics_with_disturbance. Most of the gap is the noise, not the controller. Run both on the same disturbed plant with the same disturbance sequence, averaged over 20 seeds, and they are almost indistinguishable:

Controller (same noisy car) Mean position error Mean velocity error
MPC 0.453 m 0.275 m/s
SMPC, 5 scenarios 0.457 m 0.276 m/s

For reference, MPC on the noise-free car scores 0.383 m and 0.252 m/s.

Making uncertainty matter

The result above says that, with a shared input, linear dynamics and a quadratic cost, the spread of the scenarios cannot influence the decision. Uncertainty starts to matter when one of those assumptions breaks:

  • Chance constraints — “keep speed below 2.1 m/s with 95% probability”. A constraint on the spread depends on how close the plan runs to the limit, so the plan backs off.
  • Feedback policies — optimise a rule \(u_t = K\mathbf{x}_t + v_t\) rather than fixed inputs, so later inputs react to disturbances and the spread depends on \(K\).
  • Non-quadratic costs, where the average is no longer the cost of the average.

Try it yourself. In compare_mpc_vs_smpc.py, pass actual_disturbances to the deterministic run and update its state with car_dynamics_with_disturbance, then compare the metrics again. Next, add a chance-style constraint by requiring every scenario to satisfy x_scenarios[s][t][1] <= 2.1: does SMPC now behave differently from MPC near the speed limit?

View source on GitHub →