Spatial artificial life
Write a small local rule — count your neighbours, follow the strongest smell, grow toward the
nearest unclaimed space — and a larger structure can appear: a spaceship, a coastline, a transport
network, a dendrite with a fractional dimension. This page explores those systems through grids,
continuous fields and populations of agents. For related work, see growing.kt,
interaction nets, Wave Function Collapse, and
graphs that change while you watch. For vehicles whose neurons have
memory, adaptation and learning, see Braitenberg vehicles with memory.
Almost any local rule applied to noise looks organic, so each example here prints a measurement
beside a control: the same setup with one ingredient removed, which gets the number wrong. A
glider's every live cell is displaced by exactly (1, 1) after four generations, and the same seed
under a rule one birth-count away is displaced by nothing at all. A flock of 400 agents reaches a
polarization of 0.69 where 400 independent headings would give 0.044, and the same initial state
with the alignment term zeroed reaches 0.073.
Where the numbers come from
afield.examples.stdlib.AlifeGallery renders the figures below in CI and asserts, for each, the
statistic its control fails, so the numbers printed here are the numbers it asserts.
Source files
Seven dependency-free files built on the existing imaging and mesh families. Nothing here reimplements convolution, connected-component labelling, colour mapping or marching cubes:
alife.kt—IntGrid, the toroidal-or-bounded integer lattice; the grid↔Imgbridges; and, mostly, the measurement layer that the rest of the family makes its claims through —polarizationand its random baseline,blobShapes(the isoperimetric ratio that tells spots from stripes),correlationLength,topShareandmassRadiusDimension.alife_ca.kt—LifeRuleparsed fromB3/S23, named Life patterns, exact cycle detection, cyclic multi-state CA, Wolfram's elementary 1-D rules as a space-time raster, and a 3-D life-like CA onIntVolume.alife_continuous.kt— mass-conserving diffusion, Gray–Scott reaction–diffusion, Gierer–Meinhardt Turing patterns, and Lenia.alife_agents.kt— boids, a Physarum deposit–sense–move population, and diffusion-limited aggregation with two control models.alife_lsystem.kt— a bracketed, parametric, optionally stochastic L-system with a turtle that emitsFLines, and space-colonization branching.alife_tree_growth.kt— trees that compete for light: buds in a shadow grid, light summed toward the root and growth resource split back up by Borchert–Honda.alife_field.kt— the inlineToHTMLviews, kept in their own file so the five above name nothing from Field's visualizer.
Two later files extend the agent side along an axis this page does not walk through. Every system above advances a whole population with one call and one parameter block, so two agents differ only in their state and never in what they are trying to do:
alife_script.kt—ScriptWorldgives each agent its own suspending program on a fixed-step clock:forever,phase(name),parallel,moveUntil, and steering composed from whichever behaviours are active right now. The same seed gives the same run, bit for bit, across processes.alife_walls.kt— and an environment for it to run in:ScriptObstacle, swept circle collision with a per-agentWallResponse,avoidWallsandcling, and a radius-awareNavGridsoroute()can go round a barrier that directseekpresses into. On an S-maze, over 96 agents, routedseekarrives 0.990 of the time against 0.000 for directseekat the same budget.
Both are worked through in section 12, which also says where to find them as documents you can open and edit.
Limits:
- CPU only. These are reference implementations you can read. The GPU story is Field 4, StackSL and RectSL. Four systems from this family's neighbourhood do run on the GPU, as shaders you can edit: diffusion-limited aggregation, Biham–Middleton–Levine traffic, a lattice Boltzmann fluid and a forest fire.
- One named Lenia lifeform. Orbium ships with its published cells; the rest of Chan's
catalogue loads from his
animals.jsonif you supply it. O(N²)neighbour search in the agent models. A spatial hash is right for 50 000 agents and is a different piece of code; these flocks are a few hundred agents and legibility wins.- Explicit Euler integration for the continuous systems, with the stability bound stated as a
requirerather than silently clamped.
1. Conway's Life
B3/S23 is the most-studied dynamical system in computing, which is exactly why it is the right place
to start: there are facts about it to be checked. The glider translates one cell diagonally every
four generations. The block is a still life. The blinker has period two. And the R-pentomino (five
cells) runs for 1103 generations, peaks at a population of 319 at generation 821, and settles to 116
cells, six of which are gliders still flying away.
Those R-pentomino figures are about an unbounded board. On a board with walls the
gliders are destroyed, and the system instead becomes exactly periodic at generation 1164 with 110
cells. Both answers are correct; they are answers to different questions. So the box below runs on a
620-wide board (wide enough that no glider can reach an edge in 1103 generations) and asserts
liveBounds to confirm the walls never took part rather than
assuming it.
:::static
// Conway's Life. Every claim here is about an object that predates this code, stated as an
// integer-grid equality rather than as an observation about a picture.
val glider = LifePatterns.glider.centeredOn(40, 40)
println("glider, 4 generations: rigid translation = " + translationBetween(glider, glider.stepTimes(LIFE, 4)))
println("glider, 8 generations: rigid translation = " + translationBetween(glider, glider.stepTimes(LIFE, 8)))
// The control: one birth count away and the same seed is not a spaceship at all.
println(
"same seed under B34/S23, 4 generations: " + translationBetween(glider, glider.stepTimes(lifeRule("B34/S23"), 4)),
)
val block = LifePatterns.block.centeredOn(20, 20)
println("block is bit-identical after one step: " + block.step(LIFE).sameCellsAs(block))
println("blinker's detected period: " + LifePatterns.blinker.centeredOn(20, 20).runUntilCycle(LIFE, 20).period)
println("toad's detected period: " + LifePatterns.toad.centeredOn(20, 20).runUntilCycle(LIFE, 20).period)
/*
The R-pentomino. The textbook figures -- peak 319 at generation 821, then 116 cells from
generation 1103 -- are facts about an UNBOUNDED board: six of those 116 cells are gliders still
in flight. So it needs room. 620 wide gives every glider more than the 1103/4 cells it can
travel, and liveBounds() confirms nothing reached an edge rather than assuming it.
*/
var g = LifePatterns.rPentomino.centeredOn(620, 620, wrap = false)
var peak = 0
var peakAt = 0
for (gen in 1..1103) {
g = g.step(LIFE)
val pop = g.population()
if (pop > peak) {
peak = pop
peakAt = gen
}
}
val b = g.liveBounds()!!
println("R-pentomino peak population " + peak + " at generation " + peakAt)
println(
"population at generation 1103 = " + g.population() + ", live cells " + b[0] + " clear of the board edge",
)
:::fold draw the glider's first five generations, twelve pixels to a cell
showImg(
montage(
(0..4).map {
"gen " + it to LifePatterns.glider.centeredOn(16, 16).stepTimes(LIFE, it).toImg(2).upscaleNearest(12)
},
columns = 5,
cellSide = 200,
),
caption = "one glider, five generations: the same shape displaced by (1, 1)",
)
:::endfold

