Optimal Control, LQR, and Model Predictive Control

Dive into Deep Learning · §14.6

Optimal Control, LQR, and Model Predictive Control
linear-quadratic regulation · backward Riccati recursion · local linearization · iterative LQR · receding-horizon MPC

Continuous State Spaces and Bellman’s Curse

Motivation & Mental Model

  • The curse of dimensionality: Discretizing each of d state coordinates into 100 bins requires 100^d grid cells (10^{28} for a 7-joint robot arm with d = 14).
  • Kalman’s algebraic insight (1960): Linear dynamics and quadratic stage costs preserve an exact quadratic value function V_t(\mathbf{x}) = \mathbf{x}^\top\mathbf{P}_t\mathbf{x} with only d(d+1)/2 unique matrix entries.

The Linear-Quadratic Regulator (LQR) Problem

Linear Dynamics and Quadratic Costs

\mathbf{x}_{t+1} = \mathbf{A}\mathbf{x}_t + \mathbf{B}\mathbf{u}_t J = \sum_{t=0}^{H-1}\big(\mathbf{x}_t^\top\mathbf{Q}\mathbf{x}_t + \mathbf{u}_t^\top\mathbf{R}\mathbf{u}_t\big) + \mathbf{x}_H^\top\mathbf{Q}_H\mathbf{x}_H

  • Dynamics & penalties: \mathbf{A}, \mathbf{B} govern state evolution; \mathbf{Q} \succeq \mathbf{0} penalizes state error and \mathbf{R} \succ \mathbf{0} strictly penalizes control effort.

Exact Continuous Dynamic Programming

Expanding the Bellman Backup

Assuming by backward induction from t = H (\mathbf{P}_H = \mathbf{Q}_H) that the cost-to-go at step t+1 is quadratic, V_{t+1}(\mathbf{x}') = {\mathbf{x}'}^\top\mathbf{P}_{t+1}\mathbf{x}', Bellman’s optimality equation becomes:

V_t(\mathbf{x}) = \min_{\mathbf{u} \in \mathbb{R}^m} \Big\{\mathbf{x}^\top\mathbf{Q}\mathbf{x} + \mathbf{u}^\top\mathbf{R}\mathbf{u} + (\mathbf{A}\mathbf{x} + \mathbf{B}\mathbf{u})^\top\mathbf{P}_{t+1}(\mathbf{A}\mathbf{x} + \mathbf{B}\mathbf{u})\Big\}

  • Strictly convex quadratic in \mathbf{u}: Expanding the action-value bracket Q_t(\mathbf{x}, \mathbf{u}) groups into quadratic terms in \mathbf{x}, cross terms 2\mathbf{u}^\top\mathbf{B}^\top\mathbf{P}_{t+1}\mathbf{A}\mathbf{x}, and a quadratic control term \mathbf{u}^\top(\mathbf{R} + \mathbf{B}^\top\mathbf{P}_{t+1}\mathbf{B})\mathbf{u}.
  • First-order optimality: Setting \nabla_{\mathbf{u}} Q_t(\mathbf{x}, \mathbf{u}) = 2(\mathbf{R} + \mathbf{B}^\top\mathbf{P}_{t+1}\mathbf{B})\mathbf{u} + 2\mathbf{B}^\top\mathbf{P}_{t+1}\mathbf{A}\mathbf{x} = \mathbf{0} solves the backup analytically in closed form.

The Backward Riccati Recursion and Gain Matrix

Closed-Form Linear State Feedback

\mathbf{K}_t = (\mathbf{R} + \mathbf{B}^\top\mathbf{P}_{t+1}\mathbf{B})^{-1}\mathbf{B}^\top\mathbf{P}_{t+1}\mathbf{A} \mathbf{P}_t = \mathbf{Q} + \mathbf{A}^\top\mathbf{P}_{t+1}(\mathbf{A} - \mathbf{B}\mathbf{K}_t)

  • Three structural properties: (1) O(d^3 + m^3) per step; (2) \mathbf{K}_t is independent of start state \mathbf{x}_0; (3) additive Gaussian noise \mathbf{w}_t \sim \mathcal{N}(\mathbf{0}, \boldsymbol{\Sigma}) leaves \mathbf{K}_t unchanged (certainty equivalence).

