GeneticAlgorithmPython icon indicating copy to clipboard operation
GeneticAlgorithmPython copied to clipboard

Error saving GA instance

Open Halone228 opened this issue 3 years ago • 5 comments

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

Halone228 avatar Oct 23 '22 16:10 Halone228

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

krkaufma avatar Feb 06 '23 23:02 krkaufma

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

ahmedfgad avatar Feb 14 '23 16:02 ahmedfgad

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.

Halone228 avatar Feb 19 '23 13:02 Halone228

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 avatar Feb 20 '23 19:02 ahmedfgad

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.

ahmedfgad avatar Feb 20 '23 20:02 ahmedfgad