Skip to content

Commit

Permalink
Merge pull request DEAP#42 from professor/master
Browse files Browse the repository at this point in the history
Example on using staticLimit as a decorator for Issue DEAP#41
  • Loading branch information
fmder committed Nov 1, 2014
2 parents 86dc626 + d484286 commit 5253905
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
9 changes: 6 additions & 3 deletions doc/examples/gp_symbreg.rst
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ Now, we want to register some parameters specific to the evolution process.
In DEAP, this is done through the toolbox :

.. literalinclude:: /../examples/gp/symbreg.py
:lines: 49-67
:lines: 49-70

First, a toolbox instance is created (in some problem types like coevolution,
you may consider creating more than one toolbox). Then, we can register any
Expand Down Expand Up @@ -115,6 +115,9 @@ method (a tournament of size 3), the mate method (one point crossover with
uniform probability over all the nodes), the mutation method (an uniform
probability mutation which may append a new full sub-tree to a node).

We decorate the mate and mutate method to limit the depth of generated individuals
to be 50.

At this point, any structure with an access to the toolbox instance will also
have access to all of those registered parameters. Of course, the user could
register other parameters basing on his needs.
Expand All @@ -130,7 +133,7 @@ maximum of both the individuals fitness and size. For that we'll use a
:class:`~deap.tools.MultiStatistics` object.

.. literalinclude:: /../examples/gp/symbreg.py
:lines: 75-81
:lines: 78-84

Note that a simple :class:`~deap.tools.Statistics` object can be used, as in
previous examples when statistics over a single key are desired.
Expand All @@ -144,7 +147,7 @@ creating the population and then calling a complete algorithm. In this case,
we'll use :func:`~deap.algorithms.eaSimple`.

.. literalinclude:: /../examples/gp/symbreg.py
:lines: 72,73,83-84
:lines: 75,76,86-87


The hall of fame is a specific structure which contains the *n* best
Expand Down
3 changes: 3 additions & 0 deletions examples/gp/symbreg.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ def evalSymbReg(individual, points):
toolbox.register("expr_mut", gp.genFull, min_=0, max_=2)
toolbox.register("mutate", gp.mutUniform, expr=toolbox.expr_mut, pset=pset)

toolbox.decorate("mate", gp.staticLimit(key=operator.attrgetter('height'), max_value=50))
toolbox.decorate("mutate", gp.staticLimit(key=operator.attrgetter('height'), max_value=50))

def main():
random.seed(318)

Expand Down

0 comments on commit 5253905

Please sign in to comment.