Matrix games
In a zero-sum game one player's gain is the other's loss. Von Neumann's theorem says each player has a mixed strategy that guarantees the same number, the value of the game. Linear programming finds both strategies and the value, here as exact fractions.
Rock, paper, scissors
import { matrixGame, saddlePoints } from "@ratmath/discrete";
const names = ["rock", "paper", "scissors"];
const rps = matrixGame([[0, -1, 1], [1, 0, -1], [-1, 1, 0]], { rows: names, columns: names });
String(rps); // "value 0; row plays rock 1/3, paper 1/3, scissors 1/3; column plays rock 1/3, …"
rps.fair; // true
rps.check(); // true
The matrix lists what the column player pays the row player: when row meets column . If the row player mixes with probabilities and the column player with , the expected payment is . The row player wants it large and the column player small.
The minimax theorem
A mixture guarantees , the worst it earns against any column. A mixture caps the row player at . Every guarantee is at most every cap, and von Neumann's theorem says the best guarantee equals the best cap:
The row player's problem is a linear program: maximise subject to for every column, , . Its dual is exactly the column player's problem, so one exact solve gives both strategies, and LP duality is the theorem. check() confirms the guarantee of and the cap of are both : neither player can do better, so no further proof is needed.
const g = matrixGame([[3, -1], [-2, 1]]);
String(g); // "value 1/7; row plays r1 3/7, r2 4/7; column plays c1 2/7, c2 5/7"
g.guarantee(["1/2", "1/2"]); // 0: a fifty-fifty row player can be held to 0
g.cap(["1/2", "1/2"]); // 1
g.guarantee(); // 1/7, and g.cap() is 1/7 too
For a 2×2 game without a saddle point the optimal makes the column player indifferent: gives .
Saddle points
saddlePoints([[4, 2, 3], [1, 0, 5], [3, 1, 2]]); // [{ row: 0, column: 1, value: 2 }]
matrixGame([[4, 2, 3], [1, 0, 5], [3, 1, 2]]).value; // 2: pure strategies suffice
An entry that is the smallest in its row and the largest in its column is a saddle point. There, pure strategies are optimal and the value is that entry. Many games have none, like rock, paper, scissors, and then mixing is essential.
Morra
In two-finger Morra each player shows one or two fingers and calls a guess of the total. A player who guesses right and whose opponent guesses wrong wins the total. Keeping the four sensible strategies (show 1 and call 2 or 3; show 2 and call 3 or 4) gives a fair game with a curious answer:
const s = ["1,2", "1,3", "2,3", "2,4"]; // fingers shown, total called
const morra = matrixGame([[0, 2, -3, 0], [-2, 0, 0, 3], [3, 0, 0, -4], [0, -3, 4, 0]], { rows: s, columns: s });
String(morra); // "value 0; row plays 1,3 4/7, 2,3 3/7; column plays 1,3 3/5, 2,3 2/5"
Optimal strategies need not be unique. Here any mixture of "1,3" and "2,3" between and is optimal; the simplex method returns one vertex of that set.
@ratmath/discrete and Core's Rational in scope. The last line's value is shown; PRINT(…) adds lines, TEX(…) typesets math.