Search

Search papers and lessons

Model Predictive Control

Level 3 · 11 min read

Model predictive control plans a short future with a model, applies only the first action, then re-plans from the new state at every step.

On this page

  • Colour = topic mix
  • Calculus
  • Probability
  • Linear algebra
  • Neural network basics
  • Discrete math
  • Generative models
  • Sign in to see your progress

TL;DR

Model predictive control (MPC) picks an action by simulating a short window of the future with a model of the system, and choosing the action sequence that gives the lowest cost over that window. It then applies only the first action and throws the rest away. At the next step it measures the new state and plans again, so the plan is always built from where the system actually is.

Where you'll see it: robot and vehicle controllers, chemical plants, and planning modules in model-based reinforcement learning agents.

Intuition

Planning a few moves ahead, then looking again

A driver steering along a winding road does not commit to a full route on the first glance. They look a short distance ahead, pick a steering path that keeps them in the lane, and turn the wheel a little. A moment later they look again, because the car has moved and the road may bend in a way they did not expect. Each decision uses a short plan, but only the first move is carried out.

MPC is that habit written as an algorithm. The "model" is a prediction of how the system responds to actions. The "horizon" is how far ahead the plan looks. The "receding" part is what happens next: the window slides forward one step, and the whole optimisation runs again.

Why not plan once and execute the plan?

Optimal control can solve for the best action sequence over a whole task, once, at the start. This is called open-loop control. It works only if the model is exact and nothing unexpected happens. Real systems drift. A gust of wind pushes the car, a motor is weaker than the model says, or the model simply ignores some effect. The plan built at time zero is then wrong by time five.

Re-planning fixes this. Each new plan starts from the measured state, so errors are corrected as they appear. The price is that you must solve an optimisation at every step, and that the plan only looks as far ahead as the horizon allows. That trade is the heart of MPC: a finite look-ahead, redone often, instead of one perfect plan that cannot adapt.

The loop

flowchart LR
  A[Measure current state] --> B[Predict H steps ahead with the model]
  B --> C[Choose the action sequence with lowest cost]
  C --> D[Apply only the first action]
  D --> E[System moves to a new state]
  E --> A

The math

Take a discrete-time system with state xtx_t and action utu_t. The model is a function ff that predicts the next state:

xt+1=f(xt,ut).x_{t+1} = f(x_t, u_t).

Here tt counts steps, xtx_t is the vector of numbers that describes the system at step tt, and utu_t is the input we choose. (The continuous-time version of this, with a differential equation, is in Optimal Control.)

Definition: Finite-horizon MPC problem

At time tt, with measured state xtx_t and horizon HH, solve

min⁡ut,…,ut+H−1  ∑k=0H−1ℓ(xt+k,ut+k)+ℓf(xt+H)\min_{u_t, \dots, u_{t+H-1}} \; \sum_{k=0}^{H-1} \ell(x_{t+k}, u_{t+k}) + \ell_f(x_{t+H})

subject to xt+k+1=f(xt+k,ut+k)x_{t+k+1} = f(x_{t+k}, u_{t+k}) and any constraints on the states and actions. Here ℓ\ell is the running cost, which scores each step, and ℓf\ell_f is the terminal cost, which scores where the plan ends. Apply the first action ut⋆u_t^\star, discard the rest, and repeat at time t+1t+1.

Three design choices define an MPC controller:

  • The cost. A common choice is ℓ(x,u)=x⊤Qx+u⊤Ru\ell(x,u) = x^\top Q x + u^\top R u, which penalises distance from a target and the size of the action. QQ and RR are weight matrices that set the trade-off.
  • The horizon HH. Too short and the controller is myopic; it may run into a wall it could have avoided. Too long and the optimisation is slow and the model's errors compound over the prediction.
  • The constraints. Limits on actuators (for example ∣u∣≤1|u| \le 1) and on the state (for example staying inside a safe region) are written directly into the optimisation. This is the feature that most separates MPC from simpler feedback laws.

Why re-planning gives feedback

Because only the first action is used, and the plan is rebuilt from xtx_t, the action is a function of the current state: ut=π(xt)u_t = \pi(x_t). That makes MPC a feedback law. For an unconstrained linear system with a quadratic cost, the optimal feedback is linear, u=−Kxu = -K x, with a gain KK that comes from the Riccati solution of Optimal Control. With a suitable terminal cost (or a long enough horizon), the first action of the finite-horizon problem matches that gain, which is why MPC is often described as a constrained generalisation of linear-quadratic control.