The Double Integrator: LQR Derives a PD Controller

Infinite-Horizon Stationary Feedback

def riccati_lqr(A, B, Q, R, H=100):
    """Backward Riccati recursion for horizon H; returns (Ps, Ks) from t=0..H."""
    P = Q.copy()
    Ps, Ks = [P], []
    for _ in range(H):
        BtP = B.T @ P
        K = np.linalg.solve(R + BtP @ B, BtP @ A)
        P = Q + A.T @ P @ (A - B @ K)
        Ks.append(K)
        Ps.append(P)
    return Ps[::-1], Ks[::-1]

dt = 0.1
A_di = np.array([[1.0, dt], [0.0, 1.0]])
B_di = np.array([[0.5 * dt**2], [dt]])
Q_di = np.diag([1.0, 0.1])

for ru in (0.01, 0.10, 1.00):
    Ps, Ks = riccati_lqr(A_di, B_di, Q_di, np.array([[ru]]), H=100)
    K_inf, P_inf = Ks[0][0], Ps[0]
    rho = np.abs(np.linalg.eigvals(A_di - B_di @ Ks[0])).max()
    print(f'R={ru:4.2f}  K_inf=[{K_inf[0]:5.3f}, {K_inf[1]:5.3f}]  '
          f'P_00={P_inf[0, 0]:5.2f}  rho(A - B K)={rho:.3f}')

The Double Integrator: LQR Derives a PD Controller (cont.)

Infinite-Horizon Stationary Feedback

Applying riccati_lqr to a 1D vehicle \mathbf{x}_t = (p_t, v_t)^\top driven by acceleration u_t automatically derives a proportional-derivative (PD) controller u_t^* = -k_p p_t - k_v v_t:

R=0.01  K_inf=[7.613, 4.585]  P_00= 6.02  rho(A - B K)=0.761
R=0.10  K_inf=[2.762, 2.508]  P_00= 9.08  rho(A - B K)=0.874
R=1.00  K_inf=[0.930, 1.395]  P_00=15.00  rho(A - B K)=0.930
  • Asymptotic stability: As H \to \infty, \mathbf{P}_t \to \mathbf{P}_\infty (the Discrete Algebraic Riccati Equation), placing all eigenvalues of \mathbf{A} - \mathbf{B}\mathbf{K}_\infty strictly inside the unit circle (\rho < 1).

Control Effort Penalty vs. Damping Speed

Closed-Loop Trajectories of the Double Integrator

Sweeping the control penalty R \in \{0.01, 0.10, 1.00\} from initial position \mathbf{x}_0 = (1, 0)^\top shows how \mathbf{R} trades actuator effort against convergence speed:

  • Cheap control (R = 0.01): High gains \mathbf{K}_\infty = [7.613, 4.585] contract state error rapidly (\rho = 0.761, value P_{00} = 6.02).
  • Expensive control (R = 1.00): Gentle gains \mathbf{K}_\infty = [0.930, 1.395] brake slowly (\rho = 0.930, value P_{00} = 15.00).

The Pendulum-v1 Continuous-Control Benchmark

Nonlinear Control Benchmark

\dot{\vartheta}_{t+1} = \textrm{clip}\!\left(\dot{\vartheta}_t + \Big(\frac{3g}{2l}\sin\vartheta_t + \frac{3}{ml^2}u_t\Big)\Delta t,\; -8,\; 8\right)

  • Severe underactuation: Motor torque is clipped to u_t \in [-2, 2], whereas peak gravity at horizontal (\vartheta = \pi/2) is \frac{1}{2}mgl = 5 > 2.
  • Stage cost & balancing bar: c_t = \vartheta_t^2 + 0.1\,\dot{\vartheta}_t^2 + 0.001\,u_t^2; random play scores \approx -1{,}200, while swing-up and balance averages about -200 or better over random starts.

Linearizing Nonlinear Physics at an Equilibrium

First-Order Taylor Approximation

For a smooth nonlinear system \mathbf{x}_{t+1} = f(\mathbf{x}_t, u_t) near a fixed point f(\mathbf{x}^*, u^*) = \mathbf{x}^*, a first-order Taylor expansion gives a local linear model in (\delta\mathbf{x}_t, \delta u_t):

