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.88k stars 463 forks source link

Error saving GA instance #141

Open Halone228 opened 2 years ago

Halone228 commented 2 years ago

When i trying save GA instance, a got error from pickle TypeError: cannot pickle 'Clock' object Whats worng?

krkaufma commented 1 year ago

Can you provide the minimum example of your code? Particularly the part where you try to save the GA instance?

ahmedfgad commented 1 year ago

Sometimes you use some objects that need some workaround to pickle. A minimum code sample will be helpful to figure out the issue.

Halone228 commented 1 year ago

Yes, pickle is the problem. In my program, I used tqdm to show the progress of the population, however, tqdm has a rate estimation mechanism that uses time, and time objects cannot be serialized.

def on_generations(self,*args):
        self.tqdm.update(1)

At this point, most likely serelization fall

def update(self, n=1):
    ...
    if self.n - self.last_print_n >= self.miniters:
    cur_t = self._time()
    dt = cur_t - self.last_print_t
    if dt >= self.mininterval and cur_t >= self.start_t + self.delay:
        cur_t = self._time()

Because in tqdm update functions uses time functions, which cannot be serialized.

ahmedfgad commented 1 year ago

Can you provide the minimum example of your code? Particularly the part where you try to save the GA instance?

@krkaufma,

This is the part of the code that saves the GA instance: https://github.com/ahmedfgad/GeneticAlgorithmPython/blob/7699a561611113d51054bc371536659551827eee/example.py#L59

ahmedfgad commented 1 year ago

Yes, pickle is the problem. In my program, I used tqdm to show the progress of the population, however, tqdm has a rate estimation mechanism that uses time, and time objects cannot be serialized.

def on_generations(self,*args):
        self.tqdm.update(1)

At this point, most likely serelization fall

def update(self, n=1):
    ...
    if self.n - self.last_print_n >= self.miniters:
    cur_t = self._time()
    dt = cur_t - self.last_print_t
    if dt >= self.mininterval and cur_t >= self.start_t + self.delay:
        cur_t = self._time()

Because in tqdm update functions uses time functions, which cannot be serialized.

Good that you found the issue.

You can try the cloudpickle library. https://github.com/cloudpipe/cloudpickle

It may solve your issue. I am testing it to see if it could replace the pickle library.