Foundations to Frontier
Tests
L. Reinforcement Learning and Autonomy › L6 Planning and Control

Model predictive control

Model predictive control (MPC) plans a short sequence of actions with a model, applies only the first one, measures the new state and plans again. Each plan is an optimisation problem, so MPC can respect limits on forces, speeds and positions that a fixed feedback law ignores. This page builds linear MPC for a cart that must stop at a wall, solving each quadratic program exactly, and measures how the horizon length decides whether the wall is seen in time and how a safety margin protects against random pushes.

Level 0: Foundation

★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.

Plan, act a little, look again

A driver approaching a red light does not compute the whole trip home. They look a few seconds ahead, plan how to brake smoothly without crossing the line, press the pedal a little, and re-plan a moment later with fresh information. Model predictive control works the same way: it repeatedly solves a small planning problem over a short look-ahead window and uses only the first step of each plan. Because each plan is computed from scratch, it can obey hard rules such as speed limits and walls.

Planning and control lab
beyond the wall10 s; start 5 m back
A cart starts 5 m from a wall (shaded beyond) and must stop at it, with |force| ≤ 1 and |speed| ≤ 2. The grey line is LQR with its force clipped: it overshoots by 0.98 m and spends 36 steps past the wall. The blue line is MPC with a 20-step (=2.0 s) horizon: furthest past the wall 0.000 m, 0 steps past it. Stopping from 2 m/s takes 20 steps, so shorter horizons see the wall too late; with random pushes, only a safety margin keeps the cart off the wall.
Why engineers care

MPC runs oil refineries and chemical plants, autonomous cars, quadrotors, legged robots and building climate systems, and is the most widely used advanced control method in industry.

📝 Quick check (Level 0)
0/3 answered
  1. 1. MPC applies…
  2. 2. MPC re-plans…
  3. 3. MPC's main advantage over a fixed controller is…

Level 1: Beginner

★★☆☆☆ Basic⏱ 20 minSecondary-school mathematics: arithmetic and elementary algebra.

By hand

  • Prediction model: x_(k+1) = Ax_k + Bu_k, used to predict the states x_1 … x_N that a control sequence u_0 … u_(N−1) would produce.
  • Horizon N: the number of steps planned at each decision; longer horizons see further but cost more computation.
  • Constraints: limits such as |u| ≤ u_max, |v| ≤ v_max or position ≤ wall, imposed on every predicted step.
  • Receding horizon: solve the N-step problem, apply u_0, measure the new state, shift the window and repeat.
  • Condensed quadratic program (QP): with linear dynamics and quadratic cost, the predictions are linear in the controls, so each plan is a QP: minimise ½UᵀHU + qᵀU subject to linear inequalities.
  • Terminal cost: a cost x_NᵀPx_N on the final predicted state; with P from the Riccati equation (topic L6-3), MPC equals LQR whenever no constraint is active.
  • Soft constraint: a constraint relaxed by a penalised slack variable, so a plan always exists even when the limit cannot be met.
  • Recursive feasibility: the property that if a safe plan exists now, one will exist at the next step too.
📝 Quick check (Level 1)
0/3 answered
  1. 1. Each linear MPC step solves a…
  2. 2. With the Riccati terminal cost and no active constraint, MPC equals…
  3. 3. A soft constraint uses…

Level 2: Intermediate

★★★☆☆ Intermediate⏱ 25 minUndergraduate students and practising engineers.

Linear MPC for a cart and a wall

Exercise 1.

  • A cart on a track: state (position, velocity), control = acceleration, steps of 0.1 s; cost Σ position² + 0.1·velocity² + 0.1·u², with the Riccati matrix as terminal cost.
  • Limits: |u| ≤ 1, |v| ≤ 2 and position ≤ 0, the wall. The wall is a soft constraint with slack priced at 1000 per metre (plus a tiny quadratic term).
  • Each step’s QP is solved exactly by the dual active-set method of Goldfarb and Idnani: start from the unconstrained optimum, add the most violated constraint, drop constraints whose multipliers would become negative.
  • Checks: with the limits switched off, MPC’s first move equals the LQR action; with them on, the plan from 5 m back obeys every limit.

