
Best chromosomes throughout generations
- Language of implementation: Python
- Libraries: Numpy, Random
- Algorithms: Genetic Algorithm
The goal for the algorithm is to find the inputed number of the user without simply printing it out again.
Evolution is at the core of life. It defines who we are, as a species, and what we're going to be. Thus it has always been an enormous interest for me to understand how and why it works so well.
This project is an introduction to Genetic Algorithms. I took a very simple environment that consists of a population of 1O individuals. These individual are represented by a chromosome which is an array of genes. A single gene is an integer from 0 to 9.
In Computer Science, a Genetic Algorithm is a metaheuristic inspired by natural selection. It is often used to generate high quality optimization of a task for example walking simulation or even rocket trajectories. It relies on the fundamental ideas of biology, in particular Evolution, such as Crossover, Mutation and Selection.
In this project, we'll add another idea called Elitism which takes the best individuals of a population and makes them survive to the next generation. This was done to converge faster towards an optimal solution.
First of all, we need to implement a fitness function which takes individuals in the population and estimates the best ones. In our case, the best, also called the fittest, are the one's that have the most similar digits with the number to guess.
We'll then need to have a function that takes the individuals of a generation with their according fitness scores and takes the fittest one's to use as parents. These parents will then go through a crossover phase where the genes of two parents will randomly be selected and transferred to the child.
Furthermore, to avoid early convergence we'll randomly mutate an individual in the population.
Having all those elements, we can build a Genetic Algorithm. For each generation, we'll point out the fittest one and see how the algorithm works. We can see in the image on the top left of the page how the algorithm works, especially when it finds a new fittest individual.