translationBetween is not a centroid comparison. It aligns the two
patterns' bounding boxes, shifts one by that offset, and then demands exact cell-by-cell equality, so
a pattern that moved and changed shape returns null. A wrong rule cannot fluke that: the
B34/S23 line above prints null.
The two objects, moving
Two panels, 400 generations, two generations per displayed frame: slow enough that the glider's four-generation cycle is legible rather than a strobe.
Left: the glider, on a 16-cell torus, twenty-four pixels to a cell. It laps that board every 64
generations and does it six times over, so if you watch one edge you see it leave and re-enter from the
other. That is the same claim the montage above makes, and the same claim translationBetween asserts, in
the form where it is obvious.
Right: the R-pentomino on a 64-cell walled board, with its live population in the caption. This is the walled question, not the unbounded one the numbers above are about: those need 620 cells, and 620 cells at one pixel each is a video whose interesting part is four pixels wide. Same rule, same seed, different board, and what it shows is the shape of the thing: five cells, a violent expansion, then a long quieting-down toward something stationary.
Why the boards are so small
A glider is five live cells, so the board size decides what fraction of the frame ever changes. On a 48-cell torus that is 0.2 % of the pixels: the motion is perfect and the picture is almost still. Sixteen cells makes the same five cells 2 % of the panel, which takes the mean absolute luma difference between the clip's first and last frames from 1.47 of 255 to 5.26, with half the change arriving by 37 % of its length. A frame wants to be the size of the thing being claimed.
:::static
:::fold the two boards, integer-upscaled so a cell is a block you can count
/*
Both boards are integer-upscaled to the same 384 pixels, so a cell is a block you can count:
twenty-four pixels for the glider's 16-cell torus, six for the R-pentomino's 64-cell walled
board.
*/
fun boards(g: IntGrid, r: IntGrid, n: Int) = [
"glider, gen " + n to g.toImg(2).upscaleNearest(24),
"R-pent " + n + ": " + r.population() + " live" to r.toImg(2).upscaleNearest(6),
]
:::endfold
/*
400 generations over 200 displayed frames: TWO generations per frame. Slower than every other
clip on this page -- the glider's period is FOUR generations, and a spaceship stepped
nine generations at a time is a strobe rather than a spaceship.
*/
animateEvolution(
LifePatterns.glider.centeredOn(16, 16) to LifePatterns.rPentomino.centeredOn(64, 64, wrap = false),
totalSteps = 400,
seconds = 7.0,
framesTarget = 200,
columns = 2,
cellSide = 384,
step = { (g, r) -> g.step(LIFE) to r.step(LIFE) },
panels = { (g, r), n -> boards(g, r, n) },
)
2. Wolfram's elementary rules
A one-dimensional automaton's behaviour is two-dimensional, so
elementaryCa returns the whole space-time diagram as an
IntGrid: one row per generation, time downward. Every famous statement about these 256 rules is a
statement about this picture.
Rule 90 is left XOR right, and from a single seed its space-time diagram is Pascal's triangle mod
2, the Sierpiński gasket.
sierpinskiReference computes that from Kummer's theorem
(C(n, k) is odd exactly when n and k == k) without running a cellular automaton at all. So the
equality between them is a cross-check between two unrelated computations, not a stepper agreeing with
itself, and it holds in all 32 896 cells. A second, independent oracle comes free: row g of that
diagram holds exactly 2^popcount(g) live cells (Gould's sequence).
Rule 184 is the traffic rule, and it conserves the number of live cells exactly, forever. Rule 90 does not, which is the control that stops "184 conserves density" from being a property of the harness.
:::static
/*
Wolfram's elementary rules, and the sharpest evidence in the family: rule 90 from a single seed
IS Pascal's triangle mod 2, and `sierpinskiReference` computes that from Kummer's theorem
without running a cellular automaton at all. The equality is a cross-check between two
unrelated computations rather than a stepper agreeing with itself.
*/
val w = 257
val gens = 128
val rule90 = elementaryCa(90, w, gens)
val oracle = sierpinskiReference(w, gens)
var differing = 0
for (i in rule90.cells.indices) if (rule90.cells[i] != oracle.cells[i]) differing++
println("rule 90 vs Pascal mod 2: " + differing + " differing cells of " + rule90.cells.size)
// A second, independent oracle: row g of that diagram holds exactly 2^(popcount g) live cells.
val rows = (0 until gens).map { r -> (0 until w).count { rule90.cells[r * w + it] == 1 } }
println(
"row populations are exactly 2^popcount(generation): " +
(rows == (0 until gens).map { 1 shl Integer.bitCount(it) }),
)
// Rule 184 is the traffic rule: it conserves the number of 1s exactly, forever.
val seedRow = IntArray(257) { if ((it * 7919) % 3 == 0) 1 else 0 }
val traffic = elementaryCa(184, 257, 200, seedRow)
val pops = (0 until 200).map { r -> (0 until 257).count { traffic.cells[r * 257 + it] == 1 } }
println("rule 184 distinct row populations over 200 generations: " + pops.distinct())
println("rule 90 distinct row populations: " + rows.distinct().size + " values, so NOT conserved")
val col30 = elementaryCa(30, 601, 600).centerColumn()
println(
"rule 30 centre column: " +
col30.count { it == 1 } +
"/600 ones, longest run " +
longestRunLength(col30) +
" (rule 90's is " +
longestRunLength(rule90.centerColumn()) +
")",
)
:::fold draw four of the 256 space-time diagrams, time downward
showImg(
montage(
[
"rule 90 = Pascal mod 2" to rule90.toImg(2),
"rule 30" to elementaryCa(30, 257, 128).toImg(2),
"rule 110 (universal)" to
elementaryCa(110, 257, 128, IntArray(257) { if (it % 13 == 0) 1 else 0 }).toImg(2),
"rule 184 (density-conserving)" to traffic.toImg(2),
],
columns = 2,
cellSide = 300,
),
)
:::endfold

Four rules, drawn a generation at a time
A space-time diagram is a picture of time, which makes animating one slightly strange: the motion is the downward growth of the picture, not motion within it. It pays off anyway, because the four panels grow at the same rate and the differences between the 256 rules become differences you can watch appear.
One generation per displayed frame, 128 generations in six seconds. Rule 90 lays down the Sierpiński gasket
with the self-similarity arriving in bursts at the powers of two: the row populations are
2^popcount(g), so the widest rows are the ones where g has many bits, and the diagram visibly
breathes. Rule 30 starts
identically, from a single cell, and never repeats. Rule 110, the universal one, separates into a
background of periodic structure with localised gliders travelling through it, which is what a proof of
universality has to be built out of. Rule 184 just shifts, forever, conserving its 86 live cells exactly.
This clip reveals a finished diagram rather than re-simulating one
elementaryCa computes the whole diagram, and
rowsRevealed uncovers it a row at a time. That works here for
a specific reason, and not in general: row g of a space-time diagram depends only on rows above it,
so revealing rows and re-running the automaton produce identical pixels. Every other clip on the page
steps the real system.
:::static
:::fold compute the four space-time diagrams once, in full
// Compute the four space-time diagrams once, in full: 257 cells wide, 128 generations tall.
val seedRow = IntArray(257) { if ((it * 7919) % 3 == 0) 1 else 0 }
val diagrams = [
"rule 90" to elementaryCa(90, 257, 128).toImg(2),
"rule 30" to elementaryCa(30, 257, 128).toImg(2),
"rule 110" to elementaryCa(110, 257, 128, IntArray(257) { if (it % 13 == 0) 1 else 0 }).toImg(2),
"rule 184" to elementaryCa(184, 257, 128, seedRow).toImg(2),
]
:::endfold
/*
128 generations over 128 displayed frames: ONE row of every diagram per frame. The upscale comes
AFTER the reveal, so `n` counts generations rather than pixels -- and `rowsRevealed` keeps the
full height, so montage frames each panel identically instead of rescaling a growing image.
*/
animateEvolution(
Unit,
totalSteps = 128,
seconds = 6.0,
framesTarget = 128,
columns = 2,
cellSide = 520,
step = { it },
panels = { _, n ->
diagrams.map { (name, img) -> name + ", gen " + n to img.rowsRevealed(n).upscaleNearest(2) }
},
)
3. Cyclic cellular automata
A cyclic cellular automaton has k states arranged in a ring. A cell in state s advances to s + 1
as soon as at least threshold of its neighbours are already in s + 1; otherwise it waits. Nothing
can skip a state, and there is no way back, so the only durable structures are rotating bands.
The measurement that makes "it self-organised" checkable is
cyclicAdvanceFraction. In an uncorrelated soup a cell can advance
exactly when one of its eight Moore neighbours happens to hold its successor state, which is
1 - (1 - 1/k)^8: a closed form, not a threshold. For k = 14 that is 0.4473, and the measurement is
0.4483. Four hundred generations later every cell is being driven by a neighbour and the fraction pins
at 1.0000, which is something no uncorrelated field does at any k.
The second pair of panels is the excitable corner of the parameter space (six states, threshold three, radius two), which curls into visible spiral cores. That is a different phase, and the difference is reported as a length scale rather than left to the eye, though note the direction: the wider neighbourhood gives the smaller correlation length, because the spiral arms are narrow.
This statistic's floor is 0.632, not 0
correlationLength is the lag at which the direction-averaged
autocorrelation first falls to 1/e, linearly interpolated. For a field with no correlation at all
the interpolation runs from ac(0) = 1 straight to ac(1) ≈ 0, so the answer is \( 1 - 1/e = 0.632 \):
a floor, reached from below by nothing. So compare a settled field against its own pixels
shuffled, which lands on that floor to six decimal places, rather than against a chosen
constant. A checkerboard, the shortest wavelength a lattice holds, reads exactly
\( (1 - 1/e)/(4/3) = 0.4741 \), which is as low as this statistic can go.
:::static
/*
A cyclic cellular automaton: a cell in state s advances to s+1 as soon as any neighbour is
already in s+1. Nothing can skip, so the only durable structures are rotating bands.
The measurement that makes "it self-organised" checkable: in an UNCORRELATED soup a cell can
advance exactly when one of its 8 Moore neighbours happens to hold its successor state, which
is 1 - (1 - 1/states)^8 -- a closed form. Once the field has organised, every cell is driven by
a neighbour and the advance fraction pins at 1.0, which no uncorrelated field does.
*/
val rule = CYCLIC_LABYRINTH
val soup = IntGrid.random(200, 200, states = rule.states, seed = 7L)
val predicted = 1.0 - Math.pow(1.0 - 1.0 / rule.states, 8.0)
val early = cyclicAdvanceFraction(soup, soup.step(rule))
val settled = soup.stepTimes(rule, 400)
val late = cyclicAdvanceFraction(settled, settled.step(rule))
println("soup advance fraction " + early.f4 + ", closed form " + predicted.f4)
println("after 400 generations " + late.f4 + " of cells advance every step")
// The excitable corner of the parameter space (wider neighbourhood, higher threshold) is a
// visibly different phase -- spiral cores -- and the difference is measurable as a length scale.
val ex = CYCLIC_EXCITABLE
val exSettled = IntGrid.random(200, 200, states = ex.states, seed = 7L).stepTimes(ex, 200)
println(
"correlation length: labyrinth " +
settled.toImg(rule.states).correlationLength(maxLag = 30).f2 +
", excitable " +
exSettled.toImg(ex.states).correlationLength(maxLag = 30).f2,
)
:::fold one palette per rule, and three generations of the labyrinth beside the excitable phase
val palette = cyclicPalette(rule.states)
showImg(
montage(
[
"gen 0 (soup)" to soup.toImg(palette),
"gen 40" to soup.stepTimes(rule, 40).toImg(palette),
"gen 400" to settled.toImg(palette),
"excitable, gen 200" to exSettled.toImg(cyclicPalette(ex.states)),
],
columns = 2,
cellSide = 280,
),
)
:::endfold

The two phases, running
Of everything in this family the cyclic CA is the system whose stills lie hardest, because the whole point of it is rotation and a still frame has no chirality. Both panels below run 400 generations in six and a half seconds (a little over three generations per displayed frame) from the same kind of soup. The lattices are 96 cells rather than the figure's 200, drawn three pixels to a cell: at one pixel per cell a rotating spiral core is about four pixels of video and reads as flicker.
Why 96 cells and not 128
Page weight decided that number. A cyclic CA re-colours essentially every cell of both lattices on every frame, so consecutive frames share almost nothing and the encoder has nothing to predict from; this is the heaviest clip on the page at 5.1 MB. The cost is very nearly proportional to cells × frames rather than to pixels, so magnifying a small lattice with nearest-neighbour blocks is close to free and shrinking the lattice is what pays: 96 cells at three pixels each rather than 128 at two.
The left panel is the sequence the figure above samples at generations 0, 40 and 400, without the gaps. Watch for the transition, because it is sharp and it has a name: the soup is locally advancing everywhere (that is the 0.4483 the box prints, and it is what an uncorrelated field gives) but it has no structure, and then within a few dozen generations it is entirely bands, after which every cell advances on every step forever. Nothing directs that. The right panel is the excitable rule, and what it does that the labyrinth does not is rotate: those spiral cores are pinned defects, they wind at a fixed rate, and you cannot see any of that in a photograph of one.
:::static
:::fold one palette per rule, fixed by the rule and never by the frame
/*
One palette per rule, FIXED -- it is a function of the rule's state count, never of the frame,
which is what makes generation 0 and generation 400 the same picture of the same thing. Three
pixels to a cell, nearest-neighbour, so the bands are countable and the encoder sees flat blocks.
*/
val pal = cyclicPalette(CYCLIC_LABYRINTH.states)
val exPal = cyclicPalette(CYCLIC_EXCITABLE.states)
fun soups(a: IntGrid, b: IntGrid, n: Int) = [
"labyrinth, gen " + n to a.toImg(pal).upscaleNearest(3),
"excitable, gen " + n to b.toImg(exPal).upscaleNearest(3),
]
:::endfold
// Two cyclic rules from the same kind of soup, stepped together so the schedule is shared:
// 400 generations over 130 displayed frames, so a little over THREE generations per frame.
val rule = CYCLIC_LABYRINTH
val ex = CYCLIC_EXCITABLE
animateEvolution(
IntGrid.random(96, 96, states = rule.states, seed = 7L) to IntGrid.random(96, 96, states = ex.states, seed = 7L),
totalSteps = 400,
seconds = 6.5,
framesTarget = 130,
columns = 2,
cellSide = 288,
step = { (a, b) -> a.step(rule) to b.step(ex) },
panels = { (a, b), n -> soups(a, b, n) },
)
4. Gray–Scott reaction–diffusion
Two coupled fields, u (substrate) and v (autocatalyst), the reaction U + 2V → 3V with V → P,
and two numbers: a feed rate F and a kill rate k. The entire zoo of morphologies is a function of
those two numbers. Two knobs producing that much variety is why Gray–Scott is the canonical
morphogenesis model.
Spots and a labyrinth can have identical coverage, identical mean and identical variance. Nothing in a
histogram separates them. What does separate them is the shape of their features, and
morphology() measures it: the
isoperimetric ratio \( P^2/4\pi A \) (1 for a disc, growing without bound as a region gets thinner and more
convoluted) and the share of the covered area held by the single largest connected feature.
The numbers below, from the run in the box: the spots preset gives 329 separate features with a median
ratio of 0.80 and a largest-blob share of 0.008; the two labyrinth presets give 24 and 34
features with ratios of 2.5 and 2.4 and largest shares of 0.64 and 0.45. Three times
less round, and dominated by one connected structure. The holes preset is the inverse morphology and
reads as a single blob at 0.78 coverage with a ratio of 90.
To follow the spots of a run as they are born and split, frame by frame, see
Tracking features through time.
The \( 4/\pi \) perimeter correction
The digital "crack perimeter" over-estimates a smooth boundary by about \( 4/\pi \), so blobShapes
applies that correction, and the gallery measures a synthetic 40-cell disc to confirm it lands at
1.00. The correction is asymptotic, so a 10-cell blob reads slightly under 1 (hence 0.80 for the
spots). That bias points toward "round", which makes a stripe verdict conservative.
Two controls. v = 0 is a stable fixed point of the equations, so an unseeded field stays at exactly
0.0 forever, so the pattern comes from the dynamics and not from the renderer. And with Du == Dv the
Turing instability's short-scale advantage is gone and the same seed, the same reaction and the same
6000 steps produce nothing at all: Turing's own argument, restated as an experiment.
:::static
/*
Gray-Scott reaction-diffusion. Two coupled fields, two parameters, and the whole zoo of
morphologies -- spots, worms, mazes, holes -- is a function of those two numbers.
The point of `morphology()` is that a regime claim should be a claim about numbers. Spots and a
labyrinth can have identical coverage, identical mean and identical variance; what separates
them is the SHAPE of their features (isoperimetric ratio P^2/4piA, ~1 for a disc) and whether
one connected structure dominates.
*/
val side = 128
val steps = 6000
val runs =
[
"spots 0.030/0.062" to GRAY_SCOTT_SPOTS,
"worms 0.037/0.060" to GRAY_SCOTT_WORMS,
"maze 0.029/0.057" to GRAY_SCOTT_MAZE,
"holes 0.039/0.058" to GRAY_SCOTT_HOLES,
]
.map { (label, p) ->
label to grayScottSeed(side, side, patches = 6, patchSize = 14, seed = 11L).stepTimes(p, steps)
}
for ((label, end) in runs) println(label + " -> " + end.v.morphology())
/*
Two controls. v = 0 is a fixed point of the equations, so an unseeded field never patterns;
and equal diffusion coefficients destroy the Turing instability entirely, which is Turing's
own argument restated as an experiment.
*/
val unseeded = grayScottSeed(side, side, patches = 0).stepTimes(GRAY_SCOTT_WORMS, 500)
println("CONTROL no seed: max autocatalyst after 500 steps = " + unseeded.v.max())
val equalD =
grayScottSeed(side, side, patches = 6, patchSize = 14, seed = 11L)
.stepTimes(GrayScottParams(feed = 0.037, kill = 0.060, du = 0.08, dv = 0.08), steps)
println("CONTROL Du == Dv -> " + equalD.v.morphology())
:::fold draw the four regimes and the two controls, all on one fixed magma range
val controls: List<Pair<String, GrayScottState>> = [
"CONTROL no seed" to unseeded,
"CONTROL Du == Dv" to equalD,
]
showImg(
montage(
(runs + controls).map { (label, s) ->
label to s.v.colormap("magma", 0f, 0.5f).upscaleNearest(2)
},
columns = 3,
cellSide = 260,
),
)
:::endfold

The same 6000 steps, recorded live
A montage of finished fields is the evidence, and it is also the one view that hides what
reaction–diffusion is: those numbers are the end of a process, and the process is the phenomenon. So here
is one preset again, animated: the maze field, F = 0.029, k = 0.057, the same 6000 steps.
What to watch, with the step counter in the caption as your clock:
- Step 63. Six square seed patches, each with a bright rim. That is
grayScottSeed's output plus enough steps to make it visible:vis high only where the patch meets the substrate. - Step ~600. Every square has buckled into a four-lobed cross. This is the Turing instability arriving as an event: the flat patch boundary is unstable, and it does not decay into noise, it picks a wavelength. The four lobes are that wavelength meeting the square's symmetry.
- Step ~1600. Each patch has filled its own interior with labyrinth and the fronts are advancing into
empty substrate. The black regions are unconsumed
u, and they are shrinking. - Step ~2900. The fronts have met. From here the field is entirely labyrinth and the dynamics change character completely: no more growth, only rearrangement.
- Step 6000. Annealed. Watch the last second and almost nothing moves; the little that does is at the free ends, which is where a labyrinth still has somewhere to go. That is why the section's numbers are stable enough to assert.
Those five events are not spread evenly through the 6000 steps, so the clip is not paced evenly either.
The step schedule here is geometric, not linear
Reaction–diffusion is a log-time process: step 60 to step 600 and step 600 to step 6000 are about equally large visible changes, so equal steps per displayed frame cannot be equal change per frame. A linear schedule spends four fifths of this clip's change in its first two seconds and then shows a finished labyrinth for four.
So animateEvolution takes a ramp, and the one visible line
of the fence below sets it to 40, meaning the last displayed frame advances forty times as many steps
as the first, on the geometric schedule \( \mathrm{done}(t) = 6000 \cdot (40^t - 1) / 39 \). The mid-point of the
schedule is then step 819 rather than step 3000, and half the clip's change has arrived at 47.8 %
of its length rather than at 14.5 %.
What a ramp cannot do is make a burst uniform. Gray–Scott's is the fronts filling the domain, roughly
steps 400 to 1500, which is 18 % of a linear clip and 30 % of this one. A schedule only decides how
much of the clip that window gets, and ramp is the one parameter to change if you disagree with where
it sits.
The ramp asks the last frames for about 170 reaction–diffusion steps each, which is more work than the
first frames. During docs capture that cost can make the build slower, but cannot stretch or truncate
the movie: the recorder advances AnimationClock by exactly 1 / fps only after the preceding Field
frame has been painted and stored. This clip therefore reaches step 6000 of 6000 reproducibly, with a
beat of settled labyrinth after it, and the burned-in step clock keeps that endpoint checkable.
:::static
:::fold the autocatalyst field, on a fixed colour range
/*
The autocatalyst field on a FIXED 0..0.5 magma range. Auto-ranging per frame is what makes an
animation of a growing pattern appear to stand still, and it is the single easiest way to turn
a reaction-diffusion clip into a lie.
*/
fun view(s: GrayScottState, n: Int) =
["maze - step " + n + " of 6000" to s.v.colormap("magma", 0f, 0.5f).upscaleNearest(3)]
:::endfold
/*
The `maze` preset, the same 6000 steps the figure above measures, over 140 displayed frames --
but NOT 43 steps per frame. `ramp = 40` makes the last displayed frame advance forty times as
many steps as the first, which is the schedule a log-time process needs: the mid-point of the
clip is step 819 rather than step 3000. This is the number to change if you disagree.
*/
animateEvolution(
grayScottSeed(128, 128, patches = 6, patchSize = 14, seed = 11L),
totalSteps = 6000,
seconds = 7.0,
ramp = 40.0,
cellSide = 420,
step = { it.step(GRAY_SCOTT_MAZE) },
panels = { s, n -> view(s, n) },
)
5. Lenia
Lenia is a cellular automaton whose cells hold any value from 0 to 1 instead of on or off. Each step,
every cell takes a weighted average of its neighbourhood out to radius R (the kernel K), passes it
through a growth curve G that is positive near one value mu and negative elsewhere, and moves a
small step dt in that direction:
A <- clamp(A + dt * G(K * A), 0, 1)
What makes Lenia worth studying is that some patterns under some rules behave like creatures: they
keep their shape and move. The best known is Orbium unicaudatus, from Bert Chan's catalogue of
Lenia lifeforms. LeniaPresets.orbium() loads its rule
and its 20 × 20 array of cells from that catalogue. On the left below it glides across a 64-cell
torus at a steady 0.62 cells per step, and its mass (the field's mean value, in the caption) stays
level the whole way. On the right are the same cells under the same rule with one number changed:
mu, the centre of the growth curve, moved from 0.15 to 0.12. The creature swells into a ring, the
ring tears, and the pieces multiply until they cover the torus.
:::static
val orbium = LeniaPresets.orbium()
:::fold draw both fields on a fixed 0..1 inferno scale, with the mean field in the caption
fun fields(a: Img, b: Img, n: Int) = [
"Orbium, step %d: mass %.4f".format(n, a.mean()) to a.colormap("inferno", 0f, 1f).upscaleNearest(6),
"mu 0.12, step %d: mass %.4f".format(n, b.mean()) to b.colormap("inferno", 0f, 1f).upscaleNearest(6),
]
:::endfold
// The same cells, with the growth curve centred at 0.12 instead of 0.15.
val lowMu = leniaRuleFromCatalogue(radius = 13, t = 10.0, mu = 0.12, sigma = 0.015)
// At Orbium's 0.62 cells per step, 200 steps is about two laps of the 64-cell torus.
animateEvolution(
orbium.seed(64, 64) to orbium.seed(64, 64),
totalSteps = 200,
seconds = 7.0,
columns = 2,
cellSide = 384,
step = { (a, b) -> a.leniaStep(orbium.rule) to b.leniaStep(lowMu) },
panels = { (a, b), n -> fields(a, b, n) },
)
Orbium's rule is R = 13, dt = 1/10, mu = 0.15, sigma = 0.015, with the polynomial kernel and
growth curve its catalogue entry names. leniaRuleFromCatalogue
builds any rule in that catalogue's terms, and leniaCreatures
reads a whole catalogue file, so the other creatures in Chan's animals.json load the same way.
Why the creature is loaded from Chan's file
Earlier versions of this section showed only noise seeds sorted into died, lived and filled the
field, and said a named creature needs its published array. Noise seeds did not find Orbium in
the trials made for this revision (tapered blobs and square patches under Orbium's mu and
sigma died; wider bells filled the field with fragments). So the array is read from Chan's
animals.json (MIT licensed, excerpted unchanged in afield/alife/lenia_animals.json), rather
than retyped from a paper or from memory.
Moving the growth window
The shape is the creature: shuffle Orbium's 400 values into a random arrangement, keeping its mass
and its histogram, and under the same rule the patch dies, in every one of the six shuffles the box
below tries. And the creature lives in a band of rules, not at one point. Run the
same cells with only mu changed:
from 0.13 to 0.16 Orbium survives and glides. At 0.12 the growth curve rewards thinner neighbourhoods,
and it bursts into a field of about 25 fragments that covers the torus. At 0.17 and above it needs
a denser neighbourhood than it has, and it shrinks and dies. The rows below are 0.12, 0.15 and 0.18,
each at steps 0, 5, 10, 20 and 250.
:::static
val orbium = LeniaPresets.orbium()
for (seed in 1L..6L) {
println(
"shuffled cells, seed " + seed + ": " + leniaRun(orbium.shuffled(seed).seed(64, 64), orbium.rule, steps = 300),
)
}
for (mu in [0.11, 0.12, 0.13, 0.16, 0.17, 0.18]) {
val rule = leniaRuleFromCatalogue(radius = 13, t = 10.0, mu = mu, sigma = 0.015)
println("mu = " + mu + ": " + leniaRun(orbium.seed(64, 64), rule, steps = 300))
}
:::fold three values of mu, the same five moments of each
val moments = [0, 5, 10, 20, 250]
val panels =
[0.12, 0.15, 0.18].flatMap { mu ->
val rule = leniaRuleFromCatalogue(radius = 13, t = 10.0, mu = mu, sigma = 0.015)
var current = orbium.seed(64, 64)
var done = 0
moments.map { step ->
current = current.leniaTimes(rule, step - done)
done = step
(if (step == 0) "mu " + mu else "step " + step) to current.colormap("inferno", 0f, 1f).upscaleNearest(3)
}
}
showImg(
montage(panels, columns = 5, cellSide = 192),
caption = "rows: mu = 0.12, 0.15, 0.18; columns: steps 0, 5, 10, 20 and 250",
)
:::endfold

Lenia without the grid is Particle Lenia: a fixed set of particles carries the same kernel and growth curve, and each particle slides downhill on an energy field, so the mass never changes.
Checking the implementation against Life
Lenia has a limit in which it becomes an ordinary on/off automaton, and that gives a check on the
code. With a kernel that weights the eight neighbours equally, dt = 1, and a growth curve that is
+1 on the birth counts, 0 on survive-only counts and -1 elsewhere, every step lands each cell
exactly on 0 or 1 and Lenia is an outer-totalistic B/S rule. So this file's float convolution and
alife_ca.kt's integer neighbour counting must agree bit for bit.
leniaFromLifeRule builds that limit, and over 20 generations
of a glider the largest per-cell difference is 0.0. Moving the growth window by one neighbour count
makes them disagree completely.
This checks the two update mechanisms against each other, not the rule: leniaFromLifeRule reads the
same B3/S23 tables the integer stepper reads, so a wrong table would give two wrong steppers in
perfect agreement. The rule is checked separately, in section 1, against facts about Life from the
literature: the glider moves (1, 1) every four generations, the block stays still, the blinker has
period 2. The limit also needs birth ⊆ survive, because a count that births a dead cell but kills a
live one cannot be written as a growth curve of the count alone, so leniaFromLifeRule rejects such
a rule rather than building a different automaton.
:::static
val discrete = leniaFromLifeRule(LIFE)
println("discrete-limit kernel: " + discrete.taps.size + " taps summing to " + discrete.taps.weight.sum())
var grid = LifePatterns.glider.centeredOn(32, 32)
var field = grid.toImg(2)
var worst = 0.0
for (gen in 1..20) {
grid = grid.step(LIFE)
field = field.leniaTimes(discrete, 1)
worst = maxOf(worst, fieldMaxAbsDiff(field, grid.toImg(2)))
}
println("20 generations, float Lenia vs integer Life: worst per-cell difference " + worst)
println(
"the glider the float stepper carried moved by " +
translationBetween(LifePatterns.glider.centeredOn(32, 32), field.toIntGrid(0.5f)),
)
// Control: move the growth window by one neighbour count and nothing else.
println(
"CONTROL, growth window at B4/S234: difference " +
fieldMaxAbsDiff(
LifePatterns.glider.centeredOn(32, 32).toImg(2).leniaTimes(leniaFromLifeRule(lifeRule("B4/S234")), 20),
grid.toImg(2),
),
)
:::fold draw both steppers' glider after 20 generations
showImg(
montage(
[
"float Lenia, gen 20" to field.upscaleNearest(8),
"integer Life, gen 20" to grid.toImg(2).upscaleNearest(8),
],
columns = 2,
cellSide = 256,
),
)
:::endfold

Flow Lenia: matter that moves instead of growing
Flow Lenia gives the matter in a cellular automaton somewhere to go. Ordinary Lenia adds growth to the field, so its total mass changes at each step; Particle Lenia keeps mass fixed by using particles. Flow Lenia keeps the grid and moves its mass from cell to cell. The matter carries its own rule with it, so different species can meet and mix in one world.
Each FlowLeniaWorld advances with step(); animateEvolution runs the steps and records the panels shown below.
The model comes from Plantec, Hamon, Etcheverry, Oudeyer, Moulin-Frier and Chan's "Flow-Lenia: Towards open-ended evolution in cellular automata through mass conservation and parameter localization" (ALIFE 2023). Here is how it works:
- The mass is conserved exactly. Growth is no longer added to the field. The same kernels and
growth curves make an affinity
U = Σ h_k G_k(K_k * A), and the matter flows uphill on it,F = (1 − α)∇U − α∇A, whereα = (A/θ)²(capped at 1) pushes matter apart once it piles pastθ = 2. Each cell's matter then moves tox + dt·Fas a small square and is shared among the cells that square lands on, in proportion to the overlap (reintegration tracking). The shares of every cell add up to one, so matter is only ever moved. Over 300 steps of three species the total drifts by about1e-12of itself, which is rounding. - The rule travels with the matter. The kernel weights
hare stored per cell, not per world. When matter moves, itshmoves with it. When matter from two places lands in one cell, the cell'shbecomes their mass-weighted mean, or (the paper's rule) thehof one arriving parcel drawn in proportion to its mass. Several species, each with a differenth, can share one world and one set of kernels, push into each other and blend.
A species is a list of ring kernels, each with its own mu and sigma, and a weight on each.
FlowLeniaPresets holds three found by random search over
the paper's rule space (three to seven kernels of radius 3 to 12). Starting from a disc of noise,
wanderers breaks into a dozen round bodies that drift slowly, rings settles into small hollow
rings that keep apart, and cell grows one large body with a membrane. Seed them side by side in a
96 x 96 world, drawn at three times the resolution (scale = 3: 288 x 288 cells, nine for every
one; Higher resolution says what that changes),
and each takes its own form: the wanderers a dozen round bodies, the rings a cluster of hollow rings
that later breaks into small crosses, and the cell a membrane that pulls apart into beaded chains.
The white line tracks total mass; it stays flat:
:::static
val world = flowLeniaWorld(
288,
288,
[FlowLeniaPresets.wanderers(), FlowLeniaPresets.rings(), FlowLeniaPresets.cell()],
scale = 3,
)
animateEvolution(
world,
totalSteps = 1200,
seconds = 9.0,
columns = 2,
cellSide = 480,
step = { it.step() },
panels = { w, n ->
[
("step " + n) to w.speciesImg().resize(480, 480),
("total mass " + w.totalMass().f3) to w.massPlot(480, 480),
]
},
)
flowLeniaWorld puts every species' kernels side by side in one rule and gives each species weight
only on its own, so a species with nobody near it runs exactly its own rule (the test checks this to
1e-9). Its matter still feels everyone else's through those kernels, which is how species meet.
When species meet
With mixing = FlowLeniaMixing.SAMPLED, a cell takes the parameters of one parcel of the matter
arriving in it, chosen in proportion to mass, so a cell changes hands instead of blending. Then the
mass per species is no longer fixed, and it records who is winning. Here rings (orange) and cell
(cyan) start as two touching discs. For most of the run they trade mass back and forth along their
border, a few tens of units out of about 4000. After step 1000 the rings break into the cell and take
about 400 units of it, nearly a quarter, while the white total stays flat:
:::static
val world = flowLeniaWorld(
288,
192,
[FlowLeniaPresets.rings(), FlowLeniaPresets.cell()],
positions = [38.0 to 32.0, 58.0 to 32.0],
mixing = FlowLeniaMixing.SAMPLED,
scale = 3,
)
animateEvolution(
world,
totalSteps = 1200,
seconds = 9.0,
columns = 2,
cellSide = 480,
step = { it.step() },
panels = { w, n ->
[
("step " + n) to w.speciesImg().resize(480, 320),
("rings %.0f, cell %.0f".format(w.speciesMass(0), w.speciesMass(1))) to w.massPlot(480, 320),
]
},
)
With the default FlowLeniaMixing.MEAN, the same two discs blend instead. Matter that is part rings
and part cell carries an h between the two, and that h is a rule neither species had. On the
right below is the parameter field itself: weightImg(0), the weight on the rings' first kernel at
every cell holding matter. It starts as two flat colours. By step 1200 about half the matter is a
hybrid (mixedMass(), matter no species holds 90 % of: 2051 of 4023 units). The rings' main body is
almost pure rings, two smaller bodies sit between the two rules, and the cell's beaded chains grade
from the cell's own weight towards the rings'. The species colours on the left blend the same way.
:::static
val world = flowLeniaWorld(
288,
192,
[FlowLeniaPresets.rings(), FlowLeniaPresets.cell()],
positions = [38.0 to 32.0, 58.0 to 32.0],
scale = 3,
)
animateEvolution(
world,
totalSteps = 1200,
seconds = 9.0,
columns = 2,
cellSide = 480,
step = { it.step() },
panels = { w, n ->
[
("step " + n) to w.speciesImg().resize(480, 320),
("hybrid %.0f of %.0f".format(w.mixedMass(), w.totalMass())) to
w.weightImg(0).colormap("viridis", 0f, 0.4f).resize(480, 320),
]
},
)
Here the species share a fixed supply of mass and carry their rules with them. As they collide and mix, the creatures explore the rule space themselves. Compare this with the outer search loop in section 11.
Higher resolution: the same world in more cells
scale = 3 on flowLeniaWorld seeds the world at a third of its size and then grows it with
scaledCopy(3). Every cell becomes a 3 x 3 block, and every length in the rule triples: the kernel
radii, the square each cell's matter is spread over when it lands, and the largest step. dt grows
nine times, because the gradient of a pattern three times as large is a third as steep. Matter then
moves the same fraction of a creature per step as before, and each creature is drawn with nine times
as many cells. Use scaledCopy(k) on a world that is already running to find something in a
small, fast world and then watch it at a larger scale.
Here the three species run twice, side by side, from the same seed. On the left is the 96 x 96
world, each cell drawn as a 5 x 5 block of pixels. On the right is the same start at scale = 3,
in a 288 x 288 world:
:::static
val species = [FlowLeniaPresets.wanderers(), FlowLeniaPresets.rings(), FlowLeniaPresets.cell()]
val small = flowLeniaWorld(96, 96, species)
val large = flowLeniaWorld(288, 288, species, scale = 3)
animateEvolution(
small to large,
totalSteps = 1200,
seconds = 9.0,
columns = 2,
cellSide = 480,
step = { (a, b) ->
a.step()
b.step()
a to b
},
panels = { (a, b), n ->
[
("96 cells, step " + n) to a.speciesImg().upscaleNearest(5),
("288 cells, step " + n) to b.speciesImg().resize(480, 480),
]
},
)
How close the large world stays to the small one depends on the species. Part of Flow Lenia's structure is set by the grid rather than the kernels, so the match is never cell for cell:
wandererscome out as the same dozen round bodies. At twice the resolution, 20 steps after the copy, the large world averaged back down matches the small one with a cosine similarity of 0.95. Leaving the spread at one cell instead gives 0.86 and covers every body with a one-cell speckle.ringsandcellkeep their character but not their exact forms. At three times, the rings settle as one cluster of rings that later breaks into crosses, and the cell's membrane becomes chains.- The crescent glider below covers three times the cells per step at three times the resolution, so its speed in body lengths is unchanged. At twice the resolution it covers 1.44 times the cells rather than 2.
At higher resolution, convolution has more cells to cover for every tap of every kernel.
FlowLeniaConvolution.AUTO computes affinity with whichever of two methods is cheaper for the
current state. SPARSE scatters each occupied cell through every tap.
FFT multiplies in the frequency domain and costs the same whatever the kernel radius. Any side
length works, and sides built from small primes, such as 96 or 288, are fast. The two agree to
1e-14. The three species above take about 9 ms a step at 96 x 96 and about 150 ms at 288 x 288.
Bodies that travel
bodies() splits the matter into connected pieces, joining them across the edges of the torus. It
gives each piece its mass, centroid and main species. trackLargest(steps) follows the largest body
as the world runs, returning its path and mass over time. The path is unwrapped, so it continues
across an edge instead of jumping back. FlowLeniaTrack.speed is the straight-line distance per
step, and massKept is the smallest mass over the run divided by the first.
FlowLeniaPresets.crescent() is a rule from a random search of 900 rules scored that way. From a small
noise disc it settles into one of two bodies. With noise seed 2 it folds into a crescent that travels
convex side first at about 0.02 cells per step and keeps 99.6 % of its mass. From most other seeds it
settles into a round body that stays put. Here the crescent forms on a 64-cell torus, is grown to
three times the resolution, and runs for 3000 steps. On the right, each drawn frame is kept as a
fading exposure, which shows the path:
:::static
// Noise seed 2 folds into the crescent; grow it to three times the resolution once it has formed.
val world =
flowLeniaWorld(64, 64, [FlowLeniaPresets.crescent()], seedRadius = 6.0, randomSeed = 2L).steps(200).scaledCopy(3)
val trail = FloatArray(world.width * world.height)
animateEvolution(
world,
totalSteps = 3000,
seconds = 9.0,
columns = 2,
cellSide = 480,
step = { it.step() },
panels = { w, n ->
val b = w.bodies().first()
for (c in trail.indices) trail[c] = maxOf(0.97f * trail[c], w.matter[c].toFloat())
[
("step %d: mass %.1f at (%.0f, %.0f)".format(n, b.mass, b.x, b.y)) to w.speciesImg().resize(480, 480),
"the path" to Img(w.width, w.height, 1, trail.copyOf()).colormap("magma", 0f, 1.2f).resize(480, 480),
]
},
)
Searching for travellers
FlowLeniaGenome lets you search for species with the same PopulationSearch used in the
Lenia rule search. A random draw comes from the paper's rule space,
where the presets came from too. There are three mutations: nudge moves every mu, sigma and weight a
little, rekernel redraws one kernel, and addOrDrop adds or removes one. Crossover takes each kernel,
with its weight, from one parent or the other. flowLeniaGliderObjective scores the tracked body's
speed times the square of its massKept. It scores zero for a film covering more than 40 % of the
world, because the centroid of such a film can drift without anything travelling. It averages over
seeds noise seeds, so a rule only scores well if it travels from most starts. code() prints a
species as the Kotlin that builds it, ready to paste into a box.
val ops = FlowLeniaGenome()
val objective = flowLeniaGliderObjective(size = 64, warmup = 200, window = 200, seedRadius = 6.0, seeds = 3)
val climb =
PopulationSearch.hillClimb(ops, objective, 100, seed = 7L, patience = 1000, start = FlowLeniaPresets.crescent())
val control = PopulationSearch.random(ops, objective, 100, seed = 7L)
println(climb.summary())
println(control.summary())
println(control.best!!.genome.code())
That run takes about six minutes, so it is shown here rather than run on the page. The crescent
starts at 0.0069, because it travels from one seed in three. Climbing from it for 100 evaluations
reached 0.036. The climb's best travels at 0.098 cells per step from seed 2 but loses about 45 % of its
mass from the other two. The random control, at the same budget, did better: 0.042. Its winner is
FlowLeniaPresets.dart(), a small hooked body that crosses the 64-cell torus at 0.10 cells per step
and keeps 99.6 % of its mass, five times the crescent's speed, after first shedding two rings that
stay where they are.
The dart does not survive a larger grid
At twice the resolution the dart nearly stops: 0.012 cells per step in the small world's cells, against 0.10. It is only about seven cells across, and its motion depends on effects at the scale of a single cell. To find travellers for a large world, search at that resolution, or grow the ones you find and check that they still move, as the crescent does.
Evolution inside the world
When species meet and blend, their rules mix. To introduce new rules as the world runs, set
world.mutation = FlowLeniaMutation(rate, radius, scale). On average
rate times a step, a disc of radius cells, centred on a cell picked in proportion to its matter,
has the same random offset added to its kernel weights. The offset is Gaussian with standard deviation
scale per kernel, and it is not clipped: a weight that goes negative makes that kernel push matter
away instead of drawing it in. Each mutated disc becomes a new lineage. Every cell carries the
lineage of its matter. Under SAMPLED mixing that is the lineage of the parcel it took its
parameters from, and under MEAN it is the lineage of the largest parcel that arrived.
lineageMasses(), lineageImg() and lineagePlot() read the lineages back, and lineages records
each one's parent, birth step and starting weights.
scale = 0 is the control. It cuts new lineages without changing any weight, so under MEAN mixing
the matter is bit for bit what it would have been with no mutation at all (the test checks this),
and any change in who holds the mass is drift. Here both worlds start as the three species at twice
the resolution under SAMPLED mixing, with the same mutation rate. On the left the mutations are
neutral. On the right they have size 0.3. Each world is coloured by lineage, with its mass per species
plotted underneath and printed in the caption (wanderers, rings, cell):
:::static
val species = [FlowLeniaPresets.wanderers(), FlowLeniaPresets.rings(), FlowLeniaPresets.cell()]
val neutral = flowLeniaWorld(192, 192, species, mixing = FlowLeniaMixing.SAMPLED, scale = 2)
neutral.mutation = FlowLeniaMutation(rate = 0.05, radius = 12.0, scale = 0.0)
val mutating = flowLeniaWorld(192, 192, species, mixing = FlowLeniaMixing.SAMPLED, scale = 2)
mutating.mutation = FlowLeniaMutation(rate = 0.05, radius = 12.0, scale = 0.3)
fun masses(w: FlowLeniaWorld) = w.speciesMasses().joinToString(" ") { it.f0 }
animateEvolution(
neutral to mutating,
totalSteps = 2400,
seconds = 9.0,
columns = 2,
cellSide = 384,
step = { (a, b) ->
a.step()
b.step()
a to b
},
panels = { (a, b), n ->
[
("neutral " + masses(a)) to vstack(a.lineageImg().resize(384, 384), a.massPlot(384, 128)),
("size 0.3 " + masses(b)) to vstack(b.lineageImg().resize(384, 384), b.massPlot(384, 128)),
]
},
)
With neutral mutations the three species hold their ground: 1069, 1010 and 794 units at step 600, and 1178, 892 and 803 at step 2400. With mutations of size 0.3 the wanderers' descendants take over: by step 2400 they hold 1851 units, and the cell, which started with 739, is down to 114. The total is the same in both worlds throughout. In the picture, much of the mutating world ends up as a faint haze between a few surviving bodies: most large changes to the weights produce a rule that holds nothing together. The same comparison at 96 cells, for three noise seeds over 3000 steps, went the same way each time. Neutral mutation moved no species' mass by more than about 20 %. Mutation of size 0.3 drove one species close to extinction in all three seeds, leaving 27, 77 and 38 of the 198 to 261 units it started with: the cell in two seeds, the rings in the third. Which species wins depends on where the useful mutations land: the world is selecting among mutants.
Use paintWeights(cx, cy, radius, h) to give the matter in a disc new weights by hand. The matter
stays in place and becomes a lineage of its own. The other form takes a function
of each cell and its current weights, which is how mutate is built.
How the species were found, and what did not work
The first version used the ordinary Lenia gradient scale (central differences halved) and a single
ring kernel; every creature it made sat still. The paper's Sobel filter is unnormalised, which
makes the flow eight times stronger, and with it and several kernels per species roughly one
random rule in ten gives compact bodies, a few of which move. The search ran in a numpy prototype
of the same step (dev_docs/ALIFE.md, Flow Lenia); the first three presets are three of its rules
copied exactly, and the Kotlin version reproduces the same behaviour from a different noise seed.
The crescent came from a second numpy search of 900 rules scored like
flowLeniaGliderObjective, and the dart from the Kotlin random search above. FlowLeniaTest
pins conservation (total under both mixing rules, each species under MEAN, and with mutation),
the sparse convolution against a dense one and against the FFT, the zero-weight-kernel
equivalence, the 2x rescaling, seam-crossing bodies, the dart's and the crescent's motion, and
that a mutation of size zero leaves the matter bit for bit unchanged. The clips were first
captured at 96 cells shown four times larger; the FFT path is what made three times the
resolution affordable.
6. Boids
Reynolds' three rules — separation, alignment, cohesion — on a torus. The domain wraps for a reason beyond tidiness: in a bounded box the walls do half the flocking, because agents that bounce off the same wall end up parallel, so a polarization measured in a box is not evidence about the rules.
A flock is two things at once, and both are measured here:
- Aligned. The order parameter \( \lvert \sum \hat{v} \rvert / N \) is 0.692 after 900 steps. That needs a scale, so
randomPolarizationBaselinegives it: \( \sqrt{\pi} / 2\sqrt{N} \) is the expected value forNindependent headings, which for 400 agents is 0.044. The measured value is 15.6× that. And the same initial state withalignmentWeight = 0, nothing else changed, reaches 0.073. - Spaced. The 5th-percentile nearest-neighbour distance is 1.37 cells; with
separationWeight = 0it collapses to 0.01. The floor under that distribution is the separation term, and you can see the control arm's population has collapsed to points.
0.692 rather than something near 1 is the correct answer, not a weak one: the population settles into
several separate flocks with different headings (visible in the figure), and the global order parameter
of k independent flocks tops out near \( 1/\sqrt{k} \).
A stalled flock can still score well on alignment
The three steering terms are written as desired-minus-current velocities, so in a settled flock
they largely cancel and, under a force cap, the speed drifts downward: without a floor it lands near
0.46 against a cap of 1.4, which is a flock that has mostly stopped while still looking aligned.
BoidParams.minSpeedFraction holds that floor, and it changes magnitude only and never heading, so
it cannot inflate the polarization number.
:::static
/*
Reynolds' boids on a torus. A flock is not "dots that move together"; it is a population whose
alignment order parameter is far above what independent headings would give, AND whose
nearest-neighbour distribution has a floor. Both are measured against the same initial state run
with one weight zeroed -- so the difference is attributable to that weight and nothing else.
*/
val n = 400
val start = BoidFlock.random(n, 260.0, 260.0, speed = 1.4, seed = 4L)
val params = BoidParams()
println("random-heading baseline for " + n + " agents: " + randomPolarizationBaseline(n).f3)
println("initial polarization: " + start.order().f3)
val flock = start.copy()
flock.stepTimes(params, 900)
val noAlign = start.copy()
noAlign.stepTimes(params.copy(alignmentWeight = 0.0), 900)
val noSep = start.copy()
noSep.stepTimes(params.copy(separationWeight = 0.0), 900)
println(
"flock polarization " + flock.order().f3 + ", CONTROL with no alignment " + noAlign.order().f3,
)
println(
"5th-percentile nearest neighbour: flock " +
quantileOf(flock.neighborDistances(), 0.05).f2 +
", CONTROL with no separation " +
quantileOf(noSep.neighborDistances(), 0.05).f2,
)
println("mean speed " + flock.meanSpeed().f2 + " of a " + params.maxSpeed + " cap")
:::fold draw the four populations with a five-unit motion streak behind each agent
showImg(
montage(
[
"gen 0: order " + start.order().f2 to start.toImg(1.4, tail = 5.0),
"900 steps: " + flock.order().f2 to flock.toImg(1.4, tail = 5.0),
"no alignment: " + noAlign.order().f2 to noAlign.toImg(1.4, tail = 5.0),
"no separation" to noSep.toImg(1.4, tail = 5.0),
],
columns = 2,
cellSide = 300,
),
)
:::endfold

The control arm, running beside it
The two numbers above, 0.692 and 0.073, are the same initial state with one weight changed. Both panels
are the identical 400 agents from the identical seed; the right one has alignmentWeight = 0 and nothing
else is different. 900 steps in seven seconds, six or seven per displayed frame, with the order parameter
in each caption recomputed every frame.
What to watch: the left panel's flocks are not there at the start and nobody puts them there. Within about a hundred steps local alignment has produced a handful of coherent groups, and after that the interesting event is the rare one: two flocks meeting and either merging or passing through. That is why the order parameter settles near 0.69 rather than climbing to 1: it is the global average over several flocks that each individually agree, and the caption's number wobbles as they merge and split. On the right, the population stays a gas forever. It is moving, since the speed floor is still on; it just never agrees about anything.
The tail is drawn longer here (8 units) than in the still (5) and the domain is drawn at one pixel per
unit rather than 1.4. Both are frame-rate accommodations: at six or seven steps per displayed frame an agent
covers about nine units between frames, so a short tail leaves visible gaps. Neither changes anything
measured, because order() reads the velocity array and not the picture.
:::static
:::fold draw both populations, and measure the order parameter every frame
/*
One pixel per domain unit, with an 8-unit motion streak behind each agent -- long enough to join
up at nine units of travel per frame. `order()` reads the velocity array, so neither choice
touches what is measured; the number in each caption is computed fresh every frame.
*/
fun flocks(a: BoidFlock, b: BoidFlock, n: Int) = [
"flock " + n + ": order " + a.order().f2 to a.toImg(1.0, tail = 8.0),
"no align " + n + ": " + b.order().f2 to b.toImg(1.0, tail = 8.0),
]
:::endfold
/*
The same initial state, twice, stepped together -- so the ONLY difference between the two panels
is the alignment weight. 900 steps over 140 displayed frames: six or seven steps per frame, and
no ramp -- a flock's rate of change is roughly constant, which is exactly when linear is right.
*/
val params = BoidParams()
val blind = params.copy(alignmentWeight = 0.0)
val start = BoidFlock.random(400, 260.0, 260.0, speed = 1.4, seed = 4L)
animateEvolution(
start.copy() to start.copy(),
totalSteps = 900,
seconds = 7.0,
columns = 2,
cellSide = 300,
step = { (a, b) ->
a.step(params)
b.step(blind)
a to b
},
panels = { (a, b), n -> flocks(a, b, n) },
)
7. Physarum
Jones' slime-mould model. Each agent deposits into a trail field, samples that field three ways ahead, turns toward the strongest, and moves. The network is not stored anywhere; it is the fixed point of that stigmergic loop.
The measurement is topShare(0.05): the fraction of all deposit held by
the brightest 5 % of pixels. A flat field gives 0.05 by definition. The sensing population gives
0.480; the identical population with sensing switched off (same count, same seed, same deposit,
same decay, random turns) gives 0.185. And the shapes differ as sharply: median isoperimetric ratio
17.2 against 1.2, filaments against blobs.
Mutual exclusion is the whole model, not an optimisation
Without Jones' one-agent-per-cell rule there is nothing to stop every agent within sensing range of
the brightest trail converging on it, and they do: 6000 agents collapse onto a 191-cell streak and
measure a top-5 % share of 1.000, which reads as perfect concentration and is total failure.
PhysarumParams.exclusive makes the population incompressible, so a trail can only get so strong
before it has to widen or branch, and branching under a density limit is what a transport network
is.
:::static
/*
Jones' Physarum model: agents deposit into a trail field, sense it three ways ahead, turn toward
the strongest, move. The transport network is nowhere in the code -- it is the fixed point of
that loop.
The measurement: `topShare(0.05)` is the fraction of all deposit held by the brightest 5% of
pixels. A flat field gives 0.05. A network concentrates material into filaments and gives much
more. The control is the identical population with sensing switched off -- same count, same
seed, same deposit, same decay, random turns.
*/
val side = 256
val p = PhysarumParams()
val sensing = PhysarumSwarm.disc(side, side, 6000, 0.55, seed = 6L).stepTimes(p, 400, seed = 6L)
val blind = PhysarumSwarm.disc(side, side, 6000, 0.55, seed = 6L).stepTimes(p.copy(sensing = false), 400, seed = 6L)
println(
"top-5% deposit share: sensing " +
sensing.trail.topShare(0.05).f3 +
", CONTROL sensing off " +
blind.trail.topShare(0.05).f3 +
" (a flat field gives 0.05)",
)
println("sensing morphology: " + sensing.trail.normalized().morphology())
println("blind morphology: " + blind.trail.normalized().morphology())
:::fold draw both trail fields in magma
showImg(
montage(
[
"sensing" to sensing.trail.normalized().colormap("magma"),
"CONTROL: sensing off" to blind.trail.normalized().colormap("magma"),
],
columns = 2,
cellSide = 340,
),
)
:::endfold

Watching the network find itself
topShare climbing from 0.05 to 0.48 is the evidence; the clip is what the number is about. 400 steps
in seven seconds, three per displayed frame, both populations stepped together, with the top-5 % share
recomputed and printed every frame.
Three things to watch, and the third is the one worth waiting for:
- The disc expands, in both panels. That is not the network; that is 6000 mutually exclusive agents needing room.
- In the sensing panel the expansion goes anisotropic almost immediately: some directions get reinforced and the rest go dark. Nothing chose those directions. They are wherever the deposit happened to be a little stronger when the agents first looked.
- Then the interior reorganises: trails that were separate merge, and loops close and re-open. The share number keeps climbing while the shape keeps changing, because the network is a fixed point of the loop rather than something that gets built and then stops.
The blind panel is the same 6000 agents with sensing = false, so it expands and then just sits there,
uniform, at a share that stays near a fifth of the sensing run's.
The colour range is deposit / (1 − decay)
The still above uses normalized(), which is right for one figure and wrong for a clip in two ways at
once: a per-frame maximum makes an empty early field look exactly as bright as a settled network, and
it normalises the two panels separately, so the control arm is stretched to fill the same range as
the thing it is a control for.
The trail field is unbounded above, so there is no natural hi to pick, but there is a derivable one.
A cell that an agent occupies on every step accumulates deposit and keeps decay of what it had, so
it converges to deposit / (1 − decay) = 5 / 0.08 = 62.5, and with blurRadius = 1 a cell in the
interior of a saturated filament is surrounded by cells at the same value, so the blur does not reduce
it. That is the ceiling, it comes from the parameters, and both panels are drawn on it. Guess a lower
ceiling and every filament sits over the top of the range: the network renders as flat cream with holes
in it, and the structure the section is about is clipped away.
:::static
:::fold draw both trails on one derived colour range, and measure the share every frame
/*
Both panels on ONE derived range, not on each other's maxima: a cell an agent occupies every
step accumulates `deposit` and keeps `decay` of what it had, so it converges to
deposit / (1 - decay) = 62.5. A lower ceiling clips the network to flat cream. The caption's number is
`topShare(0.05)`, recomputed every frame.
*/
val p = PhysarumParams()
val ceiling = (p.deposit / (1.0 - p.decay)).toFloat()
fun trails(a: PhysarumSwarm, b: PhysarumSwarm, n: Int) = [
"sensing " + n + " " + a.trail.topShare(0.05).f3 to a.trail.colormap("magma", 0f, ceiling),
"blind " + n + " " + b.trail.topShare(0.05).f3 to b.trail.colormap("magma", 0f, ceiling),
]
:::endfold
/*
Sensing and blind, from the same seed, stepped together: 400 steps over 140 displayed frames,
so three deposit-sense-move rounds per frame. `step` takes its rng explicitly, so each
population gets its own stream -- exactly what `stepTimes(p, 400, seed = 6L)` does internally,
which is why these panels land on the same numbers as the figure above.
*/
val rngA = kotlin.random.Random(6L)
val rngB = kotlin.random.Random(6L)
animateEvolution(
PhysarumSwarm.disc(256, 256, 6000, 0.55, seed = 6L) to PhysarumSwarm.disc(256, 256, 6000, 0.55, seed = 6L),
totalSteps = 400,
seconds = 7.0,
columns = 2,
cellSide = 300,
step = { (a, b) -> a.step(p, rngA) to b.step(p.copy(sensing = false), rngB) },
panels = { (a, b), n -> trails(a, b, n) },
)
A million agents on the GPU
The same rule runs on the GPU as ShaderPhysarumSystem: a million agents on a trail 512 pixels high, each
one a single vertex-shader invocation per frame that senses, turns, steps and deposits. The defaults are
the PhysarumParams defaults above. The trail is a TrailField, a stigmergy
field you can use on its own: four channels, each with its own half-life, plus diffusion and wind. Any
module can deposit into it, and any shader can read it with #include "trail/sample". Here there are
two species, one per channel. Each follows its own trail and avoids the other's:
// 512 pixels high and as wide as the window's shape needs, so the trail is not stretched
var physarum = ShaderPhysarumSystem() {
height = 512
width = 512 * window.width / window.height
agentCount = 1_000_000
speciesCount = 2
exposure = 0.05f
}
// steps once a frame and draws the trail, one colour per species
physarum.installInto()
A million agents scattered as noise, drawing themselves into two networks:
These agents do not exclude each other. On the GPU the density does the job that exclusion does on the
CPU: at one agent per pixel, a trail that pulls in more agents also fills up. Read back after 600 frames,
the trail gives topShare(0.05) = 0.75 with one species. The same agents with sensing = false give
0.065, close to the flat 0.05. The settings are read every frame, so you can change
physarum.trail.settings.windPx or physarum.settings.jitter while it runs.
Where the GPU numbers come from
The smoke test alife.physarum-gpu.smoke (PhysarumGpuSmoke.kt) runs a million agents on a real
renderer and reads the trail back as floats through TrailField.read(). It measured 0.752 for the
sensing run and 0.065 for the blind one, at about 8.4 ms a frame on an Apple-silicon Mac. It is not
the CPU experiment at higher resolution: the CPU uses 6000 agents with exclusion, starts them in a
disc, and measures at step 400. So 0.75 against 0.48 compares two different setups, not two
implementations. The test also checks that a deposit lands on the pixel it is sensed from. An early
version wrote every deposit to the mirrored row, and the network grew mirror-symmetric in both
axes. The four-box version, with live wind and reset boxes, is the transcript
dev_docs/agents/transcripts/gpu_physarum.yaml.
Several species on one field
Each species senses the trail through one row of an attraction matrix: attraction[s] weighs the four
channels as species s reads them, positive to follow and negative to avoid. Here three species each
follow their own channel and avoid the other two, with Jones' exclusion on and little diffusion, so the
trails stay sharp:
prop<ShaderPhysarumSystem>(physarum) = ShaderPhysarumSystem() {
width = 1024
height = 1024
agentCount = 300_000
speciesCount = 3
exclusive = true
attraction = [
vec(1f, -2f, -2f, 0f),
vec(-2f, 1f, -2f, 0f),
vec(-2f, -2f, 1f, 0f),
]
}
physarum.trail.settings.diffusion = 0.2f
physarum.installInto()
The video shows one species on the left and three on the right. The rule, the density and the exclusion are the same on both sides, and both start from an even scatter. On its own, a species closes into a net of cells: roads meet at junctions and enclose empty space. Three species that avoid each other cannot cross, so there are no junctions between colours. They pack the field into an interlocking labyrinth instead, each road running alongside roads of the other colours, and the borders keep shifting as one species pushes into another's ground.
Any other particle system can steer by the same field. TrailFollowers climbs the gradient of
dot(trail, weights), so these 20,000 particles ride the amber roads and keep off the cyan ones:
var followers = TrailFollowers(physarum.trail) {
count = 20_000
weights = vec(1f, -1f, 0f, 0f)
}
followers.installInto()

Read back after 600 frames, a pixel's brightest channel holds at least 80 % of its trail on 99.4 % of the marked pixels with two species, and 86.6 % with three. When every species is attracted by every channel alike, the same agents share one network and the figure is 0. A follower reads the field and never writes it; in the running demo 97 % of the followers stand on amber-dominant pixels, which cover 42 % of the field.
Two more settings. depositInto[s] is what one agent of species s adds to each channel, so one
species can lay another's scent: with vec(0.5f, 1f, 0f, 0f) for species 1, the cyan agents mark the
amber channel too, and an amber row of vec(1f, -2f, 0f, 0f) makes the amber species flee them.
exclusive = true is Jones' one-agent-per-cell rule on the GPU. A step into a pixel another agent
holds this frame is refused, and the agent turns to a random heading. At Jones' density of about
15 %, it turns the roads into a branching cellular net.
On the page's own experiment, 6000 agents in a disc on 256 x 256 for 400 steps, the GPU with exclusion
gives topShare(0.05) = 0.546 and the CPU gives 0.484. The blind controls give 0.222 and 0.185.
Without exclusion both collapse, to 0.998 and 1.000.
Where the multi-species numbers come from
alife.physarum-gpu.smoke (PhysarumGpuSmoke.kt) runs every figure above on a real renderer and
reads the trail back with TrailField.read(). Segregation is measured on 250,000 agents on 512 x 512.
It is the share of pixels holding more than half the mean trail where one channel holds 80 % of it.
The GPU exclusion is approximate: claims race within a frame, and an agent holds its old pixel for
the frame it leaves it. The CPU frees a cell as soon as its agent moves. That is the likeliest
reason the GPU's figure sits above the CPU's. The follower figure comes from a probe in the
gpu_stigmergy demo transcript, dev_docs/agents/transcripts/gpu_stigmergy.yaml, whose chase box
sets the deposit and attraction matrices while it runs.
Designing a network
Tero and colleagues grew Physarum polycephalum over oat flakes placed like the cities around Tokyo ("Rules for biologically inspired adaptive network design", Science 327, 2010). The mould joined the flakes with a network of tubes that compared well with the region's rail system on three numbers: what it costs (total length), how direct a trip between two cities is, and how likely one broken link is to cut a city off. Jones' agents grow the same kind of network when the food feeds their trail.
PhysarumFood is a list of points that add to the trail every step. It also gives the field an
edge: an agent that walks off it comes back in at a random food source. Without an edge the agents
would cover a torus with roads that join nothing. With it, the agent count becomes a budget of road,
because once every agent is on a road, the total length of road is set by how many agents there are.
TransportNetwork.extract reads the network back from the trail. It thresholds the trail, thins the
roads to lines one pixel wide, and turns those lines into a graph whose nodes are the food sources and
the junctions between them. score then gives Tero's three numbers:
- cost: total length, as a multiple of the minimum spanning tree (MST), the cheapest network that joins every source;
- stretch: how much longer the route between two sources is than the straight line, averaged over every pair;
- fault tolerance: the chance that one break, at a random point of the network, leaves every source still connected. A tree scores 0; a network where every road lies on a loop scores 1.
The example below grows networks between eight food sources with two budgets, 1600 and 4800 agents, and with the blind control from the start of this section. It scores each one beside the MST and the Delaunay triangulation of the same eight points:
:::static
/*
Eight food sources on a 200 x 200 field. The trail is averaged over the last 300 of 3000 steps,
because a single step's trail shows roads that are fading for a moment as breaks. A road is any
cell at a quarter of the level a cell reaches when an agent stands on it every step.
*/
val food = PhysarumFood.scattered(200, 200, 8, seed = 3L)
val p = PhysarumParams()
val road = 0.25 * p.deposit / (1 - p.decay)
fun grow(label: String, agents: Int, params: PhysarumParams) =
label to
PhysarumSwarm.random(200, 200, agents, seed = 5L)
.meanTrail(params, 3000, averageOver = 300, seed = 5L, food = food)
val trails = [
grow("1600 agents", 1600, p),
grow("4800 agents", 4800, p),
grow("CONTROL: blind", 2400, p.copy(sensing = false)),
]
val networks = trails.map { (label, trail) -> label to TransportNetwork.extract(trail, food.points, road) }
val controls: List<NetworkScore> = [
TransportNetwork.mst(food.points).score("MST"),
TransportNetwork.delaunay(food.points).score("Delaunay"),
]
markdown(
"scores",
"```\n" + NetworkScore.table(networks.map { (label, net) -> net.score(label) } + controls) + "\n```",
width = 560,
)
:::fold draw each network over its trail
showImg(
montage(
networks.zip(trails).map { (n, t) ->
n.first to n.second.drawOver(t.second.colormap("magma", 0f, (road * 4).toFloat()))
},
columns = 3,
cellSide = 300,
),
)
:::endfold

With 1600 agents the network costs 1.27 times the MST and has one loop, which already carries most of its length, so its fault tolerance is 0.89. Its stretch is 1.49, against 1.44 for the MST. With 4800 agents it costs 2.54 times the MST and has eight loops. Its fault tolerance is 1.00 and its stretch falls to 1.26. The Delaunay triangulation costs 3.31 times the MST for a stretch of 1.05. The blind control joins 4 % of the source pairs: the sources are bright because they are fed, but nothing carries the trail between them.
The range of networks: a sweep
Tero compared networks on a plane: cost across, fault tolerance up. A tree sits at the bottom left, at cost 1 and fault tolerance 0, and a dense mesh at the top right. The sweep below varies one parameter at a time about the defaults: the agent count (the budget of road), the decay (how quickly an unused road fades), the sensor angle and the blur. Each run that joins every source is a dot on the plane; the table lists every run, including the ones that break apart:
:::static
:::fold the same eight sources, and a run that grows and measures one network
val food = PhysarumFood.scattered(200, 200, 8, seed = 3L)
fun run(label: String, agents: Int, p: PhysarumParams): NetworkScore {
val trail =
PhysarumSwarm.random(200, 200, agents, seed = 5L).meanTrail(p, 3000, averageOver = 300, seed = 5L, food = food)
return TransportNetwork.extract(trail, food.points, 0.25 * p.deposit / (1 - p.decay)).score(label)
}
:::endfold
val base = PhysarumParams()
val arms = [
"1600 agents" to (1600 to base),
"2400 agents" to (2400 to base),
"3200 agents" to (3200 to base),
"4800 agents" to (4800 to base),
"decay 0.85" to (2400 to base.copy(decay = 0.85)),
"decay 0.96" to (2400 to base.copy(decay = 0.96)),
"sensors 22.5 deg" to (2400 to base.copy(sensorAngle = Math.PI / 8)),
"sensors 67.5 deg" to (2400 to base.copy(sensorAngle = 3 * Math.PI / 8)),
"no blur" to (2400 to base.copy(blurRadius = 0)),
"blur 2" to (2400 to base.copy(blurRadius = 2)),
]
// every run is independent, so run them side by side
val scores = arms.parallelStream().map { (label, a) -> run(label, a.first, a.second) }.toList()
val joined = scores.filter { it.connectedPairs > 0.99 }
val broken = scores.filter { it.connectedPairs <= 0.99 }.joinToString { it.label }
markdown("scores", "```\n" + NetworkScore.table(scores) + "\n```\nbreaks apart: " + broken, width = 560)
val controls: List<NetworkScore> = [
TransportNetwork.mst(food.points).score("MST"),
TransportNetwork.delaunay(food.points).score("Delaunay"),
]
showImg(
costTolerancePlot(
controls + joined,
width = 560,
height = 380,
costRange = 0.8..3.6,
),
)

Every network that joins all eight sources sits high on the plane, at fault tolerance 0.77 or more: the agents close loops readily. The budget moves a network along the top, from 1.27 times the MST with 1600 agents to 2.54 with 4800, and the stretch falls from 1.49 to 1.26 as the loops add shortcuts. The other parameters mostly decide whether there is a network at all. A trail that fades faster (decay 0.85) gives more loops from the same 2400 agents, 1.91 times the MST against 1.72. One that lingers (decay 0.96) lets the roads wander, and two sources fall off. Narrow sensors (22.5°) give the cheapest tolerant network at the default budget, 1.41 times the MST at fault tolerance 0.77. At 67.5° the agents circle where they are and the network falls into short pieces, and with no blur, or a blur of radius 2, it breaks as well.
None of these runs comes down to a tree. The cheapest network that joins every source still has a loop. With fewer agents the network breaks before it thins to a tree.
Drawing the food
On the GPU the same food is ShaderPhysarumSettings.food, a list of points read every frame. That
makes the drawing layer a map editor. Draw a frame named "network" (⌥+D), put dots in it, and the
box below reads them through notation as food sources in trail pixels.
notation.onChange hands every edit back to the agents, so dragging a dot moves the food while the
network grows:
val side = 512
// the frame, read as trail pixels: x across, and y up, as the window draws the field
val map = notation.space("network", x = 0.0..side.toDouble(), y = side.toDouble()..0.0)
fun dots() = map.marks.filter { it.kind == MarkType.ELLIPSE }.map { map.toSpace(it.center) }
prop<ShaderPhysarumSystem>(physarum) = ShaderPhysarumSystem() {
width = side
height = side
agentCount = 4000
exclusive = true
recycle = true
food = dots()
}
physarum.installInto()
// every edit to the drawing moves the food
notation.onChange { physarum.settings.food = dots() }
recycle = true is the GPU form of the field's edge. The video starts from 4000 agents scattered at
random over twelve dots. Four seconds in, a thirteenth dot is drawn at the top right and the network
grows a road to it:
On the GPU, with fourteen food sources on a 512 x 512 field, the same sweep lands the same way: 3000 agents cost 1.36 times the MST at fault tolerance 0.71, 4000 cost 1.79 at 0.99, and 12 000 cost 2.58. With 2000 agents only 62 % of the source pairs are joined. At 90° the sensors gather the agents into isolated spots and no road forms, and the blind control builds no road at all.
Where the network numbers come from
PhysarumNetworkTest pins the CPU numbers above and checks the extraction itself on drawn roads:
a triangle of roads with a dead-end spur reads back as three links and one loop within 6 % of its
perimeter, and a three-armed star finds its junction within 4 pixels (and costs 0.86 times the
MST, because a junction away from the sources is cheaper than any tree through them). The GPU
sweep varies the trail's half-life and diffusion too: a half-life of 4 frames gives 1.66 times the
MST at fault tolerance 0.96, 16 frames joins 62 % of the pairs and 32 frames none, and diffusion
0.25 joins 86 %. Each GPU network is measured on ten readbacks across the last 300 of 6000
frames, with all fifteen configurations stepped in the same frames at 9.3 ms a frame. Measuring
one step's trail, rather than the average over 300 steps, made the result depend on the threshold:
the 3200-agent network read as joining 75 % of its source pairs at one threshold and all of them
at another, because a faint road that is fading for a step reads as a break. The GPU numbers come
from the smoke test alife.physarum-networks-gpu.smoke (PhysarumNetworkGpuSmoke.kt), which also
checks that a food source deposits on its own pixel. The starting-point document, with a measure
box that scores the live network every 120 frames and a slider for the trail's half-life, is the
transcript dev_docs/agents/transcripts/gpu_physarum_networks.yaml.
8. Diffusion-limited aggregation
Diffusion-limited aggregation: a seed at the centre, then particles released far away that random-walk until they touch the cluster and stick. Nothing in the rule mentions branches, and the result is a dendrite.
It is the one system in the family with a quantitative number to hit: the mass–radius dimension,
fitted as N(r) ∝ r^D in log–log space by
massRadiusDimension, which also reports \( r^2 \), because a slope through a
curve is not a dimension. Planar DLA's asymptotic value is about 1.71; the cluster below measures
1.597 with \( r^2 = 0.9965 \).
The two controls are what make that number mean something. An Eden cluster uses the same neighbourhood and the same particle count, and differs only in that the growth site is chosen uniformly instead of by a diffusing walker: it measures 1.860 and is visibly compact. A uniform sprinkle over the same disc has dimension exactly 2 by construction and measures 1.898. So the estimator is reading the diffusion-limited part of DLA and not merely "a growth process happened", and it is not simply reading the bounded support.
Stated as a range, not as a match
1.597 is below 1.71 because lattice DLA clusters of this size are known to read low, and the asymptote needs far larger clusters. The assertion in CI is therefore "clearly between 1.5 and 1.8, and clearly not 2".
"The cluster reads low" and "the estimator reads low" are different claims, and the second one is
checkable. A point set placed at radii \( R\,(i/n)^{1/D} \) has
\( N(r) = n\,(r/R)^D \) by construction for any real D, and massRadiusDimension recovers
1.000, 1.262, 1.711, 2.002 from sets built with D = 1.00, 1.2619, 1.71, 2.00. Handed something
whose exponent is 1.71 it returns 1.71. Separately, a straight line of lattice points reads 0.993
and a filled lattice disc 1.986, the same 0.7 % low, because a staircase N(r) is not a power law.
So about 0.7 % of the gap is the lattice and the rest is the cluster's finite size.
A related trap, also caught by a control: fitting from where N(r) is three points measured a
uniform disc at dimension 2.32, which is not a possible answer. massRadiusDimension's
minCount raises the inner radius until the statistics exist.
:::static
/*
Diffusion-limited aggregation: a seed, then particles released far away that random-walk until
they touch and stick. Nothing in the rule mentions branches.
This is the one system here with a quantitative number to hit, so it gets the sharpest evidence:
the mass-radius dimension. An Eden cluster (same neighbourhood, growth site chosen uniformly
instead of by a diffusing walker) and a uniform sprinkle both have dimension 2, so those two
controls show the estimator is reading the diffusion-limited part and not merely "growth".
*/
val side = 300
val dla = dlaCluster(side, side, particles = 6000, seed = 2L)
val fit = dla.fractalDimension(rMin = 5.0, rMaxFraction = 0.75)
println(dla)
println("DLA mass dimension " + fit.dimension.f3 + " (r2 = " + fit.rSquared.f4 + ")")
val eden = edenCluster(side, side, particles = dla.points.size, seed = 2L)
val edenFit = eden.fractalDimension(rMin = 5.0, rMaxFraction = 0.75)
println("CONTROL Eden dimension " + edenFit.dimension.f3)
val sprinkle = sprinkleCluster(side, side, dla.points.size, dla.maxRadius, seed = 2L)
val sprinkleFit = massRadiusDimension(
sprinkle.points,
sprinkle.center,
rMin = 5.0,
rMax = sprinkle.maxRadius * 0.75,
minCount = 60,
)
println("CONTROL uniform sprinkle dimension " + sprinkleFit.dimension.f3 + " (exactly 2 by construction)")
:::fold draw all three clusters, the first two coloured by arrival order
showImg(
montage(
[
"DLA: D = " + fit.dimension.f2 to dla.arrivalOrderImg().colormap("turbo"),
"Eden: D = " + edenFit.dimension.f2 to eden.arrivalOrderImg().colormap("turbo"),
"sprinkle: D = " + sprinkleFit.dimension.f2 to sprinkle.mask,
],
columns = 3,
cellSide = 280,
),
)
:::endfold

The order the branches arrived in
A DLA cluster's shape is a record of when. The cluster is grown once, by dlaCluster, and
Aggregate.points is the order the particles stuck in. The clip reveals that recorded order at a
uniform rate. It is not a re-simulation and it is not uniform in the original walk's wall-clock either: a late particle takes far
longer to arrive than an early one, because it has to random-walk in from a larger circle past a bigger
target. What is uniform is particle count.
Screening. The interior stops growing almost immediately: once a branch has neighbours on both sides, a walker arriving from outside is overwhelmingly likely to touch a tip first, so it never reaches the inside. What grows is a small number of tips racing outward while everything behind them is frozen, and that is why a mass–radius fit gives 1.6 and not 2: the interior is permanently empty, made empty by the arrival process rather than by the rule.
Cropped, and not zoomed
The same arithmetic as section 1's small boards. A cluster that only ever grows can never differ from its first frame by more than the ink it lays down, and on a 300 × 300 domain this cluster lays down 2.7 % of the pixels. Cropping once to the cluster's own bounding box raises that to 6.0 %, and the box below prints both figures from the cluster it is about to animate.
The crop has to be taken per axis. A DLA cluster is anisotropic, one arm gets ahead and sets the
radius, and this one's bounding box is 261 × 152 while a square of side 2 · maxRadius would be
282 × 282 and leave the ink share at 3.0 %. maxRadius is the right number for fitting a dimension and
the wrong one for framing a picture.
A perfect crop is still a couple of percent of ink on black, so this will always be a quieter clip than
a field that re-colours every cell. The alternative, letting arrival order drive a growing crop so
the view zooms out as the cluster spreads, would destroy the figure: screening is a claim about the
interior standing still while the tips advance, and in a frame that rescales every step nothing stands
still. It is exactly the trap rasterizeFLinesFramed avoids in
section 9, and section 4's fixed colour range avoids in colour.
:::static
// Grow the cluster once, then replay `Aggregate.points` -- which IS the order the particles stuck
// in -- at a uniform rate over seven seconds.
val dla = dlaCluster(300, 300, particles = 6000, seed = 2L)
:::fold crop once to the finished cluster's bounding box, and colour by arrival order on its full scale
/*
The crop is the colour-range discipline in geometry: taken ONCE, from the finished cluster, never
from the frame -- a crop that tracked the growing prefix would zoom out as the cluster grew and
the screening this clip is about would be the one thing you could not see.
Per AXIS rather than a square around maxRadius: a DLA cluster is anisotropic, and maxRadius is set
by its single longest arm, so a square crop leaves a third of the panel permanently empty.
*/
val pad = 2
val x0 = (dla.points.minOf { it.x }.toInt() - pad).coerceAtLeast(0)
val y0 = (dla.points.minOf { it.y }.toInt() - pad).coerceAtLeast(0)
val cw = (dla.points.maxOf { it.x }.toInt() + pad + 1 - x0).coerceAtMost(300 - x0)
val ch = (dla.points.maxOf { it.y }.toInt() + pad + 1 - y0).coerceAtMost(300 - y0)
/*
`arrivalPrefixImg` fixes the colour ramp to the FULL cluster's particle count, so a branch keeps
the colour it arrived with; `arrivalOrderImg` normalises by its own count and would re-map the
newest particle to the top of turbo on every frame.
*/
fun cluster(dla: Aggregate, n: Int) = [
"particles " + n + " of " + dla.points.size to
dla.arrivalPrefixImg(n).crop(x0, y0, cw, ch).colormap("turbo", 0f, 1f).upscaleNearest(3),
]
:::endfold
/*
Why the crop: a cluster is a fixed amount of ink, and how legible its growth is depends entirely on
how much of the frame that ink occupies. Note the particle count in the first line -- `particles`
is a ceiling, not a promise, and this cluster stopped well short of the 6000 asked for.
*/
println(dla)
println(
"cropped to " +
cw +
" x " +
ch +
" of 300 x 300, so " +
(100.0 * dla.points.size / (cw * ch)).f1 +
"% of the panel is ever lit, against " +
(100.0 * dla.points.size / 90000.0).f1 +
"% uncropped",
)
animateEvolution(
Unit,
totalSteps = dla.points.size,
seconds = 7.0,
cellSide = 660,
step = { it },
panels = { _, n -> cluster(dla, n) },
)
9. L-systems
L-systems are the odd one out in this family. They are the one generative system here whose output can be checked exactly and combinatorially.
- Lindenmayer's original algae system (
A → AB,B → A) has derivation lengths that are the Fibonacci numbers. Exactly. - The Koch snowflake has exactly \( 3 \cdot 4^n \) forward symbols at generation
n. - Its turtle path closes: the pen returns to where it started, to \( 2.3 \times 10^{-14} \) of a step.
- Its drawn length grows by exactly 4× per generation at constant step size. (The textbook
4/3is the ratio when the figure is held at constant diameter; this turtle holds the step constant, so the figure triples in size each generation. Same identity, stated for what is actually drawn.) - And its enclosed area matches the closed form \( 9^n \cdot A_0 \cdot \bigl(1 + \tfrac{3}{5}(1 - (4/9)^n)\bigr) \) to \( 3 \times 10^{-8} \)
relative, a second independent geometric identity from the same turtle. (
FLinestores single-precision coordinates; that residual is the float, not the identity.)
Perturbing the turn angle by 0.01 radians leaves the path open by 2.7 steps, so the closure test has teeth.
The bracket pair [ ] is what makes an L-system a plant rather than a curve: it pushes and pops the
turtle state, so a production can grow a side branch and return. And stochastic productions (several
competing rules for one symbol) vary the topology rather than jittering one plant's coordinates,
which is why the three plants below have 91, 74 and 51 branches rather than three wobbly copies of the
same tree.
:::static
// L-systems are the one generative system in this family whose output can be checked exactly and
// combinatorially rather than statistically -- so they carry the sharpest evidence here.
println("algae derivation lengths (Fibonacci): " + LSYSTEM_ALGAE.derivations(9).map { it.length })
println(
"Koch snowflake forward symbols (3 * 4^n): " +
(0..5).map { gen -> LSYSTEM_KOCH_SNOWFLAKE.derive(gen).count { it == 'F' } },
)
/*
And the turtle: the path CLOSES, its drawn length quadruples exactly at constant step size, and
its enclosed area matches the closed form 9^n * A0 * (1 + 3/5(1 - (4/9)^n)). FLine stores
single-precision coordinates, so "exact" here means to float precision.
*/
val step = 1.0
val tp = TurtleParams(angle = Math.PI / 3.0, step = step)
val gens = (0..5).map { drawLSystem(LSYSTEM_KOCH_SNOWFLAKE, it, tp) }
println("worst closure residual: " + gens.map { turtleClosureError(it, step) }.max() + " of a step")
val lens = gens.map { flineTotalLength(it.lines) }
println("length ratio per generation: " + (1..5).map { (lens[it] / lens[it - 1]).f6 })
println(
"area, relative error against the closed form: " +
(0..5).map { gen ->
val a0 = Math.sqrt(3.0) / 4.0 * step * step
val predicted =
Math.pow(9.0, gen.toDouble()) * a0 * (1.0 + 0.6 * (1.0 - Math.pow(4.0 / 9.0, gen.toDouble())))
"%.1e".format(Math.abs(Math.abs(flineSignedArea(gens[gen].lines)) - predicted) / predicted)
},
)
// Control: one wrong turn and none of that holds.
println(
"CONTROL, turn angle perturbed by 0.01 rad: open by " +
"%.1f"
.format(
turtleClosureError(drawLSystem(LSYSTEM_KOCH_SNOWFLAKE, 4, tp.copy(angle = Math.PI / 3.0 + 0.01)), step),
) +
" steps",
)
// Stochastic productions vary the TOPOLOGY, not merely the coordinates.
val plants = (1L..3L).map {
drawLSystem(LSYSTEM_STOCHASTIC_PLANT, 5, TurtleParams(angle = Math.toRadians(24.0), step = 4.0), seed = it)
}
println("three seeds of the stochastic plant give branch counts " + plants.map { it.lines.size })
// The arrowhead curve draws on A and B, not F -- with the default alphabet it renders nothing.
val arrow = drawLSystem(LSYSTEM_SIERPINSKI_ARROWHEAD, 7, TurtleParams(angle = Math.PI / 3.0, drawSymbols = "AB"))
println("arrowhead, generation 7: " + arrow.drawnSegments + " drawn segments (3^7 = 2187)")
:::fold rasterize the three curves and the three plants
showImg(
montage(
[
"Koch snowflake gen 4" to rasterizeFLines(gens[4].lines, 300, 300),
"arrowhead gen 7" to rasterizeFLines(arrow.lines, 300, 300),
"dragon gen 12" to
rasterizeFLines(drawLSystem(LSYSTEM_DRAGON, 12, TurtleParams(angle = Math.PI / 2.0)).lines, 300, 300),
] + plants.mapIndexed { i, pl -> "plant seed " + (i + 1) to rasterizeFLines(pl.lines, 240, 300) },
columns = 3,
cellSide = 280,
),
)
:::endfold

