Questing · 2026-09-12 · Evolutionary Game Theory · Zero Dependencies
KIN
A grid of agents. Each one is either a cooperator or a defector. In a well-mixed population, defectors always win — the logic is airtight. But give agents a position in space, and something unexpected happens: cooperators persist, by clustering.
Open Kin →The Prisoner's Dilemma
Two players each choose simultaneously: cooperate or defect. If both cooperate, both receive the reward R. If both defect, both receive the punishment P. But if one cooperates and the other defects, the defector receives the temptation T and the cooperator receives the sucker's payoff S. The dilemma is that T > R > P > S: defecting is always individually better regardless of what the opponent does — yet mutual defection (P, P) is worse for both than mutual cooperation (R, R).
// Payoff matrix (me × opponent) // C D // C → R = 1.0 S = 0.0 // D → T = ? P = 0.0 // // Kin uses R = 1, S = P = 0, T = slider (1.0 – 2.0) // Only T matters: it sets how much defectors gain from exploiting cooperators. // // Nash equilibrium: (D, D) — mutual defection. // Social optimum: (C, C) — mutual cooperation. // The gap between these two is the tragedy of the commons.
In an infinitely large, well-mixed population evolving under any reasonable selection rule, defectors take over completely whenever T > R. The logic is merciless: a single defector in a sea of cooperators earns T > R every round, so it out-reproduces cooperators, spreads, and as the proportion of defectors rises, average payoffs fall — until everyone defects and earns P. The dilemma has no evolutionary resolution in the well-mixed case.
The Spatial Twist — Nowak & May, 1992
In their 1992 Nature paper, Martin Nowak and Robert May placed agents on a square lattice. Each agent plays the prisoner's dilemma only with its 8 immediate neighbors (Moore neighborhood). After scoring, each agent adopts the strategy of whichever of its 9 cells (itself included) scored the highest. This is called imitation dynamics.
The result was striking: cooperators survive. For T below a critical threshold Tc ≈ 1.74, cooperators don't just persist — they form intricate, ever-shifting spatial patterns, neither growing to fill the grid nor vanishing. The spatial structure creates something the well-mixed model can't: cooperator clusters. An interior cooperator plays only against other cooperators and earns nearly 8R. An edge cooperator earns less, but its interior is protected. The cluster is collectively robust even though individual boundary members are vulnerable.
// Nowak-May spatial PD: 200×200 grid, periodic boundaries // Each agent i plays PD with each of 8 Moore neighbors j: // score(i) = Σ_j payoff(state_i, state_j) // // Imitation: each agent adopts the strategy of its highest-scoring neighbor // (ties broken by current strategy — no change if tied with self) // // Update is synchronous: all agents imitate using scores from the same round // // Critical threshold (Moore neighborhood, R=1, S=P=0): // T_c ≈ 1.74 // T < T_c: cooperators survive, complex spatial patterns // T > T_c: defectors take over (nearly) completely
Why Clusters Protect Cooperators
The geometry of the square lattice determines which strategy wins each cell. Consider a cooperator deep inside a cooperator cluster: all 8 neighbors are cooperators, so its score is 8R = 8. A defector on the border of that cluster has, say, 3 cooperator neighbors and 5 defector neighbors — score = 3T + 5P = 3T. For the defector to invade, we need 3T > 8R = 8, i.e. T > 2.67. That's outside our range. Deep cooperators can't be invaded by defectors.
The battle happens at the boundary. A boundary cooperator has, say, 5 cooperator and 3 defector neighbors — score = 5R = 5. Its defector neighbor has 4 cooperator and 4 defector neighbors — score = 4T. If 4T > 5 (T > 1.25), the defector will convert the boundary cooperator. But that puts the defector now adjacent to more interior cooperators, which scored 8. The defector (4T) loses to the interior cooperator (8), and the interior cooperator reconverts a nearby cell. The cluster boundary fluctuates rather than collapsing.
At the critical temptation Tc ≈ 1.74, this dynamic balance breaks. Defectors on the boundary can successfully invade faster than the cluster core can replenish boundary cells, and the cooperator population collapses. Below Tc, the coexistence is maintained indefinitely — the beautifully complex amber-teal patterns you see in Kin are a living boundary, perpetually contested but never resolved.
Phase Diagram and Regimes
With R = 1, S = P = 0, the spatial PD has a single control parameter: T. Three qualitatively distinct regimes exist:
T < 1.0: Cooperators dominate trivially — defectors have no advantage.
(Not a proper dilemma: T ≤ R means defecting isn't tempting.)
1.0 < T < 1.74: Cooperators survive via spatial clustering.
Fractal-like amber patterns, perpetual boundary fluctuation.
Cooperator fraction settles to a positive equilibrium.
This is Nowak-May's famous coexistence regime.
T ≈ 1.74: Critical threshold. Cooperators barely survive.
Patches form and dissolve. Very slow convergence.
1.74 < T < 2.0: Defectors win. Cooperators go extinct (or nearly so).
The amber patches shrink, fragment, and disappear.
(Numbers are for Moore neighborhood, synchronous update, R=1, S=P=0.
Different neighborhoods and update rules shift T_c.)Drag the T slider in Kin from 1.4 to 1.9 and watch the transition. Below 1.74, the grid settles into a dynamic equilibrium — amber patches persist, shifting and rotating. At 1.74, the patches become unstable and slowly disappear. Above 1.74, the grid turns teal. The sparkline shows the cooperator fraction declining toward zero.
Where This Physics Appears
The spatial prisoner's dilemma is not just a model — it is a universal mechanism operating wherever agents with competing strategies are embedded in a spatial (or network) structure:
Cancer cells face a prisoner's dilemma within tumors. Some cancer cells produce growth factors that benefit nearby cells (cooperators in the evolutionary sense). Cells that free-ride on these factors without producing them are defectors. Within the spatially structured tumor, both cell types coexist — defector cells can't eliminate cooperators because the cooperators cluster and protect each other's niche. Understanding this spatial game suggests therapeutic strategies: disrupting the tumor's spatial structure can shift the balance.
Bacterial biofilms are another example. Biofilm-forming bacteria produce an extracellular matrix (cooperation) that protects the whole colony. Free-living bacteria (defectors) don't pay the cost of matrix production. In spatially structured conditions — on a surface or in a biofilm — cooperating bacteria cluster and dominate; in a well-shaken liquid culture, defectors win. The spatial prisoner's dilemma predicts exactly this experimental result, confirmed in Pseudomonas fluorescens and other model organisms.
The evolution of altruism — a puzzle since Darwin — is partly resolved by spatial structure. Related organisms tend to be spatial neighbors (kin selection), so an altruistic act benefits nearby relatives who share the altruistic gene. This is Hamilton's rule: altruism evolves when rb > c (relatedness × benefit > cost). Nowak-May's spatial model is a complementary mechanism: network reciprocity, where spatial clustering serves the same role as genetic relatedness — protecting cooperators from defector invasion.
Human cooperation in settings from fishing commons to climate agreements follows the same structure. When parties are geographically distant and interact little (well-mixed), free-riding dominates. When they are embedded in dense local networks — trading partners, reputation systems, regional agreements — cooperation can persist. The Nowak-May model is a mathematical skeleton underlying a vast body of social science research.
The Imitation Rule and Update Dynamics
Kin uses deterministic best-neighbor imitation: each agent unconditionally adopts the strategy of whichever of its 9 cells (the 8 neighbors plus itself) received the highest score in the previous round. If the agent itself has the highest score, it keeps its strategy. This rule is the one Nowak and May used in 1992. It produces clean, regular patterns — the characteristic fractal boundaries.
// Each generation:
// 1. Score phase (simultaneous):
for each cell (x, y):
score(x,y) = Σ_{8 neighbors} payoff(state(x,y), state(neighbor))
// 2. Imitate phase (simultaneous):
for each cell (x, y):
best = argmax { score(z) : z ∈ {(x,y)} ∪ neighbors(x,y) }
next_state(x,y) = state(best)
// Update is synchronous: all scores from round t before any state changes.
// This prevents wave-front artifacts from sequential updates.Other update rules produce qualitatively different behavior. Stochastic imitation (copy the best neighbor with probability proportional to payoff difference) smooths out the sharp boundaries. Asynchronous (random sequential) updates shift Tc. Moran process (birth-death with fitness-proportional selection) produces different phase diagrams entirely. Kin's synchronous deterministic rule is chosen for its visual richness and its direct correspondence to the original 1992 result.
Implementation
Kin runs on a 200 × 200 toroidal grid (periodic boundaries in both directions, so the grid wraps — no edge effects). The simulation requires two passes per generation: first a score pass (reading state), then an imitation pass (writing next state from scores). Both use typed arrays: a Uint8Array for strategies and a Float32Array for scores. The grid is rendered via ImageData at native 200 × 200 resolution, scaled 3× by CSS image-rendering: pixelated to 600 × 600 pixels.
// Performance: 200×200 × 2 passes × 8 neighbors ≈ 640,000 ops/generation // At 2 gen/frame × 60 fps → ~77M ops/sec // Fits in ~5ms/frame on a modern device (well within 16ms budget) // // Color encoding: // Cooperator: amber — brightness ∝ score/8R (dim edge, bright interior) // Defector: teal — brightness ∝ score/8T (dim edge, bright exploiter) // The score-mapped brightness makes cluster structure and boundaries visible. // // Grid: 200×200 wrapped (toroidal), so the aspect ratio is symmetric. // No boundary conditions to handle — every cell has exactly 8 neighbors.
The color encoding is intentional. Cooperators deep inside their cluster, playing against 8 cooperating neighbors, glow bright amber. Cooperators on the boundary, against 3-5 defectors, appear dim. This makes the cluster geometry — and the battlefronts — directly visible without any additional overlay or annotation.