Physarum (Slime-Mould Agents)

also: slime mould, Physarum transport network, agent trail-following

First appeared 2010 · Jeff Jones — The widely-used computational model was formalized by Jeff Jones, "Characteristics of pattern formation and evolution in approximations of Physarum transport networks" (Artificial Life, MIT Press, 2010). · timeline ↗

community / folk Name — Descriptive: named after the slime mould Physarum polycephalum whose transport networks it approximates (model formalized by Jones, 2010); not a personal eponym.

In one line An agent-based model in which many simple particles each sense, follow, and deposit onto a shared diffusing trail map. From purely local sense–deposit–move rules the agents self-organise into dynamic, vein-like transport networks that approximate the foraging behaviour of the slime mould Physarum polycephalum.

Physarum (Slime-Mould Agents) · Open in new tab ↗ · Motion begins only when you press Run.

The code

The actual sketch behind the demo above — sandboxed and reproducible. This is the whole thing.

physarum/index.html
// Physarum (slime-mould) transport networks, after Jeff Jones' agent model
// (2010). Each of many agents senses the chemo-attractant trail at three points
// ahead (left / centre / right), steers toward the strongest, steps forward and
// deposits more trail. The trail field diffuses (blur) and decays every tick.
// Purely local sensing + reinforcement → emergent vein-like networks. Canvas 2D.
const SEED = (Math.random() * 1e9) | 0;
let rs = SEED >>> 0 || 1;
function rnd(){ rs ^= rs<<13; rs ^= rs>>>17; rs ^= rs<<5; rs >>>= 0; return rs / 4294967296; }

const cv = document.getElementById('c');
const ctx = cv.getContext('2d');
const DIM = Math.floor(Math.min(window.innerWidth, window.innerHeight));
const RES = Math.min(DIM, 400);   // higher res + small sensor ⇒ many channels (a fine mesh, not a few lines)
cv.width = cv.height = RES;
cv.style.width = cv.style.height = DIM + 'px';

// --- Jones parameters (the values that reliably reticulate) ---
const N        = Math.floor(RES * RES * 0.30);   // agent density
const SO       = 5;                              // sensor offset distance (px) — small vs the grid ⇒ fine mesh
const SA       = 22.5 * Math.PI / 180;           // sensor angle
const RA       = 22.5 * Math.PI / 180;           // rotation (turn) angle
const STEP     = 1.0;                            // move distance
const DEPOSIT  = 3.0;                            // trail dropped per step
const DECAY    = 0.96;                           // multiplicative decay after diffuse (higher ⇒ veins persist,
                                                 // so more of the network is visible at once)
const CAP      = 255;                            // trail saturation (bounds runaway feedback)
const DIFFUSE  = 0.35;                           // blur strength — light, so distinct veins survive
const NOISE    = 5 * Math.PI / 180;              // heading jitter/step — breaks the perfect alignment
                                                 // that otherwise locks agents into parallel bands

let trail = new Float32Array(RES * RES);
let buf   = new Float32Array(RES * RES);
const ax = new Float32Array(N), ay = new Float32Array(N), ah = new Float32Array(N);
for (let i = 0; i < N; i++) {
  ax[i] = rnd() * RES; ay[i] = rnd() * RES; ah[i] = rnd() * Math.PI * 2;   // uniform seeding
}

const wrap = (v) => (v % RES + RES) % RES;
function senseAt(x, y, a) {
  const sx = wrap(Math.round(x + Math.cos(a) * SO));
  const sy = wrap(Math.round(y + Math.sin(a) * SO));
  return trail[sy * RES + sx];
}

function agents() {
  for (let i = 0; i < N; i++) {
    const x = ax[i], y = ay[i], h = ah[i];
    const F  = senseAt(x, y, h);
    const FL = senseAt(x, y, h - SA);
    const FR = senseAt(x, y, h + SA);
    let nh = h;
    if (F > FL && F > FR) { /* strongest ahead → keep heading */ }
    else if (F < FL && F < FR) nh = h + (rnd() < 0.5 ? -RA : RA);   // valley ahead → random turn
    else if (FR > FL) nh = h + RA;                                  // steer right
    else if (FL > FR) nh = h - RA;                                  // steer left
    nh += (rnd() - 0.5) * 2 * NOISE;                                // small jitter
    const nx = wrap(x + Math.cos(nh) * STEP);
    const ny = wrap(y + Math.sin(nh) * STEP);
    ax[i] = nx; ay[i] = ny; ah[i] = nh;
    const di = (ny | 0) * RES + (nx | 0);
    trail[di] = Math.min(CAP, trail[di] + DEPOSIT);                 // deposit (saturating)
  }
}

function diffuseDecay() {
  for (let y = 0; y < RES; y++) {
    const y0 = ((y - 1 + RES) % RES) * RES, y1 = y * RES, y2 = ((y + 1) % RES) * RES;
    for (let x = 0; x < RES; x++) {
      const xl = (x - 1 + RES) % RES, xr = (x + 1) % RES;
      const s = trail[y0+xl]+trail[y0+x]+trail[y0+xr]
              + trail[y1+xl]+trail[y1+x]+trail[y1+xr]
              + trail[y2+xl]+trail[y2+x]+trail[y2+xr];
      const c = trail[y1 + x];
      // light blur: blend current cell toward the 3x3 mean, then decay
      const v = (c + (s / 9 - c) * DIFFUSE) * DECAY;
      buf[y1 + x] = v > CAP ? CAP : v;
    }
  }
  const t = trail; trail = buf; buf = t;
}

const img = ctx.createImageData(RES, RES);
function render() {
  const d = img.data;
  for (let i = 0; i < RES * RES; i++) {
    const v = Math.min(1, trail[i] / 40);           // normalise (lower ⇒ faint secondary trails show)
    const g = Math.pow(v, 0.7);                      // gamma for glow
    d[i*4]   = 255 * Math.min(1, g * 1.15);          // warm yellow-white veins
    d[i*4+1] = 255 * Math.min(1, g * 0.95);
    d[i*4+2] = 255 * Math.min(1, g * g * 0.55 + g * 0.15);
    d[i*4+3] = 255;
  }
  ctx.putImageData(img, 0, 0);
}

function tick() { agents(); diffuseDecay(); }
function loop() { tick(); tick(); render(); requestAnimationFrame(loop); }
loop();

Open the demo source ↗

How it works

Maintain a 2D trail map (a chemoattractant field) and a population of agents, each with a position and heading. Each step: 1. Sense: each agent samples the trail map at three points ahead (left, centre, right of its heading). 2. Rotate: it turns toward whichever sensed point holds the most trail. 3. Move: it steps forward and deposits a fixed amount of trail at its new position. Between steps the trail map is blurred (diffused) and multiplied down (decayed). The interplay of deposition, diffusion and decay makes the agents reinforce shared paths, producing self-organising, continuously rewiring networks.

Parameters

Lineage

Precedents
Sage Jenson
Related
Particle System, Reaction-Diffusion, Space Colonization

Works using this algorithm (1)

Physarum
sage-jenson

Last updated 2026-07-16