Search code examples
pythongenetic-algorithm

Gene AI package python - define a custom fitness function


I am using the Gene AI package in python for testing genetic algorithm (https://github.com/diogomatoschaves/geneal/blob/master/geneal/genetic_algorithms/genetic_algorithm_base.py).

I want my own fitness function so i wrote

def my_fitness(chromosome):   
    fitness = mean_absolute_percentage_error(chromosome, [0.5 0.5 0.5 0.5])
    return fitness

And then followed the documentation and wrote below code:

    from geneal.genetic_algorithms import ContinuousGenAlgSolver
    from geneal.applications.fitness_functions.continuous import fitness_functions_continuous
    
    solver = ContinuousGenAlgSolver(
    n_genes=4, 
    fitness_function=my_fitness(chromosome),
    pop_size=10,
    max_gen=200,
    mutation_rate=0.1,
    selection_rate=0.6,
    selection_strategy="roulette_wheel",
    problem_type=float, # Defines the possible values as float numbers
    variables_limits=(-10, 10) # Defines the limits of all variables between -10 and 10. 
                               # Alternatively one can pass an array of tuples defining the limits
                               # for each variable: [(-10, 10), (0, 5), (0, 5), (-20, 20)]
)

solver.solve()

Its not clear how i can use my own fitness function. getting error that chromosome not defined (obviously!). how to use my own fitness function with this package. please show.


Solution

  • A fitness function has 2 requirements:

    • It must get passed one and only one argument - the chromossome. The chromossome is an numpy array of 1's and 0's if it's the binary genetic algorithm solver, or an numpy array of numbers between 0 and 9 if it's the continuous genetic algorithm solver. The size of this array is defined by the number of genes you initialize the solver with, and each position on the array corresponds to a different variable.

    • It must return a real number.

    The inner workings of this function are up for you to decide. And then you pass it to the object during initialization such as:

    from geneal.genetic_algorithms import ContinuousGenAlgSolver
    from geneal.applications.fitness_functions.continuous import fitness_functions_continuous
        
        solver = ContinuousGenAlgSolver(
        n_genes=4, 
        fitness_function=my_fitness,
        pop_size=10,
        max_gen=200,
        mutation_rate=0.1,
        selection_rate=0.6,
        selection_strategy="roulette_wheel",
        problem_type=float, # Defines the possible values as float numbers
        variables_limits=(-10, 10) # Defines the limits of all variables between -10 and 10. 
                                   # Alternatively one can pass an array of tuples defining the limits
                                   # for each variable: [(-10, 10), (0, 5), (0, 5), (-20, 20)]
    )
    

    I suggest you look at the examples provided in the package to get a better idea of how to define a custom fitness function: examples