How does natural selection affect ants trying to survive in a limited area?

Simon Kuijper 500761163


The process of natural selection has always intrigued me. The fact that species constantly change and that gruesome monsters that you would think would be able to survive forever, like the dinosaurs, somehow go extinct. This is also why it really interests me to make genetic algorithms, as they work in a similar way. 

In this report we will look at how natural selection plays out in a digital plain with ant colonies. I chose this topic, as I am fascinated with ants. I find it very impressive how an individual ant can’t accomplish much, but many ants as a colony together can. 

Therefore the question that I will try to answer in this report: “Which genes are fittest for ants in different digital areas?” We will look at how certain genes like speed, armour and strength chosen or discarded.

Genetic algorithms

In 1859 Charles Darwin first presented his idea of evolution. He stated that species slowly changed over time to fit a specific niche in their environment. A great example of this from Darwin’s own travels would be the different species of finches on the Galapagos Islands. At some point in history some of these birds made it over to the island and made it their home. 

On the Galapagos Islands there were many sources of food available to eat, however the finches were not specialised in eating most of them. This meant that for example the finches were not able to use their beaks for fishing effectively for example, or catching insects. But like the fact not all humans are the same, not all finches are the same either, and some of the finches’ beaks might have been smaller than the others’. This meant they had a huge advantage as they could specialise in eating from different sources of food, for which they had no competition. 

The fittest is often translated as the strongest. As we generally think of the word ‘fit’ when talking about this subject. This view is wrong however, usually. The fittest actually refers to how well an organism is able to ‘fit’ into its environment. If the environment required creatures to be really strong, then of course it will mean that survival of the strongest. Often enough on the other hand, different traits will also be important to survive in an environment. Think of a high speed, or sharp claws. But what happens if there is very little food available for your species in winter? In that case would it not be better to have a very low metabolism so you can survive for months without food? In which case you don’t need those fancy sharp claws or strong muscles, which will only cost you a lot of energy to maintain. You just need to eat as much as you can during the summer months. Hide in a hole, and wait for all the animals with strong muscles and sharp claws to die out in the winter months. 

Genetic algorithms work in a similar way. The genetic algorithm will select on the fittest solution and will make several new variants based on the fittest one from the previous generation. 

Crossover is very important in the genetic algorithm. It means that certain genes are entirely swapped from either parent. This makes sure that the genetic diversity remains high, while also getting the best gene from the parent.  

One of the reasons you don’t just average the genes is that it causes the child to never be as good as either parent and more importantly, it creates a lack of diversity. You would much rather have one of the children inherit all the bad traits and die, and another child have all the good traits and become a lot more fit in the environment and reproduce further. 

Another important thing in both genetic algorithms as well as in evolution is mutations. These are random changes in the code or DNA which can either have a positive effect on the individual or a negative. From an evolutionary point of view, mutations are a good thing, because they increase the genetic diversity in the species. It can make an individual more fit in its environment, and therefore reproduce more effectively, which in turn can help the entire species. Of course, the individuals for which the mutation was a bad thing die out. 

Of course you cannot write the full code of DNA for each creature in your programme, but you can give a creature a certain speed or size. To make sure the creatures do not become the biggest, fastest and strongest, you usually add some drawbacks to be better at something. This often involves the higher consumption of energy for example.

How the programme works


The basic drone of every hive. They try to look for food around the hive and will return to the hive once they have found it. If they find an ant from another colony they will try to attack and kill it. Because the ants are born from one queen ant in the hive, they all share the same DNA. 


Food is spawned randomly around the map, ants collect as much as they can carry from this food and carry this to the hive. It also decays slowly over time. If there is no more food left in the object, it will disappear. The food is spawned in with a random value ranging from 30 to 100 food. 


Ants store all their food in the hive.New ants will also spawn at the location of the hive. After a certain length of time, the queen in the hive will die from old age. Once the queen dies, the hive and its ants will die and disappear. Another way for the hive and colony to die off is for all the ants to be killed by the ants of other colonies. Once this happens, the hive will disappear as well. 


After a certain amount of time has passed, two ant colonies will be selected to mate and make another ant colony. Only let the fittest ant colonies are able to reproduce. In this case I define “fittest” by the amount of ants that are part of the colony. 

Fittest ant colony I define by the amount of ants they have in the colony. This is because gathering food and creating as many new ants as possible is the goal of the hives. This new colony spawns at a random location on the plain. But every time a new colony is created, there is a chance of mutation, increasing or decreasing one of the statistics of one of the genes. 


With states every ant can only have one state they can be in at a time. This makes your code more readable as well as reducing the risk of bugs. The states I used are: 


The ants are in this state by default. The ant will do this by choosing a certain location within the area of its hive, and then move towards it. It will keep doing this until the ant detects food or an ant of another hive along the way it will switch to another state. 

Found food

