Linear programs#
A linear program (LP) minimises a linear function over a polyhedron defined by linear inequalities:
where , , . The feasible set is a convex polyhedron — the intersection of half-spaces.
Standard forms. There are several equivalent representations. Any LP can be converted between them:
- Inequality form: (as above).
- Equality form: s.t. , . Introduce slack variables .
- Symmetric cone form: Both inequalities and nonnegativity on subsets of variables.
Why LPs are easy. The objective is linear, the constraints are linear — the optimum (if it exists) lies at a vertex (extreme point) of the polyhedron. The simplex method crawls along edges from vertex to vertex; interior-point methods (Week 6) cut through the interior. Both are highly reliable for problems with thousands of variables and constraints.
Worked example — Diet problem:
Rewrite in inequality form (): , , , .
Solving: vertices are intersections of binding constraints. The vertex from binding the two nutrient constraints gives cost .
Quadratic programs#
A quadratic program (QP) adds a quadratic term in the objective:
with symmetric. The classification depends on :
- (PSD): convex QP — the objective is convex, the feasible set is a polyhedron. Any local minimum is global; KKT is necessary and sufficient.
- : strictly convex QP — unique solution; KKT system reduces to solving a linear complementarity problem.
- indefinite: nonconvex QP — NP-hard in general. Multiple local minima; standard solvers only guarantee a local KKT point.
Equality-constrained QP. When (only equalities), the KKT conditions form a linear system:
This is the KKT matrix or saddle-point system — it appears everywhere from Newton's method (Week 8) to IPM steps (Week 6).
Worked example — Portfolio optimisation:
where is the covariance matrix of asset returns (), is the weight vector. This is a convex QP: minimise risk subject to budget and no-shorting constraints.
From QP to geometry#
The QP objective's level sets are ellipsoids (when ) centred at the unconstrained minimiser . The constraints carve a polyhedron from . The optimum is the point in the polyhedron that lies on the smallest ellipsoid — a purely geometric problem of finding the closest point (in the -norm) to the unconstrained centre, subject to linear inequalities.
When and , the unconstrained minimiser is the origin, and the QP reduces to projecting the origin onto the polyhedron — equivalently, finding the minimum-norm point in the set. This interpretation is powerful: many constrained problems are projections in disguise, and projected gradient descent (intermediate Optim Week 9) is a primal first-order method for them.
Contrast with unconstrained GD. An unconstrained convex QP with is solved in one Newton step (Week 8). A constrained QP requires iterative methods — either active-set, IPM, or first-order projected methods. The constraints are what make the problem interesting.
Second-order cones#
Each class strictly contains the previous one. Moving outward buys modelling power at the cost of a harder problem — which is exactly the trade a practitioner weighs when choosing between an LP/QP solver and an SDP relaxation.
A second-order cone program (SOCP) generalises LP and convex QP by allowing constraints of the form:
where , , , . The set is the second-order cone (also called the Lorentz cone or ice-cream cone). SOCP constraints express Euclidean norm bounds.
Why SOCP is powerful. Many problems that look nonlinear become SOCP-representable:
- Robust least-squares: with bounded uncertainty in can often be cast as an SOCP.
- Huber loss: The Huber penalty can be represented as a projection onto a second-order cone.
- Quadratically constrained QP (QCQP): s.t. with is SOCP-representable (each constraint is ).
Worked example — Robust constraint:
Suppose we want subject to , where is uncertain: with . The robust version requires for all admissible . The worst case is . The robust constraint becomes:
which is an SOCP constraint: .
Modeling checklist#
When facing a structured optimisation problem, ask:
- Is the objective quadratic? If yes and , you have a convex QP. If constraints are linear, IPM solvers are fast and reliable.
- Do constraints involve Euclidean norms? Check if the problem can be written as an SOCP — many robust formulations, quadratically constrained problems, and regularised models fit.
- Is the problem an LP? If the objective and constraints are all linear, simplex or IPM solve it exactly. Do not use gradient descent on an LP.
- Is the structure richer? If you see matrix variables with PSD constraints, think SDP (Week 5). If the constraints are generic nonlinear, you are in the domain of general nonlinear programming — first-order methods or Newton-based approaches from Weeks 8–10 apply.
The skill this course teaches is recognising when a problem has structure that specialised solvers can exploit, versus when you must fall back to general-purpose gradient-based methods.
Knowledge Check#
Which Q makes min ½xᵀQx + cᵀx a convex QP?
Which of these is an LP?
Browser lab#
Project the origin onto the polytope using projected gradient descent. This is a poor-man's QP: subject to linear inequalities. Compare the PG iterate to the analytic solution.
import numpy as np
import matplotlib.pyplot as plt
# Problem: min ||x||^2 s.t. x + y >= 2, x >= 0, y >= 0
# Analytic solution: (1, 1) — projection of origin onto x + y = 2 in first quadrant
def project_onto_simplex(xy):
"""Project onto constraints: x >= 0, y >= 0, x + y >= 2"""
x, y = xy
# Project onto individual nonnegativity
x = max(x, 0)
y = max(y, 0)
# If x + y < 2, project onto the line x + y = 2 in positive orthant
if x + y < 2:
# Project (x, y) onto hyperplane x + y = 2, then clip again
deficit = 2 - (x + y)
x = x + deficit / 2
y = y + deficit / 2
x = max(x, 0)
y = max(y, 0)
# Re-check — may need one more pass for corner cases
if x + y < 2 - 1e-8:
if x <= 0:
y = 2.0
else:
x = 2.0
return np.array([x, y])
# Projected gradient descent
eta = 0.5
n_steps = 30
xy = np.array([-2.0, 3.0]) # infeasible start
path_pg = [xy.copy()]
for _ in range(n_steps):
grad = 2 * xy # gradient of ||x||^2
xy = xy - eta * grad
xy = project_onto_simplex(xy)
path_pg.append(xy.copy())
path_pg = np.array(path_pg)
print(f"Final PG iterate: ({path_pg[-1,0]:.4f}, {path_pg[-1,1]:.4f})")
print(f"Analytic solution: (1.0, 1.0)")
print(f"Distance to optimum: {np.linalg.norm(path_pg[-1] - np.array([1, 1])):.6f}")
# Visualise
xs = np.linspace(-0.5, 3.5, 200)
ys = np.linspace(-0.5, 3.5, 200)
X, Y = np.meshgrid(xs, ys)
Z = X**2 + Y**2
fig, ax = plt.subplots(figsize=(6, 5.5))
ax.contour(X, Y, Z, levels=20, cmap="viridis", alpha=0.6)
# Feasible region
poly_x = [0, 2, 3, 0]
poly_y = [2, 0, 0, 3]
ax.fill(poly_x, poly_y, alpha=0.15, color="green", label="feasible region")
ax.plot(path_pg[:, 0], path_pg[:, 1], "o-", color="tab:red", markersize=4, label="PG iterates")
ax.plot(1, 1, "x", color="black", markersize=12, label="optimum (1,1)")
ax.plot(0, 0, "s", color="gray", markersize=8, label="unconstrained min (0,0)")
ax.set_xlabel("x")
ax.set_ylabel("y")
ax.set_title("Projected GD: project origin onto polytope")
ax.legend()
ax.set_aspect("equal")
plt.show()
print("Notice: projected GD lands on (1.0000, 1.0000) — the projection of the origin onto x + y ≥ 2 — with distance 0.000000.")
Key takeaways#
- The convex program hierarchy is LP inside QP inside SOCP inside SDP. Moving outward buys modelling power at the cost of a harder solve.
- An LP has a linear objective and polyhedral constraints, and its optimum (when one exists) sits at a vertex; simplex and interior-point methods both solve it reliably.
- A QP is convex exactly when . If is indefinite the problem is NP-hard and a solver returns only a local KKT point.
- Equality-constrained QPs reduce to the saddle-point KKT system in and the equality multipliers.
- SOCP constraints are Euclidean norm bounds , and many robust and quadratically constrained problems become SOCP-representable.
- Recognising structure is the skill: match the problem to LP, QP, SOCP, or SDP before reaching for a generic gradient method.
Further reading#
- Stephen Boyd & Lieven Vandenberghe, Convex Optimization (Cambridge University Press, 2004) — LP, QP, and the second-order cone in one place, with modelling examples.
- Dimitri P. Bertsekas, Convex Optimization Algorithms (Athena Scientific, 2015) — how LP and QP solvers actually work.
- Jorge Nocedal & Stephen Wright, Numerical Optimization (2nd ed., Springer, 2006) — quadratic programming in depth.
Reading is free. PDF and ePub unlock with the newsletter. Join once and every lesson opens in this browser.