Practice.

  • Use MPC when constraints matter: actuator limits, safety envelopes, comfort limits or economic objectives; use LQR when they never bind.
  • Make the horizon at least as long as the slowest manoeuvre you must foresee, such as the stopping distance, or add a terminal constraint that guarantees one.
  • Soften state constraints so the solver never fails, and tighten them by a margin (or use tube MPC) when disturbances are present.
  • Nonlinear MPC re-linearises around the previous plan (as iLQR does, topic L6-3) or solves a nonlinear program each step; learned models give learning-based MPC (topic L6-5).
  • Tools: OSQP, qpOASES and HPIPM for QPs; acados, CasADi and do-mpc for nonlinear MPC.
def qp(H, q, Nc, b, eps=1e-9):          # minimise ½xᵀHx + qᵀx s.t. Ncᵀx ≥ b
    Hi = np.linalg.inv(H); x = -Hi @ q; act, lam = [], np.zeros(0)
    while True:
        s = Nc.T @ x - b; s[act] = 0.0; p = int(np.argmin(s))
        if s[p] >= -eps: return x, act, True        # all constraints met
        lam = np.append(lam, 0.0); n_p = Nc[:, p]
        while True:
            Na = Nc[:, act]
            r = np.linalg.solve(Na.T @ Hi @ Na, Na.T @ Hi @ n_p) if act else np.zeros(0)
            z = Hi @ (n_p - Na @ r)                   # primal step direction
            pos = [j for j in range(len(act)) if r[j] > eps]
            t1, k = min(((lam[j] / r[j], j) for j in pos), default=(np.inf, -1))
            t2 = -(n_p @ x - b[p]) / (z @ n_p) if z @ n_p > eps else np.inf
            if t1 == t2 == np.inf: return x, act, False
            t = min(t1, t2); x = x + (t if t2 < np.inf else 0.0) * z
            lam[:-1] -= t * r; lam[-1] += t
            if t == t2: act.append(p); break
            del act[k]; lam = np.delete(lam, k)
🐍 Exercise 1: an exact MPC step
Condense the predictions into a QP, solve it exactly with the dual active-set method, confirm that unconstrained MPC reproduces LQR, and check that the constrained plan respects every limit.
📝 Quick check (Level 2)
0/3 answered
  1. 1. The horizon should be at least…
  2. 2. State constraints are softened so that…
  3. 3. A widely used QP solver for MPC is…

Level 3: Advanced

★★★★☆ Advanced⏱ 25 minGraduate level, with full derivations and proofs.

Theory

MPC and LQR agree when nothing binds. With linear dynamics, quadratic cost and the Riccati matrix P as terminal cost, the N-step optimal cost-to-go from any state equals the infinite-horizon LQR value xᵀPx, because P already summarises everything after step N (topic L6-3). So if no constraint is active, MPC’s first move is exactly −Kx.

Why short horizons fail. A plan only respects constraints it can see. Stopping from 2 m/s at 1 m/s² takes 20 steps of 0.1 s and 2 m. With a horizon of 5 or 10 steps, by the time the wall enters the window the cart is already too fast to stop; the plan becomes infeasible, and the soft constraint lets it cross. This is a failure of recursive feasibility, which a long enough horizon or a terminal constraint (the predicted state must end in a set from which the limits can always be met) restores.

Exact penalties. If the slack price exceeds the largest Lagrange multiplier of the hard constraint, the soft problem has the same solution as the hard one whenever the hard one is feasible; a linear penalty has this property, a purely quadratic one does not.

Disturbances and tightening. MPC re-plans from the measured state, which corrects drift, but between measurements a push can carry the cart past a limit that the plan was sitting on. Tightening the constraint by a margin that covers the likely disturbance keeps the real state inside; tube MPC sizes that margin from a disturbance bound and an ancillary feedback controller.

📝 Quick check (Level 3)
0/3 answered
  1. 1. Short horizons failed because…
  2. 2. A slack penalty is exact when it is…
  3. 3. Tightening a constraint protects against…

Level 4: Expert

★★★★★ Expert⏱ 30 minResearchers: original papers and open problems.