Every ant has a certain radius in which it is able to sense things like food. Once the ant has sensed that it is food, it will move towards it and will take as much as it can carry away from the food source. 

Returning food

Because ants work in a hive, they don’t eat all the food they find themselves, but will return it to the hive to feed the queen and the larvae of the colony. The ant will take some of the food source straight to the colony. The amount of food carried is determined by the strength value of the an. 


If the ant senses an ant from another colony in it’s ‘sense radius’, it will move towards it and fight the other ant. The armor value of these ants will be taken into account when deciding a winner. But this is an ant war after all, which means that there can be only one winner and the other ant is killed.


Once two ants of two different colonies meet each other they will engage in combat. The armor value of these ants will be taken into account when deciding a winner.


The way the genes currently work is that each of the following attributes has a certain value, these are: Armor, Speed and Strength. Armor is used in combat, with each point in armor value giving a 5 percent combat advantage. Speed like the name implies increases the speed at which the ant can move. Strength is currently only used to increase the gathering rate, with each point in Strength being able to take 20% more food with them on the way back to the hive. 

The results

First iteration

I was very excited to test the programme to see what would happen. 

As you can see, the genes didn’t diverge that much. This is probably due to the fact that the new hive colonies only copy from their parents’ genes, and don’t necessarily improve upon them. Normally mutations will change the genes of the new hive a little bit, but because the mutations also have a chance of lowering the stats, it ended up both increasing as well as decreasing the genes, thus not much happened. One notable conclusion however is, that speed seems to be really important. This is probably because this gene helps a lot in gathering food. And because food helps getting more ants, this gene was clearly the fittest. 

Second iteration

For the second iteration I changed it so mutations are less likely to happen, but have a larger impact. With this change I hope to create a larger difference in genes. 

Another change I made is that if both parents have the same highest gene, it will cause this stat 

To become even greater. With this change I hope to really see if certain genes are more fit in a certain area. Something else which I wanted to test is whether some genes would become fitter over time. Maybe another gene than speed would become really handy in later generations. 

What we can see is that now speed has become an even more fit trait in the ant hives. 

Third iteration

For the third iteration I decided to see what would happen if I made the playing field twice as small. Theoretically this should mean that speed should become less important, as ants do not have to walk as far to the nearest food source on average.

As you can see, speed has become a lot less relevant in this new and smaller environment. This is probably because ant hives are a lot closer together, meaning conflict is a lot more common. Something which I did not expect was strength not being a fit gene. It should have been better on a smaller plane as it helps in gathering food. The fact that the gene of speed also reduced in iteration seems to mean that gathering food is not as important this time. 


In the beginning I asked the question: “Which genes are fittest for ants in different digital areas?” After doing research on evolution and genetic algorithms, I was able to create a programme in which was able to simulate a population of several ant hives. Over the course of multiple generations, these ants became more and more fit in their environment. Every time we changed the environment, or how the environment worked, we saw that a different kind of ant would become the most fit. Therefore we can conclude that the environment indeed has a huge impact on which gene is the fittest in our area. Which means that we can answer the question stated in the beginning: in its current state, speed is the fittest gene in larger areas, and armour is the fittest gene in smaller areas.

Possible improvements

I would like to implement a more realistic version of “survival of the fittest” into the programme. As of now, the programme selects the two “best” ant hives to reproduce. This is inaccurate as in nature not always the strongest or the individual. Sometimes it is just dumb luck that helps an individual reproduce. Therefore I would like to add more drawbacks to having higher stats. 

More effective scouting

Currently the scouting patterns of the ants overlap a lot. Perhaps in the future I could code in that the more ants of the same hive are in the same area, the less attractive it would be to scout there. The way the programme works now is that 

Improved combat

Currently the ants will always fight ants of different colonies even if the chances of defeat are close to 100%. Perhaps some ants could enact a more defensive playstyle, while ants with a higher armor value will try to actively find other colonies in order to reduce competition. The reason this is difficult to implement is that often the ants which focus more on gathering food, will have a larger number of ants in comparison to the more armored ants, so you would have to determine for the aggressive ants how likely they are to win with lower numbers, and whether this gamble will pay off in the long run. Instead of having them be the ‘suicide ants’. 

Random events

Most of us can agree that at some point in our past when we were young we caught some ants and did things to them which inevitably led to their deaths. After making this programme, I can only begin to imagine how I might have influenced their evolution in a way. But it would be interesting to see what would happen if in the programme some kid would step on the plain and kill all the ants in an area, and see how that would impact the ants. Would impact the bigger colonies more, because they have a high chance of losing many ants? Or would it impact the smaller colonies more because a relatively large part of their colony can get killed off in a heart beat?


Vijini Mallawaarachchi. Jul 8, 2017. Introduction to Genetic Algorithms — Including Example Code

Sangeet Lamichhaney. Adaptive evolution in Darwin’s Finches’s%20finches%20is,feeding%20behavior%20and%20song%20types).

Related Posts