Skip to content

Maximize & Minimize Fitness

Daniel Wilczak edited this page May 5, 2021 · 12 revisions

Fitness functions fall into two categories.

Maximizing the fitness is used if your problem requires your fitness to keep growing.

Minimizing your fitness is the inverse of maximizing but this is also very useful when your trying to reduce some type of error.

Maximize Fitness

If the fitness is to be Maximized, then this is set by default in EasyGA. But if you want to make it explicated in your code:

ga.target_fitness_type = 'max'

The EasyGA default fitness function

The default fitness function for EasyGA is rather simple. Lets see if we can make the chromosome all 5's. For example the best chromosome would be: [5,5,5,5,5,5,5,5,5,5] which would give you a fitness of 10!

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:

import EasyGA
import random

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

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

ga.fitness_function_impl = is_it_5

# Create random genes from 0 to 10
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

Minimize Fitness

If the fitness is to be minimized, then set it using the following:

ga.target_fitness_type = 'min'

Fitness function

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
import random

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

# Make the package minimize the fitness
ga.target_fitness_type = 'min'

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

Build your own fitness function

You can easily change the fitness function to any function you want. A list of example fitness functions can be found on the example fitness functions page. To define a user fitness function:

def user_def_fitness(chromosome):
    pass

Then set the ga.fitness_impl to your function:

ga.fitness_function_impl = user_def_fitness

Next Steps

See the different things you can do with your ga Ways to Print, Ways to Run and Graph Data

Clone this wiki locally