The turtle, walking
An L-system could be animated by derivation depth, generation 0 then 1 then 2, and that would be a slideshow: six generations over seven seconds is 23 duplicate frames between each change, and each generation is a completely different picture, so there is no motion to read, only cuts.
What an L-system does have is a turtle, and a turtle walks in an order.
flinePrefix truncates the emitted FLines at a
fraction of their total segment count in walk order, so both panels below are drawn by a pen, in the
sequence the pen moved.
Which makes the bracket pair visible in the right-hand panel. [ pushes the turtle's state and ] pops
it, so a plant is drawn depth-first: the pen runs all the way out to the tip of one branch, then jumps
back to a fork it remembered and runs out to the next. You can watch it do exactly that, and the jumps are
the stack. The Koch snowflake on the left has no brackets at all and is therefore one
continuous stroke: 768 segments at generation 4, traced without the pen ever leaving the paper, closing
back on its start to \( 2.3 \times 10^{-14} \) of a step.
The framing has to be pinned, or nothing moves
rasterizeFLines fits its raster to the bounds of what it is
given, which for a growing curve means the figure is rescaled on every frame: the curve appears to
stay the same size while the world shrinks around it, and the growth becomes invisible.
rasterizeFLinesFramed takes the framing from the finished curve
instead. This is the same class of mistake as an auto-ranged colour map, in geometry rather than in
colour.
:::static
:::fold rasterize each prefix in the finished curve's frame
/*
`rasterizeFLinesFramed` takes its framing from the FINISHED curve. Plain `rasterizeFLines` fits
the raster to whatever it is handed, so a growing prefix rescales every frame: the curve appears
to stay the same size while the world shrinks around it, and the growth becomes invisible.
*/
fun drawn(koch: List<FLine>, plant: List<FLine>, f: Double): List<Pair<String, Img>> {
val pct = (f * 100).toInt()
return [
"Koch gen 4: " + pct + "% drawn" to rasterizeFLinesFramed(flinePrefix(koch, f), koch, 420, 420),
"plant gen 5: " + pct + "% drawn" to rasterizeFLinesFramed(flinePrefix(plant, f), plant, 420, 420),
]
}
:::endfold
/*
Animated by DRAWING, not by cutting between derivation depths: an L-system's turtle walks in an
order, and `flinePrefix` truncates the emitted FLines at a fraction of their segment count in
exactly that order. 240 ticks over 140 displayed frames.
*/
val koch = drawLSystem(LSYSTEM_KOCH_SNOWFLAKE, 4, TurtleParams(angle = Math.PI / 3.0, step = 1.0))
val plant = drawLSystem(LSYSTEM_STOCHASTIC_PLANT, 5, TurtleParams(angle = Math.toRadians(24.0), step = 4.0), seed = 1L)
val ticks = 240
animateEvolution(
Unit,
totalSteps = ticks,
seconds = 7.0,
columns = 2,
cellSide = 420,
step = { it },
panels = { _, n -> drawn(koch.lines, plant.lines, n.toDouble() / ticks) },
)
10. Bridges out: FLines, trees and a watertight mesh
What makes this family Field's rather than a generic ALife library is where its output goes. Three bridges, all into machinery that was already here:
- A reaction–diffusion field →
FLines.Img.isoContourFLinesruns the marching-squares extractor incontours.ktover thevfield, so a Gray–Scott result becomes drawable, animatable, exportable geometry: 195 contours atv = 0.25below. That count is checked against a second measurement rather than printed: CI asserts at least one loop per thresholded blob, and pins the extractor itself against circle geometry (a disc's contour encloses \( \pi r^2 \) to 0.16 %). - Space colonization → a tree of
FLines. Grown into an annulus, which no upward-with-noise heuristic reaches, so the branching really is following the space.ColonizationTree.isTree()checks the thing a picture cannot show (one root, \( n - 1 \) edges, no cycles) because a growth algorithm that produces a cycle looks identical. - A 3-D life-like automaton → a
TriMesh.IntVolume.toMeshsmooths the occupancy and runsmesh_sdf.kt's marching cubes, so the automaton's surface is a real mesh you can boolean, subdivide and export. The claim asserted is that it is watertight: a leaking mesher produces a picture that looks fine and a model that cannot be printed.
An empty mesh is vacuously watertight
A dead volume meshes to zero triangles and passes a watertight check, so the triangle count is part of the claim.
:::static
/*
What makes this family Field's rather than a generic ALife library: the bridges out into the
things Field already draws, meshes and exports.
(a) A reaction-diffusion field -> iso-contour FLines, through the marching-squares extractor in
`contours.kt` that was already here.
*/
val rd = grayScottSeed(96, 96, patches = 5, patchSize = 12, seed = 4L).stepTimes(GRAY_SCOTT_SPOTS, 5000)
val contours = rd.v.isoContourFLines(0.25, cells = 96)
println("Gray-Scott -> " + contours.size + " iso-contour FLine(s) at v = 0.25")
/*
(b) Space colonization -> a tree of FLines. Grown into an ANNULUS, which no upward-with-noise
heuristic reaches, so the branching really is following the space. isTree() checks the thing
a picture cannot show: one root, n-1 edges, no cycles.
*/
val ring = annulusAttractors(1400, 150.0, 140.0, 60.0, 120.0, seed = 3L)
val tree = spaceColonization(ring, 150.0, 285.0, influenceRadius = 30.0, killRadius = 7.0, stepSize = 5.0)
println(tree)
println("is a tree (one root, n-1 edges, acyclic): " + tree.isTree())
println("CONTROL, no attractors: " + spaceColonization(emptyList(), 150.0, 285.0).nodeCount + " nodes")
/*
(c) A 3-D life-like automaton -> a TriMesh, through `mesh_sdf.kt`'s marching cubes. The claim
that matters for a mesh is that it is WATERTIGHT -- a leaking mesher looks fine and produces
a model that cannot be printed, booleaned or subdivided. Note an EMPTY mesh is vacuously
watertight, so the triangle count is part of the claim.
*/
val volume =
IntVolume.randomBall(48, 48, 48, density = 0.55, ballFraction = 0.70, seed = 8L).stepTimes(LIFE3D_CLOUDS, 6)
println(volume)
val mesh = volume.toMesh(resolution = 96, smoothRadius = 1)
println(
"marched: " +
mesh.triangleCount +
" triangles, watertight = " +
mesh.isWatertight() +
", Euler characteristic = " +
mesh.eulerCharacteristic(),
)
:::fold draw the field, the contours, the tree and two views of the volume
showImg(
montage(
[
"Gray-Scott field" to rd.v.colormap("magma", 0f, 0.5f).upscaleNearest(3),
"its iso-contours as FLines" to rasterizeFLines(contours, 300, 300),
"space colonization tree" to rasterizeFLines(tree.toFLines(), 300, 300),
"3-D CA: z slice" to volume.slice(24).toImg(2).upscaleNearest(5),
"3-D CA: depth projection" to volume.depthProjection().colormap("viridis").upscaleNearest(5),
],
columns = 3,
cellSide = 280,
),
)
:::endfold

The bridge, live: a field and its contours at the same time
The first of those three bridges is the one worth animating, because it is the one that produces geometry
rather than a picture, and geometry that tracks a moving field is the thing you would actually want in a
Field document. Left: the v field of a spots run, 5000 steps in seven seconds. Right: the same field's
v = 0.25 iso-contours, re-extracted every frame through contours.kt's marching squares, with the
loop count in the caption.
The loop count climbs while the spots are dividing, because each division is one loop becoming two, and it is the one number on this page that is a topological measurement rather than a statistical one: it changes by integers, at the moment a thing happens. The contours stay locked to the field's level set as it moves.
:::static
:::fold pin the contour panel's framing to the field's extent
/*
The contour panel is framed by the FIELD's extent -- two invisible single-node FLines at the
corners of the 96x96 sample grid, in the contour extractor's own pixel coordinates. Framed by
the contours' own bounds instead, the early frames (two loops in one corner) would be drawn
zoomed right in and the whole figure would zoom out as the pattern spread.
*/
val fieldFrame = [FLine().moveTo(0, 0), FLine().moveTo(95, 95)]
fun bridge(s: GrayScottState, n: Int): List<Pair<String, Img>> {
val loops = s.v.isoContourFLines(0.25, cells = 96)
return [
"v field, step " + n to s.v.colormap("magma", 0f, 0.5f).upscaleNearest(4),
"iso v=0.25: " + loops.size + " loops" to rasterizeFLinesFramed(loops, fieldFrame, 384, 384),
]
}
:::endfold
/*
A reaction-diffusion field and its iso-contour FLines, re-extracted afresh every frame -- so the
geometry tracks the field instead of being a snapshot of it. 5000 steps over 140 displayed
frames: 36 reaction-diffusion steps AND one marching-squares pass per frame.
*/
animateEvolution(
grayScottSeed(96, 96, patches = 5, patchSize = 12, seed = 4L),
totalSteps = 5000,
seconds = 7.0,
columns = 2,
cellSide = 384,
step = { it.step(GRAY_SCOTT_SPOTS) },
panels = { s, n -> bridge(s, n) },
)
Trees that compete for light
Space colonization grows toward points you place. The model below, after Palubicki and colleagues'
self-organizing tree models
(SIGGRAPH 2009), needs no target. A tree is a chain of metamers: an internode with a bud at its tip
and an axillary bud beside it. Each bud measures the light that reaches it, and the shape of the crown
comes from buds competing for that light. Chema Guerra's
Growing trees walks through the same
method; this is a 2-D version of it in
alife_tree_growth.kt.
One season of TreeGrowth.step() does five things:
- Shadow. Every bud adds shadow to a wedge of grid cells below it, widening by one cell per row
and halving roughly every row and a half (
ShadowGrid). Shadows add up, so the inside of the crown goes dark. - Light. A bud reads
Q = 1 − (shadow from everyone else), clamped to0 .. 1. - Light flows down.
Qis summed from the tips to the root, so every branch knows how much light its whole subtree collects. - Resource flows up. The root receives
v = α · Q_rootunits of growth resource and passes them back up. At each fork the Borchert–Honda rule splits it between the continuing axis and the lateral branch:v_main = v · λQ_main / (λQ_main + (1 − λ)Q_lateral). Atλ = 0.5the resource follows the light. Above 0.5 the leader takes more of it (apical dominance), and below 0.5 the side branches do. - Growth and shedding. A bud given
v ≥ 1grows a shoot of⌊v⌋metamers, bending toward the brightest part of the cone ahead of it and toward the sky. A side branch whose light per metamer falls under a threshold is dropped.
Each season checks that the structure is still a tree (isTree(), also run after every shed), and
that all the resource handed to the root reaches the buds. The allocation rule only splits it; it
doesn't create or discard any. Adding shadow never makes a bud brighter.
The figure grows one tree for 30 seasons. The top row shows it at seasons 2, 5, 10, 20 and 30. The
middle row shows the season-20 tree three ways: every bud coloured by the light it read, every
internode coloured by the resource flowing through it, and the shadow grid itself. The bottom row
shows the same seed under four values of λ.
:::static
// One tree, 30 seasons. Each step() is one season: shadow, light, Borchert-Honda allocation,
// growth and shedding.
val tree = TreeGrowth(seed = 4L)
val seasons = [2, 5, 10, 20, 30]
val strip = ArrayList<Pair<String, Img>>()
var views: List<Pair<String, Img>> = []
for (s in 1..30) {
tree.step()
if (s in seasons)
strip.add("season " + s + ": " + tree.nodeCount + " metamers" to tree.render(TreeView.STRUCTURE, 3))
if (s == 20) {
views = [
"season 20: bud light" to tree.render(TreeView.LIGHT, 3),
"resource flow (log)" to tree.render(TreeView.RESOURCE, 3),
"shadow grid" to tree.render(TreeView.SHADOW, 3),
]
}
}
println(tree)
println("a tree (one root, n-1 edges, acyclic): " + tree.isTree())
// Borchert-Honda only ever splits, so what reaches the buds is what the root was given.
println(
"worst resource conservation error over 30 seasons: " + "%.1e".format(tree.history.maxOf { it.conservationError }),
)
val last = tree.history.last()
println(
"season 30: " +
last.buds +
" buds read " +
last.baseLight.f6 +
" units of light; root gets " +
last.baseResource.f6,
)
// Apical control: the same seed, four values of lambda.
val lambdas = [0.44, 0.5, 0.56, 0.62]
val crowns = lambdas.map { lam ->
val t = TreeGrowth(params = TreeGrowthParams(lambda = lam), seed = 4L).stepTimes(30)
val xs = (0 until t.nodeCount).map { t.x(it) }
val ys = (0 until t.nodeCount).map { t.y(it) }
println(
"λ = " +
lam +
": " +
t.nodeCount +
" metamers, crown " +
(xs.max() - xs.min()).f0 +
" wide, " +
(ys.max() - ys.min()).f0 +
" tall",
)
"lambda = " + lam to t.render(TreeView.STRUCTURE, 3)
}
showImg(montage(strip + views + crowns, columns = 5, cellSide = 300))

The light view shows how the crown takes shape. Only the buds on its upper and outer surface
read much light. The resource follows them, so the tree grows at its edge, and the shaded branches
inside lose their resource and are shed. By season 30 the tree has shed more metamers than it keeps.
The bottom row shows what λ does. At 0.44 the resource spreads into side branches and the crown is
wider than it is tall. At 0.62 the leaders keep most of it, and the tree is a narrow column that reaches
the top of the grid.
Thirty seasons, growing
Each frame of the video below is a season or part of one, and the framing is the whole grid, so the tree grows inside a fixed frame. The left panel is the structure. On the right the buds are coloured by the light they read: the lit edge moves outward and upward as the tree grows, and the inside goes dark.
:::static
// Thirty seasons over seven seconds. TreeGrowth is mutable and step() returns it, so it is the
// `step` here directly. render() frames the whole grid, so the tree grows inside a fixed frame.
animateEvolution(
TreeGrowth(seed = 4L),
totalSteps = 30,
seconds = 7.0,
columns = 2,
cellSide = 450,
step = { it.step() },
panels = { t, n ->
[
"season " + n + ": " + t.nodeCount + " metamers" to t.render(TreeView.STRUCTURE, 4),
"bud light, " + t.shedMetamersTotal + " shed" to t.render(TreeView.LIGHT, 4),
]
},
)
11. Evolution: searching the rule space
Every section above picks a rule and measures what it does. This one runs the question backwards: given a measurement, find a rule that scores well on it.
The statistics the sections above measure with are the fitness functions this one searches on:
alife.kt's polarization, blobShapes, correlationLength, massRadiusDimension,
stateEntropyBits and centroidDrift, read as a score instead of as a readout.
Each search is compared with a control,
PopulationSearch.random. It draws from exactly the
distribution evolve mutates within, at the same budget, under the same seed, and every result below
is a margin over it. Without it the numbers would mean nothing: a generator good enough to stumble
onto a decent rule now and then looks identical, from the outside, to a search that works.
A Lenia rule that travels
A soliton, meaning alive and in one piece and moving, is the thing continuous Lenia is famous for, and it is
three measurements Field already had. Every term of the score is read off leniaRun, which classifies a
run as DEATH, BOUNDED or SATURATION from the mean field value and reports a wrapped centroid drift. That
matters because the failure mode here is a rule that scores well by saturating the grid and
sitting still.
Against 220 evaluations of the control at the same seed, evolution finds a rule 17.2 % better:
2.09372 against 1.78636, a margin of 0.3074. The first panel of the figure below is the control's
best, so the thing evolution had to beat is in the same picture at the same budget.
The search scored what it was asked to score, and that is less than a creature. At the 90 steps the
objective runs, the evolved rule is BOUNDED with one blob and a centroid drift of 4.82 cells: the
smooth ring in the last panel. Run the same rule from the same seed for 300 steps and it has broken
into 14 blobs with a mean field of 0.32, so what evolution found is a ring on its way to filling
the board. Orbium, by contrast, holds its mass level for as long as you run it. To search
for creatures, score a longer run, or score how little the mass changes over its second half.
What the panels after the first one are
The successive record holders: the leader of each generation that improved on the best so far.
That is not a lineage: with mu = 6 and crossover on, the odds that six record holders are literally
descended from one another are close to zero. A record-holder chain is still the right thing to
draw.
:::static
// Ask for a soliton: alive, in ONE piece, and travelling. Every term is read off leniaRun, which
// already classifies a run DEATH / BOUNDED / SATURATION and already reports a wrapped centroid drift.
val ops = LeniaGenome(radius = 6, dt = 0.1, shells = 3)
val objective = leniaSolitonObjective(width = 48, height = 48, steps = 90, initialSeed = 5L, driftScale = 8.0)
val budget = 220
// The control arm: the same distribution evolve mutates within, same budget, same seed.
val control = PopulationSearch.random(ops, objective, budget, seed = 13L, label = "random")
val evolved = PopulationSearch.evolve(
ops,
objective,
budget,
seed = 13L,
mu = 6,
lambda = 12,
crossoverRate = 0.3,
immigrantRate = 0.08,
label = "evolve",
)
println(control.summary())
println(evolved.summary())
println(
"margin %.4f (%.1f%% above the control)"
.format(
evolved.bestFitness - control.bestFitness,
100.0 * (evolved.bestFitness / control.bestFitness - 1.0),
),
)
val best = evolved.best!!.genome
println("evolved rule: mu=%.4f sigma=%.4f".format(best.mu, best.sigma))
println(" at 90 steps: " + leniaRun(leniaSeed(48, 48, best, seed = 5L), best, steps = 90, sampleEvery = 8))
println(" at 300 steps: " + leniaRun(leniaSeed(48, 48, best, seed = 5L), best, steps = 300, sampleEvery = 8))
:::fold the control's best, then each successive record holder
val panels = mutableListOf<Pair<String, Img>>()
val cRule = control.best!!.genome
panels +=
"rnd %.2f".format(control.bestFitness) to
leniaRun(leniaSeed(48, 48, cRule, seed = 5L), cRule, steps = 90, sampleEvery = 8).field.upscaleNearest(3)
// Keep the full measured history; display at most five roughly evenly spaced record holders.
val picks =
evolved.improvements.indices.toList().let { idx ->
if (idx.size <= 5) idx else (1..5).map { (it * idx.size / 5) - 1 }
}
for (i in picks) {
val imp = evolved.improvements[i]
panels +=
"g%d %.2f".format(imp.generation, imp.fitness) to
leniaRun(leniaSeed(48, 48, imp.genome, seed = 5L), imp.genome, steps = 90, sampleEvery = 8)
.field
.upscaleNearest(3)
}
// A FIXED 0..1 range, for the reason section "Evaluating one of these in a box" gives: a saturated
// Lenia field is constant at 1, and an auto-ranged colour map draws a constant field BLACK.
showImg(
montage(panels, columns = 3, cellSide = 200, displayRange = 0f..1f),
caption = "left to right: the random control's best, then each successive record holder",
)
:::endfold

The same engine, a genome with nothing in common
A life-like rule is a table: which neighbour counts cause a birth, which cause survival. At radius 2
that is 24 birth bits and 25 survival bits: a fixed-length bit string, and therefore a genome two
parents can be recombined across. LifeRuleGenome.crossover is uniform crossover over the rule table:
real recombination rather than a graft.
The radius decides whether evolution has anything to do. At radius 1 the rule space is
2¹⁷, a few hundred independent draws land near the optimum on their own, and evolution wins by only
3 %. At radius 2 the space is 2⁴⁹ and the margin is 27.4 %, 0.54436 against 0.42744. A control
arm that nearly wins is telling you the problem is easy rather than that the search is good, so run
the control arm at whatever radius you pick.
The figure below reads oddly until you notice what the first panel is. The control is the best of 400
independent draws, so it is a high bar early on: the evolved run's first two record holders score
0.336 and 0.398 below the control's 0.427, and only overtake it around generation five. A
population that started above its own control would mean the control was weak, not that the search was
strong.
Selection is lexicase rather than tournament, because this objective genuinely has a case structure: one case per soup. A rule that holds together on one initial condition and collapses on the other is exactly what an average hides, and lexicase never averages.
:::static
/*
The same engine, a completely different genome.
A life-like rule is a TABLE: which neighbour counts cause a birth, which cause survival. At a
radius-2 neighbourhood that is 24 birth bits and 25 survival bits, a fixed-length bit string, and
therefore a genome two parents can be recombined across. LifeRuleGenome.crossover is uniform
crossover over the rule table: real recombination, not a graft.
Radius 2 rather than the classic Moore 8 is a deliberate choice and it is the most useful thing
in this box. At radius 1 the space is 2^17 rules, and a few hundred INDEPENDENT draws land close
to the optimum on their own, and evolution beats them by only about 3%. A control arm that nearly wins is telling you
the problem is easy, not
that the search is good. At radius 2 the space is 2^49 and the margin is 27%.
*/
val ops = LifeRuleGenome(radius = 2)
val soups = [
randomSoup(48, 48, density = 0.35, seed = 101L),
randomSoup(48, 48, density = 0.12, seed = 202L),
]
/*
"Alive but not saturated, and still changing" -- activity times a Gaussian band on density.
Each factor alone is trivially gamed. A rule that fills the grid and stays filled scores 1 on
density and 0 on activity; a rule that flickers every cell scores ~1 on activity and ~0 on the
band; an extinct rule scores the floor. Only the product is large for a rule that keeps a
moderate population AND keeps rearranging it, which is what everybody means by an interesting
rule and what nobody can state by looking.
*/
val objective = caEdgeOfChaosObjective(soups, warmup = 20, scored = 30)
val budget = 400
val control = PopulationSearch.random(ops, objective, budget, seed = 7L, label = "random")
/*
LEXICASE, because this objective genuinely has a case structure: one case per soup. A rule that
holds together on one initial condition and collapses on the other is exactly what a
tournament's average hides, and lexicase never averages.
*/
val evolved = PopulationSearch.evolve(
ops,
objective,
budget,
seed = 7L,
mu = 8,
lambda = 16,
selection = Selection.LEXICASE,
crossoverRate = 0.3,
immigrantRate = 0.1,
label = "evolve",
)
println(control.summary())
println(evolved.summary())
println()
println(
"margin %.4f (%.1f%% above the control)"
.format(
evolved.bestFitness - control.bestFitness,
100.0 * (evolved.bestFitness / control.bestFitness - 1.0),
),
)
println("recombined ${evolved.recombined} children by uniform crossover over the rule table")
println()
println("evolved rule ${lifeRuleSignature(evolved.best!!.genome)}")
println("control rule ${lifeRuleSignature(control.best!!.genome)}")
println()
println("The first record holders score BELOW the control: the control is the best of 400")
println("independent draws, so a population starts under it and has to climb past it.")
println()
println("Try Selection.TOURNAMENT above and compare. Try LifeRuleGenome() with no radius argument")
println("and watch the margin collapse -- that is the easy problem, and it is worth seeing.")
:::fold the control's best, then each successive record holder, on the same soup
val panels = mutableListOf<Pair<String, Img>>()
panels +=
"rnd %.3f".format(control.bestFitness) to
caRun(soups[0], control.best!!.genome, 20, 30).finalGrid.toImg(2).upscaleNearest(3)
// Keep the full measured history; display at most five roughly evenly spaced record holders.
val picks =
evolved.improvements.indices.toList().let { idx ->
if (idx.size <= 5) idx else (1..5).map { (it * idx.size / 5) - 1 }
}
for (i in picks) {
val imp = evolved.improvements[i]
panels +=
"g%d %.3f".format(imp.generation, imp.fitness) to
caRun(soups[0], imp.genome, 20, 30).finalGrid.toImg(2).upscaleNearest(3)
}
showImg(
montage(panels, columns = 3, cellSide = 200),
caption =
"the control's best, then each successive record holder, all on the same soup -- the\n early ones score BELOW the control, which is what overtaking looks like",
)
:::endfold

Illuminating the space instead of optimising it
MAP-Elites keeps the best rule found in each cell of a grid over the behaviour space: here (density,
activity), the two numbers caEdgeOfChaosObjective already reports. What comes out is not one answer
but a map of "the best rule that behaves like this".
Coverage is the wrong claim here
The obvious headline would be that MAP-Elites covers more of the behaviour space than random sampling. It does not, and it should not be expected to: random sampling spreads over a descriptor space for free, because that is what random means, while MAP-Elites reaches a new cell only when a mutation happens to move a descriptor across a bin boundary. What illumination buys is the quality held in each cell it does occupy, so that is what the box measures, against a control built the only way that makes sense: the same budget of independent draws, binned identically and reduced to the best genome per cell.
Measured over 600 evaluations: both archives occupy 20 of 36 cells, so coverage is a tie, and the QD scores are 3.477 illuminated against 3.361 sampled, a margin of 3.5 %. Twenty-eight of the 600 candidates landed off-grid and were clamped to an edge cell.
Two limits on that number. It is n = 1: one seed, no variance estimate, and a 3.5 % margin is small enough that a handful of seeds could move it. And the archive's diversity is real, twenty occupied cells that genuinely differ, while its interest is not uniform: the activity-zero row is near-frozen by construction and several of its cells are hard to tell apart.
:::static
/*
Illuminate the rule space instead of optimising it.
MAP-Elites keeps the best rule found in each cell of a grid over the BEHAVIOUR space -- here
(density, activity), the same two numbers caEdgeOfChaosObjective already reports as its
Evaluation.behaviour. What comes out is not one answer but a map of "the best rule that behaves
like THIS", which is the contact sheet below.
This is the figure worth looking at. Top left is sparse and nearly dead; bottom right is dense
and noisy; and somewhere in the middle band are the rules that hold structure while still moving.
*/
val ops = LifeRuleGenome(radius = 2)
val soups = [randomSoup(48, 48, density = 0.35, seed = 303L)]
val objective = caEdgeOfChaosObjective(soups, warmup = 20, scored = 25)
val bins = [6, 6]
val axes = [0.0..1.0, 0.0..0.6]
val budget = 600
val archive = PopulationSearch.mapElites(ops, objective, budget, axes, bins, seed = 31L, initial = 40)
println(archive)
println("off-grid (clamped to an edge cell): ${archive.offGridCandidates} of $budget")
println()
/*
The control arm for a quality-diversity claim is the same budget of independent draws, binned
identically and reduced to the best genome per cell. Comparing against a single best random rule
would be the wrong control, because MAP-Elites is not trying to find one good rule.
*/
val controlCells = HashMap<List<Int>, Double>()
val rng = kotlin.random.Random(31L)
repeat(budget) {
val e = objective.evaluate(ops.random(rng))
val cell =
axes.indices.map { d ->
(((e.behaviour[d] - axes[d].start) / (axes[d].endInclusive - axes[d].start)) * bins[d])
.toInt()
.coerceIn(0, bins[d] - 1)
}
val incumbent = controlCells[cell]
if (incumbent == null || e.fitness > incumbent) controlCells[cell] = e.fitness
}
val controlQd = controlCells.values.sum()
println("random-sampled archive: ${controlCells.size}/${archive.cells} cells, QD %.3f".format(controlQd))
println("illuminated archive: ${archive.elites.size}/${archive.cells} cells, QD %.3f".format(archive.qdScore))
println()
println("Quality, not coverage, is the claim. Random sampling spreads over a descriptor space for")
println("free -- that is what random means -- while MAP-Elites reaches a new cell only when a")
println("mutation moves a descriptor across a bin boundary. What illumination buys is the quality")
println("held in each cell it does occupy.")
:::fold one panel per occupied cell, labelled (density bin)(activity bin) and score
val panels =
archive.elites.entries.sortedWith(compareBy({ it.key[0] }, { it.key[1] })).map { (cell, elite) ->
"%d%d %.2f".format(cell[0], cell[1], elite.fitness) to
caRun(soups[0], elite.genome, 20, 25).finalGrid.toImg(2).upscaleNearest(3)
}
showImg(
montage(panels, columns = 6, cellSide = 150),
caption = "one panel per occupied cell; the label is (density bin)(activity bin) and the score",
)
:::endfold

All three searches run on one engine, PopulationSearch, and each is a genome (a GenomeOps: how to
make, mutate and recombine one) plus an Objective that scores it. Evolving the shape of a function
for animation runs on the same engine; see evolving and optimizing
functions.
Does search beat random?
One seed of evolution beside one seed of random sampling says little: the gap between two seeds of
the same method is often larger than the gap between the methods. compareSearch runs every method
on the same genome and objective for exactly the same number of evaluations, at many seeds, and
reports the spread. Its four default arms are random sampling, a restarting hill climber, (µ6 + λ12)
evolution, and (µ12 + λ24) evolution where a quarter of each generation are fresh random draws
("immigrants"). Any GenomeOps and Objective will do:
val soups = [randomSoup(48, 48, density = 0.35, seed = 101L), randomSoup(48, 48, density = 0.12, seed = 202L)]
val c = compareSearch(
LifeRuleGenome(radius = 2),
caEdgeOfChaosObjective(soups, warmup = 20, scored = 30),
budget = 120,
seeds = [1L, 2L, 3L, 4L],
label = "life rule, edge of chaos",
)
println(c.table() + c.budgetTable([30, 60, 120]))
showImg(c.plot(360, 260))
table() gives each arm's median, interquartile range and range of per-seed bests, and how many
seeds it beat random at. budgetTable reads the same runs at smaller budgets, which gives the same
numbers as running those budgets separately. plot draws each arm's median best-so-far as a line
with its interquartile band: random in grey, hill climbing blue, evolution orange, immigrants green.
SearchArm builds other arms, and the first arm in the list is the one the others are counted
against.
At 1000 evaluations and 12 seeds per arm, on the three genomes this section uses (medians of the per-seed best; in brackets, the seeds out of 12 where the arm beat random):
evals random hillclimb evolve immigrants
life rule, edge of 100 0.412 0.423 (7) 0.458 (9) 0.429 (5)
chaos 300 0.426 0.470 (9) 0.486 (9) 0.470 (9)
1000 0.481 0.483 (7) 0.491 (6) 0.498 (8)
lenia, soliton 100 1.686 1.673 (6) 1.645 (5) 1.715 (8)
300 1.790 1.919 (9) 1.747 (4) 1.907 (8)
1000 1.899 2.197 (11) 2.037 (8) 2.135 (8)
l-system, 100 0.239 0.240 (6) 0.237 (7) 0.241 (6)
dimension 1.7 300 0.242 0.245 (10) 0.246 (10) 0.247 (9)
1000 0.247 0.247 (6) 0.248 (8) 0.247 (7)
Searching helps on Lenia, and the hill climber helps most: at 1000 evaluations it beats random at 11
of 12 seeds, and three arms reach the objective's ceiling of 2.5 at their best seed. Plain evolution
loses to random at 300 evaluations on Lenia and catches up only later. On Life-like rules every arm
edges out random by a small median margin, but the spread is wide: random's upper quartile (0.58) is
above every searcher's median, and one or two seeds of each searcher find a rule scoring above 0.95 that
random never does. The L-system objective is saturated: every arm, random included, sits within 0.002
of 0.25, its practical ceiling (an exact dimension plus the straightness bonus), so this objective
cannot tell the methods apart. A harder target or a finer score would be needed before it could.
The vehicle brains of Braitenberg vehicles §10 run through the same
compareSearch. Searching for MorphoHDL Circuits sets random sampling, hill
climbing and evolution against each other on circuit programs, where random sampling sometimes beats
the hill climber.
Evolved grain sets: an L-system as a score
The same L-system genome can be read as music instead of a drawing. In Nathan Ho's
EvoGrain, a derived string is a list of commands for a turtle
whose state is a time and a pitch: G plays a grain and moves forward in time, f rests,
+ and - step the pitch, and [ and ] save and restore the state. Because ] returns to the
time the branch started at, every bracket is a new voice, so brackets give polyphony. A branch also
plays quieter and quicker than its parent (GrainTurtle.branchAmp, branchDuration). A trunk
grain lasts about a second and the next starts 0.72 s later; GrainTurtle(timeScale = 0.5) plays
the same set twice as fast.
decodeGrains(system, generations) derives the grammar and returns its grains; GrainSetStats
measures the set (count, median pitch, pitch spread, duration, density, mean polyphony); and
renderGrains plays it sample-accurately as Hann-windowed tones, or as grains read from a sample if
you pass source =. Here is one grammar, G → G[+G-G]fG, at three generations:
val system = lSystem("G", 'G' to "G[+G-G]fG")
for ((i, s) in system.derivations(2).withIndex()) println("gen $i: $s")
val grains = decodeGrains(system, 3)
println(GrainSetStats(grains))
showImg(grainScoreImg(grains, 640, 220), caption = "gen 3: time across, pitch up; gold trunk, blue and red branches")
display("audio", renderGrains(grains).wav())

Fitness is a weighted mean of trapezoids, one per statistic. A trapezoid scores 0 below its first
corner, rises to 1 at its second, stays at 1 to its third and falls to 0 at its fourth, so each term
says "about this much, and these are the limits". defaultGrainTerms() asks for 40 to 120 grains over
12 to 30 seconds, a median near middle C, a few semitones of spread, a little polyphony, and no more
than about a third of the grains on any one pitch.
grainSetObjective(terms) turns that into an Objective for PopulationSearch, and
grainLSystemGenome() is the LSystemGenome above with the alphabet G f + - [ ], so mutation,
crossover and bracket repair are the ones the drawn L-systems already use:
val terms = defaultGrainTerms()
val run = PopulationSearch.evolve(
grainLSystemGenome(),
grainSetObjective(terms),
budget = 400,
seed = 7L,
crossoverRate = 0.3,
immigrantRate = 0.1,
label = "grains",
)
println(run.summary())
for (imp in run.improvements) println("$imp ${grainRule(imp.genome)}")
showImg(fitnessTraceImg(run.trace), channel = "trace", caption = "best-so-far fitness against evaluations, 0 to 1")
val best = decodeGrains(run.best!!.genome, 3)
val stats = GrainSetStats(best)
println(stats)
val ranges = mapOf(
GrainStat.COUNT to (0.0 to 450.0),
GrainStat.MEDIAN_PITCH to (40.0 to 84.0),
GrainStat.PITCH_SPREAD to (0.0 to 18.0),
GrainStat.DURATION to (0.0 to 70.0),
GrainStat.POLYPHONY to (0.0 to 9.0),
GrainStat.MODE_SHARE to (0.0 to 1.0),
)
val panels = terms.map { t ->
val (lo, hi) = ranges.getValue(t.stat)
"%s %.2f".format(t.stat.label, stats[t.stat]) to trapezoidImg(t.shape, lo, hi, stats[t.stat])
}
showImg(
montage(panels, columns = 3, cellSide = 220),
channel = "terms",
caption = "each term's trapezoid, the best set's value marked",
)
showImg(grainScoreImg(best, 640, 220), channel = "score", caption = "the best set")
display("audio", renderGrains(best).wav())

The automatic run optimises the trapezoids. Whether the result sounds good is up to you. Without
the last term, the winners were a long overlapping trunk at a single pitch: a drone that counts as
polyphony. The term discourages that result, though the search can still find other ways to satisfy
the numbers. EvoGrain also offers interactive evolution: you listen and choose the parents.
GrainBreeder holds a population of six grammars; breed([2, 4]) keeps the chosen parents and fills
the rest with their crossovers, each mutated once. The same seed and the same choices replay the same
lineage. In a box, give each child a player and a text field for the choice:
val breeder = GrainBreeder(seed = 1L)
fun show() {
displayClearAll()
val scores = breeder.scores()
val listing =
breeder.population.withIndex().joinToString("<br>") { (i, g) ->
"%d: %s fitness %.2f".format(i, grainRule(g), scores[i])
}
display("main", "generation ${breeder.generation}<br>" + listing)
for ((i, g) in breeder.population.withIndex()) display("child $i", renderGrains(decodeGrains(g, 3)).wav())
stringInput("parents (e.g. 1 4)") { s ->
val picks = s.split(" ", ",").mapNotNull { it.trim().toIntOrNull() }
if (picks.isNotEmpty()) {
breeder.breed(picks)
show()
}
}
}
show()
Every trapezoid corner and weight is a dial. Building the terms from slider callbacks and
re-running PopulationSearch.evolve on release moves the target while you listen; the loadable
demo below does exactly that.
Shaping the grains: length, density, pan and a silent symbol
The six symbols above place grains; three more pairs shape them. < and > make the grains that
follow shorter or longer by GrainTurtle.lengthStep (1.5) without moving the time step, so they
change how much the grains overlap. * and / make the time step shorter or longer by
densityStep (1.5), so the grains that follow come denser or sparser. l and r move the pan by
panStep (a quarter of the way to one side). Brackets save and restore all three, so a branch can
change them without touching the trunk. A grammar may also have a second rule, for a symbol such as
X that is rewritten like G but never plays; it carries structure from one generation to the
next. Here the trunk steps down and speeds up, and every branch is an X that steps up and slows
down a little more each generation:
// < and > make the grains that follow shorter or longer, * and / denser or sparser,
// l and r move them left or right. X is rewritten like G but never plays.
val system = lSystem("G", 'G' to "G[lX]-*<G", 'X' to "+>G/X")
for ((i, s) in system.derivations(2).withIndex()) println("gen $i: $s")
val grains = decodeGrains(system, 4)
println(GrainSetStats(grains))
showImg(grainScoreImg(grains, 640, 220), caption = "gen 4: the trunk falls and quickens; each branch climbs and slows")
display("audio", renderGrains(grains).wav())

The search can use them too. grainLSystemGenome(alphabet = GRAIN_ALPHABET_EXTENDED) builds rules
from all twelve symbols, and nonterminals = "X" adds a rule for X to every genome.
A target taken from a recording
You can write the trapezoids by hand, or take their target from a sound.
grainsFromAudio(samples, sampleRate) reads a monophonic recording as a grain set: one grain per
note Field's pitch detector can name, at its onset, length and pitch, with its loudness as the
amplitude. grainTermsAround(stats) then puts a trapezoid on each of that set's statistics, with a
plateau a quarter of the value either side (two semitones for the median pitch) and feet four times
as far out. The recording here is Tidal's eleven arpy samples played one after another, a rising
scale of plucked notes from middle C up two octaves in a second and a half. Its notes are about
0.13 s apart, five times closer than a trunk grain's 0.72 s. The plain alphabet can only get that
close with deep branches, which are quicker; * gives the search a second way. The box compares the
two alphabets over eight seeds, then plays the winner from the first arpy note:
// The recording: Tidal's eleven arpy samples back to back, a rising scale.
val arpy = DirtRender.requirePinnedLibrary().family("arpy")!!
val notes = (0 until arpy.size).map { arpy.sample(it)!! }
val rate = notes[0].sampleRate.toInt()
val recording = notes.flatMap { it.left.asList() }.toFloatArray()
// Read it as grains, and centre a trapezoid on each of its statistics.
val heard = grainsFromAudio(recording, rate, minHz = 100.0)
val target = GrainSetStats(heard)
println("recording: $target")
val terms = grainTermsAround(target)
fun search(alphabet: String, seed: Long) = PopulationSearch.evolve(
grainLSystemGenome(alphabet = alphabet),
grainSetObjective(terms),
budget = 600,
seed = seed,
crossoverRate = 0.3,
immigrantRate = 0.1,
label = "toward arpy",
)
// The plain alphabet against the one with < > * / l r, over eight seeds each.
for (alphabet in [GRAIN_ALPHABET, GRAIN_ALPHABET_EXTENDED]) {
val bests = (1L..8L).map { search(alphabet, it).bestFitness }
println("%-14s mean best %.3f, worst %.3f".format(alphabet, bests.average(), bests.min()))
}
val run = search(GRAIN_ALPHABET_EXTENDED, 7L)
val best = decodeGrains(run.best!!.genome, 3)
println(grainRule(run.best!!.genome))
println("evolved: " + GrainSetStats(best))
showImg(grainScoreImg(heard, 640, 160), channel = "recording", caption = "the recording, read as grains")
showImg(grainScoreImg(best, 640, 160), channel = "score", caption = "the evolved set")
// Played from the first arpy note, which the detector heard at this pitch.
display("audio", renderGrains(best, source = notes[0].left, sourceRate = rate, sourcePitch = heard[0].pitch).wav())

Over the eight seeds the extended alphabet's mean best is higher (0.871 against 0.823) and its worst
is lower (0.634 against 0.801): twelve symbols make a larger space, and some seeds get lost in it.
The statistics are also a coarse description: a set can match the count, span, spread and median of a
rising scale and still not rise evenly, as the evolved set shows. Nothing in GrainSetStats says
which way the pitch moves over time, so nothing in the search pushes toward it. grainsFromAudio
names one note at a time; a chord or a drum hit gives it nothing to read, so polyphony in a target
taken from a recording is always about 1.
A map of grain sets instead of one winner
PopulationSearch.evolve returns the one set that scores best. PopulationSearch.mapElites keeps
the best set of each kind: it divides two statistics into a grid and keeps the highest-scoring set
in every cell. grainSetObjective(behaviour = [...]) chooses the two statistics, here pitch spread
across and polyphony up, so the grid runs from narrow, thin sets at the bottom left to wide, thick
ones at the top right:
// MAP-Elites over pitch spread and polyphony: the best set of each kind, not one winner.
val axes = [GrainStat.PITCH_SPREAD, GrainStat.POLYPHONY]
val map = PopulationSearch.mapElites(
grainLSystemGenome(),
grainSetObjective(behaviour = axes),
budget = 800,
axes = [0.0..12.0, 0.0..6.0],
bins = [4, 4],
seed = 1L,
)
println(map)
// One score per cell: spread grows to the right, polyphony upward.
val panels =
(3 downTo 0).flatMap { row ->
(0..3).map { col ->
val elite = map.elites[[col, row]]
if (elite == null) {
"empty" to Img.zeros(240, 120, 3)
} else {
val grains = decodeGrains(elite.genome, 3)
val st = GrainSetStats(grains)
"%.1f st x%.1f %.2f".format(st.pitchSpread, st.polyphony, elite.fitness) to
grainScoreImg(grains, 240, 120)
}
}
}
showImg(
montage(panels, columns = 4, cellSide = 240, textScale = 2),
caption = "pitch spread across, polyphony up; trapezoid fitness in each label",
)

All sixteen cells fill in 800 evaluations. Every cell is still scored by defaultGrainTerms(), so
the corners, which ask for more spread or polyphony than those terms like, score lower; the map
shows what each kind of set costs against the target.
12. Agents with their own programs
Every system above moves its whole population with one rule: BoidFlock.step(p) applies the same
parameters to every agent on every tick. ScriptWorld
gives each agent a program of its own instead, written as an ordinary Kotlin function that runs on the
world's clock, so one agent can circle for four seconds, head for the middle and then wander off, and
the code says so in that order. The same file adds walls, and a planner for getting round them.
The clip below uses both. Twelve agents start at the left of a room divided by two walls, and each has
to reach the green ring at the right. The only way through is an S: over the top of the first wall,
then under the second. In the left room each agent plans a path with route(goal); in the right room
each one heads straight at the goal with seek(goal). Both rooms also run avoidWalls, so the right
room has good local steering, and its agents still end up pressed against the first wall, which is the
part of the room nearest the goal.
:::static
// Twelve agents, one goal, two walls. The way through is an S: over the first wall, under the second.
fun maze(routed: Boolean) =
ScriptWorld(width = 200.0, height = 200.0, seed = 1L, wrap = false).also { w ->
w.addWall(70.0, 45.0, 70.0, 200.0, name = "gap above")
w.addWall(130.0, 0.0, 130.0, 155.0, name = "gap below")
val jitter = kotlin.random.Random(1L) // the same start positions in both rooms
repeat(12) {
w.spawn(
18.0 + jitter.nextDouble(-8.0, 8.0),
100.0 + jitter.nextDouble(-28.0, 28.0),
maxSpeed = 35.0,
radius = 1.2,
steerResponse = 0.3,
) {
val goal = point(175.0, 100.0)
moveUntil(maxSeconds = 30.0, speed = 35.0, until = { near(goal, 6.0) }) {
// The one line that differs between the two rooms.
if (routed) route(goal, clearance = 3.5) else seek(goal)
avoidWalls(clearance = 3.0, weight = 0.6)
separate(range = 4.0, weight = 0.4)
}
}
}
}
:::fold draw both rooms, and count the agents inside the goal ring
fun arrived(w: ScriptWorld) = w.actors.count { it.distanceTo(175.0, 100.0) <= 6.0 }
fun rooms(a: ScriptWorld, b: ScriptWorld) = [
"route %4.1f s: %d/12 in".format(a.time, arrived(a)) to a.toImg(2.0, marks = [175.0 to 100.0]),
"seek %4.1f s: %d/12 in".format(b.time, arrived(b)) to b.toImg(2.0, marks = [175.0 to 100.0]),
]
:::endfold
// 30 simulated seconds (1800 ticks at 60 Hz), played in seven.
animateEvolution(
maze(routed = true) to maze(routed = false),
totalSteps = 1800,
seconds = 7.0,
columns = 2,
cellSide = 400,
step = { (a, b) ->
a.step()
b.step()
a to b
},
panels = { (a, b), _ -> rooms(a, b) },
)
seek points at the goal and keeps pushing; avoidWalls and separate only react to what is within
a few units. Deciding to go away from the goal for a while, to get round a wall, needs a plan.
NavGrid makes one: an A* search on a grid where every
wall is thickened by the agent's clearance, shortened by cutting corners wherever the straight line is
clear, then followed waypoint by waypoint. route(goal) is that plan used as a steering term. Over 96
agents per room (8 different start layouts of 12), 0.990 of the routed agents arrive within 30 seconds
and none of the direct ones do.
One agent's program
An agent's program is built from a few blocks that nest: forever, phase(name) to label a stretch
of it, moveFor(seconds) to steer for a while, and parallel to run two things at once. Inside
moveFor, steering terms such as orbit, arrive, wander and avoidEdges add together, so a
phase can combine several. point, actor and nearest give them something to steer at.
This agent circles the centre for four seconds, closes in on it for two, and wanders for three, over and over. The caption shows the phase it is in, and the small ring is the centre it is circling.
:::static
val w = ScriptWorld(width = 200.0, height = 200.0, seed = 1L, wrap = false)
w.spawn(100.0, 40.0, maxSpeed = 45.0, radius = 1.5) {
val centre = point(100.0, 100.0)
forever {
phase("orbit") { moveFor(4.0) { orbit(centre, distance = 42.0) } }
phase("close in") { moveFor(2.0) { arrive(centre, slowRadius = 20.0) } }
phase("wander") {
moveFor(3.0) {
wander(turn = 40.0)
avoidEdges(margin = 18.0)
}
}
}
}
// 18 simulated seconds, two times through the program, played in seven.
animateEvolution(
w,
totalSteps = 1080,
seconds = 7.0,
cellSide = 400,
step = {
it.step()
it
},
panels = { world, _ ->
val agent = world.actors[0]
["%4.1f s %s".format(world.time, agent.phase) to world.toImg(2.0, marks = [100.0 to 100.0], markRadius = 3.0)]
},
)
The clock is a fixed step, so a run depends only on its seed: w.stateSignature() after the same
number of steps is the same string in any process, and w.events records the tick at which every
phase began.
Hitting a wall
A wall is a line segment with no thickness, so checking whether an agent's new position overlaps it
would miss any step long enough to jump over it.
ObstacleField.sweep instead finds the first moment
along the step at which the agent touches the wall, and stops it there. What the agent does next is
set per agent by its WallResponse: SLIDE keeps
the part of its velocity along the wall, BOUNCE reflects off it, and STOP halts.
The three rooms below are identical, each with one agent launched at the same speed and angle towards
the barrier. Only wallResponse differs.
:::static
// The room's outline is a wall too. Without it the world only clamps at its edge, and the BOUNCE
// agent would come to rest in a corner instead of bouncing.
fun room(response: WallResponse) =
ScriptWorld(width = 200.0, height = 200.0, seed = 1L, wrap = false).also { w ->
w.addPolygon(12.0, 12.0, 188.0, 12.0, 188.0, 188.0, 12.0, 188.0, name = "room")
w.addWall(120.0, 30.0, 120.0, 170.0, name = "barrier")
w.spawn(
25.0,
85.0,
maxSpeed = 90.0,
radius = 2.0,
steerResponse = 1.0,
vx = 90.0,
vy = 16.0,
wallResponse = response,
restitution = 0.9,
) {
moveFor(7.0) { keepVelocity() }
}
}
val rooms = [WallResponse.SLIDE, WallResponse.BOUNCE, WallResponse.STOP].map { it to room(it) }
// 7 simulated seconds, in real time.
animateEvolution(
rooms,
totalSteps = 420,
seconds = 7.0,
columns = 3,
cellSide = 300,
step = {
it.forEach { (_, w) -> w.step() }
it
},
panels = { rs, _ ->
rs.map { (r, w) -> "%s: %d contact(s)".format(r.name, w.actors[0].wallContacts) to w.toImg(1.5) }
},
)
avoidWalls steers away from walls within a given clearance, and cling follows a wall at a set
distance. Both work from the walls near the agent, which is why the maze at the top of this section
also needs route.
Both files are also starting points on the welcome screen, alife_script and
alife_walls, because the way to learn a scripted agent is to edit its program
and run it again. In the alife_walls document, try changing route(goal, clearance = 3.5) to 0.5:
the planned path then passes closer to the walls than the avoidWalls(clearance = 3.0) term will let
the agent go, the two terms push against each other at every corner, and about a fifth fewer agents
arrive.
13. Bonded-particle organisms
Everything above lives on a grid or moves as a population of points. In Christian Heinemann's
ALIEN (5.0-beta was the reference here) a creature is a set of
particles, called cells, held together by bonds. Some cells have a job: a muscle changes the length
of its bonds, a sensor reads its surroundings, a constructor builds new cells from a genome.
alife_bonded.kt is a small version of that idea with bonds, muscles and energy, written to run on
the GPU:
- Bonds are damped springs with a rest length. A bond strained past its limit breaks at both ends on the same step.
- Muscles are bonds whose rest length oscillates,
rest * (1 + amp * sin(omega * t + phase)). - Energy sits in each cell, flows along bonds toward the emptier end, and is spent by muscles. A muscle's stroke shrinks with the energy of its two cells, so a creature that runs out stops.
- The medium is a viscous fluid. Moving sideways to a body costs more than moving along it
(
dragNormalagainstdragTangent), which is what lets a bending wave push a worm forward.
A body is grown from a genome string, one character per segment of a ladder: two rails joined by
rungs and braces. M is a muscle segment, whose left and right rails beat half a cycle apart so
the segment bends one way and then the other. L and R are muscles that also curve the body, so
it swims in a circle. S is stiff, and W is weak, the place the body tears first:
val world = BondedWorld(256)
val worm = world.grow("SMMMMMMMMMMS", 0f, 0f, waveStep = 0.8f)
world.run(3000)
world.centroid(worm)
waveStep is the phase difference between neighbouring segments, so a non-zero value sends a
wave down the body. The default medium makes moving sideways a hundred times dearer than moving
lengthways (dragNormal = 15, dragTangent = 0.15) and each muscle stretches and shrinks by 40%
(amplitude = 0.4). Once the wave is running, this 11-unit worm travels 0.26 body lengths per
stroke, about 2.9 units every two time units; over 60 time units its centre moves 83. Its
controls fail: with waveStep = 0 (every segment bending in step) it moves 1.4, with equal
drags along and across the body it moves 6e-7, and with the wave reversed it swims the other
way, 65 units.
Four worms on the GPU
BondedWorld.step is written one cell at a time and reads only the previous state, so it ports to
a compute shader unchanged. Every bond is stored at both its ends, so no invocation writes another
cell, and both ends of a bond reach the same break decision from the same numbers. The
bonded_organisms document grows four worms and 500 loose cells on the CPU,
uploads them, and steps them in its organisms box's compute tab; a draw box beneath it draws
the cells and bonds straight from the GPU buffers. The genomes are the first line of the
organisms box:
val genomes = ["SMMMMMMMMMMS", "SLLLLLLLLLS", "SMMMMMMMMMMMMMMS", "SRRRRRRRRS"]
Edit one and run the box again to grow different creatures. Each genome has a start and a
heading: the two straight worms start at opposite sides heading inwards, the L and R worms
between them. The world box takes two steps every renderer frame (clock.stepsPerFrame = 2), so
the worms beat about once a second on screen; raise it to speed them up. In the 16-second video
the orange M worm swims from the left edge to the right wall, about three of its body lengths,
the cyan one crosses from the right side to the lower left, and the green L and magenta R worms swim
in curves:
The document's GPU against CPU box restarts the GPU world, freezes it after 600 steps, reads the
cells back and steps a CPU BondedWorld built by the same code 600 times. The largest distance
between a GPU cell and its CPU twin was 5.8e-5, while cells had moved up to 9.8.
What this cut leaves out, and a push-constant trap
No sensors, no constructor cells that grow a body over time, no food, and no audio: the genome
is read once, when the body is built. Repulsion between cells is a brute-force loop over every
pair, which is fine for a few thousand cells and needs a neighbour grid
beyond that. The step counter is BondedClock, which advances once per dispatch, keyed on
the renderer frame and the step within it: a ComputeMesh push-constant lambda runs more than
once per dispatch, so a lambda that counted its own calls advanced two steps per dispatch, the
ping-pong buffers never swapped, and the world stood still. BondedOrganismTest pins the CPU claims above.
Evaluating one of these in a box: the inline views
IntGrid, GrayScottState and LeniaRun implement Field's ToHTML, so evaluating one of them in a
box, or grid.h, draws it. Nothing to set up, no montage call, no colour map to choose.
The range policy is the same in each: every scale is fixed by meaning and named in the caption, never derived from the frame:
| view | panels | scale |
|---|---|---|
IntGrid |
the lattice, one pixel per cell | two states → grayscale, 0 = dead, 1 = alive; more states → cyclicPalette, and the caption says the hues repeat |
GrayScottState |
v then u |
magma and viridis, both fixed 0 … 1 — the range the reaction is defined on |
LeniaRun |
the final field, then the mass history | inferno, fixed 0 … 1, and the sparkline's y fixed to the same 0 … 1 |
Auto-ranging here has a specific victim. A saturated Lenia field is constant at 1. Normalise it
per-frame and it maps to the dark end of the colour map and renders solid black, pixel-for-pixel
indistinguishable from a field that died. On the fixed scale a DEATH run is the bottom of
inferno and a SATURATION run the top, and an unseeded GrayScottState renders as one flat
colour, where Img's auto-normalising thumbnail would turn its numerical dust into a texture.
The box below shows one of each view on something that is doing something.
How the views are checked
AlifeGallery's inline-view figure decodes the base64 PNG back out of the emitted HTML and
asserts individual pixels: all five of a glider's live cells read #ffffff and all 139 dead ones
read #000000 at coordinates taken from the grid (which is also how "one pixel per cell" is
asserted); a DEATH run's centre pixel reads #000004 and a SATURATION run's reads #fcffa4;
and the unseeded GrayScottState renders as exactly one colour, the bottom of magma.
:::static
/*
IntGrid, GrayScottState and LeniaRun implement ToHTML, so evaluating one draws it. The scales are
fixed by MEANING (0..1 for a reagent or a Lenia field, the observed state count for a lattice) and
named in every caption -- never taken from the frame, because a saturated Lenia field is constant
at 1 and an auto-ranged colour map renders it BLACK, identical to a dead one.
*/
display("a. a Life board", LifePatterns.glider.centeredOn(16, 16).h)
// More than two states: the cyclic palette, with the repeat disclosed in the caption. This is the
// cyclic automaton from section 3, 300 generations after a random start.
display(
"b. a cyclic automaton",
IntGrid.random(96, 96, states = CYCLIC_LABYRINTH.states, seed = 7L).stepTimes(CYCLIC_LABYRINTH, 300).h,
)
// Gray-Scott: both reagents, both on a FIXED 0..1 scale.
val seeded = grayScottSeed(64, 64, patches = 4, patchSize = 10, seed = 11L).stepTimes(GRAY_SCOTT_SPOTS, 2000)
display("c. Gray-Scott, seeded", seeded.h)
// Lenia: Orbium's cells with mu moved down to 0.12, where they burst into fragments that fill the
// field. The sparkline is the mean field, on the same fixed 0..1 scale.
val orbium = LeniaPresets.orbium()
val burst = leniaRuleFromCatalogue(radius = 13, t = 10.0, mu = 0.12, sigma = 0.015)
display("d. Lenia, mu = 0.12", leniaRun(orbium.seed(64, 64), burst, steps = 200).h)

