Genetic Algorithm Implementation: Code from scratch in Python

Cyborg
4 min readMay 5, 2024

--

Genetic algorithms are a class of optimization algorithms inspired by the process of natural selection. They are used to find approximate solutions to optimization and search problems. GAs are based on the principles of genetics and evolution, such as inheritance, mutation, selection, and crossover (recombination).

How Genetic Algorithms Work

Genetic Algorithms work by mimicking the process of natural selection and evolution to find approximate solutions to optimization and search problems. Here’s a step-by-step explanation of how they work:

Step 1️⃣: Initialization

Genetic algorithms begin by creating a population of random candidate solutions, known as chromosomes, to represent potential solutions to the problem.

Initialising a population of 10 chromosomes

In above code, each chromosome is a binary representation of an integer ranging between 0–255. As closer their value is to a threshold (we will define) as better that chromose is for our current scenario.

Step 2️⃣: Selection

The fitness of each chromosome is evaluated based on how well it solves the problem. The fittest chromosomes are selected to serve as parents for the next generation.

fitness function to test each chromosome

As closer the fitness score of a chromosome is to 255 as likely they would be selected to serve as parents for the up coming generations (and its only possible when their integer representation is close to threshold).

Step 3️⃣: Crossover

Selected parents are used to create new offspring through a process called crossover, where genetic material from two parents is combined to produce one or more offspring.

New born childs/chromosomes would have combined genes of both parents as we see in any human child.

Step 4️⃣: Mutation

To maintain genetic diversity in the population, random changes, or mutations, are introduced to the offspring’s genetic material.

Mutation makes sure that each child would maintain diversity from each other and parents altogether.

Step 5️⃣: Replacement

The least fit members of the population are replaced by the new offspring, ensuring that the population evolves towards better solutions over time.

Replacement of population (offsprings taking place of old chromosomes)

Step 6️⃣: Termination

The process is repeated for multiple generations until a termination condition is met, such as finding an acceptable solution or reaching a maximum number of generations.

In most scenarios you would notice the best chromosome until the last generation has reached the best fitness score.

Genetic algorithms are widely used in optimization and search problems across various domains. They are particularly useful when the search space is large and complex.

You would see genetic algorithms (along with neural networks) widely being used in developing ai agents to play various games like flappy bird, snake, ping pong and many other single & multi player games.

In my next blog, I will guide you through how to use neural networks along with genetic algorithms to create such agents. This technique is called NEAT (NeuroEvolution of Augmenting Topologies). I think you might have heard about it before, if you have dived well enough in the field of reinforcement learning.

🧑🏻‍💻 Code your own genetic algorithm from scratch using python

👨🏻‍🔬 Genetic algorithms explained (but this time visually)

If you’d like to learn more about genetic algorithms or reinforcement learning in general, then don’t forget to follow my page. See ya’ 👋🏻

--

--