TExcelTAD
Home › Math for Machine Learning › Linear and Quadratic Programs

Linear and Quadratic Programs

Two families of constrained convex problems have special, highly optimized solvers. A linear program (LP) has a linear objective and linear constraints; a quadratic program (QP) has a convex quadratic objective and linear constraints. The SVM is a QP, ℓ1\ell_1 regression can be written as an LP, and recognizing these forms lets you hand a problem to a solver instead of writing one.

Linear programs

min⁡x∈RdcTxsubject toAx≤b\min_{\mathbf{x} \in \mathbb{R}^d} \mathbf{c}^T\mathbf{x} \quad \text{subject to} \quad A\mathbf{x} \le \mathbf{b}

The feasible set is a convex polyhedron, and a linear objective is minimized at a vertex of it (if the minimum exists). The simplex method walks from vertex to vertex; interior-point methods cut through the middle.

Example. A bakery makes bread (profit 3 per batch) and cakes (profit 5). Bread needs 1 hour of oven time and cakes 2 hours, with 8 hours available; bread needs 3 kg of flour and cakes 1 kg, with 12 kg available. Maximize profit:

max⁡ 3x1+5x2s.t.x1+2x2≤8,3x1+x2≤12,x1,x2≥0\max\ 3x_1 + 5x_2 \quad \text{s.t.}\quad x_1 + 2x_2 \le 8,\quad 3x_1 + x_2 \le 12,\quad x_1, x_2 \ge 0

Since the optimum is at a vertex, we can find it by checking every intersection of two constraint lines:

python · try it yourself
import numpy as np
from itertools import combinations

A = np.array([[1.0, 2.0], [3.0, 1.0], [-1.0, 0.0], [0.0, -1.0]])   # A x <= b
b = np.array([8.0, 12.0, 0.0, 0.0])
c = np.array([3.0, 5.0])
best = None
for i, j in combinations(range(4), 2):
    M = A[[i, j]]
    if abs(np.linalg.det(M)) < 1e-12:
        continue
    v = np.linalg.solve(M, b[[i, j]])
    if np.all(A @ v <= b + 1e-9):                    # feasible vertex
        print("vertex", np.round(v, 4), "profit", round(c @ v, 4))
        if best is None or c @ v > c @ best:
            best = v
print("optimum:", np.round(best, 4), "profit", round(c @ best, 4))
vertex [3.2 2.4] profit 21.6
vertex [0. 4.] profit 20.0
vertex [4. 0.] profit 12.0
vertex [0. 0.] profit 0.0
optimum: [3.2 2.4] profit 21.6

Enumerating vertices is exponential in general; real solvers are much smarter, but the vertex property is why LP solutions are often "corner" solutions with many variables at a bound.

Quadratic programs

min⁡x12xTQx+cTxsubject toAx≤b\min_{\mathbf{x}} \frac{1}{2}\mathbf{x}^TQ\mathbf{x} + \mathbf{c}^T\mathbf{x} \quad \text{subject to} \quad A\mathbf{x} \le \mathbf{b}

with QQ positive semidefinite, so the problem is convex. Unlike an LP, the optimum can lie inside the feasible set or on a face, not only at a vertex. The SVM is the QP "minimize 12∥w∥2\frac{1}{2}\lVert \mathbf{w} \rVert^2 subject to yn(wTxn+b)≥1y_n(\mathbf{w}^T\mathbf{x}_n + b) \ge 1" (Separating Hyperplanes).

A simple QP solver for box constraints l≤x≤u\mathbf{l} \le \mathbf{x} \le \mathbf{u} is projected gradient descent: take a gradient step, then clip back into the box.

python · try it yourself
import numpy as np

Q = np.array([[2.0, 0.5], [0.5, 1.0]]); c = np.array([-4.0, -3.0])
lo, hi = np.array([0.0, 0.0]), np.array([1.5, 1.0])
x = np.zeros(2)
for _ in range(500):
    x = np.clip(x - 0.3 * (Q @ x + c), lo, hi)      # gradient step, then project onto the box
unconstrained = np.linalg.solve(Q, -c)
print("unconstrained minimizer:", np.round(unconstrained, 4), "(outside the box)")
print("box-constrained minimizer:", np.round(x, 4))
print("gradient at solution:", np.round(Q @ x + c, 4), "(nonzero only where a bound is active)")
unconstrained minimizer: [1.4286 2.2857] (outside the box)
box-constrained minimizer: [1.5 1. ]
gradient at solution: [-0.5  -1.25] (nonzero only where a bound is active)

Both variables end at their upper bounds, and both gradient components are negative: ff would still decrease if x1x_1 or x2x_2 could grow, but the box stops them. A nonzero gradient component is allowed only where a bound is active, and its sign must point out of the feasible set: exactly the KKT picture from Lagrange Multipliers.

In software

  • scipy.optimize.linprog solves LPs (HiGHS); cvxpy and OSQP solve QPs.
  • Lasso can be solved as a QP; quantile and ℓ1\ell_1 regression as LPs.
  • Portfolio optimization (Markowitz) is a QP with a covariance matrix as QQ.

Exercises

Exercise 1: Vertex optimum

Maximize x+yx + y subject to x≤3x \le 3, y≤2y \le 2, x,y≥0x, y \ge 0.

Answer

5, at (3,2)(3, 2).

Solution

The feasible set is a rectangle with vertices (0,0),(3,0),(0,2),(3,2)(0,0), (3,0), (0,2), (3,2). The objective at these is 0,3,2,50, 3, 2, 5.

Exercise 2: Recognize the form

Is "minimize ∥Ax−b∥2\lVert A\mathbf{x} - \mathbf{b} \rVert^2 subject to x≥0\mathbf{x} \ge \mathbf{0}" (non-negative least squares) an LP, a QP or neither?

Answer

A QP.

Solution

Expanding gives xTATAx−2bTAx+bTb\mathbf{x}^TA^TA\mathbf{x} - 2\mathbf{b}^TA\mathbf{x} + \mathbf{b}^T\mathbf{b}, a convex quadratic (Q=2ATA⪰0Q = 2A^TA \succeq 0), and x≥0\mathbf{x} \ge \mathbf{0} is linear.

Exercise 3: Projected gradient (code)

Minimize (x−2)2+(y+1)2(x - 2)^2 + (y + 1)^2 over the box 0≤x,y≤10 \le x, y \le 1 with projected gradient descent.

Answer

(1,0)(1, 0).

Solution

The objective is separable, so each coordinate is just clipped: 2→12 \to 1 and −1→0-1 \to 0.

python · try it yourself
import numpy as np
x = np.array([0.5, 0.5])
for _ in range(100):
    x = np.clip(x - 0.2 * 2 * (x - np.array([2.0, -1.0])), 0, 1)
print(x)
[1. 0.]

Test yourself

If a linear program has an optimum, it is attained…

  1. only in the interior
  2. at a vertex of the feasible polyhedron (among others possibly)
  3. at the origin
  4. nowhere specific

The hard-margin SVM training problem is a…

  1. linear program
  2. quadratic program
  3. non-convex problem
  4. eigenvalue problem

Projected gradient descent handles box constraints by…

  1. ignoring them
  2. clipping each iterate back into the box
  3. adding a penalty
  4. changing the objective to linear