\delta\mathbf{x}_{t+1} \approx \mathbf{A}\,\delta\mathbf{x}_t + \mathbf{B}\,\delta u_t, \qquad \mathbf{A} = \frac{\partial f}{\partial \mathbf{x}}(\mathbf{x}^*, u^*), \quad \mathbf{B} = \frac{\partial f}{\partial u}(\mathbf{x}^*, u^*)

  • Upright equilibrium of Pendulum-v1: At (\vartheta^*, \dot{\vartheta}^*) = (0, 0) with u^* = 0, \sin\vartheta_t \approx \vartheta_t yields an exact local linear model (\mathbf{A}_p, \mathbf{B}_p) around the unstable top saddle point.

Stabilizing Upright Pendulum via Local LQR

Shifting Unstable Eigenvalues Inside the Unit Circle

Computing the exact Jacobians (\mathbf{A}_p, \mathbf{B}_p) of Pendulum-v1 at the upright equilibrium \vartheta = 0 and running the Riccati recursion stabilizes the saddle point:

A at upright:
[[1.0375 0.05  ]
 [0.75   1.    ]]
open-loop eigenvalues:   1.2133, 0.8242
LQR gain K_inf:          [19.693, 5.263]
closed-loop eigenvalues: 0.8537, 0.2467
  • Open-loop instability vs. closed-loop stability: The open-loop Jacobian has an unstable eigenvalue \lambda_1 = 1.2133 > 1 (tilts grow 21\% per step); LQR gain \mathbf{K}_\infty = [19.693, 5.263] pulls both eigenvalues inside the unit circle (0.8537 and 0.2467).

Where Local LQR Fails: Torque Saturation

Underactuation and the Catch Basin

Gravity pulls the pendulum down with torque \frac{1}{2}mgl\sin\vartheta = 5\sin\vartheta, whereas the motor is clipped to |u_t| \leq 2, setting a static holding boundary at |\vartheta| = \arcsin(2/5) \approx 0.412\text{ rad} (23.6^\circ):

th0=0.20 rad: 200-step return=   -0.32, final theta=+0.000 rad
th0=0.40 rad: 200-step return=   -2.97, final theta=+0.000 rad
th0=0.50 rad: 200-step return= -770.94, final theta=+2.592 rad
th0=0.60 rad: 200-step return= -546.37, final theta=+0.001 rad
th0=3.14 rad: 200-step return=-1471.86, final theta=-2.249 rad
  • Sharp basin threshold: Starting at \vartheta_0 = 0.20 or 0.40\text{ rad}, clipped linear LQR catches and balances the rod at \vartheta_{200} = 0.000\text{ rad}.
  • Failure outside the basin: At \vartheta_0 = 0.50\text{ rad} (5\sin 0.50 = 2.40 > 2) or from the bottom (\vartheta_0 = \pi), full torque u = -2 cannot arrest gravity; swinging up requires multi-swing energy pumping. From 0.60\text{ rad} the rod also falls, and is caught only by luck after several swings.

Iterative LQR (iLQR): Trajectory Linearization

Gauss-Newton Trajectory Optimization

v_t^{\textrm{new}} = \bar{v}_t + \alpha\,k_t + \mathbf{K}_t(\mathbf{x}_t^{\textrm{new}} - \bar{\mathbf{x}}_t)

  • Smooth torque squashing: u_t = 2\tanh(v_t) enforces u_t \in (-2, 2) differentiably.
  • Backward Riccati + forward line search: Compute time-varying Jacobians (\mathbf{A}_t, \mathbf{B}_t), solve backward for (k_t, \mathbf{K}_t), and roll out true nonlinear physics with backtracking \alpha.

iLQR Discovers Multi-Phase Energy Pumping

Nonlinear Swing-Up from the Bottom

