Skip to content

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

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

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