Demystifying Genetic Algorithm
From post on Medium
“It is not the strongest species that survive, nor the most intelligent, but the ones most responsive to change” — Charles Darwin
There are Five phases in a genetic algorithm:
Creating an Initial population
Defining a Fitness function
Selecting the parents
Making a Crossover
Generate the initial population
UNTIL population has converged
Let’s Understand the phases of genetic algorithms in detail
Let’s take a simple example:
Suppose we want “Unicorn” word.
Step 1: Population
The process begins with a set of individuals which is called a Population. Each individual is a solution to the problem you want to solve.
Select randomly population of three elements, suppose we get
Step 2: Fitness Function
The fitness function determines how fit an individual is. It gives a fitness score to each individual. The probability that an individual will be selected for reproduction is based on its fitness score.
Here, suppose we define fitness as number of each words match with the desired output.
- Unijorm — 4 (4 words matches with Unicorn)
- caesore — 2 (2 words matches with Unicorn)
- popcorn — 4 (4 words matches with Unicorn)
Step 3: Selection
The idea of selection phase is to select the fittest individuals and let them pass their genes to the next generation.
We take top 2 parents with high fitness score that is, Unijorm & popcorn.
Step 4: Crossover
Crossover, also called recombination, is a genetic operator used to combine the genetic information of two parents to generate new offspring. Here, we combined the genetic information from parents we selected. We get “Unicorn”.
Step 5: Mutation
Mutation occurs to maintain diversity within the population and prevent premature convergence. We already got our desired result after crossover so it will not mutate.
Implementation of Genetic Algorithm in Code
Finding “Unicorn” with (Java & Processing)
Don't Run Directly here, You'll need libraries. It is just for reference. Check on Medium Post for complete reference.
You can ask your questions or submit feedback in the comments.
Follow me on Medium for awesome posts
By Shubham Kumar on 2019-03-20
This is the first post.
This is the latest post