Loading all of this into a live Field
Every box above is one box of a shipped starting point, so you can open the page as a document and
edit it rather than retyping it: open it from the welcome screen. Sections 1 to 10 are
alife_worked_example, and section 11 is
alife_evolution, kept separate because its four search boxes take about
fifty seconds between them. The grain boxes of "Evolved grain sets" are
alife_grains, and the Flow Lenia boxes are
flow_lenia. Every box runs on its own; none opens a Window or
a Renderer. To replay the first three starting points from a terminal instead:
python3 tools/field_backdoor.py run-transcript dev_docs/agents/transcripts/alife_worked_example.yaml
python3 tools/field_backdoor.py run-transcript dev_docs/agents/transcripts/alife_evolution.yaml
python3 tools/field_backdoor.py run-transcript dev_docs/agents/transcripts/alife_grains.yaml
The public declarations used on this page are listed, with their KDoc, in the codebase reference:
alife.kt, alife_ca.kt,
alife_continuous.kt,
alife_agents.kt,
alife_lsystem.kt and
alife_field.kt. Start from
what's already in the box.
Where to go next
- Evolving creatures runs the same kind of search on bodies that move,
and Braitenberg vehicles evolves vehicle wirings on
PopulationSearch. - Cellular automata as sound and
Boolean network music read rule systems as rhythm and
pitch, as the grain sets above do; granular is Field's live
GrainCloud, which plays a sample rather than a decoded list of grains. - Imaging supplies the
Imgoperations every figure here is built from. - Open the starting points:
alife_worked_example(sections 1 to 10),alife_evolution,alife_search_comparison,alife_tree_growthandalife_grains, plusflow_lenia. - Source:
alife_lsystem.kt,alife_evolve.ktandalife_grains.kt.
Open questions and next builds for the grain sets:
- A statistic for contour. The recording target matches
count, span, spread and median and still does not rise evenly, because no
GrainStatsays which way pitch moves over time. A slope of pitch against onset, or the share of steps that go up, would let a target ask for a shape. - Polyphonic targets.
grainsFromAudiois monophonic, so a recorded target always has a polyphony of about 1. Reading a chordal recording needs a multi-pitch detector, which Field does not have yet. - A target that needs a second rule.
nonterminals = "X"works, but on the arpy target, at the same budget and two seeds, it did not raise the best score with either alphabet. A target that rewards growth that changes from one generation to the next, such as a voice that gets one note longer each time, is the experiment that would show whether it helps. - Statistics for the new symbols.
<,>,landrchange grain length and pan, and nothing inGrainSetStatsmeasures either, so the search uses them only for their side effects on duration and polyphony. A length-spread and a pan-width statistic would make them targets. - A breeder you can drive live. The interactive box replaces all its players after every
choice; keeping past generations in a column would let you hear a lineage.
GrainBreeder.historyalready holds every generation and the parents chosen from it. - Listening to the map. The map shows sixteen
scores; a player per cell would let you choose a parent for
GrainBreederfrom it.
Open questions and next builds for Flow Lenia:
- Flow Lenia on the GPU. The FFT path makes 288 x 288 cells about 150 ms a step on the CPU, so
tens of thousands of steps, or 1024-cell worlds, still need the GPU. The step is a convolution, two
Sobel gradients and a scatter, the shape of the ping-pong compute lessons.
The scatter needs atomic adds or a gather
rewrite. Check it against
FlowLeniaWorldwithaffinity()and onestep(), asFlowLeniaTestchecks the FFT against the sparse convolution. - Search at the resolution you want to watch. The dart travels at 64 cells and stops at 128.
Run
flowLeniaGliderObjectiveonflowLeniaWorld(..., scale = 2)worlds, or score a found rule at both scales, so the winners keep moving when they are drawn large. - Selection with a measure of what is selected. Mutation changes who holds the mass, but nothing
yet says what the winning lineages do differently. Read
lineages[id].hagainst each lineage's mass over time, and test whether a winner's weights win again when seeded fresh. - Painting a rule with the pen.
paintWeightstakes a disc. Driving it fromnotation.onStroke, a stroke laying a rule along its path, would let a hand introduce mutants where it wants them. - Per-species mass as sound.
speciesMasses()andlineageHistoryare smooth, conserved series, a natural control signal for a synth: a competition you can hear.
Open questions for the rest of the page:
- Search beyond the saturated L-system objective. The dimension target in Does search beat random? cannot separate the search methods. A harder drawn target, such as matching a given silhouette, would.
- Lenia solitons in three dimensions. The Lenia genome and soliton objective are 2-D; the 3-D life-like automaton shows the grid side of that extension already exists.
- Trees that compete for sound. The light-competition trees could be read with the grain decoder, each branch a voice, so a forest's growth becomes a score.
See also
Growing neural cellular automata trains a local update rule to grow a target.
API reference
Implementation and parameter details for the helpers used above:
ShaderPhysarumSystem.installIntoTrailFollowers.installIntoLifeRuleGenomePopulationSearch.hillClimb-
Initialising the flock and measuring alignment:
BoidFlock—order,random. - The preset Flow Lenia species:
FlowLeniaPresets. - Advancing the mass-conserving simulation:
FlowLeniaWorld—totalMass. - Seeding the catalogue lifeform:
LeniaCreature—seed. -
The catalogue lifeform:
LeniaPresets—orbium. -
Growing and advancing bonded bodies:
BondedWorld,BondedWorld.grow,BondedWorld.run,BondedWorld.centroid.