EasyGA: Genetic Algorithms made Easy. Genetic algorithm in 5 lines of python. Seriously 5 lines.

To keep my promise, here is all the code you will need to get your first genetic algorithm working with the EasyGA package.
pip3 install EasyGA
Run the code below in a python file anywhere on your computer.
import EasyGA
# Create the Genetic algorithm
ga = EasyGA.GA()
# Evolve the genetic algorithm until termination has been reached
ga.evolve()
# Print out the current generation and the population
ga.print_generation()
ga.print_population()
Output:
Current Generation : 15
Current population:
Chromosome - 0 [7][4][4][5][3][5][5][8][3][7] / Fitness = 3
Chromosome - 1 [7][4][4][5][3][5][5][8][3][7] / Fitness = 3
Chromosome - 2 [7][4][4][5][3][5][5][8][3][7] / Fitness = 3
Chromosome - 3 [7][4][4][5][3][5][5][8][3][7] / Fitness = 3
Chromosome - 4 [7][2][4][5][3][5][5][8][3][7] / Fitness = 3
Chromosome - 5 [7][2][4][5][3][5][5][8][3][7] / Fitness = 3
Chromosome - 6 [5][8][8][6][10][10][5][7][2][7] / Fitness = 2
Chromosome - 7 [5][8][8][6][10][10][5][7][2][7] / Fitness = 2
Chromosome - 8 [5][8][8][6][10][10][5][7][2][7] / Fitness = 2
Chromosome - 9 [7][2][8][10][3][5][5][8][1][7] / Fitness = 2
Congratulations, you have done some python import magic and created a genetic algorithm to solve the problem of getting all 5’s in the chromosome. You can basically call yourself an A.I data scientist. Now let’s unpack everything that happened and how we can change this beast.
Default Attributes:
Genetic algorithms can be very complex but somehow, we’ve reduced it to five lines of code. This has been accomplished by using a lot of default attributes. Some of those attributes are:
# Attributes
ga.population_size = 10
ga.chromosome_length = 10
ga.generation_goal = 15
Here is a great example of how population_size, chromosome_length attributes effect the genetic algorithm using the popular game among us.

Create the genes for the chromosome:
Let’s use gene_impl, gene implementation allows you to make all the genes in one function. You may have noticed the random.randint function, this is part of the the standard python library. Better known as “import random”. A full list can be found here or you can use the EasyGA wiki.
# Create random genes from 0 to 10
ga.gene_impl = lambda: random.randint(0, 10)

Default Fitness Function
Last thing needed is a fitness function. This is probably one of the most useless fitness functions, but it works great to explain to anyone who has never created a fitness function. The goal of the default EasyGA fitness function is to get all 5’s in the chromosome.
To prove what I mean. Here is the is_it_5() fitness function.
def is_it_5(chromosome): # Overall fitness value
fitness = 0
# For each gene in the chromosome
for gene in chromosome.gene_list:
# Check if its value = 5
if(gene.value == 5):
# If its value is 5 then add one to
# the overal fitness of the chromosome.
fitness += 1
return fitness
Full Example
Now I love examples, so let’s use what we’ve just learned. However, I don’t like the number 5 so let’s change it to 7 and let’s see if we can get all 7’s after 100 generations and a population size of 50.
import EasyGA
import random# Create the Genetic algorithm
ga = EasyGA.GA()# Attributes
ga.population_size = 50
ga.chromosome_length = 10
ga.generation_goal = 100# Create random genes from 0 to 10
ga.gene_impl = lambda: random.randint(0, 10)# Fitness function to get all 7's
def is_it_7(chromosome):
# Overall fitness value
fitness = 0
# For each gene in the chromosome
for gene in chromosome.gene_list:
# Check if its value = 7
if(gene.value == 7):
# If its value is 7 then add one to
# the overal fitness of the chromosome.
fitness += 1 return fitness# Set the fitness function to the one we made
ga.fitness_function_impl = is_it_7# Evolve the genetic algorithm until termination has been reached
ga.evolve()# Print out the current generation and the population
ga.print_generation()
ga.print_population()
Output:
Current Generation : 100
Current population:
Chromosome - 0 [7][7][7][7][7][7][7][7][7][7] / Fitness = 10
Chromosome - 1 [7][7][7][7][7][7][7][7][7][7] / Fitness = 10
Chromosome - 2 [7][7][7][7][7][7][7][7][7][7] / Fitness = 10
Chromosome - 3 [7][7][7][7][7][7][7][7][7][7] / Fitness = 10
Chromosome - 4 [7][7][7][7][7][7][7][7][7][7] / Fitness = 10
Chromosome - 5 [7][7][7][7][7][7][7][7][10][7] / Fitness = 9
Chromosome - 6 [7][7][7][7][7][7][7][7][7][8] / Fitness = 9
Chromosome - 7 [7][7][7][7][7][2][7][7][7][7] / Fitness = 9
Chromosome - 8 [2][7][7][7][7][7][7][7][7][7] / Fitness = 9
etc...
Let’s go, all 7’s! Now this might be different for you since a genetic algorithm is heuristic and is based off a random start. This article is just an introduction to the EasyGA python package. If you want to learn more about it, just check out the EasyGA wiki. They have great tutorials and even use the popular game among us to explain some of the functions.
Thank you for reading,
Daniel Wilczak