Ant Colony Simulations


This is a simple example of an agent-based model that tries to emulate behavior of ants as they forage for food.

The basic principals of the simulation are;
1. Ants leave the nest moving randomly.
2. If an ant finds some food it picks it up and takes it back to the nest.
3. Ants carrying food leave a pheromone trail on their way back to the nest.
4. Ants looking for food will follow pheromone trails to have a better chance at finding food.

Once an ant finds food, it turns around 180 degrees and then heads towards the nest (with some random turns thrown in so their paths are not always straight lines). In reality ants seem to be able to use the sun position and landmarks to find their way back to the nest.

Using those very simple principals shows how a colony of ants with no other means of communication can coordinate to locate and retrieve food in the surrounding areas.

Programming the ants

For these simulations each ant has some simple properties that control its behavior like position, direction, maximum angle it can turn, move speed, sense angle and sense direction.

Position is the X,Y coordinates of the ant. Direction is the angle the ant is facing. Maximum angle determines how far an ant can turn left or right each step of the simulation. Move speed is how far the ant moves forward each simulation step.

For the process of how an ant looks for pheromones I based this on a similar way to how the Physarum Simulation particles work. An ant looks a certain distance and angle to the left, front and right ahead of itself and senses how many pheromones are in these three locations. The ant then turns towards the spot with the highest pheromone count. If an ant is moving randomly and gets near a pheromone trail it will turn towards the trail and hopefully follow the trail to find food.

The pheromones can be slightly blurred out each frame to simulate diffusion.

If an ant hits the edge of the world it turns 90 degrees and keeps moving.

I had an early attempt at ants simulation for many years that kept the ants on a fixed integer coordinate 2D grid. The change here to using floating point values for ant positions and angles really helped give more natural looking movement as shown in the animated gifs in this post.


Obstacles turned out to be a problem. If there is an obstacle between the food and nest and ant who finds the food will then head towards the nest and get stuck on the obstacle wall. They do eventually bump along the edge of the obstacle and make it around, but they should be smarter and follow a known “good trail”.

If an ant hit an obstacle I got it to only move randomly for the next 100 simulation steps and not leave any pheromones (as we don’t want a trail heading to an obstacle). Still doesn’t really help. Ants still get stuck at the obstacle. More thought and testing required.

Then I got a twitter reply from @psychobiotic suggesting having a foraging pheromone trail left by ants as they look for food. When an ant finds food it can then use the foraging trail to get back to the nest.

That works much better than my previous method above.


This new version of the ant simulation is now available in Visions of Chaos.


3 responses to “Ant Colony Simulations

  1. Been following your Blog and videos for several years. Its truly very inspiring and helped me get interested in coding simulations and mathematics! I look forward to future posts!

  2. Pingback: Can you recommend an interactive cellular automata/2D sparse grid visualization and computation framework? – Windows Questions

  3. Pingback: Can you recommend an interactive cellular automata/2D sparse grid visualization and computation framework? ~ Software Recommendations ~

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s