How long must the MPC horizon be for the cart to stop at the wall, how does MPC compare with clipped LQR, and how much safety margin do random pushes require?

History.

  • J. Richalet and colleagues (1978) introduced model predictive heuristic control; C. Cutler and B. Ramaker (1980) introduced dynamic matrix control at Shell.
  • D. Mayne, J. Rawlings, C. Rao and P. Scokaert (2000) set out the stability theory of constrained MPC.
  • D. Goldfarb and A. Idnani (1983) introduced the dual active-set QP method used here; B. Stellato and colleagues (2020) introduced the OSQP solver.
  • J. Rawlings, D. Mayne and M. Diehl’s book (2017) is the standard reference.

Exercise 2.

  • The cart starts 5 m from the wall at rest and runs for 10 s (100 steps).
  • Controllers: LQR with its output clipped to |u| ≤ 1, and MPC with horizons of 5, 10, 20 and 40 steps.
  • Random pushes: velocity noise with standard deviation 0.03 per step; MPC with N = 20 and the wall tightened by 0, 0.1 and 0.2 m. Only this part depends on the seed.
  • Measured: total cost, how far past the wall the cart went, steps more than 1 cm past it, top speed, and steps where no plan could keep the cart behind the wall (slack used).

Without pushes (identical on all seeds):

controller cost furthest past wall (m) steps past wall top speed steps with no safe plan
clipped LQR 466.9 0.975 36 2.374 —
MPC, N = 5 457.4 0.391 17 2.000 21
MPC, N = 10 455.3 0.225 13 2.000 22
MPC, N = 20 454.1 0.000 0 2.000 0
MPC, N = 40 454.1 0.000 0 2.000 0

With random pushes, MPC N = 20 (furthest past the wall in m, steps past it):

margin seed 0 seed 1 seed 2
none 0.078, 8 0.135, 9 0.141, 10
0.1 m 0.000, 0 0.023, 3 0.082, 8
0.2 m 0.000, 0 0.000, 0 0.000, 0

Findings.

  • Clipped LQR ignores the limits it cannot see: it reached 2.37 m/s against a limit of 2 and crossed the wall by 0.975 m.
  • The horizon must cover the stopping distance: braking from 2 m/s takes 20 steps, and N = 5 and 10 crossed the wall by 0.391 and 0.225 m, while N = 20 and 40 never did. N = 40 improved the cost over N = 20 by only 0.001.
  • Pushes defeat a plan that sits on the limit: without a margin the cart crossed the wall on every seed (by 0.078 to 0.141 m); a 0.1 m margin was enough on one seed out of three, and a 0.2 m margin on all three, at a cost of 444.7 against 440.9 without a margin on seed 0.
🐍 Exercise 2 (research): how long must the MPC horizon be for the cart to stop at the wall, how does MPC compare with clipped LQR, and how much safety margin do random pushes require?
Compare clipped LQR with MPC of horizons 5 to 40 on a cart that must stop at a wall, then add random pushes and find the safety margin that keeps it off the wall.

MPC in practice

  • Process industry: MPC has run refineries and chemical plants since the 1980s, where slow dynamics leave seconds to minutes per solve.
  • Autonomous driving: MPC tracks planned paths under steering, acceleration and comfort limits.
  • Legged robots and drones: MIT’s Cheetah and many quadrotors use convex MPC at hundreds of hertz.
  • Learning and MPC: learned dynamics models (PETS, TD-MPC) and learned terminal values combine RL with MPC (topic L6-5).

References

  1. J. Rawlings, D. Mayne and M. Diehl (2017). Model Predictive Control: Theory, Computation, and Design. Nob Hill.
  2. D. Mayne, J. Rawlings, C. Rao and P. Scokaert (2000). “Constrained model predictive control: stability and optimality.” Automatica.
  3. D. Goldfarb and A. Idnani (1983). “A numerically stable dual method for solving strictly convex quadratic programs.” Mathematical Programming.
  4. B. Stellato and colleagues (2020). “OSQP: an operator splitting solver for quadratic programs.” Mathematical Programming Computation.
  5. S. Qin and T. Badgwell (2003). “A survey of industrial model predictive control technology.” Control Engineering Practice.