Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

LQR Example

LQR Example

This notebook demonstrates how to solve a finite-horizon Linear Quadratic Regulator (LQR) problem using a Gaussian Factor Graph in GTSAM. The goal is to minimize a quadratic cost w.r.t. dynamics constraints over a linear dynamical system using factor-graph-based optimization. The results are compared against results from classical Riccati equations. Adapted from https://gtsam.org/2019/11/07/lqr-control.html

Author(s): Zhouyu Zhang

Open In Colab

GTSAM Copyright 2010-2025, Georgia Tech Research Corporation, Atlanta, Georgia 30332-0415 All Rights Reserved

Authors: Frank Dellaert, et al. (see THANKS for the full author list)

See LICENSE for the license information

Proceed (Y/n)? y
Proceed (Y/n)? y

Discrete-Time State-Space Form (2D State: Position and Velocity)

We look to work on a 1D double integrator system, in this sense, we define the state and input as:

xk=[xx˙],uk=x¨x_k = \begin{bmatrix} x \\ \dot{x} \end{bmatrix}, \quad u_k = \ddot{x}

We can think of it as a linear vehicle for which we can control the gas paddle (acceleration). Then the discrete-time dynamics become:

xk+1=Axk+Bukx_{k+1} = A x_k + B u_k

Where:

A=[1Δt01],B=[12Δt2Δt]A = \begin{bmatrix} 1 & \Delta t \\ 0 & 1 \end{bmatrix}, \quad B = \begin{bmatrix} \frac{1}{2} \Delta t^2 \\ \Delta t \end{bmatrix}

So:

[xk+1x˙k+1]=[1Δt01][xkx˙k]+[12Δt2Δt]uk\begin{bmatrix} x_{k+1} \\ \dot{x}_{k+1} \end{bmatrix} = \begin{bmatrix} 1 & \Delta t \\ 0 & 1 \end{bmatrix} \begin{bmatrix} x_k \\ \dot{x}_k \end{bmatrix} + \begin{bmatrix} \frac{1}{2} \Delta t^2 \\ \Delta t \end{bmatrix} u_k

LQR Problem Setup

We seek to minimize the infinite-horizon cost function:

J=∑k=0∞(xk⊤Qxk+uk⊤Ruk)J = \sum_{k=0}^\infty \left( x_k^\top Q x_k + u_k^\top R u_k \right)

Choose the weighting matrices:

Q=[q100q2],R=rQ = \begin{bmatrix} q_1 & 0 \\ 0 & q_2 \end{bmatrix}, \quad R = r
  • Q⪰0 Q \succeq 0 : penalizes position and velocity

  • R≻0 R \succ 0 : penalizes control effort

Graphical Model Representation of Finite-Horizon LQR

We formulate the finite-horizon Linear Quadratic Regulator (LQR) problem as a factor graph, allowing us to interpret optimal control as a maximum a posteriori (MAP) estimation problem.


Problem Statement

Given discrete-time linear dynamics:

xk+1=Axk+Bukx_{k+1} = A x_k + B u_k

and a quadratic cost function:

J(x,u)=∑k=0N−1(xk⊤Qxk+uk⊤Ruk)+xN⊤QxNJ(x, u) = \sum_{k=0}^{N-1} \left( x_k^\top Q x_k + u_k^\top R u_k \right) + x_N^\top Q x_N

we aim to find the state-control sequence {xk,uk}k=0N−1 \{x_k, u_k\}_{k=0}^{N-1} minimizing J J , subject to initial state x0 x_0 and dynamics.


MAP Estimation View

This control problem is equivalent to a MAP estimation over a factor graph:

arg⁡max⁡x0:N,u0:N−1  p(x0:N,u0:N−1∣costs, dynamics)\arg\max_{x_{0:N}, u_{0:N-1}} \; p(x_{0:N}, u_{0:N-1} \mid \text{costs, dynamics})

This posterior can be written as a product of factors:

p(x,u)∝ϕ0(x0)∏k=0N−1ϕQ(xk)ϕR(uk)ϕf(xk,uk,xk+1)⋅ϕQ(xN)p(x, u) \propto \phi_0(x_0) \prod_{k=0}^{N-1} \phi_Q(x_k) \phi_R(u_k) \phi_f(x_k, u_k, x_{k+1}) \cdot \phi_Q(x_N)

