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

Daniel Wilczak
Dec 8, 2020 · 4 min read
Image for post
Image for post

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:

# 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.

Image for post
Image for post
EasyGA Initialization Variables:

Create the genes for the chromosome:

# Create random genes from 0 to 10
ga.gene_impl = lambda: random.randint(0, 10)
Image for post
Image for post
Gene_impl example:

Default Fitness Function

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

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

Nerd For Tech

Technical Media House

Daniel Wilczak

Written by

Dedicated college student who loves to write simple code for everyone. Passion to make machine learning for everyone.

Nerd For Tech

We are tech nerds because we believe in reinventing the world with the power of Technology. Our articles talk about some of the most disruptive ideas, technology, and innovation.

Daniel Wilczak

Written by

Dedicated college student who loves to write simple code for everyone. Passion to make machine learning for everyone.

Nerd For Tech

We are tech nerds because we believe in reinventing the world with the power of Technology. Our articles talk about some of the most disruptive ideas, technology, and innovation.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store