# Genetic Algorithm Tutorial: What It Is And How They Work

by | Sep 22, 2018 | Data Analytics

## Before we start

Genetic Algorithm is one advance topic. Even though I will write this post in a manner that it will be easier for beginners to understand, reader should have fundamental knowledge of programming and basic algorithms before starting with this tutorial.

In this tutorial with example, I will talk about the general idea behind Genetic Algorithms followed by the required Genetic Algorithm steps to create your own Algorithm for a totally different problem. And before concluding, I will give you some real-life Genetic Algorithm examples that can be useful in learning more about Genetic Algorithms. Ready to learn something new? Let’s get started this Genetic Algorithm Python tutorial.

## Introduction to Genetic Algorithms

To understand Genetic Algorithm better, let us first answer a question: How to create good Artificial Intelligence? The naïve solution is that we generate empirical algorithm, which is basically set of rules. The standard procedure is, “if you meet this condition, act like that”. However, no matter how much of work you pour into this method, the final solution will never be able to outsmart its creator.

To avoid this, a new idea called Genetic Algorithms was developed. Before learning what Genetic Algorithm is, let us first understand the theory behind it, the theory of natural selection by Darwin. The theory is simple:

If a population want to thrive, it must improve by itself constantly, it’s the survival of the fittest. The best element of the population should inspire the offspring, but the other individuals must not be forgotten in order to maintain some diversity and be able to adapt in case of a variation of the natural environment.

In simple terms, instead of creating a direct solution, we recreate evolution. This idea is called the Genetic Algorithm. They are mainly efficient for optimization problems. We will create our own algorithm for a problem later in this post. But before doing that, let’s understand this better with an example.

Date: 23rd Jan, 2021 (Saturday)
Time: 10:30 AM - 11:30 AM (IST/GMT +5:30)
• This field is for validation purposes and should be left unchanged.

## Genetic Algorithm Steps

The chart here shows the steps you require in creating a Genetic Algorithm.

### Initial Population

First, we create individuals and then we group them and call Population. An individual is distinguished by set of variables known as Genes. These Genes are combined into a string to form Chromosome, which is basically the solution.

In order to understand the whole process better, let’s solve a simple problem by Genetic Algorithm: How could I crack my workmate’s password?

#### Creating Individuals

In this scenario, our individuals are going to be words, with of course equal length as the password. Now the tricky part, each letter is a gene and the value of that letter is allele. You know what a gene is, but allele in simple terms means one of the possible forms of a gene. Once we create individuals with the same length as passwords, our genetic algorithm can now explore all possible amalgamations.

#### Creating our first population

Whenever we are creating our population, the main idea to keep in mind is that we should not point it towards the solution which seems good. Rather than that, we must make it as wide as possible so that we can make a nearly perfect Genetic Algorithm which covers as many possibilities as possible. So, in our case, we will just create the world only composed of random letters with an equal length of password.

Date: 23rd Jan, 2021 (Saturday)
Time: 10:30 AM - 11:30 AM (IST/GMT +5:30)
• This field is for validation purposes and should be left unchanged.

### Fitness Function

Fitness function determines the fitness of individuals, in general terms, its ability to compete with other individuals and be the final desired output. It gives fitness scores to everyone and the probability that an individual will be selected for reproduction is dependent on its fitness score. The basic solution is,

`fitness score = (number of char correct) / (total number of char)`

This way, individual with higher fitness score is a specimen genetically closer to success than the others. Our fitness function will precisely sort the population.

Date: 23rd Jan, 2021 (Saturday)
Time: 10:30 AM - 11:30 AM (IST/GMT +5:30)
• This field is for validation purposes and should be left unchanged.

### Selection

As we now have the fitness score, in this phase we select the individuals with the highest scores and let them pass their genes to the next generations. After selecting individuals, we now group them into a pair of two(parents) based on their fitness score because obviously it’s not possible to reproduce without two entities.

There are many ways to do this, however; we should keep two things in our mind. Firstly, to generate the best solution of the previous generation but not keeping others aside. And secondly, also the hazard, if you select only good solutions at the beginning, you will quickly reach towards the local minimum, which is the limitation of Genetic Algorithms, and not towards the best solution.

To do this, we will take N better specimen (N = best_sample) on the one hand and on the other we will take random individuals (M = lucky_few).

Date: 23rd Jan, 2021 (Saturday)
Time: 10:30 AM - 11:30 AM (IST/GMT +5:30)
• This field is for validation purposes and should be left unchanged.

### Crossover

The ultimate goal of reproduction is to mix the DNA of two individuals. So, we will do the same thing here. Let’s take two individuals Oswald and Daisy, their DNA is defined by their alleles (value of each letter). Therefore, in order to mix their DNA, we just have to mix their letters. Thus, for each letter of the child, we will take a random letter of Oswald and Daisy.

One thing to note here is that Oswald and Daisy isn’t going to reproduce only one child. You have to define the number of children per couple in order to keep stabilization in the population of your Genetic Algorithm.

Date: 23rd Jan, 2021 (Saturday)
Time: 10:30 AM - 11:30 AM (IST/GMT +5:30)
• This field is for validation purposes and should be left unchanged.

### Mutation

The primary goal of this step is to prevent our algorithm to be blocked in a local minimum. After the crossover, each individual must have a small probability to see change in their DNA.

To find out how to choose the precise mutation rate for our Genetic Algorithm, I followed this article.

We have come to an end. You have all it needs to get started with Genetic Algorithms. Also, for each step, there are many solutions possible. Take your time to study more about it and choose the fittest to solve your problem for an accurate solution.

Date: 23rd Jan, 2021 (Saturday)
Time: 10:30 AM - 11:30 AM (IST/GMT +5:30)
• This field is for validation purposes and should be left unchanged.

## A Step Further

There are several resources out there for understanding, training your own algorithm and even competing. Let’s look at some of them here.

#### BoxCar— A Web App

It is an example, which is online, of a genetic algorithm. The primary goal is to create vehicles that are most efficient with two wheels, the body of the vehicle is basically geometry shapes.

#### Evolution— A Mobile Application

More solicited than the previous one, in this, you have to create a creature with joints, muscles, and bones. After you create your creature, the Genetic Algorithm tries to optimize the moves of your creature in order to execute a task which may include jump, run, climb, stairs and so on.

#### Practice Practice and Practice

Finally, to code is the best way to learn. Codingame.com is the platform that links lots of developers with common passion, Artificial Intelligence. Once a month they host contests which lasts one-week long, and you must create the best AI possible. Most winners use Genetic Algorithms to solve problems. So, take a deep breath and dive right in.

## End Notes

As I mentioned earlier, this is an advanced topic. It takes a while to get around it. I tried my best to explain this in the simplest way possible. if you are currently working on a problem try Genetic Algorithm optimization to solve it even better and faster. Apart from that, if you still have any doubts or confusion in any of the steps mentioned above, feel free to ask your queries. The key to success is asking the right question.

Happy Learning.

##### Register for FREE Orientation Classon Data Science & Analytics for Career Growth

Date: 23rd Jan, 2021 (Saturday)
Time: 10:30 AM - 11:30 AM (IST/GMT +5:30)

• This field is for validation purposes and should be left unchanged.

## Linear Programming and its Uses

Optimization is the new need of the hour. Everything in this world revolves around the concept of optimization.  It...

## An overview of Anomaly Detection

Companies produce massive amounts of data every day. If this data is processed correctly, it can help the business to...

## How to become a Data Mining Specialist

With the advancement of technologies, we can collect data at all times. Every internet user has a digital footprint....