Worked examples

Example
A one-dimensional system, by hand

The system is xt+1=xt+utx_{t+1} = x_t + u_t, a position that moves by the action. The cost is ℓ(x,u)=x2+u2\ell(x,u) = x^2 + u^2 with terminal cost ℓf(x)=x2\ell_f(x) = x^2. Use horizon H=2H = 2, and start at x0=2x_0 = 2.

  1. Write the states in terms of the actions. x1=2+u0x_1 = 2 + u_0 and x2=2+u0+u1x_2 = 2 + u_0 + u_1.
  2. Write the cost. J=(4+u02)+(x12+u12)+x22J = (4 + u_0^2) + (x_1^2 + u_1^2) + x_2^2, where the 44 is x02x_0^2 and is fixed.
  3. Minimise over u1u_1 first. Set ∂J/∂u1=0\partial J / \partial u_1 = 0, which gives 2u1+2x2=02u_1 + 2x_2 = 0, so u1=−x1/2u_1 = -x_1 / 2.
  4. Substitute. With x2=x1/2x_2 = x_1 / 2, the cost in u0u_0 becomes u02+1.5x12u_0^2 + 1.5 x_1^2 plus constants, where x1=2+u0x_1 = 2 + u_0.
  5. Minimise over u0u_0. The derivative is 2u0+3(2+u0)=02u_0 + 3(2 + u_0) = 0, so 5u0=−65u_0 = -6 and u0=−1.2u_0 = -1.2.
  6. Apply only the first action, u0=−1.2u_0 = -1.2. The state becomes x1=0.8x_1 = 0.8.
  7. Re-plan at x1=0.8x_1 = 0.8. The problem is linear and the cost is quadratic, so the optimal first action scales with the state. The gain is −1.2/2=−0.6-1.2 / 2 = -0.6, so u0=−0.6⋅0.8=−0.48u_0 = -0.6 \cdot 0.8 = -0.48, and the new state is x=0.32x = 0.32.

Interpretation: the first action pulls the state toward zero by a factor of 0.40.4 per step, and the same rule is applied again each time. Planning two steps ahead produced a simple feedback law, u=−0.6xu = -0.6x. The figure below shows the closed-loop state.

00.511.522.533.5400.511.52step tstate x_t
  • closed loop, x_0 = 2
  • target
Re-planning at every step makes the state shrink by a factor of 0.4 each time. The dots are the states at whole steps.
Example
The same problem with an actuator limit

Keep the system, cost and horizon, but add the constraint ∣ut∣≤1|u_t| \le 1. The unconstrained rule would ask for u0=−1.2u_0 = -1.2, which the actuator cannot deliver.

  1. Search over action pairs (u0,u1)(u_0, u_1) with −1≤u0,u1≤1-1 \le u_0, u_1 \le 1.
  2. The cost is J=(4+u02)+((2+u0)2+u12)+(2+u0+u1)2J = (4 + u_0^2) + ((2 + u_0)^2 + u_1^2) + (2 + u_0 + u_1)^2.
  3. Evaluate the candidates that matter. (u0,u1)=(−1.0,−0.5)(u_0, u_1) = (-1.0, -0.5) gives x1=1.0x_1 = 1.0, x2=0.5x_2 = 0.5, and J=4+1+(1+0.25)+0.25=6.5J = 4 + 1 + (1 + 0.25) + 0.25 = 6.5. The unconstrained-optimal pair (−1.2,−0.4)(-1.2, -0.4) costs 6.46.4, but it is not allowed.
  4. A grid search over the box confirms that (−1.0,−0.5)(-1.0, -0.5) is the best feasible pair, with J=6.5J = 6.5.
  5. Apply u0=−1.0u_0 = -1.0. The state becomes x1=1.0x_1 = 1.0, and the controller re-plans from there.

Interpretation: the constraint changes the first action from −1.2-1.2 to −1.0-1.0. Clipping the unconstrained answer to −1.0-1.0 happens to give the same result here, but in general the best feasible first action also depends on how the constraint affects later steps, so the constrained problem has to be solved, not clipped. This is why MPC is used when limits matter: a linear feedback gain cannot respect a hard actuator bound, but a constrained optimisation can.

Tradeoffs and failure modes

MPC is powerful because it handles constraints and nonlinear models. It is expensive because it solves an optimisation at every step. The table gives the common situations.

SituationWhat goes wrongBetter choice
Fast system, small compute budgetThe optimisation takes longer than the control interval, so the action is lateShorter horizon, warm-start from the previous plan, or explicit MPC with precomputed regions
Model is inaccurate over long predictionsErrors grow with the horizon, and the plan is confidently wrongShorter horizon, or a model that is accurate near the operating point
Nonlinear or non-differentiable model, many actionsGradient solvers stall at local minima or cannot runSampling-based planners such as the Cross-Entropy Method or random shooting
Linear system, quadratic cost, no constraintsMPC is overkill, and the optimisation adds delayLinear-quadratic regulator from Optimal Control
Rare but dangerous statesA short horizon does not see the danger until it is too lateLonger horizon, or a terminal constraint that keeps the plan inside a safe set

There is also a stability question. With a short horizon and no terminal cost, MPC can be unstable even when each plan looks reasonable. Standard designs add a terminal cost and a terminal constraint so that the controller's cost keeps decreasing from step to step. This is a theorem about the design, not a property you get for free, so it must be checked for the specific controller.

When the model is learned rather than given, MPC is combined with a World Model. The planner then depends on the model's accuracy, which is the main reason model-based methods are sensitive to prediction errors. The Model-Based Reinforcement Learning lesson covers how the model is fitted.

History

Model predictive control grew out of process-industry practice in the late 1970s. Engineers in chemical and oil plants needed controllers that could respect limits on temperatures, flows and pressures. Early industrial methods, associated with Richalet and colleagues and with Cutler and Ramaker, used linear models of the plant, so the optimisation stayed simple enough for the computers of the time.

Theory followed in later decades, with stability results built on terminal costs and constraints and extensions to nonlinear models. Faster optimisers and cheaper computers then made MPC practical for fast systems such as robots and vehicles.

In machine learning, the idea reappears as a planning method on top of a learned model. TD-MPC2 plans in the latent space of a learned world model using MPPI, a sampling-based optimiser: it samples action sequences, rolls them out with the model, and executes only the first action before re-planning.

Check yourself

QuestionWhy does MPC apply only the first action of the planned sequence, rather than executing the whole plan?Show answer

Because the model is imperfect and the system is disturbed. Each step, the controller measures the real state and re-plans from it, so errors are corrected before they grow. Executing the full plan would be open-loop control, and small errors would accumulate across the horizon.

QuestionIn the one-dimensional example with x_0 = 2 and H = 2, the first unconstrained action is u_0 = -1.2. What is the state x_1, and what is the cost of the pair (u_0, u_1) = (-1.2, -0.4)?Show answer

x_1 = 2 + (-1.2) = 0.8. The cost of the pair is 4 + 1.44 + (0.64 + 0.16) + 0.16 = 6.4, using x_0 squared, u_0 squared, x_1 squared plus u_1 squared, and x_2 squared with x_2 = 0.4. This is the lowest cost reachable without the constraint.

QuestionA planner with horizon 2 is never able to see a wall that is 5 steps ahead. Why might it still hit the wall, and what changes would help?Show answer

The wall is outside the planned window, so the cost of hitting it is never added to the plan. The controller can drive toward it while every plan it builds looks fine. Lengthening the horizon, or adding a terminal cost or constraint that encodes the safe region, lets the cost see the danger earlier.

QuestionFor a system with a learned model, why does a longer horizon not always give a better plan?Show answer

Model errors compound with each predicted step, so the far end of a long plan is built on inaccurate predictions. The optimiser may then choose actions that exploit those errors. Each re-plan also takes longer as the horizon grows. The horizon should be long enough to see the consequences that matter, and no longer.

QuestionAn unconstrained linear system with a quadratic cost has a closed-loop rule u = -0.6x. Name a constraint that this rule cannot respect, and how MPC handles it.Show answer

An actuator limit such as |u| at most 1. The rule asks for u = -1.2 when x = 2, which the actuator cannot deliver. MPC writes the limit into the optimisation, so the first action it applies is the best feasible one, here u = -1.0, and it re-plans from the resulting state.

Sources