Interactive explorable · Ant-inspired collective behavior
SwarmRules.
How can a swarm organize without a leader? These simplified ant-inspired agents move, deposit fading chemical trails, and steer using only nearby signals. Change their local rules and watch collective patterns emerge—without a leader or map.
01 / Trail formation
How does the model work?
Can global structure emerge from purely local interactions? This model deliberately gives every ant-inspired agent only a heading, a position, two local pheromone readings, and random angular noise. There is no leader, global map, planning, or direct interaction between agents.
1. Agents and environment
We simulate N = 128 point-like agents in a continuous two-dimensional world of size 96 × 72 (simulation length units). Agent i has a position ri = (xi, yi) and a direction θi. Agents move at the fixed speed v = 0.5 length units per turn. They can overlap: there are no collisions, exclusion forces, or physical obstacles in this first experiment.
The boundaries are periodic: moving beyond the right edge re-enters at the left, and similarly for top and bottom. The pheromone field and sensing also wrap periodically. Initial positions and headings are random; the seed determines the random sequence.
2. A shared pheromone field
The environment stores one nonnegative scalar concentration φ on a 96 × 72 grid, with 0 ≤ φ ≤ 1. All agents read and modify this same field. There is no diffusion: the pheromone spreads only because moving agents deposit it in different places.
Each turn, pheromone first loses a fraction δ through evaporation. After moving, each agent adds D = 0.01 to the grid cell it occupies. Deposition is additive but concentrations are capped at 1:
Here c is a grid cell and nc is the number of agents that arrive in that cell during the turn. Without new deposition, concentration follows exponential decay, φt+k = (1 − δ)k φt. Thus δ = 0 means no evaporation, while a larger δ makes trails short-lived.
3. Local sensing
Each agent samples φ at two points ahead of its current position, separated symmetrically by ±45° around its heading. Both sensors are at distance ds = 2.5 from the agent:
Sensor locations are continuous, although the field is stored on grid cells. Values between cell centers are obtained by bilinear interpolation from the four nearby cells. The agents never measure the entire pheromone field or the positions of other agents.
4. Steering and movement
Agents turn toward whichever of their two sensors detects the stronger signal. The difference is normalized by the total signal plus φ₀; this limits the response and prevents division by zero when no pheromone is present. Independent random angular noise is added at every turn:
Each ξi is an independent random number from a standard normal distribution (mean 0, variance 1). Once the heading has changed, the agent moves straight by one fixed-length step:
In the absence of pheromone differences, only the turning-noise term changes an agent's direction. In the absence of noise, the agent turns deterministically according to its local pheromone readings.
5. Parameters you control
| Parameter | What changing it does |
|---|---|
| σ · Turning noise | Standard deviation of the random turn per step (radians). Increasing it makes movement less predictable and can disrupt trails. |
| λ · Trail following | Strength of steering toward the larger local signal. At λ = 0, sensing does not affect motion. |
| δ · Trail evaporation | Fraction removed each turn. Lower values preserve environmental memory longer; higher values favor recent activity. |
| φ₀ · Steering damping | Positive offset in the steering denominator: increasing it suppresses turning, particularly in weak pheromone fields. Lower values strengthen the response. |
The population N is also adjustable from 1 to 1,024. Because each ant independently deposits the same amount D, doubling N changes how quickly pheromone can accumulate and how much collective reinforcement occurs. At N = 1 there are no interactions between different agents.
The remaining controls, other-state following (λₒ) and marking distance (L), belong to the later food–nest experiments and are inactive here. The separate simulation speed control only changes how many turns are computed per displayed frame; the physical rules are unchanged. Changing sliders affects subsequent turns immediately; Reset restarts the agents and clears the pheromone field using the current settings, population, and seed. Changing the population slider adds or removes agents without clearing the existing field.
6. Turn order and what you see
- Every agent samples the same current field using its two forward sensors.
- Agents change heading (trail response + noise) and move one step with periodic wrapping.
- The entire field evaporates by the factor (1 − δ).
- Agents deposit D in their destination cells, up to the concentration cap of 1.
- The display redraws the current field and ant positions.
About the colors: To make small concentrations visible, the display adjusts contrast to the current field (using its 95th percentile) and hides extremely weak traces. This is visualization only: the agents sense the actual concentrations, not the displayed colors. The ant drawings and walking animation likewise have no effect on the physics.
Why patterns emerge: an ant reinforces places it visits; those trails influence the motion of other ants, which may reinforce them further. This feedback competes with noise and evaporation. The resulting networks or traveling trails are collective effects of local interactions—not paths encoded in any agent's memory. These are simplified ant-inspired agents, not a detailed biological model of real ants.
How can independent ants establish a two-way route between food and nest? Foraging adds two source-specific chemical fields and a two-state behavioral rule. The ants still sense only nearby concentrations; they have no map, target coordinates, or global path planner.
1. Agents and the arena
N = 128 agents move in a 96 × 72 continuous arena at constant speed v = 0.5 units per turn. Each agent has position ri, heading θi, a behavioral state, and one scalar source-memory strength qi. The FOOD region occupies x ∈ [6,18), y ∈ [30,42), and NEST occupies x ∈ [78,90), y ∈ [30,42). Unlike trail formation, the arena has solid black reflecting walls rather than periodic boundaries. Ants can overlap and can pass through the endpoint regions.
How can a swarm discover a detour through an opening? This experiment uses exactly the same local sensing, state changes, steering, deposition, and evaporation as Foraging, but places a solid barrier between FOOD and NEST. No agent knows the location of the opening.
1. Agents, sources, and obstacle geometry
N = 128 agents move at speed v = 0.5 in a 96 × 72 world. The FOOD region is [6,18) × [6,18), and NEST is [78,90) × [6,18). One-unit-thick black outer walls bound the domain. The vertical internal barrier consists of two solid rectangles, [46,50) × [1,38) and [46,50) × [50,71). Only the 12-unit opening, 38 ≤ y < 50, connects the two sides. Food and nest are intentionally above the opening, so the shortest direct line is blocked.
All agents initially occupy free space, outside the food, nest, and solid wall regions. They can overlap each other but cannot enter walls. The two pheromone fields are not diffused through the obstacle; agents only deposit at their own locations. Sensor readings are local field samples, not sight of a destination.
2. Two environmental pheromone fields
The arena contains two independently decaying, non-diffusing grid fields, both bounded between 0 and 1: PN (nest-sourced, blue) and PF (food-sourced, amber). Neither field provides direct information about its source's direction; it only contains concentrations left by previous visitors.
3. Behavioral states and source memory
Each ant is either a FORAGER (searching for FOOD) or a RETURNER (transporting FOOD back to NEST). All agents start as FORAGERs with zero source memory. When a FORAGER enters FOOD, it becomes a RETURNER; when a RETURNER enters NEST, a delivery is counted and it becomes a FORAGER again. Each such state switch includes an exact 180° turn. Merely revisiting the source for one's current state refreshes memory without another U-turn.
On each fresh visit to either endpoint, source memory resets to 1. As the ant travels, memory decreases linearly with distance traveled d since its most recent endpoint visit:
L is the marking distance: how far an ant can travel from its last source visit before its deposit vanishes. The memory strength q affects deposition only, not the steering rule. A newly initialized FORAGER has q = 0, so it does not falsely mark a nest it has never visited.
4. Local sensing and steering
As in trail formation, each agent has two virtual sensors at distance rs = 2.5, located at angles θ ± π/4. Concentrations at sensor positions are found by bilinear interpolation of each grid field. For either field P, define its normalized side-to-side contrast:
FORAGERs respond to their same-state nest trail PN and the opposite-state food trail PF. RETURNERs exchange those two roles. The steering equations are:
λs controls following of trails deposited by ants in the same state; λo controls following of trails from the other state. σ sets the random turning noise, while a larger φ₀ dampens steering toward pheromones, especially weak signals. Neither rule says which way along a trail to travel; heading persistence and the U-turn at each endpoint produce two-way traffic.
Positions update using the new heading:
Wall reflections: after steering and a tentative displacement, a step that enters a wall is reflected specularly at its first contact. A vertical face changes the x-component of velocity and θ → π − θ; a horizontal face changes the y-component and θ → −θ. The remaining part of the turn is completed on the accessible side. The arena outer walls behave the same way. Walls do not attract ants, carry pheromone, or inform them about the gap.
5. Deposition and evaporation
Each turn, pheromone concentrations are multiplied by (1 − δ). Then a RETURNER deposits Dq into PF at its new position; a FORAGER deposits Dq into PN. Here D = 0.01, and deposits at a grid cell add and are capped at 1. There is no pheromone diffusion.
Here Ii,cell is 1 if ant i occupies the cell. The order of operations is: sense and steer → move and reflect from solid walls → reduce source memory → handle endpoint visits (including state changes and possible U-turns) → evaporate both fields → deposit pheromone.
6. Parameters and emergent routing
| Slider | Effect |
|---|---|
| Turning noise σ | Standard deviation of the random turn each step (radians). |
| Same-state following λs | Steering response to pheromone deposited by agents in the same state. |
| Other-state following λo | Steering response to pheromone deposited by agents in the other state. |
| Trail evaporation δ | Fraction of each field lost per turn. |
| Steering damping φ₀ | Denominator offset; larger values weaken the response to trail differences. |
| Marking distance L | Travel distance after a food or nest visit at which pheromone marking reaches zero. |
What the colors show: To show subtle structures, each displayed field is contrast-scaled using the 95th percentile of positive concentrations. Its color intensity is not the absolute pheromone concentration and is not used by the ants. The two colors represent separate, actual environmental fields—not directions painted onto the screen.
Population: N is adjustable from 1 to 1,024. Adding agents increases the number of possible deposits and encounters; no per-ant deposition correction is made when the population changes.
Why routing can emerge: source visits create chemically distinct trail segments; these affect the behavior of other ants, reinforcing frequently traversed paths. The number of completed FOOD → NEST trips is shown as deliveries. In the obstacle scenario, successful trips must traverse the opening; repeated passage can reinforce this detour. There is no global coordinator or route optimization algorithm. The agents are ant-inspired abstractions, not a detailed biological model of ant foraging.
Background and references
This is a simplified, educational, ant-inspired model. The following publications provide biological and active-matter context; the applet does not numerically reproduce their models or findings.
- Deneubourg, J.-L., Aron, S., Goss, S. & Pasteels, J. M. (1990). “The self-organizing exploratory pattern of the Argentine ant.” Journal of Insect Behavior 3, 159–168. doi:10.1007/BF01417909.
- Czaczkes, T. J., Grüter, C. & Ratnieks, F. L. W. (2015). “Trail pheromones: An integrative view of their role in social insect colony organization.” Annual Review of Entomology 60, 581–599. doi:10.1146/annurev-ento-010814-020627.
- Ziepke, A., Maryshev, I., Aranson, I. S. & Frey, E. (2022). “Multi-scale organization in communicating active matter.” Nature Communications 13, 6727. doi:10.1038/s41467-022-34484-2.