Nerdy creations with numbers, words, sounds, and pixels

Games, Science

Ant Nation

Ant Nation - ant simulation coded in MATLAB by Anton Hoyer

I cannot say precisely what drove me to start this project, as it has been a couple of years. Therefore, I will keep it shorter than usual for a project of this scope. “Ant Nation” is a simulation-like environment programmed in MATLAB developed to mimic the behavior of ants. They collect food from randomly spawned sources and carry it into their nest, thereby building ant roads.

If you are not familiar with how ants build their roads, they excrete pheromone trails to communicate with other ants when they find food. When an idle ant crosses the trail, it will begin following it to help. The more ants, the more pheromones, and the thicker their trails, creating a self-reinforcing phenomenon. Things get curious when these trails form around objects, tightening until the most efficient path is established, although I never quite got this far.

The first version was very simple, featuring only one single ant tribe and no pheromones. The ants would walk across the map until they found a source, then lock onto that source and carry food into their nest until it was depleted. I was surprised by how lifelike the ants appeared, even though they were represented by only two dots: one for the position of the current time step and one for the previous. I used polar coordinates because adding a random angular segment to the current orientation angle in each time step made the resemblance of actual, convoluted ant walks uncanny.

First version of Ant Nation

The first version was good enough to be used in some homework for an Academic English class where I was stubborn enough to use it instead of some topic from my engineering studies. Afterwards, I let the project rest, only picking it up again when I had become much more proficient at MATLAB due to my Brushing MBS.

For the second version, I decided to start from scratch and implement pheromones this time. Therefore, I discretized the map into a grid of pixels. While the ants could move freely, they would always have an actual position to calculate their motions and a discretized position to mark which field they are on. Whenever an ant carries food, it adds a bit of pheromone to the respective field on the pheromone map, which decays over time.

The ants only interact with a 3-by-3 neighborhood around them, meaning that three rear pixels drop pheromones, and three front pixels sniff them, with the center pixel always being twice as efficient as the side pixels. (One particular comment from the code stuck with me over the years: “Center ass lays twice.”) Whenever an ant sniffs a trail, it follows it. The direction does not matter because even if the ant finds the nest first, it will follow its peers that just dropped food off back to the source.

Naturally, the ants also needed a collision boundary to prevent them from wandering off. The question was, how do ants turn around? I tested square and circular boundaries and found out that, just like in physics, “incidence angle equals emergent angle” looked like the real thing. After turning around, they would often begin to follow the contour of the boundary. To mess with my ants a bit, I also tried distributing random collision pixels across the entire map. In fact, their motions and trails looked a lot more realistic afterward.

I developed an object-like structure to store the individual ant tribes, which in turn contained the individual ants. They had variables for health points and could starve to death when no food was in the nest. (I took the liberty to feed them telekinetically, inspired by a late update of one of my favorite games, Banished). As the ants were displayed as single pixels this time, their transparency was an easy way to show their health.

Stable simulation with four ant tribes

Some ants were tanks, which had a lot of health points, could carry more food, had a lower maximum velocity, and were more expensive to spawn. Others were quite the opposite. I never got around to making the AI smart, so it often would let all ants starve and spawn new cheap ones with the fresh food, always maintaining an overall food stock of around zero.

However, my ants had one major flaw: once their inventories were full of food, they did not find their way back to the nest. I do not know how real ants do it; maybe they use the sun for orientation, or they are just so plentiful that there is always a pheromone atmosphere that gets thicker when moving towards the nest. Thus, I simplified matters by letting a percentage of ants know intuitively where the nest is at. I called these ants “scouts” and found that around 37% of scouts are needed in total for the tribe to survive, depending on the other parameters.

To keep things interesting, and since I had already laid the foundation with my data structure, I implemented multiple tribes that combat each other. Whenever two ants of different tribes met, they would fight, either harming each other or fighting until death. The tanks would always win. Theoretically, there could be as many tribes as there were ants, but I developed color schemes for only four: fire tribe (magenta ants, red trails), water tribe (cyan ants, blue trails), earth tribe (yellow ants, green trails), and air tribe (white ants, gray trails).

Next up was implementing seasons, starting in “early spring” and ending in “late winter,” which I also took from Banished. The temperature would change over the year, and with it, the total velocity of the ants. Also, food would be abundant in fall and scarce in late winter. All these parameters, for example, the number of food sources, the food per source, the ants’ inventory size, the map size, and the pheromone decay rate, required a lot of balancing. If the number of food sources was too low, or the distances became too large, or there were not enough scout ants, the simulation would become unstable, and the ant populations would die. You can see this in the time-lapse animation below.

Timelapse of unstable simulation (not enough food, map too large)

Another phenomenon I noticed, especially with non-scout ants, were closed loops on which the ants would walk until they either broke loose or more likely starved to death. On a larger scale, this happens with actual ants as well if they are separated from their main party. The ants begin following their pheromone trail and walk in a death spiral until they die from exhaustion, which is called an “ant mill.” You can see a simulated ant mill in the image below (if you have not already spotted one in the animations above).

Eight ants being trapped in an ant mill

Often, I just sat in front of the screen and rooted for the little guys to survive. Not to sound profane, but sometimes I felt like a god watching my own creation suffer or thrive—not being able to intervene because it all just depended on the starting parameters of the simulation. However, unlike humankind, I never gave my ants the ability to evolve.

In fact, letting the ant tribes adapt their behavior to that of other tribes and environmental variables would be an interesting path to take from here, if I ever were to continue the project. For example, letting the tribes decide intelligently when to spawn slow, costly tank ants or fast, cheap ones. Tribes could choose to coexist peacefully or act aggressively to dominate. Giving the earth, fire, water, and air tribes slightly different characteristics might lead to interesting results similar to historic experiments in game theory, such as the Axelrod’s Tournament. I imagine the most successful ant tribes to be tendentially nice, retaliative when wronged, but eventually forgiving.

← Return to “Simulation”

← Return to “Science”

Leave a Reply