ahmedfgad / GeneticAlgorithmPython

Source code of PyGAD, a Python 3 library for building the genetic algorithm and training machine learning algorithms (Keras & PyTorch).
https://pygad.readthedocs.io
BSD 3-Clause "New" or "Revised" License
1.84k stars 462 forks source link

Fitness value of a solution computed multiple times per generation? #256

Closed sunderved closed 8 months ago

sunderved commented 9 months ago

I put a print statement in my fitness function, and I noticed that it gets called multiple times for a given solution in a given generation. Is this expected?

def fitness_func(ga_instance, solution, sol_idx):
    solution_fitness = test(solution)
    print(ga_instance.generations_completed, sol_idx, solution_fitness)
    return solution_fitness
sunderved commented 9 months ago

A full example to reproduce the issue:

import pygad
import numpy

function_inputs = [4,-2,3.5,5,-11,-4.7]
desired_output = 44

def fitness_func(ga_instance, solution, solution_idx):
    output = numpy.sum(solution*function_inputs)
    solution_fitness = 1.0 / numpy.abs(output - desired_output)
    print(ga_instance.generations_completed, solution_idx, solution_fitness)    
    return solution_fitness

def callback_generation(ga_instance):
    solution, solution_fitness, solution_idx = ga_instance.best_solution()
    print(' ---> End of generation', ga_instance.generations_completed, ":", solution_fitness)

num_generations = 2
num_parents_mating = 4
sol_per_pop = 10
mutation_percent_genes = 10
keep_parents = 1
num_genes = len(function_inputs)
init_range_low = -2
init_range_high = 5
parent_selection_type = "sss"
crossover_type = "single_point"
mutation_type = "random"

ga_instance = pygad.GA(num_generations=num_generations,
                       num_parents_mating=num_parents_mating,
                       fitness_func=fitness_func,
                       sol_per_pop=sol_per_pop,
                       num_genes=num_genes,
                       init_range_low=init_range_low,
                       init_range_high=init_range_high,
                       parent_selection_type=parent_selection_type,
                       keep_parents=keep_parents,
                       crossover_type=crossover_type,
                       mutation_type=mutation_type,
                       mutation_percent_genes=mutation_percent_genes,
                       on_generation=callback_generation)

ga_instance.run()

solution, solution_fitness, solution_idx = ga_instance.best_solution()
print("Parameters of the best solution : {solution}".format(solution=solution))
print("Fitness value of the best solution = {solution_fitness}".format(solution_fitness=solution_fitness))

prediction = numpy.sum(numpy.array(function_inputs)*solution)
print("Predicted output based on the best solution : {prediction}".format(prediction=prediction))
xino1010 commented 8 months ago

@sunderved how did you resolve this issue? I have a fitness function that takes a lot of time to execute it... it's a lost of time re-execute the same solution

ahmedfgad commented 8 months ago

The reason for this behavior is calling the best_solution() method.

When it is called this way, then it forces the fitness function to be called against the latest generation's population. solution, solution_fitness, solution_idx = ga_instance.best_solution()

To re-use the fitness instead of calling the fitness function, pass the pop_fitness parameter. It should be assigned to the fitness of the latest generation's population. solution, solution_fitness, solution_idx = ga_instance.best_solution(pop_fitness=ga_instance.last_generation_fitness)

This new code does not call the fitness function again for the same population.

import pygad
import numpy

function_inputs = [4,-2,3.5,5,-11,-4.7]
desired_output = 44

def fitness_func(ga_instance, solution, solution_idx):
    output = numpy.sum(solution*function_inputs)
    solution_fitness = 1.0 / numpy.abs(output - desired_output)
    print(ga_instance.generations_completed, solution_idx, solution_fitness)    
    return solution_fitness

def callback_generation(ga_instance):
    solution, solution_fitness, solution_idx = ga_instance.best_solution(pop_fitness=ga_instance.last_generation_fitness)
    print(' ---> End of generation', ga_instance.generations_completed, ":", solution_fitness)

num_generations = 2
num_parents_mating = 4
sol_per_pop = 10
mutation_percent_genes = 10
keep_parents = 1
num_genes = len(function_inputs)
init_range_low = -2
init_range_high = 5
parent_selection_type = "sss"
crossover_type = "single_point"
mutation_type = "random"

ga_instance = pygad.GA(num_generations=num_generations,
                       num_parents_mating=num_parents_mating,
                       fitness_func=fitness_func,
                       sol_per_pop=sol_per_pop,
                       num_genes=num_genes,
                       init_range_low=init_range_low,
                       init_range_high=init_range_high,
                       parent_selection_type=parent_selection_type,
                       keep_parents=keep_parents,
                       crossover_type=crossover_type,
                       mutation_type=mutation_type,
                       mutation_percent_genes=mutation_percent_genes,
                       on_generation=callback_generation)

ga_instance.run()

solution, solution_fitness, solution_idx = ga_instance.best_solution(pop_fitness=ga_instance.last_generation_fitness)
print("Parameters of the best solution : {solution}".format(solution=solution))
print("Fitness value of the best solution = {solution_fitness}".format(solution_fitness=solution_fitness))

prediction = numpy.sum(numpy.array(function_inputs)*solution)
print("Predicted output based on the best solution : {prediction}".format(prediction=prediction))
sunderved commented 8 months ago

Thanks @ahmedfgad , this indeed fixes the issue.

xino1010 commented 8 months ago

Thanks @ahmedfgad