The world’s leading publication for data science, AI, and ML professionals.

Introduction to Genetic algorithms using the EasyGA python package – Includes example code.

Creating genetic algorithm applications is easier than ever before.

Photo from Unsplash by Clément Hélardot
Photo from Unsplash by Clément Hélardot

A Genetic algorithm (GA) is the offspring of Charles Darwin’s theory of natural evolution. The algorithm is built around the idea of natural selection where individuals in a population reproduce in the hopes of producing better offspring. This process continues for multiple generations, in hopes of producing the desired result.

With python packages, this complex process has been simplified. The EasyGA python package has brought the complexity and time consuming process of writing a proper GA to minutes rather than hours.

Introduction to genetic algorithms – Including Example Code

Let’s start with a beginners example. I was inspired by Vijini Mallawaarachchi‘s introductory article when I first started writing natural selection Algorithms so for me it is natural to use her problem of getting all 1’s in the gene pool or in this case we will call a gene pool a chromosome.

First, you will want to install the python package.

pip3 install EasyGA

Here is the full example code of her problem using EasyGA.

Output:

Current Generation     : 1
Best Chromosome     : [1][1][0][0][1]
Best Fitness        : 3
--------------------------------------------------
Current Generation     : 2
Best Chromosome     : [1][1][0][1][1]
Best Fitness        : 4
--------------------------------------------------
Current Generation     : 3
Best Chromosome     : [1][1][0][1][1]
Best Fitness        : 4
--------------------------------------------------
Current Generation     : 4
Best Chromosome     : [1][1][1][1][1]
Best Fitness        : 5

As you can see, we have created a Genetic Algorithm to solve having five 1’s in our chromosome. Pretty simple for such a powerful package. Let’s try another example, a root finding algorithm. A root finding algorithm as defined in Wikipedia:

In mathematics and computing, a root-finding algorithm is an algorithm for finding zeroes, also called "roots", of continuous functions. A zero of a function f, from the real numbers to real numbers or from the complex numbers to the complex numbers, is a number x such that f(x) = 0.

In elementary school/high school we are taught ways of reducing our equations to find roots. However, in this example we are going to use a genetic algorithm to do this for us. No more thinking, just enter and get an answer.

Our root finding function. Photo by author.
Our root finding function. Photo by author.

Below is a set up of what we are going to do. As you can see, not only does EasyGA allow us to run a complex genetic algorithm, but it does it in only 33 lines of Python code.

Output:

Image by author.
Image by author.

Output:

Current Generation     : 10000
Chromosome - 0:

[0.0004391616070176241]
[-0.00018549426948410996]
[0.0014267659834530377]
 Fitness = 2.26293221262846e-06
Chromosome - 1:
[0.0004391616070176241]
[-0.00018549426948410996]
[0.0014267659834530377]
Fitness = 2.26293221262846e-06
Chromosome - 3:
[0.3639195265169164]
[-0.00018549426948410996]
[0.0014267659834530377] 
Fitness = 0.00013243949184959217
Chromosome - 4:
[0.3639195265169164]
[-0.00018549426948410996]
[0.0014267659834530377]
Fitness = 0.00013243949184959217
etc

Summary

In this tutorial we have illustrated how to use the EasyGA Python package for a simple "find all 1’s" in a gene pool problem and then used to it for finding roots to mathematical equations.

After package installation, five steps are needed to run EasyGA:

  • Import EasyGA
  • Setup your gene_impl or chromosome_impl variable
  • Create and define your fitness function
  • Evolve your genetic algorithm
  • Finally, print your results using a graph or printing your solution.

If you want to be updated on my research and other activities, you can follow me on Youtube, Github and even Twitch doing some live coding.

Thank you for reading, Daniel Wilczak


Related Articles