Fitness Function Examples - danielwilczak101/EasyGA GitHub Wiki

Here you can find multiple examples on how to create a fitness function with an associated gene or chromosome implementation.

The EasyGA Default

Gene Impl

ga.gene_impl = lambda: random.randint(0, 10)

Fitness function

def is_it_5(chromosome):
        """A very simple case test function - If the chromosomes gene value is a 5 add one
         to the chromosomes overall fitness value."""
        # 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:

# Create the Genetic algorithm
ga = EasyGA.GA()

ga.gene_impl = lambda: random.randint(0, 10)

ga.evolve()

# Print your default genetic algorithm
ga.print_generation()
ga.print_population()

Output

Current Generation: 15
Current population:
Chromosome - 0 [10][5][9][5][5][3][9][4][5][3] / Fitness = 4
Chromosome - 1 [10][5][9][5][5][3][9][4][5][3] / Fitness = 4
Chromosome - 2 [10][5][9][5][5][3][9][9][5][3] / Fitness = 4
Chromosome - 3 [10][5][9][5][5][3][9][4][5][3] / Fitness = 4
etc

Crack the password

Here you can see the password cracker example. It tries to crack the password of the string you gave it.

import EasyGA
import random
# Create the Genetic algorithm
ga = EasyGA.GA()

password = 'password'
ga.chromosome_length = len(password)
ga.population_size = 20
ga.generation_goal = 100


def password_cracker(chromosome):

    return sum(
        1
        for gene, letter
        in zip(chromosome, password)
        if gene.value == letter
    )

ga.fitness_function_impl = password_cracker

# Create random genes from 0 to 10
ga.gene_impl = lambda: random.choice(["p","a","s","w","o","r","d"]) # Create the letters
ga.evolve()

# Print your default genetic algorithm
ga.print_generation()
ga.print_population()

Output:

Current Generation 	: 100
Chromosome - 0 [p][a][s][s][w][o][r][d] / Fitness = 8
Chromosome - 1 [p][a][s][s][w][o][r][d] / Fitness = 8
Chromosome - 2 [p][a][w][s][a][o][s][s] / Fitness = 4
Chromosome - 3 [p][a][r][p][w][o][p][s] / Fitness = 4
Chromosome - 4 [p][a][w][s][a][o][s][s] / Fitness = 4
Chromosome - 5 [d][a][d][o][w][w][r][w] / Fitness = 3
Chromosome - 6 [p][a][r][w][o][o][d][a] / Fitness = 3
Chromosome - 7 [p][o][d][d][r][s][r][r] / Fitness = 2
Chromosome - 8 [s][a][a][s][o][p][d][r] / Fitness = 2
Chromosome - 9 [p][r][d][a][d][a][o][d] / Fitness = 2
Chromosome - 10 [s][a][w][o][d][o][p][r] / Fitness = 2
Chromosome - 11 [s][a][w][o][d][o][p][s] / Fitness = 2
Chromosome - 12 [d][a][d][o][w][w][d][s] / Fitness = 2
Chromosome - 13 [p][r][w][w][d][o][p][r] / Fitness = 2
Chromosome - 14 [r][r][o][a][w][a][o][p] / Fitness = 1
Chromosome - 15 [p][r][a][o][d][p][p][a] / Fitness = 1
Chromosome - 16 [o][r][r][p][p][o][p][p] / Fitness = 1
Chromosome - 17 [s][w][s][p][d][a][d][r] / Fitness = 1
Chromosome - 18 [p][r][w][p][d][a][d][s] / Fitness = 1
Chromosome - 19 [w][r][r][o][d][o][w][w] / Fitness = 1

Minimize the fitness to all 0's

The goal of this example fitness function is to get all the genes to the smallest number. The best case scenario would be to get a chromosome with [0,0,0,0,0,0,0,0,0,0] which would have a fitness of zero.

def user_def_fitness(chromosome):
    """The sum of the gene values. Take each gene value
     and add it to the chromosomes overall fitness."""

    fitness = 0
    
    # For each gene in the chromosome
    for gene in chromosome:
        
        # Add the genes value to the overall fitness
        fitness += gene.value

    return fitness

Full Example

import EasyGA

# Create the Genetic algorithm
ga = EasyGA.GA()

# Create 25 chromosomes each with 10 genes and 200 generations
ga.population_size = 25
ga.chromosome_length = 10
ga.generation_goal = 200

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

# Minimize the sum of the genes
ga.target_fitness_type = 'minimize'

def user_def_fitness(chromosome):
    """The sum of the gene values. Take each gene value
     and add it to the chromosomes overall fitness."""

    fitness = 0

    for gene in chromosome:
        fitness += gene.value

    return fitness

ga.fitness_function_impl = user_def_fitness


ga.evolve()

# Print your default genetic algorithm
ga.print_generation()
ga.print_population()

Output

Current Generation: 200
Current population:
Chromosome - 0 [0][0][0][0][0][0][0][0][0][0] / Fitness = 0
Chromosome - 1 [0][0][0][0][0][0][0][0][0][0] / Fitness = 0
Chromosome - 2 [0][0][0][0][0][0][0][0][0][0] / Fitness = 0
etc

Genetic Algorithm for sentence guessing:

Get EasyGA to find out your sentence.

import EasyGA
import random

ga = EasyGA.GA()

word = input("Please enter a word: \n")
ga.chromosome_length = len(word)
ga.fitness_goal = len(word)

ga.population_size = 50
ga.generation_goal = 10000


def alpha_organizer(chromosome):

    return sum(
        1
        for gene, letter
        in zip(chromosome, word)
        if gene.value == letter
    )

ga.fitness_function_impl = alpha_organizer

ga.adapt_population_flag = False
ga.adapt_rate = 0

ga.gene_impl = lambda: random.choice(["A","a","B","b","C","c","D","d","E","e",
                                      "F","f","G","g","H","h","I","i","J","j",
                                      "K","k","L","l","M","m","N","n","O","o",
                                      "P","p","Q","q","R","r","S","s","T","t",
                                      "U","u","V","v","W","w","X","x","Y","y",
                                      "Z","z"," "])

ga.evolve()

ga.print_generation()
ga.print_population()

ga.graph.highest_value_chromosome()
ga.graph.show()

Output:

Current Generation 	: 170
Chromosome - 0 [E][a][s][y][G][A] / Fitness = 6
Chromosome - 1 [E][a][s][y][B][A] / Fitness = 5
Chromosome - 2 [E][a][s][y][D][A] / Fitness = 5
Chromosome - 3 [E][a][s][y][B][A] / Fitness = 5
Chromosome - 4 [E][a][s][y][p][A] / Fitness = 5
Chromosome - 5 [E][a][s][y][B][A] / Fitness = 5
etc