Natural Selection Simulator
View on GitHub
EvoNoodles is a natural selection simulator that looks at creatures, known as noodles, as they evolve in a environment competing for resources and surviving in the presence of predators.
Simulation
- To run the simulation simply run main.py
- There are two settings in main.py,
SIMULATE
andSAVE
, which can be set totrue
SIMULATE
will rapidly jump through generations of evolution, speeding up the sim and infrequently redrawing the Pygame window, the console will continue to log the epoch and population to keep track of timeSAVE
will output the creature and attribute statistics in a CSV file, with noodle and pred staistics saved to data/noodle_output.csv and data/pred_output.csv by default- The starting parameters can be found in sim/settings.py, where starting numbers of noodles, preds, food, as well as width and height for the pygame display and environment size can be edited
Noodles
Noodles are simple, (mainly) vegetarian creatures which search for food in their nearby enviornment. They attempt to find food at all times to replenish their health, with the goal of reproducing. Food appears regularly in their environment, or at the location of a dead noodle which they will also eat.
They have a chance at reproducing after a certain number of timesteps, where they produce a genetically similar offspring that potentially has a mutation in their DNA to differ in a single trait from their parent.
All noodles start with these attributes, which are randomly chosen in the following ranges:
Attribute | Description | Range |
---|---|---|
Size | size, max health=size*20 | 6 - 10 |
Speed | how fast they move | 1 - 4 |
Sight | range of vision | 30 - 200 |
View | degree angle of vision | 40 - 240 |
Reproduction | % chance of reproducing | 15 - 35 |
These attributes all contribute to the loss function of a noodle, or how much energy they consume per timestep. The function can be edited to favor certain attributes over others, but the default function that allows for stable populations is:
loss = size*2 + speed*3 + sight/2 + view/3
Preds
Preds are the predators of the environment, with their prey being noodles. Preds are also smart creatures, which gives them two distinct advantages:
- Preds will chase the closest noodle to them, even if they already have a target, improving their hunting efficiency
- Preds will only hunt noodles if their health drops below half, to reduce overeating and wasting resources
Their default starting attributes are also slightly different, with greater size, speed, and range of vision, with a much narrower vision angle.
Development
I started with green, circular blobs that I called noodles. The noodles started by moving around a map looking for food, which I implemented with a simple food-seeking algorithm where noodles moved towards the closest food with a certain velocity. When they reach the food, they would “eat” it by removing it from the list of foods, and a new food would be added in a random location.
Next came giving the noodles the gift of sight; I planned on them competing for food and evolving their attributes which would include vision. The first step was to give noodles a range (variable: sight
) of how far they can see food. Then they are assigned a viewing angle (variable: view
) which dictates the angle they can see with relation to their velocity. This was implemented by calculating the points on an arc given a noodle’s range and view and rotating that arc with respect to the noodle velocity.
A boundary detection method was added to noodle movements to constrain them to the environment limits. Noodles will move in a random direction away from boundaries to help make noodles not get stuck.
A smooth turning function was added to give the noodles more realistic movements. Their sight, view, and other attributes of size and speed are now all be randomized to give a hetergeneous starting population. There is a countdown for adding new food if multiple foods are consumed within a short timespan, which helps prevent population explosions. There is also a chance of a new, random noodle immigrating to the local environment every few thousand timesteps, to introduce more diversity.
An energy loss function was added which can be changed to give greater importance to certain attributes, but the default is:
loss = size*2 + speed*3 + sight/2 + view/3
Noodles will die when they run out of health and leave behind a new food object which is proportional to their size. A noodle’s size also affects its health - their maximum health is size*20. A noodle who is size 4 will only have a maximum of 80 health compared with a size 10 noodle with 200 health, but the smaller noodle will also lose health less quickly.
A health color function was added which shows noodles turn from green to red the closer they get to losing all health.
After observing how noodles evolve competing with just each other, the next step was to introduce a predator. Preds are blue triangular hunters, which use the same creature class as the noodles, except they only eat noodles.
They have been given a slightly different range of starting attributes to encourage a stable but diverse population:
Attribute | Description | Range |
---|---|---|
Size | size, max health | 8 - 12 |
Speed | increases velocity | 3 - 5 |
Sight | range of vision | 200 - 400 |
View | degree angle of vision | 20 - 80 |
Reproduction | % chance of reproducing | 25 - 45 |
Notable differences are that predators are bigger, faster, and can see further but with a greatly reduced range of vision, mimicking the way predators have eyes on the front of their head instead of the side like prey.
The other important difference is that they are “smart” creatures, since they are hunters and a smaller population. This means they
- will chase the closest noodle to them, even if they already have a target, improving their hunting efficiency
- will only hunt noodles if their health drops below half, to reduce overeating and wasting resources
Examples of predators switching targets to a closer noodle, and ignoring noodles within their sight due to being satiated enough that they have full health can be seen above.
Dozens of simulations were ran over hundreds of generations to identify appropriate starting parameters, as well as to analyze the long term natural selection forces exerted on these populations.
View post-hoc Analysis page
Credit
This was inspired by papers I presented and did related work to in a course I took on probabilistic modeling for computational biology. I was specifically interested in neutral biodiversity theory, looking at Hubbell’s work as well as approaches to fit models to species abundance distributions. The interplay between speciation, extinction, and immigration inspired me to develop my own model of evolution on a small scale.
Relevant papers:
- Etienne, R.S. and Olff, H. (2004). A novel genealogical approach to neutral biodiversity theory. Ecology Letters, 7, 170-175.
- Hubbell, S.P. (2001). The Unified Neutral Theory of Biodiversity and Biogeography. Princeton University Press, Princeton, NJ.
I also took inspiration from Primer Learning and MinuteLabs.io (https://www.youtube.com/primerlearning) and their work on creating evolutionary simulations to demonstrate basic principles of natural selection, and Luke Garbutt’s genetic steering algorithm (https://github.com/lukegarbutt).