Prior Factor (Initial Condition)

We encode the prior on the initial state as a Gaussian:

ϕ0(x0)∝exp⁡(−12∥x0−xinit∥Σ02)\phi_0(x_0) \propto \exp\left( -\frac{1}{2} \| x_0 - x_{\text{init}} \|^2_{\Sigma_0} \right)

where Σ0≈0⋅I \Sigma_0 \approx 0 \cdot I enforces the initial condition tightly.


Quadratic Cost Factors

Each quadratic cost xk⊤Qxk x_k^\top Q x_k is rewritten via Cholesky decomposition Q=Lx⊤Lx Q = L_x^\top L_x as:

xk⊤Qxk=∥Lxxk∥2⇒ϕQ(xk)∝exp⁡(−12∥Lxxk∥2)x_k^\top Q x_k = \| L_x x_k \|^2 \quad \Rightarrow \quad \phi_Q(x_k) \propto \exp\left( -\frac{1}{2} \| L_x x_k \|^2 \right)

Similarly, for control:

ϕR(uk)∝exp⁡(−12∥Luuk∥2),R=Lu⊤Lu\phi_R(u_k) \propto \exp\left( -\frac{1}{2} \| L_u u_k \|^2 \right), \quad R = L_u^\top L_u

These are soft constraints, penalizing deviation from zero.


Dynamics Constraints (Hard Constraints)

Each dynamics equation:

xk+1=Axk+Bukx_{k+1} = A x_k + B u_k

is encoded as a hard constraint, modeled as a Gaussian with zero variance:

ϕf(xk,uk,xk+1)∝δ(xk+1−Axk−Buk)=lim⁡Σ→0exp⁡(−12∥xk+1−Axk−Buk∥Σ2)\phi_f(x_k, u_k, x_{k+1}) \propto \delta(x_{k+1} - A x_k - B u_k) = \lim_{\Sigma \to 0} \exp\left( -\frac{1}{2} \| x_{k+1} - A x_k - B u_k \|^2_{\Sigma} \right)

This enforces the equality exactly during optimization.


Terminal Cost

The terminal cost is also encoded as a soft quadratic factor:

ϕQ(xN)∝exp⁡(−12∥LxxN∥2)\phi_Q(x_N) \propto \exp\left( -\frac{1}{2} \| L_x x_N \|^2 \right)
array([[0. , 1. ], [0.0968027 , 0.36053946], [0.13167395, 0.12400146], [0.14365837, 0.04085496]])
<Figure size 1000x400 with 1 Axes>

Visualizing the Factor Graph and Variable Elimination (Bayes Net)

Initial Factor Graph

The first plot generated by graphviz.Source(graph.dot()) visualizes the factor graph representation of the LQR problem.

  • Ellipses represent variable nodes: states xk x_k and controls uk u_k .

  • Black dots are factor nodes connecting variables — each represents either:

    • A cost factor (state or control penalty),

    • A dynamics constraint,

    • Or a prior (for x0 x_0 ).

The structure reflects the temporal nature of the problem:

  • Each state xk x_k is connected to its predecessor and successor via dynamics,

  • Each control uk u_k connects two consecutive states,

  • Cost factors attach individually to each xk x_k and uk u_k .

Loading...

From Factor Graph to Bayes Net via Variable Elimination

After constructing a Gaussian factor graph to represent the finite-horizon LQR problem, we can use GTSAM’s eliminateSequential(ordering) to transform the factor graph into a Bayes Net — a directed acyclic graphical model representing the posterior distribution over all variables in a factored form.

The following plots will visualize the Bayes Net obtained using the elimination order:

Ordering=[x0,u0,x1,u1,x2,u2,x3]\text{Ordering} = [x_0, u_0, x_1, u_1, x_2, u_2, x_3]

Structure of the Bayes Net

Each node in the plot represents a variable, and each arrow represents a dependency in a conditional Gaussian distribution arising from the variable elimination process. For example:

  • x2←{x3,u2} x_2 \leftarrow \{x_3, u_2\}
    ⇒ p(x2∣x3,u2) p(x_2 \mid x_3, u_2)

  • u1←{x2} u_1 \leftarrow \{x_2\}
    ⇒ p(u1∣x2) p(u_1 \mid x_2)

