Agent-based models

Rabbits, Foxes and Grass

A live model of a small ecosystem, built from individual animals that each follow a few local rules that run on chance. Cycles, a steady balance or a collapse all emerge from those rules, though no equation for them appears anywhere in the model.

Step 0 Rabbits 0 Foxes 0 Grass 0%
Bare ground to full grass Rabbit Fox
Choose a scenario

Population over time

Phase plot: foxes against rabbits

Sliders change the running world at once; a setting you have moved away from the scenario's value turns garnet. The starting setup takes effect when you press Restart. Enter a seed to replay exactly the same run; leave it blank for a new run each time.

How to use it

The model starts running as soon as the page opens. Each square on the grid is one cell. Its colour shows how much grass it holds, from bare brown ground to full green, and a blue or red square is a rabbit or a fox standing on it. The chart below the grid traces both populations step by step.

The phase plot beside it shows the same history a different way. Each point is one moment, placed by the number of rabbits across and the number of foxes up, and the trail joins the moments in order. The last 200 steps are drawn dark, older history fades, and the dot marks the present. The axes fit the last 500 steps, so the early boom does not squash the later picture. Time disappears from this picture, which leaves the shape of the behaviour.

  • A loop means a cycle. The trail runs anticlockwise: rabbits rise (rightward), then foxes rise (upward), then rabbits fall (leftward), then foxes fall (downward), and round again.
  • A tight knot means a near-equilibrium. The trail wanders in a small region and never leaves it.
  • A line into an axis means extinction. If the trail hits the bottom edge the foxes are gone, and if it hits the left edge the rabbits are.
  • Choose a scenario to load a tested set of values and restart the run. Only one scenario is active at a time. They are the quickest way to see the model’s main behaviours.
  • Pause and Run stop and restart time. Step advances one step at a time, which is the best way to watch individual animals move, eat and breed.
  • Restart builds a new world from the current settings and starting setup.
  • Speed sets how many steps run each second, from 1 to 120.

Every event in the model is random, so two runs of the same scenario differ in detail. The pattern each scenario describes is what you should see on most runs.

The five scenarios

Each scenario changes only a few settings from the defaults. I tested each one on several random seeds, running 1,500 steps on the 80 by 80 grid, and the description gives what happened on all of them.

Fox, rabbit and grass cycles

The default settings

All three species coexist, and the populations rise and fall over and over. Rabbits multiply while grass is plentiful. The foxes, now well fed, multiply in turn and eat the rabbits down. The foxes then starve back, and the grass recovers in the gaps the rabbits leave. A full cycle takes roughly 80 to 160 steps. On the phase plot this draws large, irregular anticlockwise loops, no two quite the same.

Watch the grid as well as the chart. The cycle is not in step everywhere at once. Waves of rabbits sweep across green patches with foxes close behind, and leave brown ground that slowly greens again.

Near-equilibrium

Fox energy per rabbit 8 · fox energy to breed 30

Each rabbit is worth less to a fox, but a fox needs less stored energy to breed. The swings shrink and the populations settle and level off: rabbits stay between roughly 550 and 1,400 and foxes between roughly 400 and 550. The world still churns at the level of single animals, but the totals stay steady. The phase plot shows a small knot instead of a loop.

Rabbits and grass only

No foxes · grass regrowth 0.01 · rabbit energy to breed 15

With no predator, the only check on rabbits is food. They boom, strip the grass, starve back, and boom again when the grass returns, repeating about every 80 steps. These are clean cycles with no predator involved, driven by grass alone. With no foxes, the phase plot is a line running back and forth along the bottom edge.

Foxes starve

Grass regrowth 0.01 · fox energy per rabbit 8 · fox energy to breed 30

Slow-growing grass keeps the rabbits too thin on the ground to feed the foxes, and the foxes die out at around step 55 to 75. The rabbits survive and settle into their own boom and bust with the grass, about every 60 steps. The phase plot drops to the bottom edge and then slides back and forth along it.

Overhunting

Catch chance 0.5 · fox energy to breed 30

The foxes are too good at hunting. They eat every rabbit by around step 40 to 70, and then starve themselves by around step 80 to 100. The grass is left to grow back over an empty world. A predator that is too efficient destroys its own food supply. On the phase plot the trail swings out to the right as the rabbits boom, curls up and to the left as the foxes eat them, then runs down the left edge to zero.

The rules

The world is an 80 by 80 grid that wraps around, so an animal walking off the right edge reappears on the left. Every cell holds an amount of grass between 0 (bare) and 1 (full), and at most one animal. Each animal carries its own energy, its age and its own lifespan.

One step of time works like this.

  1. Grass grows. Every cell gains the regrowth amount, up to full.
  2. The animals take turns, in a random order. Each animal in turn does the following.
  3. It ages and burns energy. It gets one step older and uses one unit of energy. It dies if its energy reaches zero (starvation), if it reaches its lifespan (old age), or with a small chance each step (disease and accidents). Each animal’s lifespan is drawn when it is born and varies by about 20% around the setting.
  4. A rabbit moves and eats. With the move chance, it steps to whichever empty neighbouring cell has the most grass. It then eats up to half a unit of grass from its cell and gains energy in proportion to what it ate.
  5. A fox hunts or follows its nose. If a rabbit is next to it, the fox tries to catch it and succeeds with the catch chance. It then moves into the rabbit’s cell and gains energy. Otherwise it moves to the empty neighbouring cell with the most rabbits around it.
  6. It may breed. An animal with at least the breeding energy has the breeding chance to produce one young in an empty neighbouring cell. Parent and young split the parent’s energy evenly, so breeding costs the parent half its reserves.

