The Simplex Method
George Dantzig's simplex method (1947) solves an LP by walking from vertex to vertex of the feasible polyhedron, each step improving the objective, until no neighbouring vertex is better. Despite an exponential worst case, it is extremely fast in practice and is still the core of commercial LP and MIP solvers.
One iteration in matrix form
Work with the standard form , , . Split the variables into basic (, of them) and non-basic (). At the current vertex , .
- Prices: solve (these are the dual variables).
- Reduced costs: for non-basic . It is the rate at which the cost changes if increases from 0. If all , the vertex is optimal.
- Entering variable: pick with (Dantzig's rule: the most negative).
- Direction: solve . Increasing by changes by . If , the LP is unbounded.
- Ratio test: ; the minimizing basic variable leaves the basis.
- Swap them and repeat.
import numpy as np
def simplex(c, A, b, basis, verbose=True):
m, n = A.shape
for it in range(50):
AB = A[:, basis]
xB = np.linalg.solve(AB, b)
y = np.linalg.solve(AB.T, c[basis])
nonbasic = [j for j in range(n) if j not in basis]
reduced = {j: c[j] - A[:, j] @ y for j in nonbasic}
j = min(reduced, key=reduced.get)
if verbose:
x = np.zeros(n); x[basis] = xB
print(f"iter {it}: basis {basis}, x = {np.round(x, 3) + 0}, cost = {c @ x:.3f}")
if reduced[j] >= -1e-12:
return basis, xB
d = np.linalg.solve(AB, A[:, j])
if np.all(d <= 1e-12):
raise ValueError("unbounded")
ratios = [xB[i] / d[i] if d[i] > 1e-12 else np.inf for i in range(m)]
basis[int(np.argmin(ratios))] = j
# max 3a + 5b s.t. a + 2b <= 8, 2a + b <= 10 -> standard form with slacks s1, s2
c = np.array([-3.0, -5.0, 0.0, 0.0])
A = np.array([[1.0, 2.0, 1.0, 0.0], [2.0, 1.0, 0.0, 1.0]]); b = np.array([8.0, 10.0])
simplex(c, A, b, basis=[2, 3]) # start at the slack basis: a = b = 0iter 0: basis [2, 3], x = [ 0. 0. 8. 10.], cost = 0.000
iter 1: basis [1, 3], x = [0. 4. 0. 6.], cost = -20.000
iter 2: basis [1, 0], x = [4. 2. 0. 0.], cost = -22.000
Two pivots: from to (variable enters, it has the more negative reduced cost) to the optimum .
Finding a first vertex: phase I
The slack basis is only feasible when and all constraints are . Otherwise, phase I solves an auxiliary LP with artificial variables : minimize s.t. (with rows sign-flipped so ). Its starting basis is . If the optimum has , you have a feasible vertex of the original LP; if not, the LP is infeasible.
import numpy as np
def simplex(c, A, b, basis):
m, n = A.shape
while True:
AB = A[:, basis]; xB = np.linalg.solve(AB, b); y = np.linalg.solve(AB.T, c[basis])
red = {j: c[j] - A[:, j] @ y for j in range(n) if j not in basis}
j = min(red, key=red.get)
if red[j] >= -1e-12:
return basis, xB
d = np.linalg.solve(AB, A[:, j])
basis[int(np.argmin([xB[i] / d[i] if d[i] > 1e-12 else np.inf for i in range(m)]))] = j
# constraints a + b = 4 and a - b >= 1 (surplus s): no obvious starting vertex
A = np.array([[1.0, 1.0, 0.0], [1.0, -1.0, -1.0]]); b = np.array([4.0, 1.0])
A1 = np.hstack([A, np.eye(2)]); c1 = np.array([0, 0, 0, 1.0, 1.0]) # phase I with artificials
basis, xB = simplex(c1, A1, b, [3, 4])
print("phase I basis:", basis, " artificial sum:", round(float(c1[basis] @ xB), 10))
basis, xB = simplex(np.array([2.0, 3.0, 0.0]), A, b, basis) # phase II: min 2a + 3b
x = np.zeros(3); x[basis] = xB
print("optimum (a, b, s):", np.round(x, 4), " cost:", 2 * x[0] + 3 * x[1])phase I basis: [1, 0] artificial sum: 0.0
optimum (a, b, s): [4. 0. 3.] cost: 8.0
Practical simplex
| Issue | What real solvers do |
|---|---|
| solving with each iteration | keep an LU factorization and update it after each pivot |
| choosing the entering variable | steepest-edge or devex pricing instead of Dantzig's rule |
| degeneracy and cycling | Bland's rule, perturbation of |
| numerical error | tolerances, refactorization, scaling of |
| re-solving after a change | warm start from the old basis; the dual simplex after adding constraints |
The dual simplex method (pivoting on a dual-feasible but primal-infeasible basis) is the default in branch and bound because adding a cut keeps the dual feasible.
In software
- HiGHS (SciPy's
linprog(method="highs-ds")for dual simplex,"highs-ipm"for interior point), Gurobi, CPLEX and CLP all ship primal and dual simplex. - The simplex tableau (all of written out) is a teaching device; production codes never form it.
Exercises
Exercise 1: Reduced costs at the start
At the slack basis of the example (), what are the reduced costs of and , and which enters?
Answer
and ; enters.
Solution
With , , so . Dantzig's rule picks the most negative, .
Exercise 2: Ratio test
Entering at the slack basis: and . Which variable leaves, and how far does move?
Answer
leaves; .
Solution
The ratios are and ; the minimum, 4, belongs to , so the first constraint becomes tight.
Exercise 3: A third product (code)
Add a third product with profit 4 that uses 1 unit of each resource (column ). Run simplex from the slack basis. Is the new product used?
Answer
Yes: the best plan now makes only the new product, 8 units, for a profit of 32 (up from 22).
Solution
import numpy as np
from scipy.optimize import linprog
r = linprog([-3, -5, -4], A_ub=[[1, 2, 1], [2, 1, 1]], b_ub=[8, 10], bounds=[(0, None)] * 3, method="highs")
print(np.round(r.x, 4) + 0, -r.fun)[0. 0. 8.] 32.0
At the old optimum the shadow prices of the two resources are and , so the new column's reduced cost is : it enters the basis, and further pivots push the old products out.
Test yourself
The simplex method stops at an optimal vertex when…
- all reduced costs are non-negative (for minimization)
- the ratio test fails
- the basis has variables
- the cost is zero
The ratio test chooses…
- the entering variable
- the leaving variable, the first basic variable to hit zero
- the step direction
- the objective
Phase I of the simplex method…
- finds a feasible starting vertex or proves infeasibility
- computes the dual
- rounds the solution
- removes redundant constraints