Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Alleviating Python metaclass to simplify things in DEAP #58

Closed
wants to merge 0 commits into from

Conversation

cmd-ntrf
Copy link
Member

This pull-request solves two problems we have in DEAP using Python metaclass.

  1. Calling 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.
  2. Genetic programming (GP) ephemeral constant could not be pickled unless the 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 with type and it is added to the gp module scope. This is hackish, but allowed to a certain extend the pickling of PrimitiveTree that would contain Ephemerals.

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 in copy_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.

  1. creator.MetaCreator
  2. gp.MetaEphemeral

The idea should be exploited further. For example, we could have a MetaFitness that would make sure the weights attribute is set when creating the class. The behavior of the Fitness class could also be adjusted, so for example the evaluation function would not have to return a tuple for single objective problem.

@cmd-ntrf
Copy link
Member Author

The pickling test for Ephemeral is failing because we are trying to pickle a lambda.

@soravux
Copy link
Contributor

soravux commented Feb 22, 2015

Here's how lambda function pickling is done in SCOOP. It's somewhat similar to metaclasses, actually... (Don't check the "unpickleLambda" functions later on in this file, it's dead code :S .)

@fmder
Copy link
Member

fmder commented Feb 28, 2015

How did we pickled lambd functions in ephemerals?

On Sun, Feb 22, 2015, 16:43 Yannick Hold notifications@github.com wrote:

Here's how lambda function pickling is done in SCOOP
https://code.google.com/p/scoop/source/browse/scoop/encapsulation.py#56.
It's pretty similar to metaclasses, actually... (Don't check the
"unpickleLambda" functions later on in this file, it's dead code :S .)


Reply to this email directly or view it on GitHub
#58 (comment).

@cmd-ntrf
Copy link
Member Author

cmd-ntrf commented May 2, 2015

I have reviewed my code, integrated a cache, modified the pickling test to use partial instead of lambda and add a warning in MetaEphemeral when someone defines an ephemeral using a lambda function.

@cmd-ntrf cmd-ntrf force-pushed the master branch 6 times, most recently from e264b09 to 376b283 Compare May 2, 2015 23:51
@cmd-ntrf cmd-ntrf added this to the 1.1.0 milestone May 2, 2015
@cmd-ntrf cmd-ntrf modified the milestones: 2.0, 1.1.0 May 3, 2015
@cmd-ntrf cmd-ntrf closed this May 5, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants