Understanding evolution, through coding !
Tags: evolution, cpp, ai, machinelearning
Published: 2023-09-01 13:17:31

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/759ighcgr6t1pf7zwzdd.png)


## **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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bigme7o8d80d80cadr8k.png)


## **What are the conditions necessary for evolution?**

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


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kdx4xwy82s9ryijepw0x.png)

**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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fgo7s8njxss3co6g5h8r.jpg)
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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uqe69pfds07zwwsjbmrx.jpg)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zdhf853npo5222vp13aj.jpg)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/24prmgtpd8r37ekdkl07.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8o104j5yzc2es9f4y2rg.png)

- _**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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q603ilqxr8vlx7nggoxt.png)

## Gen 20


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9pa35iltgmgppoaq3xsa.png)

## Gen 50


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mz7j90408reedbk0ziny.png)


## Gen 1200


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ox58tzxtn6dl78er03pt.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d0xmbum0xjfzvkfivvz4.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/a6bku4qvpcyab6yrh8n5.png)

## Gen 25

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/edvhcnczp8g1fwcb4bfa.png)

## Gen 100
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zhb0zazn8ifa02h76t35.png)

## 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3b50khfnu5vd5xhtjo0j.png)

## Gen 1000

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/a6ms0ryc4a59n8zmtb2w.png)

## Gen 8300


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uz9e45iakv4ragdohiy1.png)





**_Now what happens when we put a couple of barriers? Let's see!_**





## Gen 8301

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/aceay9jku69gzjthw4jk.png)

## Gen 9000

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ryi8lqbsjkulssunom9j.png)

## Gen 177,000


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3sqsb584h3dt2ja0n8te.png)

**_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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/v5bqd8z254cqqe41w3v0.png)

## Gen 100

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xwe95qg1f421c5geg8dy.png)

## Gen 8000

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fmdd3n1874028yajxqvc.png)

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


## Gen 8001
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/52joxvmdlasshejsgxzt.png)


## Gen 20,000

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/x9nyjd1pfcbfplm8xm8z.png)

## Gen 500,000

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jem935hxuv7oikv3xnbu.png)

## Gen 1,000,000

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/648cnk6zw4myg58n3tlh.png)


**_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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sqjcaau6r2ykvy2xtf39.png)

- 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6ymq9xho82xlubszob64.png)


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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/219pide6oa4udkg2oxim.png)

## Genome length: 8, Neurons: 2

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0flii0w8698ih7rrgyla.png)

## Genome length: 32, Neurons: 5

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/oqu0kbeya0vp40t6ljer.png)

## Genome length: 1000, Neurons: 127


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/x2owmymmpkjdcvfhptwz.png)


**Let's compare the brain sizes of the creatures**

## Genome length: 2, Neurons: 1

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qsyve3wv0gdkhh7gwrs7.png)

## Genome length: 8, Neurons: 2

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qmywmawpb9s9u3rgwlct.png)

## Genome length: 32, Neurons: 5

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nttp8yjj0wbjpxg3oz0r.png)

## Genome length: 1000, Neurons: 127


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3y6zo7jamhem4ugn2exj.png)


You can see here that, as **_the number of neurons and the connections increases_** the **_survival rate also increases_** exponentially.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0n83w1kfppm0gmto4dpi.png)






## **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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d75t229kw9vfwbxdeyi6.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nr9af4foxzk6btlt4vxf.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gdo9sqc7sf7gmyucfud9.png)

## Gen 200

Around this time, the **_creatures evolved and found a way to survive by keeping the kill count to a minimum_**.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ssnia77mzgvusldlkxrq.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ewzq73mezb28wdwvnzn2.png)

## Gen 3400

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/u4sb3np9ouzyajk2hj12.png)


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


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kz85h0j49hce77r42kca.png)


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 :)_**