pymoo icon indicating copy to clipboard operation
pymoo copied to clipboard

CMA-ES save algorithm with pickle

Open 2ThomasAnd opened this issue 3 years ago • 1 comments

Description

pickle can not save algorithm.es because it is a <generator object my_fmin at ...> So if the CMAES algorithm is loaded again, es is missing and continuing the optimization with this algorithm is not possible.

Error: AttributeError: 'CMAES' object has no attribute 'es'

Example Code

import pickle
import numpy as np
from pymoo.algorithms.soo.nonconvex.cmaes import CMAES
from pymoo.problems import get_problem
from pymoo.optimize import minimize
from pymoo.termination import get_termination

algorithm = CMAES()    
res = minimize(get_problem("sphere"), algorithm, ('n_gen', 3), verbose=True)
# save algorithm object
with open('pymoo_alg.pkl', 'wb') as f:
    pickle.dump(res.algorithm, f)
with open('pymoo_alg.pkl', 'rb') as f:
    alg = pickle.load(f)

# continue optimization run
# alg = res.algorithm  # uncomment to use original algorithm
print("new termination")
alg.termination = get_termination('n_gen', 40)
while alg.has_next():
    alg.next()

2ThomasAnd avatar Aug 16 '22 13:08 2ThomasAnd

same error occurs, when making a copy of the algorithm algorithm = CMAES() alg = copy.deepcopy(algorithm) alg.es → Error: 'CMAES' object has no attribute 'es'

ghost avatar Aug 16 '22 13:08 ghost

CMAES can not be pickled because it uses a generator. This is how the original implementation has been incorporated into pymoo. I have not found an easy solution for this issue. Let me know if you now one or have even found one by now.

blankjul avatar Dec 13 '22 05:12 blankjul