You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
keep_elitism not holding elites between instances of ga_instance.run() #344
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.
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!
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.
Activity
ahmedfgad commentedon Mar 28, 2026
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
GeneticAlgorithmInstanceobject using thepygad.GAclass,generations_completedis set to 0.The following
ifstatement is never executed then becausegenerations_completed = 0.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.To make things simple, I created 2 scripts. This is the first script that creates the GA and run it.
The second script just loads the pickled file and it prints the highest fitness achieved previously.
Hope this clarifies your doubts.