Looking for simple method to save a hall of fame individual when the code completes. Couldn't find method in library or example in forums to complete this task. The hall of fame methods are not pickable. I am sure there is some parent class or function in which to convert this to that would make this simple, not seeing it at the moment.
I've tried a standard python pickle. Also attempted using checkpoint method. Get exact same pickle failure.
## end of code
def main():
random.seed(10)
pop = toolbox.population(n=100)
hof = tools.HallOfFame(10)
stats = tools.Statistics(lambda ind: ind.fitness.values)
stats.register("avg", tools.mean)
stats.register("std", tools.std)
stats.register("min", min)
stats.register("max", max)
algorithms.eaSimple(pop, toolbox, 0.5, 0.2, 50, stats, halloffame=hof)
return pop, stats, hof
if __name__ == "__main__":
pop, stats, hof = main()
output = open('hof_10.pkl', 'wb')
cPickle.dump(hof[0],output)
output.close()
##Error
Traceback (most recent call last):
File "C:\Users\user\folder\gp_ver3.pyw", line 158, in <module>
cPickle.dump(hof[0],output)
File "C:\Python27\lib\copy_reg.py", line 77, in _reduce_ex
raise TypeError("a class that defines __slots__ without "
TypeError: a class that defines __slots__ without defining __getstate__ cannot be pickled
my attempt at checkpoint
if __name__ == "__main__":
pop, stats, hof = main()
cp = Checkpoint()
cp.add("my_object", hof[0])
cp.dump(open("example.ecp", "w"))
File "C:\Python27\lib\copy_reg.py", line 77, in _reduce_ex
raise TypeError("a class that defines __slots__ without "
TypeError: a class that defines __slots__ without defining __getstate__ cannot be pickled