I'll explain iLQR (iterative Linear Quadratic Regulator) starting with the conceptual overview and then diving into the mathematics.
Big Picture
iLQR is an algorithm for solving optimal control problems where you want to find the best sequence of control inputs to guide a system from a starting state to a desired goal while minimizing some cost. Think of it like planning the steering and acceleration inputs for a car to parallel park efficiently, or determining the motor commands for a robot arm to reach a target position smoothly.
The key insight of iLQR is that even though many real-world systems are nonlinear (their behavior doesn't scale proportionally with inputs), we can still use the powerful tools from linear control theory by repeatedly approximating the system as linear around our current trajectory. It's like navigating a curved road by treating small segments as straight lines and constantly updating your path.
The algorithm works iteratively: starting with an initial guess for the control sequence, it simulates the system forward, then works backward to compute improvements to the controls, applies these improvements, and repeats until convergence. Each iteration refines the trajectory by solving a simpler linear-quadratic problem around the current trajectory.
Mathematical Details
Let me walk through the mathematics step by step.
Problem Setup
We have a discrete-time nonlinear dynamical system described by:
x_{t+1} = f(x_t, u_t)
where x_t is the state at time t, u_t is the control input, and f is the (potentially nonlinear) dynamics function. We want to minimize a total cost over a time horizon from t=0 to t=T:
J = ∑_{t=0}^{T-1} ℓ(x_t, u_t) + ℓ_f(x_T)
where ℓ(x_t, u_t) is the running cost at each timestep and ℓ_f(x_T) is the final cost at the terminal state.
Step 1: Forward Pass
Given an initial control sequence {u_0, u_1, ..., u_{T-1}}, we simulate the system forward starting from initial state x_0 to get a nominal trajectory {x_0, x_1, ..., x_T}. This is straightforward: just apply the dynamics function repeatedly.
Step 2: Linearization
Around this nominal trajectory, we approximate the nonlinear dynamics with a first-order Taylor expansion. For small deviations δx_t and δu_t from the nominal trajectory:
f(x_t + δx_t, u_t + δu_t) ≈ f(x_t, u_t) + A_t δx_t + B_t δu_t
where:
- A_t = ∂f/∂x evaluated at (x_t, u_t) is the state Jacobian
- B_t = ∂f/∂u evaluated at (x_t, u_t) is the control Jacobian
Similarly, we approximate the cost function to second order:
ℓ(x_t + δx_t, u_t + δu_t) ≈ ℓ(x_t, u_t) + q_t^T δx_t + r_t^T δu_t + ½δx_t^T Q_t δx_t + δx_t^T P_t δu_t + ½δu_t^T R_t δu_t
where q_t, r_t are the gradients and Q_t, P_t, R_t are the Hessian components of the cost function.
Step 3: Backward Pass (Dynamic Programming)
Now we solve for the optimal control modifications δu_t using dynamic programming, working backward from t=T to t=0. We define the value function V_t(δx_t) as the optimal cost-to-go from time t with state deviation δx_t.
Starting at the final time, the value function is just the terminal cost:
V_T(δx_T) = ℓ_f(x_T + δx_T)
For each time step going backward, we compute the optimal control modification and update the value function. The key insight is that for our quadratic approximation, the value function will also be quadratic:
V_t(δx_t) = ½δx_t^T S_t δx_t + s_t^T δx_t + constant
The optimal control modification at each step is:
δu_t* = K_t δx_t + k_t
where K_t is the feedback gain matrix and k_t is the feedforward term, computed as:
K_t = -(R_t + B_t^T S_{t+1} B_t)^{-1}(P_t^T + B_t^T S_{t+1} A_t)
k_t = -(R_t + B_t^T S_{t+1} B_t)^{-1}(r_t + B_t^T s_{t+1})
The value function parameters are then updated:
S_t = Q_t + A_t^T S_{t+1} A_t + K_t^T R_t K_t + K_t^T P_t + P_t^T K_t
s_t = q_t + A_t^T s_{t+1} + K_t^T r_t + K_t^T R_t k_t + P_t^T k_t
Step 4: Forward Pass with Updated Controls
Using the computed gains K_t and feedforward terms k_t, we simulate forward again with the improved control law:
u_t^{new} = u_t + α k_t + K_t(x_t^{new} - x_t)
where α ∈ (0,1] is a line search parameter to ensure convergence. The feedback term K_t(x_t^{new} - x_t) helps correct for deviations from the nominal trajectory due to the nonlinearity.
Step 5: Iterate
We repeat steps 1-4 until the cost stops decreasing significantly or we reach a maximum number of iterations. Each iteration improves the trajectory by solving a linear-quadratic approximation around the current trajectory.
The beauty of iLQR is that it combines the computational efficiency of LQR (which has closed-form solutions for linear systems) with the ability to handle nonlinear dynamics through iterative refinement. It's particularly effective for systems where the dynamics are smooth and the time horizon is moderate, making it popular in robotics for trajectory optimization tasks.