Energy is capped, at 20 for rabbits and 60 for foxes, so an animal cannot store food without limit.

The settings

SettingDefaultWhat it controls
Rabbits
Energy to breed10Stored energy a rabbit needs before it can breed. Higher values mean slower breeding and better-fed parents.
Breeding chance0.30Chance each step that a rabbit with enough energy actually breeds.
Energy per meal4Energy from eating a full half-unit bite of grass. Less grass in the cell means a smaller meal.
Lifespan60Average maximum age in steps.
Random death0.010Chance each step of dying from disease or accident, whatever the rabbit’s condition.
Moves0.80Chance each step that a rabbit moves to better grass.
Foxes
Catch chance0.20Chance that a fox catches a neighbouring rabbit it goes after.
Energy per rabbit15Energy a fox gains from one rabbit.
Energy to breed45Stored energy a fox needs before it can breed.
Breeding chance0.20Chance each step that a fox with enough energy breeds.
Lifespan150Average maximum age in steps.
Random death0.005Chance each step of dying from other causes.
Grass and starting setup
Regrowth per step0.030Grass added to every cell each step. At 0.03 a bare cell takes about 33 steps to grow back fully.
Rabbits, foxes400, 20Animals placed at random when the world is built. They start with random energy and random ages.
Grid size80Width and height of the world in cells.
SeedblankFixes the random numbers, so the same seed and settings replay the same run.

Why this is an agent-based model

An agent-based model simulates a system as a collection of individuals, each following its own simple rules, and lets the behaviour of the whole emerge from their interactions. Nothing in the model describes the population directly.

Here the agents are the rabbits and foxes. Three features matter.

  • Each agent has its own state. Every animal keeps its own energy, age and lifespan. That is what lets hunger, breeding condition and old age matter, one animal at a time.
  • All interaction is local. An animal sees only the eight cells around it: the grass it can eat, the rabbit it can catch, the empty cell it can move or breed into. There is no global view and no plan.
  • Everything runs on chance, one agent at a time. Almost every rule fires with a probability, so the same starting world can unfold in different ways. The animals act in a fresh random order each step, and each one sees the world as the animals before it left it. This is what stops two foxes from eating the same rabbit, or two animals from moving into the same empty cell.

The grass is the landscape the agents live on: a grid of cells that each regrow on their own and are eaten down by the rabbits standing on them.

How this differs from the textbook model

The classical account of predators and prey is the Lotka–Volterra equations, two differential equations for the sizes of the two populations. They produce smooth, endless cycles, and their only inputs are population totals.

To work with totals alone, the equations have to assume that every fox is equally likely to meet every rabbit, as if the whole population were constantly stirred together. Real animals are not like that. A rabbit meets the foxes near it, eats the grass under it, and can survive in a patch the foxes have not reached yet. The equations never claimed to be realistic. They were built to show, as simply as possible, that predation alone can make populations cycle. A model on a grid puts back what that simplification leaves out.

This model has no population equation at all. It has individual animals that are born, eat, starve, grow old and die, on a landscape where food runs out locally and grows back slowly. The cycles it shows come out of those individual lives. They are irregular in length and height, differ from place to place on the grid, and can end, which the classical cycles never do. The rabbits-and-grass scenario shows that a food supply which takes time to recover is enough on its own to make a population cycle, with no predator at all.

It is still a toy. The units of energy and time are arbitrary, and the settings are chosen to make the behaviours clear, not fitted to any real population of rabbits or foxes.

Things to try

Each of these starts from the default scenario and changes one thing. I tested each on six random seeds; the results below held on all six unless noted.

  • Make the grass grow faster (regrowth 0.06). It sounds like it should help everyone, but the system collapses. The rabbits boom, the foxes boom after them and eat every rabbit, then starve. This echoes what ecologists call the paradox of enrichment, where more food at the bottom of a food chain destabilises it.
  • Make the grass grow slower (regrowth 0.015). All three survive, and the cycles become more regular, at about 70 steps.
  • Make the foxes clumsier (catch chance 0.1). The swings flatten out and all three coexist at steadier levels.
  • Make the foxes better hunters (catch chance 0.35). Usually the foxes overhunt and everything dies; on two of the six seeds the system survived with large swings.
  • Shrink the world (grid size 30, with 60 rabbits and 3 foxes, then Restart). Every run ends in extinction, because small populations are easily wiped out by a run of bad luck. A larger world (120, with 900 rabbits and 45 foxes) survived on every seed. Space itself keeps the system alive.
  • Stop the rabbits moving (Moves 0). The rabbits eat out their own cells and starve within about 50 steps, and the foxes follow. Being able to move to fresh grass is essential.