This structure means that once we solve for the “latest” variable (e.g., x3 x_3 ), we can back-substitute through the DAG to recover the entire trajectory.


What Does Elimination Do?

In mathematical terms, elimination rewrites the joint Gaussian density:

p(x0,u0,x1,u1,…,xN)∝∏iϕi(xi,ui) p(x_0, u_0, x_1, u_1, \dots, x_N) \propto \prod_i \phi_i(x_i, u_i)

as a sequence of conditionals:

p(x0,u0,x1,… )=p(x3)⋅p(x2∣x3,u2)⋅⋯⋅p(x0) p(x_0, u_0, x_1, \dots) = p(x_3) \cdot p(x_2 \mid x_3, u_2) \cdot \dots \cdot p(x_0)

Each conditional is Gaussian:

xk=Ak⋅Parents(xk)+ηk,ηk∼N(0,Σk) x_k = A_k \cdot \text{Parents}(x_k) + \eta_k, \quad \eta_k \sim \mathcal{N}(0, \Sigma_k)

This allows us to represent the problem as a triangular system, suitable for efficient solving.


Effect of Elimination Ordering

Will the result (optimal trajectory) change if we change the elimination ordering?

No — the MAP solution is invariant under ordering for linear-Gaussian systems, because the solution to:

min⁡x∥Ax−b∥2\min_x \|Ax - b\|^2

is unique and does not depend on the factorization path.

What changes?

  1. Structure of the Bayes Net:
    The set of conditionals and their dependencies change. E.g., if we eliminate all controls first, each uk u_k may depend on more than just xk x_k , introducing wider conditionals.

  2. Computational efficiency:
    Different orderings lead to different fill-in patterns during matrix factorization (e.g., Cholesky). A poor ordering increases memory and time.

  3. Sparsity pattern:
    An optimized ordering (e.g., using COLAMD or METIS) maintains sparse Jacobians and minimal cross-dependencies.

  4. Numerical stability (nonlinear problems):
    Poor ordering may lead to ill-conditioned systems or less stable Gauss-Newton updates.


Analogy to QR/Cholesky

The process is equivalent to structured Gaussian elimination or QR factorization of the system Ax=b Ax = b , where variable elimination corresponds to pivoting rows and columns of the matrix in a specific order.

Ordering: Position 0: x3, u2, x2, u1, x1, u0, x0

Loading...
<Figure size 640x480 with 1 Axes>
New Ordering: Position 0: x3, x2, x1, x0, u2, u1, u0

Loading...

Solving the LQR Problem via the Discrete-Time Riccati Equation

This code solves a finite-horizon Linear Quadratic Regulator (LQR) problem using the classical backward Riccati recursion followed by a forward rollout of optimal control inputs.


Backward Riccati Recursion

We compute the optimal feedback gain KtK_t by solving the discrete Riccati equation backward from the terminal cost:

St=R+B⊤Pt+1BS_t = R + B^\top P_{t+1} B
Kt=St−1B⊤Pt+1AK_t = S_t^{-1} B^\top P_{t+1} A
Pt=Q+A⊤Pt+1A−A⊤Pt+1BKtP_t = Q + A^\top P_{t+1} A - A^\top P_{t+1} B K_t

The recursion builds the list of gains K0,K1,K2K_0, K_1, K_2, used to compute the optimal control law ut=−Ktxtu_t = -K_t x_t.


Forward Rollout of the Trajectory

Given the initial state x0x_0 and feedback gains:

  • Compute each optimal control input:
    ut=−Ktxtu_t = -K_t x_t

  • Apply dynamics to get the next state:
    xt+1=Axt+Butx_{t+1} = A x_t + B u_t

This yields the full trajectory of states and controls.

<Figure size 1000x400 with 1 Axes>
States:
 [[0.         1.        ]
 [0.09685631 0.37126203]
 [0.13283732 0.14222434]
 [0.14670236 0.07074541]]
Controls:
 [[-0.62873797]
 [-0.22903769]
 [-0.07147893]]