Skip to content

keep_elitism not holding elites between instances of ga_instance.run() #344

Open

Description

@tgr001

Hello,

I may be misunderstanding how elites should work, but I am under the impression that elites should be held between instances of running the genetic algorithm. An example of where this would be detrimental would be when saving and loading an instance but losing elites in the process.

I've put together an example of this problem occurring, assuming random seed usage works fine between different machines.

import pygad
import numpy as np

def fitnessfunction(ga_instance, solution, solutionID):
    fitness = np.sum(solution)
    return fitness
def on_generation(ga_instance):
    print (ga_instance.generations_completed, max(ga_instance.cal_pop_fitness()))
num_generations = 3
GeneticAlgorithmInstance = pygad.GA(random_seed=6, num_genes=100, num_generations=num_generations,
                                    sol_per_pop=100, num_parents_mating=5, parent_selection_type="sus", on_generation=on_generation,
                                    gene_type=float, fitness_func=fitnessfunction, keep_elitism=5, mutation_probability=0.2)

while (GeneticAlgorithmInstance.generations_completed < 100):
    if (GeneticAlgorithmInstance.generations_completed > num_generations-1):
        GeneticAlgorithmInstance = pygad.load("phonysaves/"+str(GeneticAlgorithmInstance.generations_completed))
    GeneticAlgorithmInstance.run()
    GeneticAlgorithmInstance.save("phonysaves/"+str(GeneticAlgorithmInstance.generations_completed))
    highest = max(GeneticAlgorithmInstance.cal_pop_fitness())
    if (GeneticAlgorithmInstance.generations_completed > num_generations and prevbest > highest):
        print ("elitism violated here?")
    prevbest = highest

Specifically, it looks like between generation 33 and 34, there is a decrease in the max fitness of solutions. Once again, please let me know if I am simply misunderstanding something!

Thank you!

Activity

  1. ahmedfgad commented on Mar 28, 2026

    @ahmedfgad
    Owner

    The saved pickled files are not loaded properly.

    Assuming there are pickled files already saved, the code still builds a new GA instance and run it from scratch.

    Each time you build the GeneticAlgorithmInstance object using the pygad.GA class, generations_completed is set to 0.

    The following if statement is never executed then because generations_completed = 0.

    if (GeneticAlgorithmInstance.generations_completed > num_generations-1):

    Therefore, no GA is loaded.

    You proceed normally to call the run() method which uses the randomly initialized population. In other words, the pre-computed elites in the previous run are not used.

    GeneticAlgorithmInstance.run()

    To make things simple, I created 2 scripts. This is the first script that creates the GA and run it.

    import pygad
    import numpy as np
    
    def fitnessfunction(ga_instance, solution, solutionID):
        fitness = np.sum(solution)
        return fitness
    
    def on_generation(ga_instance):
        print (ga_instance.generations_completed, max(ga_instance.cal_pop_fitness()))
    
    num_generations = 100
    GeneticAlgorithmInstance = pygad.GA(random_seed=6, num_genes=100, num_generations=num_generations,
                                        sol_per_pop=100, num_parents_mating=5, parent_selection_type="sus", on_generation=on_generation,
                                        gene_type=float, fitness_func=fitnessfunction, keep_elitism=5, mutation_probability=0.2)
    
    GeneticAlgorithmInstance.run()
    GeneticAlgorithmInstance.save("E://phonysaves//"+str(GeneticAlgorithmInstance.generations_completed))
    highest = max(GeneticAlgorithmInstance.cal_pop_fitness())
    
    print(highest)

    The second script just loads the pickled file and it prints the highest fitness achieved previously.

    import pygad
    import numpy as np
    
    def fitnessfunction(ga_instance, solution, solutionID):
        fitness = np.sum(solution)
        return fitness
    
    def on_generation(ga_instance):
        print (ga_instance.generations_completed, max(ga_instance.cal_pop_fitness()))
    
    GeneticAlgorithmInstance = pygad.load("E://phonysaves//"+str(100))
    highest = max(GeneticAlgorithmInstance.cal_pop_fitness())
    print(highest)

    Hope this clarifies your doubts.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    help wantedExtra attention is needed

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions