Bimatrix games
When the players' interests are not exactly opposed, each has their own payoff matrix, and the solution is a Nash equilibrium: a pair of strategies where neither player gains by changing alone. A game can have several. Here they are found exactly and each one is checked.
The prisoner's dilemma
import { bimatrixGame } from "@ratmath/discrete";
const c = ["cooperate", "defect"];
const pd = bimatrixGame([[3, 0], [5, 1]], [[3, 5], [0, 1]], { rows: c, columns: c });
pd.dominance();
// { rows: ["defect"], columns: ["defect"], steps: [
// { player: "row", removed: "cooperate", by: { defect: 1 } }, { player: "column", … } ] }
pd.equilibria().map(String); // ["(defect; defect) paying 1 and 1"]
holds the row player's payoffs and the column player's. A strategy is strictly dominated if another does better against everything the opponent might do; no rational player uses it, so it can be removed, and the removal may expose more. In the prisoner's dilemma defecting dominates for both, so both defect and get 1 each, though cooperating would give each 3.
Dominated by a mixture
const g = bimatrixGame([[3, 0], [0, 3], [1, 1]], [[1, 0], [0, 1], [0, 0]],
{ rows: ["top", "middle", "bottom"], columns: ["left", "right"] });
g.dominance({ pureOnly: true }).rows; // ["top", "middle", "bottom"]: no pure strategy beats bottom
g.dominance().steps[0]; // { player: "row", removed: "bottom", by: { top: 1/2, middle: 1/2 } }
Bottom pays 1 against either column. Neither top nor middle beats it everywhere, but the fifty-fifty mixture pays against both. Whether some mixture strictly dominates a strategy is a small linear program (maximise the margin by which the mixture wins), solved exactly.
Every equilibrium
const chicken = bimatrixGame([[0, -1], [1, -10]], [[0, 1], [-1, -10]],
{ rows: ["swerve", "straight"], columns: ["swerve", "straight"] });
const eq = chicken.equilibria();
eq.map(String);
// ["(swerve; straight) paying -1 and 1",
// "(straight; swerve) paying 1 and -1",
// "(9/10 swerve + 1/10 straight; 9/10 swerve + 1/10 straight) paying -1/10 and -1/10"]
eq.complete; // true: the game is nondegenerate, so this is all of them
At an equilibrium each player mixes only over strategies that are best responses, so every strategy in the support pays the same. Support enumeration tries each pair of supports of equal size, solves the indifference equations
exactly, and keeps the solutions that are positive on the supports and best responses overall. In a nondegenerate game (no mixture over strategies has more than best responses) equilibria have supports of equal size and this finds them all, and their number is odd. When an indifference system has a whole line of solutions the game is degenerate, it may have infinitely many equilibria, and the list is marked complete: false. The work grows like ; beyond a million pairs of supports it refuses.
Lemke–Howson
chicken.lemkeHowson(0).toString(); // "(swerve; straight) paying -1 and 1"
chicken.lemkeHowson(1).toString(); // "(straight; swerve) paying 1 and -1"
The Lemke–Howson method finds one equilibrium by a path. Label each pure strategy; an equilibrium is a pair of points in two polytopes that together carry every label. Dropping one label at the artificial starting point and pivoting, alternately in each polytope, to pick up the label just lost, follows a path that must end at an equilibrium. Different starting labels can reach different equilibria. The argument also proves that every game has an equilibrium, and that a nondegenerate game has an odd number of them; the test suite checks both on 200 random games.
@ratmath/discrete and Core's Rational in scope. The last line's value is shown; PRINT(…) adds lines, TEX(…) typesets math.