Optimisation and games

Linear programming

Maximise or minimise a linear function subject to linear inequalities. The simplex method here works in exact fractions, and every answer arrives with the evidence that proves it: an optimum with its dual prices, an impossibility with a Farkas combination, an unbounded problem with a ray.

A first problem

// A workshop makes chairs (£30 profit) and tables (£50) from wood and labour.
import { linearProgram } from "@ratmath/discrete";

const plan = linearProgram({
  maximize: { chairs: 30, tables: 50 },
  constraints: [
    [{ chairs: 2, tables: 4 }, "<=", 100, "wood"],
    [{ chairs: 3, tables: 2 }, "<=", 90, "labour"],
  ],
});

String(plan);     // "optimal: 1350 at chairs = 20, tables = 15"
plan.prices;      // { wood: 45/4, labour: 5/2 }: what one more unit of each is worth
plan.binding;     // ["wood", "labour"]: both are used up
plan.check();     // true: the prices prove that 1350 is the best possible

A linear program asks for xx maximising c⋅xc\cdot x subject to constraints ai⋅x≤bia_i\cdot x \le b_i (or ≥\ge, or ==) and, unless a variable is declared free, x≥0x \ge 0. The feasible set is a convex polyhedron, and if the optimum exists it is reached at a vertex. The simplex method walks from vertex to vertex along edges, improving the objective at each step. Here each step is an exact pivot on a tableau of fractions, and Bland's rule (always the lowest-numbered improving column) guarantees the walk never cycles.

Why the prices are a proof

Multiply each constraint by a non-negative price yiy_i and add. If the combined left side is at least the objective, coefficient by coefficient, then for every feasible xx

c⋅x  ≤  ∑iyi(ai⋅x)  ≤  ∑iyibi=b⋅y. c\cdot x \;\le\; \sum_i y_i (a_i\cdot x) \;\le\; \sum_i y_i b_i = b\cdot y.

That is weak duality: any such yy gives an upper bound. For the workshop, y=(45/4,5/2)y = (45/4, 5/2) gives 2⋅454+3⋅52=302\cdot\tfrac{45}{4} + 3\cdot\tfrac52 = 30 and 4⋅454+2⋅52=504\cdot\tfrac{45}{4} + 2\cdot\tfrac52 = 50, exactly the profits, and a bound of 100⋅454+90⋅52=1350100\cdot\tfrac{45}{4} + 90\cdot\tfrac52 = 1350. The plan reaches 1350, so nothing does better. Strong duality says such a matching pair always exists when there is an optimum, and the simplex method finds both at once.

check() uses none of the simplex code. It confirms that xx satisfies every constraint, that the prices have the right signs, that they cover the objective, and that c⋅x=b⋅yc\cdot x = b\cdot y. The prices are also sensitivities: one more unit of wood raises the profit by exactly 45/445/4, as long as the same constraints stay binding.

Minimising, with fractions

const diet = linearProgram({
  minimize: { bread: "1/2", milk: "3/4", beans: "6/5" },     // cost per portion
  constraints: [
    [{ bread: 4, milk: 8, beans: 7 }, ">=", 30, "protein"],
    [{ bread: 10, milk: 3, beans: 20 }, ">=", 60, "iron"],
    [{ bread: 300, milk: 150, beans: 200 }, ">=", 2000, "energy"],
    [{ milk: 1 }, "<=", 4, "milk limit"],
  ],
});
String(diet);   // "optimal: 65/18 at bread = 115/18, milk = 5/9, beans = 0"
diet.prices;    // { protein: 1/12, iron: 0, energy: 1/1800, "milk limit": 0 }
String(diet.problem);   // the problem written out, as the solver read it

Coefficients can be fractions ("6/5"), decimals ("0.25" or 0.25) or whole numbers. Variables can be named through objects, as here, or listed by position: maximize: [3, 2] with rows such as [[1, 1], "<=", 4].

When there is no answer

const none = linearProgram({ maximize: { x: 1, y: 1 }, constraints: [
  [{ x: 1, y: 2 }, "<=", 4, "a"], [{ x: 3, y: 1 }, "<=", 6, "b"], [{ x: 1, y: 1 }, ">=", 3, "c"] ] });
none.status;    // "infeasible"
none.farkas;    // [2/5, 1/5, −1]: (2/5)·a + (1/5)·b says x + y ≤ 14/5, but c says x + y ≥ 3

const open = linearProgram({ maximize: { x: 1, y: 1 }, constraints: [
  [{ x: 1, y: -1 }, "<=", 2], [{ x: -1, y: 1 }, "<=", 4] ] });
open.status;    // "unbounded"
open.x;         // [2, 0]: a feasible point
open.ray;       // [1, 1]: from it, move in this direction for ever

An infeasible problem comes with a Farkas certificate: multipliers (non-negative on ≤\le rows, non-positive on ≥\ge rows) whose combination of the constraints has left side ≥0\ge 0 on every x≥0x \ge 0 but a negative right side. No point can satisfy that, so none can satisfy the constraints. Farkas' lemma says such multipliers exist whenever a system has no solution; phase one of the simplex method finds them. An unbounded problem comes with a feasible point and a ray dd with ai⋅da_i\cdot d of the right sign for every constraint and c⋅d>0c\cdot d > 0. Both are checked by check() in the same way as an optimum.

The transportation problem

const t = transportation([20, 30], [10, 25, 15], [[8, 6, 10], [9, 12, 13]]);
t.cost;   // 465
t.plan;   // [[0, 20, 0], [10, 5, 15]]: warehouse 1 sends 20 to store 2, …
t.lp.check();   // true: the prices prove no plan is cheaper

Sources with supplies sis_i ship to sinks with demands djd_j at cost cijc_{ij} per unit. With xijx_{ij} the amount from ii to jj, minimise ∑cijxij\sum c_{ij}x_{ij} subject to ∑jxij=si\sum_j x_{ij} = s_i and ∑ixij=dj\sum_i x_{ij} = d_j. transportation(supply, demand, cost) builds that program, solves it exactly and returns the shipping table. If total supply exceeds total demand, a dummy sink with cost 0 takes the rest (leftover says how much stays at each source); if demand exceeds supply, a dummy source records the unmet demand. The duals of the source and sink rows are the potentials uiu_i, vjv_j of the stepping-stone method: every route has cij−ui−vj≥0c_{ij} - u_i - v_j \ge 0, which is the optimality certificate.

Try itEdit the code and run it with @ratmath/discrete and Core's Rational in scope. The last line's value is shown; PRINT(…) adds lines, TEX(…) typesets math.
Ctrl/⌘ + Enter runs