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, 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
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:
Since the optimum is at a vertex, we can find it by checking every intersection of two constraint lines:
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
with 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 subject to " (Separating Hyperplanes).
A simple QP solver for box constraints is projected gradient descent: take a gradient step, then clip back into the box.
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: would still decrease if or 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.linprogsolves LPs (HiGHS);cvxpyand OSQP solve QPs.- Lasso can be solved as a QP; quantile and regression as LPs.
- Portfolio optimization (Markowitz) is a QP with a covariance matrix as .
Exercises
Exercise 1: Vertex optimum
Maximize subject to , , .
Answer
5, at .
Solution
The feasible set is a rectangle with vertices . The objective at these is .
Exercise 2: Recognize the form
Is "minimize subject to " (non-negative least squares) an LP, a QP or neither?
Answer
A QP.
Solution
Expanding gives , a convex quadratic (), and is linear.
Exercise 3: Projected gradient (code)
Minimize over the box with projected gradient descent.
Answer
.
Solution
The objective is separable, so each coordinate is just clipped: and .
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…
- only in the interior
- at a vertex of the feasible polyhedron (among others possibly)
- at the origin
- nowhere specific
The hard-margin SVM training problem is a…
- linear program
- quadratic program
- non-convex problem
- eigenvalue problem
Projected gradient descent handles box constraints by…
- ignoring them
- clipping each iterate back into the box
- adding a penalty
- changing the objective to linear