Skip to content

The on_generation callback is not called at the end of generation 0 #257

@sunderved

Description

@sunderved

The on_generation callback is called at the of the generation starting with generation 1, but it is not called at the end of generation 0

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionFurther information is requested

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions