3 new revisions:
Revision: 2474ce497f44
Branch: dev
Author: François-Michel De Rainville <
f.dera...@gmail.com>
Date: Wed Mar 26 12:42:52 2014 UTC
Log: Changed the type of file opened to accomodate python 3 that
requires p...
http://code.google.com/p/deap/source/detail?r=2474ce497f44
Revision: 07a5ab6c82ff
Branch: dev
Author: François-Michel De Rainville <
f.dera...@gmail.com>
Date: Fri Apr 4 17:44:47 2014 UTC
Log: Added constraint decorator for evaluation function to the
framework.
http://code.google.com/p/deap/source/detail?r=07a5ab6c82ff
Revision: 72fb30254cd0
Branch: dev
Author: François-Michel De Rainville <
f.dera...@gmail.com>
Date: Fri Apr 4 17:45:13 2014 UTC
Log: Merge
http://code.google.com/p/deap/source/detail?r=72fb30254cd0
==============================================================================
Revision: 2474ce497f44
Branch: dev
Author: François-Michel De Rainville <
f.dera...@gmail.com>
Date: Wed Mar 26 12:42:52 2014 UTC
Log: Changed the type of file opened to accomodate python 3 that
requires pickle to be written in binary files.
http://code.google.com/p/deap/source/detail?r=2474ce497f44
Modified:
/doc/tutorials/advanced/checkpoint.rst
=======================================
--- /doc/tutorials/advanced/checkpoint.rst Sat Dec 7 04:23:59 2013 UTC
+++ /doc/tutorials/advanced/checkpoint.rst Wed Mar 26 12:42:52 2014 UTC
@@ -59,7 +59,7 @@
# Fill the dictionary using the dict(key=value[, ...])
constructor
cp = dict(population=population, generation=gen,
halloffame=halloffame,
logbook=logbook, rndstate=random.getstate())
- pickle.dump(cp, open("checkpoint_name.pkl", "w"))
+ pickle.dump(cp, open("checkpoint_name.pkl", "wb"))
Now, the whole data will be written in a pickled dictionary every *FREQ*
generations. Loading the checkpoint is done if the main function is given
a path
==============================================================================
Revision: 07a5ab6c82ff
Branch: dev
Author: François-Michel De Rainville <
f.dera...@gmail.com>
Date: Fri Apr 4 17:44:47 2014 UTC
Log: Added constraint decorator for evaluation function to the
framework.
http://code.google.com/p/deap/source/detail?r=07a5ab6c82ff
Added:
/deap/tools/constraint.py
Modified:
/deap/tools/__init__.py
/doc/api/tools.rst
/doc/tutorials/advanced/constraints.rst
=======================================
--- /dev/null
+++ /deap/tools/constraint.py Fri Apr 4 17:44:47 2014 UTC
@@ -0,0 +1,62 @@
+
+from functools import wraps
+
+class Penality(object):
+ """Contraint decorator for evaluation function. This decorator requires
+ a *feasibility* function that returns :data:`True` for a valid
*individual*
+ and :data:`False` otherwise. The *delta* parameter is a constant value
+ added as offset to the optional *distance* function. The distance
+ function shall return a value growing as the individual moves away
+ the valid zone.
+
+ :param feasibility: A function returning the validity status of any
+ individual
+ :param delta: Constant returned for an invalid individual.
+ :param distance: Distance between the individual and a given valid
point.
+ :returns: A decorator for evaluation function.
+
+ See the :doc:`/tutorials/advanced/constraints` for an example.
+ """
+ def __init__(self, feasibility, delta, distance=None):
+ self.fbty_fct = feasibility
+ self.delta = delta
+ self.dist_fct = distance
+
+ def __call__(self, func):
+ @wraps(func)
+ def wrapper(individual, *args, **kwargs):
+ if self.fbty_fct(individual):
+ return func(individual, *args, **kwargs)
+
+ dist = 0
+ if self.dist_fct is not None:
+ dist = self.dist_fct(individual)
+ return self.delta + dist,
+
+ return wrapper
+
+# List of exported function names.
+__all__ = ['Penality']
+
+if __name__ == "__main__":
+ def feasible(individual):
+ """A user defined function that returns :data:`True` for
+ a valid *individual* and :data:`False` otherwise.
+ """
+ if individual[0] > 1.0:
+ return False
+ return True
+
+ @Penality(feasible, 100, lambda ind: ind[0]**2)
+ def evaluate(individual):
+ """A standard evaluation function decorated with the
+ :class:`Penality` decorator, for which *delta* is set to
+ 100 and the distance is set quadratic on the first variable.
+ """
+ return sum(individual),
+
+ ind1 = [0.5, 1.0, 1.0, 1.0]
+ print evaluate(ind1)
+
+ ind2 = [3.0, 1.0, 1.0, 1.0]
+ print evaluate(ind2)
=======================================
--- /deap/tools/__init__.py Fri Jun 21 18:51:47 2013 UTC
+++ /deap/tools/__init__.py Fri Apr 4 17:44:47 2014 UTC
@@ -21,6 +21,7 @@
:class:`History`.
"""
+from .constraint import *
from .crossover import *
from .emo import *
from .init import *
=======================================
--- /doc/api/tools.rst Fri Feb 14 20:09:22 2014 UTC
+++ /doc/api/tools.rst Fri Apr 4 17:44:47 2014 UTC
@@ -200,3 +200,6 @@
.. automethod:: deap.tools.History.getGenealogy(individual[, max_depth])
+Constraints
+-----------
+.. autoclass:: deap.tools.Penality
=======================================
--- /doc/tutorials/advanced/constraints.rst Fri Mar 7 15:13:49 2014 UTC
+++ /doc/tutorials/advanced/constraints.rst Fri Apr 4 17:44:47 2014 UTC
@@ -32,7 +32,7 @@
center of the valid zone.
In DEAP, a penality function can be added to any evaluation function
using the
-:func:`~deap.tools.cntPenality` decorator provided in
the :mod:`~deap.tools`
+:class:`~deap.tools.Penality` decorator provided in the :mod:`~deap.tools`
module. ::
from math import sin
@@ -57,7 +57,7 @@
toolbox = base.Toolbox()
toolbox.register("evaluate", evalFct)
- toolbox.decorate("evaluate", tools.cntPenality(feasible, 7.0, distance))
+ toolbox.decorate("evaluate", tools.Penality(feasible, 7.0, distance))
The penality decorator takes 2 mandatory arguments and an optional one. The
first argument is a function returning the validity of an individual
according
==============================================================================
Revision: 72fb30254cd0
Branch: dev
Author: François-Michel De Rainville <
f.dera...@gmail.com>
Date: Fri Apr 4 17:45:13 2014 UTC
Log: Merge
http://code.google.com/p/deap/source/detail?r=72fb30254cd0