Ant trail following simulation, reproducing results from the paper Modeling the Formation of Trail Networks by Foraging Ants (Watmough and Edelstein-Keshet, 1995).
The simulation can be run online here!
Alternatively, you can clone and run the code locally:
# Clone the repository
git clone https://github.com/olincollege/scicomp-p1-il-ants.git
cd scicomp-p1-il-ants
# Create virtual environment (optional but recommended)
python -m venv ants-venv
# Activate virtual environment (if created)
# Linux / macOS:
source ants-venv/bin/activate
# Windows:
ants-venv\Scripts\activate
# Install dependencies
pip install -r requirements.txt
# Run simulation
python src/main.pyThe simulation is depended on a set of constants, which can be modified using the UI. Here is a brief description of each constant, further explanation can be found in the Algorithm Overview section.
- seed: Random seed for reproducibility.
- fidelity min: Minimum random chance (out of 256) for an ant to lose the trail it's following. See Fidelity Function.
- fidelity delta: Maximum increase in fidelity based on pheromone concentration. See Fidelity Function.
- pheromone deposition: Amount of pheromones deposited by each ant at each time step.
- pheromone saturation: Pheromone concentration at which fidelity reaches its maximum value. See Fidelity Function.
-
turning kernel: Probability distribution used to determine how an ant
turns when it moves randomly.
$B_n$ is the probability of turning$n \times 45°$ . See Turning Kernel.
Constant presets match of specific figures from the paper, see Results for more details.
The code is organized into five main files:
main.py: Main entry point for the simulation. Initializes the simulation and interactable app.ant.py: Contains theAntclass and logic for ant movement.simulation.py: Contains theSimulationclass, which spawns in ants and tracks pheromone levels.app.py: Contains theAppclass, which uses pygame to create an interactive visualization of the simulation.constants.py: Contains the constant presets for different figures in the paper.
Ants are the main creatures of the simulation. Each ant is represented by a position and a heading. The heading is one of 8 possible directions (N, NE, E, SE, S, SW, W, NW).
This simulation shows the trail formation of ants on a 256x256 grid world. As ants traverse the world, they deposit pheromones that other ants can sense and follow. The simulation performs the following steps at each time step:
- A new ant is spawned in the center of world, facing a random diagonal direction.
- Each ant deposits pheromones at their current location. The amount of
pheromones deposited is determined by
$\tau$ ,pheromone_deposition. - Each ant moves according to the trail following algorithm (explained below).
- Ants that move out of bounds are removed from the simulation.
- Pheromones everywhere evaporate by 1 unit.
Each ant senses pheromones in the spaces directly in front, 45° to the left, and 45° to the right of where it's facing. It moves according to the following algorithm:
- There is a random chance for the ant lose the trail it's following, determined by the fidelity function (explained below). If the ant loses the trail, it moves randomly according to the turning kernel (explained below).
- If there are pheromones directly in front of the ant, it moves forward.
- If there are pheromones to the left and right of the ant, it turns in the direction with more pheromones.
- If there are equal amounts of pheromones to the left and right of the ant, it moves randomly according to the turning kernel (explained below).
The fidelity function is used to determine the probability that an ant will lose
the trail it's following. It is a function of the amount of pheromones in the
ant's current location,
-
$\phi_{min}$ :fidelity_min -
$\phi\Delta$ :fidelity_delta -
$C_s$ :pheromone_saturation
The fidelity function is defined as follows:
The ant has a
The turning kernel
In figure 3, all constants were kept the same except for fidelity minimum,
- fidelity delta,
$\phi\Delta$ = 0 - pheromone deposition,
$\tau$ = 8 - pheromone saturation,
$C_s$ = 0 - turning kernel,
$B$ = (0.581, 0.36, 0.047, 0.008, 0.004) - simulation run for 1500 time steps
| My Simulation | Paper Figure 3A |
|---|---|
![]() |
![]() |
| My Simulation | Paper Figure 3B |
|---|---|
![]() |
![]() |
| My Simulation | Paper Figure 3C |
|---|---|
![]() |
![]() |
In all three of these figures, my simulation yielded much higher trail density with many more ants still in the simulation. Curiously, the figures in the paper write that less than 500 ants remain in the simulation at the end of 1500 time steps for all three examples, while in each of my simulations ~1100 ants remain. The result from the paper would imply that 1000 ants leave the world in the 1500 time steps, which seems unlikely. Some of my peers have speculated that the number of ants in the paper is capped to a certain value, which could explain the discrepancy.
In figure 4, all constants were kept the same except for pheromone deposition,
- fidelity minimum,
$\phi_{min}$ = 247 - fidelity delta,
$\phi\Delta$ = 0 - pheromone saturation,
$C_s$ = 0 - turning kernel,
$B$ = (0.581, 0.36, 0.047, 0.008, 0.004) - simulation run for 4000 time steps
| My Simulation | Paper Figure 4A |
|---|---|
![]() |
![]() |
| My Simulation | Paper Figure 4B |
|---|---|
![]() |
![]() |
| My Simulation | Paper Figure 4C |
|---|---|
![]() |
![]() |
A similar result to figure 3. My simulation yielded much higher trail density with many more ants still in the simulation. Figure 4C looks quite similar between my simulation and the paper, likely the low pheromone deposition means most trails evaporate, leaving only the large "X" pattern.
In figure 5, a new turning kernel is used, favoring narrower turns. All
constants were kept the same except for fidelity minimum,
- fidelity delta,
$\phi\Delta$ = 0 - pheromone deposition,
$\tau$ = 8 - pheromone saturation,
$C_s$ = 0 - turning kernel,
$B$ = (0.822, 0.135, 0.031, 0.008, 0.004) - simulation run for 1500 time steps
| My Simulation | Paper Figure 5A |
|---|---|
![]() |
![]() |
| My Simulation | Paper Figure 5B |
|---|---|
![]() |
![]() |
| My Simulation | Paper Figure 5C |
|---|---|
![]() |
![]() |
These figures match up fairly well, though the my simulation still has many more ants remaining. The narrower turning kernel results in much straighter and more clean trails, which matches the paper's results.
In figure 6, pheromone saturation is finally introduced and is the main variable being changed. The following constants were used:
- fidelity minimum,
$\phi_{min}$ = 20 - fidelity delta,
$\phi\Delta$ = 235 - pheromone deposition,
$\tau$ = 6 - turning kernel,
$B$ = (0.581, 0.36, 0.047, 0.008, 0.004) - simulation run not listed in paper, my simulation was run for 1500 time steps
| My Simulation | Paper Figure 6A |
|---|---|
![]() |
![]() |
| My Simulation | Paper Figure 6B |
|---|---|
![]() |
![]() |
These results also don't match up so well. In the paper, strong trails struggled to form, while my simulation shows a network of paths simular to figure 3. However, compared my figure 3 the trails are spareser, matching the trend of the paper's results slightly. Again, my simulation has many more ants remaining than the paper's.
























