DEV Community

Cover image for Understanding evolution, through coding !
Adwaith Jayasankar
Adwaith Jayasankar

Posted on

Understanding evolution, through coding !

Recently I came through this crazy video showcasing how evolution works through the theory of natural selection. The video was made by David Miller, you can check out the full video through this link: https://youtu.be/N3tRFayqVtk?si=9DHAYaoSnmo6Z3Ed.

If you just want the revised version of the video, hope on!

What is evolution and natural selection?

Evolution is the gradual change in living organisms over time. It is based on natural selection which is nature's way of favoring traits that help organisms survive and have babies, making species better suited to their environment over time.

Image description

What is this video about?

So what David Miller did is that he created a 2 dimensional world where little creatures live that have brains (made up of neural networks) and have genes which the creatures are capable of passing onto the next generation.

He then set up the conditions necessary for evolution and wanted to see how the creatures would adapt to it.

The simulations which he showed were some of the most fascinating concepts I've seen in a long time and it honestly blew my mind away!

Image description

What are the conditions necessary for evolution?

1) Self - replication : This refers to just reproduction, as evolution cannot occur without it.

Image description

How does it work?
Each creature is simply a data structure that contains tiny bits of information such as:

  • Coord: Information regarding where it lives in the world.
  • Genome: Contains the information necessary to pass onto it's kids.
  • Neural Net: The brain of the creature.

Now if you just copy this data structure and memory, voila! You achieved self - replication.

2) Blueprint: In nature, every creature that is born, has to be made from some sort of a blueprint. This blueprint is referred to as genes, which consists of the information that is passed onto the next generation.

Image description
3) Inherit Blueprint: This blueprint that is used to make an individual, has to be inherited. Just like how babies inherit characteristics from their parents.

Image description

4) Mutation: Usually when we inherit genes from our parents, the information is copied a 100% accurately MOST of the times, but once in a while, we'll get a gene that has a slightly different information, leading to mutations.

Image description

5) Selection: For evolution to occur, there must be some sort of selection procedure on who gets to reproduce and who doesn't.
For humans, it involves escaping from predators, finding mates to reproduce, stay healthy and not catch any diseases etc. but for this simulation, our selection criteria is wayy simple!

SIMULATION 1 : HOW IT WORKS

Image description

Couple of points to note to understand the above simulation.

  • The world size is 128x128 pixels.
  • Generation Zero or Gen 0 has a population of 1000 creatures.
  • The tiny different colored dots are the creatures. The different colors showcases the variety in genes.
  • Steps/Gen refer to the amount of time the simulation will run, in this case about 10 seconds.
  • Genome length refers to the number of genes each creature has, which in this case is 4. Each genome is a bunch of hexadecimal letters that contain the instructions for how to make the connections in their little neural network brain, which is pictured below.

Image description

  • The selection criteria in this case is that the creatures that are present in the right side of the world gets to live, others do not.

Now let's see the simulation!

Gen 1

Image description

Gen 20

Image description

Gen 50

Image description

Gen 1200

Image description

So what can you understand from the above pictures? How were the creatures able to move on their own towards East, i.e the safe side? Let's discuss.

So the creatures that were already on the safe side by luck, got the chance to live and reproduce. But not all the creatures that survived are from the safe side.

See, all the creatures have their brain wired to move in a certain direction, say East, South, West and North. Some of the creature's brains were wired to move towards East, which resulted in them surviving. Now this gene of always moving East was handed down generation to generation, which lead to more creatures moving East and then surviving.

THE CRAZY FACT ABOUT ALL OF THIS IS THAT THESE CREATURES WERE NOT EXPLICITLY PROGRAMMED TO EVOLVE! They did it all on their own.

SIMULATION 2: ARE MUTATIONS NECESSARY FOR EVOLUTION TO TAKE PLACE?

Image description

Couple of points to note to understand the above simulation.

  • The parameters are all same as simulation 1, with a couple of changes.
  • The genome length is now 24, which means there are 24 genes leading to a smarter creature.
  • The mutation rate is 0.001, which allows the creatures to get a mutation at a random period of time.
  • Here the selection criteria is that the creatures that are able to get to the left and right sides of the world as pictured above gets to survive.

Now let's see the simulation!

Gen 0

Image description

Gen 25

Image description

Gen 100

Image description

Gen 700

NOTE: From Gen 700 onwards, some of the creatures get a mutation to move towards left, leading to a higher percentage of survival.

Image description

Gen 1000

Image description

Gen 8300

Image description

Now what happens when we put a couple of barriers? Let's see!

Gen 8301

Image description

Gen 9000

Image description

Gen 177,000

Image description

What do we understand from the above simulations?

Because of mutation the creatures were able to adapt to their environment, which lead to a better chance of survival. When the barriers were put up, they mutated to be able to move all the directions so that they could reach the safe zone.

What happens when there's no mutation? Let's see.

Gen 0

Image description

Gen 100

Image description

Gen 8000

Image description

Till now all seems good and normal right? What happens when we put up a couple of barriers?

Gen 8001

Image description

Gen 20,000

Image description

Gen 500,000

Image description

Gen 1,000,000

Image description

Since the mutation rate is 0, the creatures were not able to adapt and change their behavior according to their surroundings which lead to their survival percentage remaining constant.

This shows us that as a species how mutations are so important. Without mutations our species would not live long enough in the future to survive and eventually reach the stars.

SIMULATION 3: BRAIN SIZES

Do brain sizes affect evolution? How large does your brain has to be to ensure continuous survival of your species? Let's take some examples for comparison.

Image description

  • The average human being brain has 80 billion neurons and more than a 100 trillion connections between them.
  • The average cat brain has 7000 million neurons and 10 trillion connections between them.
  • The average housefly brain has about 250 thousand neurons and 10 million connections between them.
  • The brain of a nematode worm consists of 302 neurons and about 7000 connections between them.

The creatures in our simulation has about 20 neurons and 200 connections between them. Take that for comparison!

Image description

Some factors to note to understand the above simulation.

  • We will keep on changing the genome length, which increases or decreases the size of the brain accordingly.
  • The selection criteria in this simulation is that the creatures that are able to move towards the corners of the world get to live and reproduce.

Let's start the simulation!

Genome length: 2, Neurons: 1

Image description

Genome length: 8, Neurons: 2

Image description

Genome length: 32, Neurons: 5

Image description

Genome length: 1000, Neurons: 127

Image description

Let's compare the brain sizes of the creatures

Genome length: 2, Neurons: 1

Image description

Genome length: 8, Neurons: 2

Image description

Genome length: 32, Neurons: 5

Image description

Genome length: 1000, Neurons: 127

Image description

You can see here that, as the number of neurons and the connections increases the survival rate also increases exponentially.

Image description

SIMULATION 4: THE 'KILL' NEURON

So all this while, David Miller disabled the kill neuron. What it does is that it enables the creature to kill the neurons that are blocking it's path.

Image description

Here the orange creature 'kills' the violet creature because it is blocking it's path.

What happens when we enable this 'kill' neuron? Let's see!

Image description

Some factors to consider for the above simulation.

  • The selection criteria is that the creatures that are able to move towards the circle in the center get to live and reproduce.
  • There are about a 1000 creatures in this simulation.

Gen 0

Image description

Gen 200

Around this time, the creatures evolved and found a way to survive by keeping the kill count to a minimum.

Image description

Around Gen 700, a mutation occurred. A creature that killed other creatures got into the safe zone and started spreading it's 'killer' genes towards the next generation, which caused the generations after Gen 700 to be full of killers.

Gen 900

Image description

Gen 3400

Image description

Now what happens when we keep the number of creatures to a minimum, say 100?

Image description

We can see that there are zero kills. Since there was enough space for all the creatures to move around, killing the creatures suddenly dropped as an option!

CONCLUSION

This video created by David Miller was a fascinating insight into the world of programming and biology. It further cemented my love for science and it's capabilities. I'm currently trying to learn Machine Learning and AI, so if these are some of the things which we are capable of doing, the possibilities to create lifechanging discoveries are endless!

This article is not sponsored by David Miller, but please do consider watching his video and be a subscriber to his channel. Content like this must be supported at all costs!

Link to his video: https://youtu.be/N3tRFayqVtk?si=9DHAYaoSnmo6Z3Ed

If you've managed to read till here, I thank you from the bottom of my heart for the support. Makes me happy knowing my effort in writing this article contributed to someone learning something new :)

Top comments (0)