GeneticAlgorithmPython icon indicating copy to clipboard operation
GeneticAlgorithmPython copied to clipboard

pygad.load() tries to run the model

Open krkaufma opened this issue 2 years ago • 4 comments

Hello and thank you for building PyGAD! It is an awesome module.

I am attempting to load a model checkpoint simply to export the plots.

The issue I am running into is the model appears to try and start running when I use pygad.load() on a model checkpoint (I set the number of generations arbitrarily high). Similarly, I found it odd that I had to have the callback and fitness function defined to be able to load the model.

The code:

import pygad
from main import callback_generation
from fitness import fitness_function

path = '../results/2023_01_31_15_05/model/genetic_2023_01_31_15_05_1744.pkl'

model = pygad.load(path)

model.plot_fitness()
model.plot_new_solution_rate()
model.plot_genes(plot_type='scatter')
model.plot_genes(graph_type='boxplot')

When I run pygad.load, the loaded genetic algorithm immediately creates a population and tries to run. Because I moved this model to another machine, this ultimately fails due to a missing file.

The error message (slightly redacted): 
FileNotFoundError: [Errno 2] No such file or directory: '../results/2023_02_06_14_42/...

It would be ideal if a user could load any model checkpoint to investigate it and the solutions without needing the callback and fitness functions or the model immediately attempting to run().

krkaufma avatar Feb 06 '23 23:02 krkaufma

Hi @krkaufma,

Thank you.

The pygad.load() function does run the algorithm. It is a very simple function that just loads the pickled object and return it.

def load(filename):
    try:
        with open(filename + ".pkl", 'rb') as file:
            ga_in = pickle.load(file)
    ...
    return ga_in

Regarding defining the fitness and callback functions again, we will consider finding an alternative.

ahmedfgad avatar Feb 14 '23 16:02 ahmedfgad

Thanks for the reply @ahmedfgad.

Is there another method to load the pickled model and query data from it without running it? I did not find such a method in the docs.

What happens if I save the model after it reaches a stop criteria? Will it remember that it has already reached completion and not continue to run?

Thank you!

krkaufma avatar Feb 14 '23 16:02 krkaufma

No there is no other method to load the picked object.

I just tested saving the model after and before completion and it does not happen to continue running after calling the load() function.

ahmedfgad avatar Feb 14 '23 17:02 ahmedfgad

@krkaufma,

Just to let you know that a new library called cloudpickle (https://github.com/cloudpipe/cloudpickle) will be used instead of pickle. This supports pickling the fitness function and all callback functions so that you do not have to redefine them.

ahmedfgad avatar Feb 20 '23 23:02 ahmedfgad