Adding genetics

3 Jan 2021 Code on Github


The previous article introduced a basic simulation where creatures move about eating food. Now I've add some very basic genetics: each creature has a single gene that determines its size (by which I mean, area; its radius is the square root of this value). When creatures replicate the child has a 50% chance of getting the same value, a 25% chance of it being one unit bigger, and a 25% chance of it being one unit smaller (unless it is size one already).

The code for this simulation and all the others in this series is here.


I ran the simulation for the equivalent of 100 minutes at speed 1, to give creatures long enough to evolve. The graph below shows how the gene for size changed over time.

Time (minutes) Population size Creature size 0 10 20 30 40 50 60 70 80 90 100 0 50 100 150 200 250 300 350 400 0 10 20 30 40 50 60 70 80 Creatures Food Mean size

The graph shows that the mean size of creatures grows 8 fold over 100 minutes. For people familiar with genetic algorithms, this may come as a surprise since there's no fitness function or any explicit selection. What we have is "natural" selection - larger creatures are more likely to hit food, so are more likely to survive and replicate.

The graph also shows that the average amount of food decreases over the course of the simulation. By the end of the simulation, the mean area of creatures is about 72, making the radius about 8.5. Using the formula from previous article, we can predict the stable amount of food for creatures this big would be about 125, which looks about right. We should be a bit careful with this formula, because one of the assumptions we made is that creatures don't overlap and as creatures get bigger, the chance of them overlapping increases to become not insignificant.

Throughout the simulation, the number of creatures is pretty stable at 50. This is what we'd expect as the number of creatures is purely determined by the rate of metabolism, food energy and rate of food creation, which don't change.


One confusion I've seen about evolution is how it can result in organisms becoming better adapted when most mutations have a negative effect. Leaving aside the truth of most mutations having a negative effect, the answer is: selection. Even if most mutations are negative, they are weeded out by the non-random effects of selection.

To investigate this idea in the mutation, I tried a range of mutation effects. In the original simulation, there was a 25% chance of positive mutation (the size increasing) and a 25% chance of a negative mutation (the size decreasing). The remaining 50% of the time the child had the same size as the parent.

I re-ran the simulation three more times. In each case, there was a 50% chance of no-mutation, but the chance of a positive mutation was decreased (and the chance of a negative mutation was increased by the same amount). The graph below shows the mean creature size over time.

Time (minute) Creature size 0 10 20 30 40 50 60 70 80 90 100 0 10 20 30 40 50 60 70 80 25% 20% 15% 10%

The average creature size bounces around a lot since the population is small so random events can have a big effect. I should probably repeat each run multiple times to get an average. That said, it's clear (and probably unsurprising) that as the chance of positive mutations decreases, the rate at which creature size increases slows. Even when the chance of a positive mutation is 15% (so 35% chance of a negative mutation), the mean creature size increases, slowly, but convincingly.

It's not until the chance a positive mutation is 10% (meaning negative mutations are four times more likely), that creature size decreases. Even then, it seems to recover, but I think that might be a combination of randomness and that the size can't decrease below 1.

Conclusion: The bigger the better?

As evolution simulations go, what we have so far is fairly unexciting. There's a single gene and the only selection pressure is for it to increase, so it inevitably increases until the Bloops fill the screen. At which point, all food created will be immediately eaten by the first creature and all the others will die. In the next simulation, we'll look at a more nuanced gene where the selection pressure is more balanced, as it is in real life.

Still to do

  • Repeat experiment with different mutation rates multiple times to get an average
  • Graph showing min, max and mean creature size over time
  • Graph showing all creature size over time
  • Get some measure of selection pressure and see what affects it
  • Add mutation bias for smaller creatures and see how strong this can be before creatures tend to shrink.