Optimisation and games

Combinatorial games

Two players move in turn, everything is known, and whoever cannot move loses. Every impartial position is worth a nim heap, and every partizan position has a canonical value in Conway's sense: numbers, nimbers, up, and more.

Nim and the Sprague–Grundy theorem

import { nim, impartialGame, subtractionGame, mex } from "@ratmath/discrete";

nim([3, 4, 5]);   // nimSum 2n, losing false, winningMoves [{ heap: 0, from: 3n, to: 1n }]
const g = impartialGame((n) => [1, 2].filter((m) => m <= n).map((m) => n - m));
[0, 1, 2, 3, 4, 5, 6].map(g.grundy).join("");   // "0120120"
subtractionGame([1, 3, 4], 30).period;           // { start: 0, length: 7 }

The Grundy value of a position is the smallest non-negative integer not among its options' values,

G(P)=mex⁡{G(Q):P→Q}, \mathcal G(P) = \operatorname{mex}\{\mathcal G(Q) : P \to Q\},

and a position is lost for the player to move exactly when G(P)=0\mathcal G(P) = 0. The Sprague–Grundy theorem says a sum of games has value the nim-sum (bitwise XOR) of the parts, which is why nim is solved by XOR. Subtraction games with a finite set are eventually periodic; the period is found by looking for a repeat as long as the largest move.

Partizan games

import { cgame, starGame, up, down } from "@ratmath/discrete";
String(cgame([0], []));         // "1"
String(cgame([0], [1]));        // "1/2"
String(cgame([0], [0]));        // "*"
String(starGame(2).add(starGame(3)));   // "*": nimbers add by XOR
String(up().add(up()));         // "⇑"
up().compare(0);                // ">": up is positive …
up().compare(starGame(1));      // "‖": … yet confused with *
cgame([2], [-1]).outcome;       // "first player"

A game is G={GL∣GR}G = \{G^L \mid G^R\}, its options for Left and for Right. G≥0G \ge 0 means Left wins moving second, and the order, sum and negation follow Conway's recursive definitions. Each game is reduced to canonical form by removing dominated options and bypassing reversible ones, so equal games print the same: {0∣1}=1/2\{0 \mid 1\} = 1/2, {0∣0}=∗\{0\mid 0\} = *, {0∣∗}=↑\{0 \mid *\} = \uparrow.

Hackenbush

partizanGame computes the value of any position from the two players' move functions. In blue-red Hackenbush on a single stalk, the value is a binary fraction you can read off the colours:

import { partizanGame } from "@ratmath/discrete";
const stalk = (s) => partizanGame(s,
  (p) => [...p].map((c, i) => (c === "B" ? p.slice(0, i) : null)).filter((x) => x !== null),
  (p) => [...p].map((c, i) => (c === "R" ? p.slice(0, i) : null)).filter((x) => x !== null));
String(stalk("BR"));     // "1/2"
String(stalk("BBRB"));   // "7/4"

Backward induction

import { backwardInduction } from "@ratmath/discrete";
const entry = { player: "A", moves: {
  in:  { player: "B", moves: { fight: { payoff: { A: -1, B: -1 } }, share: { payoff: { A: 1, B: 1 } } } },
  out: { payoff: { A: 0, B: 2 } } } };
backwardInduction(entry).path;   // ["in", "share"]: the threat to fight is not credible

In a finite game tree with perfect information, solving from the leaves up gives a subgame-perfect equilibrium. Ties are reported rather than broken silently.

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