Skip to content

pygad.load() tries to run the model #159

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
krkaufma opened this issue Feb 6, 2023 · 4 comments
Open

pygad.load() tries to run the model #159

krkaufma opened this issue Feb 6, 2023 · 4 comments
Labels
enhancement New feature or request

Comments

@krkaufma
Copy link

krkaufma commented Feb 6, 2023

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

@ahmedfgad
Copy link
Owner

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.

@krkaufma
Copy link
Author

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!

@ahmedfgad
Copy link
Owner

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
Copy link
Owner

@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 ahmedfgad added the enhancement New feature or request label Feb 20, 2023
ahmedfgad added a commit that referenced this issue Feb 22, 2023
PyGAD 2.19.0 Release Notes
1. A new `summary()` method is supported to return a Keras-like summary of the PyGAD lifecycle.
2. A new optional parameter called `fitness_batch_size` is supported to calculate the fitness function in batches. If it is assigned the value `1` or `None` (default), then the normal flow is used where the fitness function is called for each individual solution. If the `fitness_batch_size` parameter is assigned a value satisfying this condition `1 < fitness_batch_size <= sol_per_pop`, then the solutions are grouped into batches of size `fitness_batch_size` and the fitness function is called once for each batch. In this case, the fitness function must return a list/tuple/numpy.ndarray with a length equal to the number of solutions passed. #136.
3. The `cloudpickle` library (https://github.com/cloudpipe/cloudpickle) is used instead of the `pickle` library to pickle the `pygad.GA` objects. This solves the issue of having to redefine the functions (e.g. fitness function). The `cloudpickle` library is added as a dependancy in the `requirements.txt` file. #159
4. Support of assigning methods to these parameters: `fitness_func`, `crossover_type`, `mutation_type`, `parent_selection_type`, `on_start`, `on_fitness`, `on_parents`, `on_crossover`, `on_mutation`, `on_generation`, and `on_stop`. #92 #138
5. Validating the output of the parent selection, crossover, and mutation functions.
6. The built-in parent selection operators return the parent's indices as a NumPy array.
7. The outputs of the parent selection, crossover, and mutation operators must be NumPy arrays.
8. Fix an issue when `allow_duplicate_genes=True`. #39
9. Fix an issue creating scatter plots of the solutions' fitness.
10. Sampling from a `set()` is no longer supported in Python 3.11. Instead, sampling happens from a `list()`. Thanks `Marco Brenna` for pointing to this issue.
11. The lifecycle is updated to reflect that the new population's fitness is calculated at the end of the lifecycle not at the beginning. #154 (comment)
12. There was an issue when `save_solutions=True` that causes the fitness function to be called for solutions already explored and have their fitness pre-calculated. #160
13. A new instance attribute named `last_generation_elitism_indices` added to hold the indices of the selected elitism. This attribute helps to re-use the fitness of the elitism instead of calling the fitness function.
14. Fewer calls to the `best_solution()` method which in turns saves some calls to the fitness function.
15. Some updates in the documentation to give more details about the `cal_pop_fitness()` method. #79 (comment)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants