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

Dec 8, 2020 · 4 min read

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 algorithmga = EasyGA.GA()# Evolve the genetic algorithm until termination has been reachedga.evolve()# Print out the current generation and the populationga.print_generation()ga.print_population()`

Output:

`Current Generation      : 15Current population:Chromosome - 0 [7][4][4][5][3][5][5][8][3][7] / Fitness = 3Chromosome - 1 [7][4][4][5][3][5][5][8][3][7] / Fitness = 3Chromosome - 2 [7][4][4][5][3][5][5][8][3][7] / Fitness = 3Chromosome - 3 [7][4][4][5][3][5][5][8][3][7] / Fitness = 3Chromosome - 4 [7][2][4][5][3][5][5][8][3][7] / Fitness = 3Chromosome - 5 [7][2][4][5][3][5][5][8][3][7] / Fitness = 3Chromosome - 6 [5][8][8][6][10][10][5][7][2][7] / Fitness = 2Chromosome - 7 [5][8][8][6][10][10][5][7][2][7] / Fitness = 2Chromosome - 8 [5][8][8][6][10][10][5][7][2][7] / Fitness = 2Chromosome - 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:

`# Attributesga.population_size = 10ga.chromosome_length = 10ga.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:

`# Create random genes from 0 to 10ga.gene_impl = lambda: random.randint(0, 10)`

## 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 EasyGAimport random# Create the Genetic algorithmga = EasyGA.GA()# Attributesga.population_size = 50ga.chromosome_length = 10ga.generation_goal = 100# Create random genes from 0 to 10ga.gene_impl = lambda: random.randint(0, 10)# Fitness function to get all 7'sdef 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 madega.fitness_function_impl = is_it_7# Evolve the genetic algorithm until termination has been reachedga.evolve()# Print out the current generation and the populationga.print_generation()ga.print_population()`

## Output:

`Current Generation     : 100Current population:Chromosome - 0 [7][7][7][7][7][7][7][7][7][7] / Fitness = 10Chromosome - 1 [7][7][7][7][7][7][7][7][7][7] / Fitness = 10Chromosome - 2 [7][7][7][7][7][7][7][7][7][7] / Fitness = 10Chromosome - 3 [7][7][7][7][7][7][7][7][7][7] / Fitness = 10Chromosome - 4 [7][7][7][7][7][7][7][7][7][7] / Fitness = 10Chromosome - 5 [7][7][7][7][7][7][7][7][10][7] / Fitness = 9Chromosome - 6 [7][7][7][7][7][7][7][7][7][8] / Fitness = 9Chromosome - 7 [7][7][7][7][7][2][7][7][7][7] / Fitness = 9Chromosome - 8 [2][7][7][7][7][7][7][7][7][7] / Fitness = 9etc...`

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.

Daniel Wilczak

Written by

Written by