Optimisation and games

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 AA lists what the column player pays the row player: AijA_{ij} when row ii meets column jj. If the row player mixes with probabilities pp and the column player with qq, the expected payment is p⊤Aqp^{\top} A q. The row player wants it large and the column player small.

The minimax theorem

A mixture pp guarantees min⁡j(p⊤A)j\min_j (p^{\top}A)_j, the worst it earns against any column. A mixture qq caps the row player at max⁡i(Aq)i\max_i (Aq)_i. Every guarantee is at most every cap, and von Neumann's theorem says the best guarantee equals the best cap:

max⁡pmin⁡q p⊤Aq  =  min⁡qmax⁡p p⊤Aq  =  v. \max_p \min_q\, p^{\top}Aq \;=\; \min_q \max_p\, p^{\top}Aq \;=\; v.

The row player's problem is a linear program: maximise vv subject to (p⊤A)j≥v(p^{\top}A)_j \ge v for every column, ∑pi=1\sum p_i = 1, p≥0p \ge 0. 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 pp and the cap of qq are both vv: 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 pp makes the column player indifferent: 3p−2(1−p)=−p+(1−p)3p - 2(1-p) = -p + (1-p) gives p=3/7p = 3/7.

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 4/7:3/74/7 : 3/7 and 3/5:2/53/5 : 2/5 is optimal; the simplex method returns one vertex of that set.

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