ahmedfgad / GeneticAlgorithmPython

Source code of PyGAD, a Python 3 library for building the genetic algorithm and training machine learning algorithms (Keras & PyTorch).
https://pygad.readthedocs.io
BSD 3-Clause "New" or "Revised" License
1.79k stars 451 forks source link

ga_instance.save will not work with tqdm example #262

Open mrh335 opened 5 months ago

mrh335 commented 5 months ago

I am running the example on how to use TQDM. I have used this successfully in a prior version and installation, but when running my previous code or the example code, I receive the error: File C:\ProgramData\Anaconda3\envs\pygad_2_2_0-2\Lib\site-packages\cloudpickle\cloudpickle.py:1245 in dump return super().dump(obj)

TypeError: cannot pickle '_hashlib.HMAC' object

Pygad version 3.1.0 and also tried 3.2.0

This is the code that was run returning this result.

import pygad
import numpy
import tqdm

equation_inputs = [4,-2,3.5]
desired_output = 44

def fitness_func(ga_instance, solution, solution_idx):
    output = numpy.sum(solution * equation_inputs)
    fitness = 1.0 / (numpy.abs(output - desired_output) + 0.000001)
    return fitness

def on_generation_progress(ga):
    pbar.update(1)

num_generations = 100
with tqdm.tqdm(total=num_generations) as pbar:
    ga_instance = pygad.GA(num_generations=num_generations,
                           sol_per_pop=5,
                           num_parents_mating=2,
                           num_genes=len(equation_inputs),
                           fitness_func=fitness_func,
                           on_generation=on_generation_progress)

    ga_instance.run()

ga_instance.plot_result()

ga_instance.save("test")
ahmedfgad commented 5 months ago

The hash object cannot be pickled. It is an issue with the cloudpickle library used by PyGAD to pickle the pygad.GA objects.