In 35 iterations from the hanging equilibrium (\pi, 0), iLQR discovers a three-phase swing-up and catch trajectory over H = 80 steps (4.0\text{ s}):

  • Steps 0–10 (back-swing): Pushes left (u_0 = -1.78) to \vartheta_{10} = 2.65\text{ rad}.
  • Steps 10–30 (energy pump): Reverses torque (u_{20} = +1.68) to swing through the bottom to \vartheta_{30} = 4.04\text{ rad}.
  • Steps 30–80 (whip & catch): Reverses again (u_{40} = -1.83), still moving at \dot{\vartheta}_{40} = -4.44\text{ rad/s} on the way up, and braking to rest at \vartheta_{80} = 0.0001\text{ rad}.

iLQR Discovers Multi-Phase Energy Pumping (cont.)

Nonlinear Swing-Up from the Bottom

The swing-up trajectory every 10 steps, from the hanging start to the caught upright state:

t= 0  theta= 3.1416  thetadot= 0.0000  u=-1.782
t=10  theta= 2.6515  thetadot=-1.0260  u=+0.965
t=20  theta= 3.5453  thetadot= 3.3225  u=+1.681
t=30  theta= 4.0383  thetadot=-2.6013  u=-1.807
t=40  theta= 1.5668  thetadot=-4.4402  u=-1.832
t=50  theta= 0.3746  thetadot=-1.3417  u=-1.209
t=60  theta= 0.0009  thetadot=-0.0132  u=+0.066
t=70  theta= 0.0002  thetadot=-0.0006  u=-0.000
t=80  theta= 0.0001  thetadot=-0.0000  u=+0.000

Open-Loop vs. TV-LQR vs. Receding-Horizon MPC

From Trajectory Plans to Closed-Loop Control

  1. Open-loop (u_t = \bar{u}_t): Applies precomputed torques blind to state \mathbf{x}_t; any knock causes the unstable top saddle to fall.
  2. TV-LQR (v_t = \bar{v}_t + \mathbf{K}_t(\mathbf{x}_t - \bar{\mathbf{x}}_t)): Wraps the path in a local feedback tube at zero online cost, but fails if pushed out of the tube.
  3. Receding-horizon MPC: Re-solves iLQR from measured \mathbf{x}_t at every step, warm-started from the shifted plan, applies u_{t|t}^*, and re-plans at t+1. If the warm-started plan misses the top, it plans a fresh 60-step swing-up.

Disturbance Rejection Under an Impulse at t = 45

Testing Open-Loop, TV-LQR, and MPC

Applying an external angular-velocity kick \Delta\dot{\vartheta} \in \{0.0, +0.5, +2.2\}\text{ rad/s} at step t = 45 (just as the pendulum climbs toward the catch basin) separates the three controllers:

Disturbance Rejection Under an Impulse at t = 45 (cont.)

Testing Open-Loop, TV-LQR, and MPC

Final angle and return of each controller under each kick:

impulse=+0.0 rad/s at t=45:
  Open-loop:  final theta=+0.075, return=-333.6
  TV-LQR:     final theta=+0.000, return=-333.7
  MPC (iLQR): final theta=+0.000, return=-331.6
impulse=+0.5 rad/s at t=45:
  Open-loop:  final theta=-2.939, return=-502.3
  TV-LQR:     final theta=-0.001, return=-333.9
  MPC (iLQR): final theta=+0.000, return=-331.7
impulse=+2.2 rad/s at t=45:
  Open-loop:  final theta=+2.941, return=-621.1
  TV-LQR:     final theta=+1.959, return=-797.9
  MPC (iLQR): final theta=-0.000, return=-558.2

Phase Portraits and Recovery from Strong Knocks

Why Online Re-Planning Is Essential

Comparing the three panels below shows how local LQR, trajectory optimization, and receding-horizon re-planning fit together:

  • Moderate knock (\Delta\dot{\vartheta} = +0.5): Open-loop falls (\vartheta_{120} = -2.94), while both TV-LQR and MPC absorb the perturbation inside the linear tube (\vartheta_{120} = 0.000).
  • Strong knock (\Delta\dot{\vartheta} = +2.2): Arrests the upward climb; TV-LQR saturates its linear feedback around the old plan and fails (\vartheta_{120} = +1.96), whereas MPC sees its warm-started plan miss the top, re-seeds a fresh swing-up, and restores \vartheta_{120} = 0.000.

Classical Optimal Control vs. Reinforcement Learning

