Model predictive control

A controller that solves a fresh optimization problem every tenth of a second — running live, below, on the repo's car.

What you'll learn
  • Formulate a tracking problem over a finite horizon as a QP
  • Explain the receding horizon — plan N steps, apply one, re-plan
  • Predict how horizon length, input weight and limits change the behaviour

Everything so far comes together here. Model predictive control (MPC) turns control into repeated convex optimization: at every time step, use a model to predict the future, choose the best inputs for the next \(N\) steps, apply only the first one, and start again.

The car

The repo’s example is a car moving along a road. Its state is position and velocity, and the input is acceleration:

\[\mathbf{x}_{k+1} = A\mathbf{x}_k + B u_k, \qquad A = \begin{bmatrix} 1 & \Delta t \\ 0 & 1 \end{bmatrix},\; B = \begin{bmatrix} 0 \\ \Delta t \end{bmatrix},\; \Delta t = 0.1\,\text{s}\]

The goal is to follow a reference velocity that jumps from 0 to 2 m/s at \(t = 1\) s and back to 0 at \(t = 5\) s, with acceleration limited to \(\vert u\vert \le 1\).

The optimization problem

At step \(k\), from the measured state \(\mathbf{x}_k\), solve

\[\min_{u_0,\dots,u_{N-1}} \;\sum_{t=0}^{N-1} (\mathbf{x}_t - \mathbf{r}_t)^{\mathsf{T}} Q\, (\mathbf{x}_t - \mathbf{r}_t) + R\,u_t^2 \quad\text{s.t.}\quad \mathbf{x}_{t+1} = A\mathbf{x}_t + Bu_t,\;\; |u_t| \le 1\]

with \(Q = \operatorname{diag}(0.1, 1)\) and \(R = 0.1\). The cost is a convex quadratic, the dynamics are linear equalities and the input limits are linear inequalities — a quadratic program, exactly the family from the paint-mixing lesson. In CVXPY:

u = cp.Variable((N, nu))
x_var = cp.Variable((N+1, nx))

cost = 0
for t in range(N):
    cost += cp.quad_form(x_var[t] - ref_horizon[t], Q) + cp.quad_form(u[t], R)

constraints = [x_var[0] == x]
for t in range(N):
    constraints.append(x_var[t+1] == A @ x_var[t] + B @ u[t])
    constraints += [u[t] >= -1, u[t] <= 1]

cp.Problem(cp.Minimize(cost), constraints).solve(solver=cp.OSQP)
u_optimal = u.value[0, 0]          # apply only the first input

The receding horizon, live

The MPC loop: measure the state, solve the horizon QP to plan N inputs, apply only the first input, the car moves, and the horizon shifts one step before the loop repeats. The MPC loop: measure the state, solve the horizon QP to plan N inputs, apply only the first input, the car moves, and the horizon shifts one step before the loop repeats.
The controller never executes a full plan. It re-plans from fresh measurements every step.

The animation below runs this controller in your browser. It eliminates the state variables so each step is a small box-constrained QP in the inputs alone, and solves it by accelerated projected gradient. Over the full simulation it reproduces the CVXPY version’s inputs to within \(3 \times 10^{-8}\).

Orange: the car's actual velocity. Blue dotted: the plan just computed for the next N steps (shaded window); only its first input is applied before everything is re-planned. Watch the acceleration (green) sit on its ±1 limit while the car catches up. Change the horizon, input weight or limit and press Reset.

A few things worth trying:

  • Shorten the horizon to 2 or 3. The controller only looks a few steps ahead, so it cannot see the reference drop coming and reacts late.
  • Raise R. Acceleration becomes expensive, so the car responds gently and lags further behind.
  • Loosen the limit. With \(\lvert u\rvert \le 3\) the car can almost follow the step.

What the controller actually does

The full closed loop with the repo's settings. The acceleration saturates at +1 while the car speeds up and at −1 while it slows: the constraint is active for long stretches, which is exactly why MPC is used instead of a simple linear controller.

The velocity briefly overshoots to about 2.04 m/s. That is the position term at work: \(Q\) also penalises position error, and while the car was accelerating it fell behind the reference position, so it runs slightly fast to catch up.

One detail of the formulation: the cost sums over \(t = 0, \dots, N-1\), so the final predicted state \(\mathbf{x}_N\) is never penalised. The last input \(u_{N-1}\) therefore affects nothing in the cost but its own \(R\) term, and the solver always sets it to zero. Production MPC adds a terminal cost on \(\mathbf{x}_N\) to fix this and to guarantee stability.

Try it yourself. Add a terminal cost cp.quad_form(x_var[N] - ref, P) with \(P = 10Q\) to the script and compare the overshoot. Then add a speed limit x_var[t][1] <= 2.0 as a state constraint: is the problem still a QP, and what happens to the overshoot?

View source on GitHub →