Alleviating Python metaclass to simplify things in DEAP #58
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This pull-request solves two problems we have in DEAP using Python metaclass.
creator.create
outside the global scope would prevent instances of the created classes to be pickled. This complicated things when trying to communicate the objects in a context of distributed computation.PrimitiveSet
is instantiated in the main module global scope. The reason is that when adding ephemeral constant to a PrimitiveSet, a new class is generated withtype
and it is added to thegp
module scope. This is hackish, but allowed to a certain extend the pickling ofPrimitiveTree
that would containEphemerals
.The solutions to each problem are similar and are inspired by Python issue 7689 Pickling of classes with a metaclass and copy_reg. The idea is to replace calls to
type
by our own metaclass and to register incopy_reg
how classes were created using this metaclass. This means that when we pickle an instance of a class created dynamically, we also pickled how this class can be built from scratch.This pull request introduces two metaclasses, one for each problem.
creator.MetaCreator
gp.MetaEphemeral
The idea should be exploited further. For example, we could have a
MetaFitness
that would make sure theweights
attribute is set when creating the class. The behavior of theFitness
class could also be adjusted, so for example the evaluation function would not have to return a tuple for single objective problem.