Unifying Dictionary Across Communities

  • Variables & objective: Optimal control minimizes stage cost \sum_t c(\mathbf{x}_t, \mathbf{u}_t) over (\mathbf{x}_t, \mathbf{u}_t) \in \mathbb{R}^d \times \mathbb{R}^m; RL maximizes discounted return \mathbb{E}[\sum_t \gamma^t r(s_t, a_t)].
  • Backups: Riccati recursion on \mathbf{x}^\top\mathbf{P}_t\mathbf{x} mirrors Bellman operator \mathcal{T} on V(s), Q(s, a).
  • Unknown physics: System identification + MPC bridges directly to model-based RL (§15.6) and model-free policy gradients (§14.7).

Concept Check: LQR, iLQR, and MPC

Check Your Understanding

Test your intuition on Riccati backups, torque saturation, and receding-horizon control:

  1. Riccati dimensionality: For a humanoid robot with d = 36 state dimensions, how many entries would a coarse 10-bin-per-axis grid require versus the symmetric Riccati matrix \mathbf{P}_t?
  2. Certainty equivalence: If zero-mean Gaussian wind gusts \mathbf{w}_t \sim \mathcal{N}(\mathbf{0}, \boldsymbol{\Sigma}) perturb the linear double integrator \mathbf{x}_{t+1} = \mathbf{A}\mathbf{x}_t + \mathbf{B}\mathbf{u}_t + \mathbf{w}_t, how does the optimal feedback gain \mathbf{K}_t change?
  3. TV-LQR vs. MPC: Why did TV-LQR recover from the +0.5\text{ rad/s} impulse at t = 45 but fail completely under the +2.2\text{ rad/s} impulse?

Answers:

  1. A 10-bin grid requires 10^{36} cells, whereas \mathbf{P}_t \in \mathbb{R}^{36 \times 36} has only d(d+1)/2 = 36 \times 37 / 2 = 666 unique numbers.
  2. \mathbf{K}_t does not change at all. In \mathbb{E}[(\mathbf{A}\mathbf{x} + \mathbf{B}\mathbf{u} + \mathbf{w})^\top\mathbf{P}_{t+1}(\mathbf{A}\mathbf{x} + \mathbf{B}\mathbf{u} + \mathbf{w})], the linear cross term in \mathbf{w} vanishes and \mathbb{E}[\mathbf{w}^\top\mathbf{P}_{t+1}\mathbf{w}] = \textrm{tr}(\mathbf{P}_{t+1}\boldsymbol{\Sigma}) adds only a constant independent of \mathbf{u}.
  3. The +0.5\text{ rad/s} push kept the rod inside the local linear tube around the nominal trajectory, whereas +2.2\text{ rad/s} knocked it back down where torque saturates and a brand-new multi-swing energy pump (re-planned by MPC) is required.

Summary: Classical Optimal Control Methods

Key Takeaways

Method Dynamics & Cost Model Policy Form & Capabilities
Finite/Infinite LQR Linear \mathbf{A}\mathbf{x} + \mathbf{B}\mathbf{u}, quadratic \mathbf{x}^\top\mathbf{Q}\mathbf{x} + \mathbf{u}^\top\mathbf{R}\mathbf{u} Exact linear feedback \mathbf{u}_t^* = -\mathbf{K}_t\mathbf{x}_t via backward Riccati recursion
Local Equilibrium LQR Jacobian linearization (\mathbf{A}, \mathbf{B}) at fixed point (\mathbf{x}^*, u^*) Stabilizes unstable equilibria locally inside actuator saturation limits
Iterative LQR (iLQR) Time-varying Jacobians (\mathbf{A}_t, \mathbf{B}_t) along nominal rollout Discovers nonlinear multi-swing trajectories via backward Riccati + line search
TV-LQR Tracking Linear perturbation tube around nominal (\bar{\mathbf{x}}_t, \bar{v}_t) Closed-loop feedback v_t = \bar{v}_t + \mathbf{K}_t(\mathbf{x}_t - \bar{\mathbf{x}}_t) for small disturbances
Receding-Horizon MPC Re-solves the finite-horizon optimization from \mathbf{x}_t online Global closed-loop recovery; plans fresh swing-ups after large knocks