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 maximising subject to constraints (or , or ) and, unless a variable is declared free, . 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 and add. If the combined left side is at least the objective, coefficient by coefficient, then for every feasible
That is weak duality: any such gives an upper bound. For the workshop, gives and , exactly the profits, and a bound of . 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 satisfies every constraint, that the prices have the right signs, that they cover the objective, and that . The prices are also sensitivities: one more unit of wood raises the profit by exactly , 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 rows, non-positive on rows) whose combination of the constraints has left side on every 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 with of the right sign for every constraint and . 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 ship to sinks with demands at cost per unit. With the amount from to , minimise subject to and . 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 , of the stepping-stone method: every route has , which is the optimality certificate.
@ratmath/discrete and Core's Rational in scope. The last line's value is shown; PRINT(…) adds lines, TEX(…) typesets math.