TExcelTAD
Home › Optimization › The Simplex Method

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 min⁡cTx\min \mathbf{c}^T\mathbf{x}, Ax=bA\mathbf{x} = \mathbf{b}, x≥0\mathbf{x} \ge 0. Split the variables into basic (BB, mm of them) and non-basic (NN). At the current vertex xB=AB−1b≥0\mathbf{x}_B = A_B^{-1}\mathbf{b} \ge 0, xN=0\mathbf{x}_N = 0.

  1. Prices: solve ABTy=cBA_B^T\mathbf{y} = \mathbf{c}_B (these y\mathbf{y} are the dual variables).
  2. Reduced costs: cˉj=cj−ajTy\bar{c}_j = c_j - \mathbf{a}_j^T\mathbf{y} for non-basic jj. It is the rate at which the cost changes if xjx_j increases from 0. If all cˉj≥0\bar{c}_j \ge 0, the vertex is optimal.
  3. Entering variable: pick jj with cˉj<0\bar{c}_j < 0 (Dantzig's rule: the most negative).
  4. Direction: solve ABd=ajA_B\mathbf{d} = \mathbf{a}_j. Increasing xjx_j by tt changes xB\mathbf{x}_B by −td-t\mathbf{d}. If d≤0\mathbf{d} \le 0, the LP is unbounded.
  5. Ratio test: t=min⁡i:di>0xBi/dit = \min_{i: d_i > 0} x_{B_i}/d_i; the minimizing basic variable leaves the basis.
  6. Swap them and repeat.
python · try it yourself
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 = 0
iter 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 (0,0)(0, 0) to (0,4)(0, 4) (variable bb enters, it has the more negative reduced cost) to the optimum (4,2)(4, 2).

Finding a first vertex: phase I

The slack basis is only feasible when b≥0\mathbf{b} \ge 0 and all constraints are ≤\le. Otherwise, phase I solves an auxiliary LP with artificial variables z≥0\mathbf{z} \ge 0: minimize 1Tz\mathbf{1}^T\mathbf{z} s.t. Ax+z=bA\mathbf{x} + \mathbf{z} = \mathbf{b} (with rows sign-flipped so b≥0\mathbf{b} \ge 0). Its starting basis is z=b\mathbf{z} = \mathbf{b}. If the optimum has z=0\mathbf{z} = 0, you have a feasible vertex of the original LP; if not, the LP is infeasible.

python · try it yourself
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 ABA_B 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 b\mathbf{b}
numerical error tolerances, refactorization, scaling of AA
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 AB−1AA_B^{-1}A 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 (y=0\mathbf{y} = 0), what are the reduced costs of aa and bb, and which enters?

Answer

−3-3 and −5-5; bb enters.

Solution

With cB=0\mathbf{c}_B = 0, y=0\mathbf{y} = 0, so cˉj=cj\bar{c}_j = c_j. Dantzig's rule picks the most negative, −5-5.

Exercise 2: Ratio test

Entering bb at the slack basis: d=(2,1)\mathbf{d} = (2, 1) and xB=(8,10)\mathbf{x}_B = (8, 10). Which variable leaves, and how far does bb move?

Answer

s1s_1 leaves; b=4b = 4.

Solution

The ratios are 8/2=48/2 = 4 and 10/1=1010/1 = 10; the minimum, 4, belongs to s1s_1, 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 (1,1)(1, 1)). 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
python · try it yourself
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 73\frac{7}{3} and 13\frac{1}{3}, so the new column's reduced cost is −4+73+13=−43<0-4 + \frac{7}{3} + \frac{1}{3} = -\frac{4}{3} < 0: it enters the basis, and further pivots push the old products out.

Test yourself

The simplex method stops at an optimal vertex when…

  1. all reduced costs are non-negative (for minimization)
  2. the ratio test fails
  3. the basis has nn variables
  4. the cost is zero

The ratio test chooses…

  1. the entering variable
  2. the leaving variable, the first basic variable to hit zero
  3. the step direction
  4. the objective

Phase I of the simplex method…

  1. finds a feasible starting vertex or proves infeasibility
  2. computes the dual
  3. rounds the solution
  4. removes redundant constraints