[deap] 6 new revisions pushed by f.derain...@gmail.com on 2014-04-09 16:41 GMT

12 views
Skip to first unread message

de...@googlecode.com

unread,
Apr 9, 2014, 12:41:34 PM4/9/14
to deap-de...@googlegroups.com
6 new revisions:

Revision: 4f49b169790e
Branch: dev
Author: François-Michel De Rainville <f.dera...@gmail.com>
Date: Tue Apr 8 13:19:23 2014 UTC
Log: Added weights in the computation of the penality...
http://code.google.com/p/deap/source/detail?r=4f49b169790e

Revision: 050b2a62b72a
Branch: dev
Author: François-Michel De Rainville <f.dera...@gmail.com>
Date: Tue Apr 8 14:53:01 2014 UTC
Log: Changed name of Penality to DeltaPenality and enhanced the
documentati...
http://code.google.com/p/deap/source/detail?r=050b2a62b72a

Revision: d7ff42a37c76
Branch: dev
Author: François-Michel De Rainville <f.dera...@gmail.com>
Date: Wed Apr 9 13:02:43 2014 UTC
Log: Added a check for the the fitness size in ClosestValidPenality
decorat...
http://code.google.com/p/deap/source/detail?r=d7ff42a37c76

Revision: 4bfe1aecab5a
Branch: dev
Author: François-Michel De Rainville <f.dera...@gmail.com>
Date: Wed Apr 9 13:03:37 2014 UTC
Log: Corrected the operation between the evolution path that should be
an o...
http://code.google.com/p/deap/source/detail?r=4bfe1aecab5a

Revision: 46be639a89a2
Branch: default
Author: François-Michel De Rainville <f.dera...@gmail.com>
Date: Wed Apr 9 13:03:37 2014 UTC
Log: Corrected the operation between the evolution path that should be
an o...
http://code.google.com/p/deap/source/detail?r=46be639a89a2

Revision: 11d07ea12b13
Branch: dev
Author: François-Michel De Rainville <f.dera...@gmail.com>
Date: Wed Apr 9 16:40:01 2014 UTC
Log: Merge
http://code.google.com/p/deap/source/detail?r=11d07ea12b13

==============================================================================
Revision: 4f49b169790e
Branch: dev
Author: François-Michel De Rainville <f.dera...@gmail.com>
Date: Tue Apr 8 13:19:23 2014 UTC
Log: Added weights in the computation of the penality
Changed ClosestPenality to ClosestValid Penality
http://code.google.com/p/deap/source/detail?r=4f49b169790e

Modified:
/deap/tools/constraint.py

=======================================
--- /deap/tools/constraint.py Fri Apr 4 20:03:32 2014 UTC
+++ /deap/tools/constraint.py Tue Apr 8 13:19:23 2014 UTC
@@ -15,6 +15,8 @@
:param distance: Distance between the individual and a given valid
point.
:returns: A decorator for evaluation function.

+ This function relies on the individual weights to the distance
+
See the :doc:`/tutorials/advanced/constraints` for an example.
"""
def __init__(self, feasibility, delta, distance=None):
@@ -28,14 +30,19 @@
if self.fbty_fct(individual):
return func(individual, *args, **kwargs)

+ weights = tuple(1 if w >= 0 else -1 for w in
individual.fitness.weights)
+
dist = 0
if self.dist_fct is not None:
dist = self.dist_fct(individual)
- return self.delta + dist,
+ return tuple(self.delta - w * dist for w in weights)

return wrapper

-class ClosestPenality(object):
+class ClosestValidPenality(object):
+ """
+ """
+
def __init__(self, feasibility, feasible, alpha, distance=None):
self.fbty_fct = feasibility
self.fbl_fct = feasible
@@ -51,16 +58,18 @@
f_ind = self.fbl_fct(individual)
f_fbl = func(f_ind, *args, **kwargs)

+ weights = tuple(1.0 if w >= 0 else -1.0 for w in
individual.fitness.weights)
+
dist = 0
if self.dist_fct is not None:
dist = self.dist_fct(f_ind, individual)

- return tuple(f + self.alpha * dist for f in f_fbl)
+ return tuple(f - w * self.alpha * dist for f, w in zip(f_fbl,
weights))

return wrapper

# List of exported function names.
-__all__ = ['Penality', 'ClosestPenality']
+__all__ = ['Penality', 'ClosestValidPenality']

if __name__ == "__main__":
def feasible(individual):

==============================================================================
Revision: 050b2a62b72a
Branch: dev
Author: François-Michel De Rainville <f.dera...@gmail.com>
Date: Tue Apr 8 14:53:01 2014 UTC
Log: Changed name of Penality to DeltaPenality and enhanced the
documentation.
http://code.google.com/p/deap/source/detail?r=050b2a62b72a

Modified:
/deap/tools/constraint.py
/doc/api/tools.rst
/doc/tutorials/advanced/constraints.rst

=======================================
--- /deap/tools/constraint.py Tue Apr 8 13:19:23 2014 UTC
+++ /deap/tools/constraint.py Tue Apr 8 14:53:01 2014 UTC
@@ -1,27 +1,43 @@

from functools import wraps
+from itertools import repeat
+from collections import Sequence

-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.
+class DeltaPenality(object):
+ """This decorator returns penalized fitness for invalid individuals
and the
+ original fitness value for valid individuals. The penalized fitness is
made
+ of a constant factor *delta* added with an (optional) *distance*
penality. The
+ distance function, if provided, 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.
+ individual.
+ :param delta: Constant or array of constants returned for an invalid
individual.
+ :param distance: A function returning the distance between the
individual
+ and a given valid point (optional, defaults to 0).
:returns: A decorator for evaluation function.

- This function relies on the individual weights to the distance
+ This function relies on the fitness weights to add correctly the
distance.
+ The fitness value of the ith objective is defined as
+
+ .. math::
+
+ f^\mathrm{penality}_i(\mathbf{x}) = \Delta_i - w_i d(\mathbf{x})
+
+ where :math:`\mathbf{x}` is the individual, :math:`\Delta_i` is a user
defined
+ constant and :math:`w_i` is the weight of the ith
objective. :math:`\Delta`
+ should be worst than the fitness of any possible individual, this means
+ higher than any fitness for minimization and lower than any fitness for
+ maximization.

See the :doc:`/tutorials/advanced/constraints` for an example.
"""
def __init__(self, feasibility, delta, distance=None):
self.fbty_fct = feasibility
- self.delta = delta
+ if not isinstance(delta, Sequence):
+ self.delta = repeat(delta)
+ else:
+ self.delta = delta
self.dist_fct = distance

def __call__(self, func):
@@ -35,12 +51,38 @@
dist = 0
if self.dist_fct is not None:
dist = self.dist_fct(individual)
- return tuple(self.delta - w * dist for w in weights)
+ return tuple(d - w * dist for d, w in zip(self.delta, weights))

return wrapper

class ClosestValidPenality(object):
- """
+ """This decorator returns penalized fitness for invalid individuals
and the
+ original fitness value for valid individuals. The penalized fitness is
made
+ of the fitness of the closest valid individual added with a weighted
+ (optional) *distance* penality. The distance function, if provided,
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 feasible: A function returning the closest feasible individual
+ from the current invalid individual.
+ :param alpha: Multiplication factor on the distance between the valid
and
+ invalid individual.
+ :param distance: A function returning the distance between the
individual
+ and a given valid point (optional, defaults to 0).
+ :returns: A decorator for evaluation function.
+
+ This function relies on the fitness weights to add correctly the
distance.
+ The fitness value of the ith objective is defined as
+
+ .. math::
+
+ f^\mathrm{penality}_i(\mathbf{x}) =
f_i(\operatorname{valid}(\mathbf{x})) - \\alpha w_i
d(\operatorname{valid}(\mathbf{x}), \mathbf{x})
+
+ where :math:`\mathbf{x}` is the individual,
+ :math:`\operatorname{valid}(\mathbf{x})` is a function returning the
closest
+ valid individual to :math:`\mathbf{x}`, :math:`\\alpha` is the distance
+ multiplicative factor and :math:`w_i` is the weight of the ith
objective.
"""

def __init__(self, feasibility, feasible, alpha, distance=None):
@@ -69,7 +111,7 @@
return wrapper

# List of exported function names.
-__all__ = ['Penality', 'ClosestValidPenality']
+__all__ = ['DeltaPenality', 'ClosestValidPenality']

if __name__ == "__main__":
def feasible(individual):
@@ -80,10 +122,10 @@
return False
return True

- @Penality(feasible, 100, lambda ind: ind[0]**2)
+ @DeltaPenality(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
+ :class:`DeltaPenality` decorator, for which *delta* is set to
100 and the distance is set quadratic on the first variable.
"""
return sum(individual),
=======================================
--- /doc/api/tools.rst Fri Apr 4 17:44:47 2014 UTC
+++ /doc/api/tools.rst Tue Apr 8 14:53:01 2014 UTC
@@ -202,4 +202,6 @@

Constraints
-----------
-.. autoclass:: deap.tools.Penality
+.. autoclass:: deap.tools.DeltaPenality(feasibility, delta[, distance])
+
+.. autoclass:: deap.tools.ClosestValidPenality(feasibility, feasible,
alpha[, distance])
=======================================
--- /doc/tutorials/advanced/constraints.rst Fri Apr 4 17:44:47 2014 UTC
+++ /doc/tutorials/advanced/constraints.rst Tue Apr 8 14:53:01 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
-:class:`~deap.tools.Penality` decorator provided in the :mod:`~deap.tools`
+:class:`~deap.tools.DeltaPenality` 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.Penality(feasible, 7.0, distance))
+ toolbox.decorate("evaluate", tools.DeltaPenality(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: d7ff42a37c76
Branch: dev
Author: François-Michel De Rainville <f.dera...@gmail.com>
Date: Wed Apr 9 13:02:43 2014 UTC
Log: Added a check for the the fitness size in ClosestValidPenality
decorator.
http://code.google.com/p/deap/source/detail?r=d7ff42a37c76

Modified:
/deap/tools/constraint.py

=======================================
--- /deap/tools/constraint.py Tue Apr 8 14:53:01 2014 UTC
+++ /deap/tools/constraint.py Wed Apr 9 13:02:43 2014 UTC
@@ -102,6 +102,9 @@

weights = tuple(1.0 if w >= 0 else -1.0 for w in
individual.fitness.weights)

+ if len(weights) != len(f_fbl):
+ raise IndexError("Fitness weights and computed fitness are
of different size.")
+
dist = 0
if self.dist_fct is not None:
dist = self.dist_fct(f_ind, individual)

==============================================================================
Revision: 4bfe1aecab5a
Branch: dev
Author: François-Michel De Rainville <f.dera...@gmail.com>
Date: Wed Apr 9 13:03:37 2014 UTC
Log: Corrected the operation between the evolution path that should be
an outer product not a dot product.
http://code.google.com/p/deap/source/detail?r=4bfe1aecab5a

Modified:
/deap/cma.py

=======================================
--- /deap/cma.py Tue May 7 12:26:29 2013 UTC
+++ /deap/cma.py Wed Apr 9 13:03:37 2014 UTC
@@ -271,10 +271,10 @@
self.parent = copy.deepcopy(population[0])
if self.psucc < self.pthresh:
self.pc = (1 - self.cc)*self.pc + sqrt(self.cc * (2 -
self.cc)) * x_step
- self.C = (1-self.ccov)*self.C + self.ccov *
numpy.dot(self.pc, self.pc.T)
+ self.C = (1-self.ccov)*self.C + self.ccov *
numpy.outer(self.pc, self.pc)
else:
self.pc = (1 - self.cc)*self.pc
- self.C = (1-self.ccov)*self.C + self.ccov *
(numpy.dot(self.pc, self.pc.T) + self.cc*(2-self.cc)*self.C)
+ self.C = (1-self.ccov)*self.C + self.ccov *
(numpy.outer(self.pc, self.pc) + self.cc*(2-self.cc)*self.C)

self.sigma = self.sigma * exp(1.0/self.d * (self.psucc -
self.ptarg)/(1.0-self.ptarg))


==============================================================================
Revision: 46be639a89a2
Branch: default
Author: François-Michel De Rainville <f.dera...@gmail.com>
Date: Wed Apr 9 13:03:37 2014 UTC
Log: Corrected the operation between the evolution path that should be
an outer product not a dot product.
http://code.google.com/p/deap/source/detail?r=46be639a89a2

Modified:
/deap/cma.py

=======================================
--- /deap/cma.py Tue May 7 12:26:29 2013 UTC
+++ /deap/cma.py Wed Apr 9 13:03:37 2014 UTC
@@ -271,10 +271,10 @@
self.parent = copy.deepcopy(population[0])
if self.psucc < self.pthresh:
self.pc = (1 - self.cc)*self.pc + sqrt(self.cc * (2 -
self.cc)) * x_step
- self.C = (1-self.ccov)*self.C + self.ccov *
numpy.dot(self.pc, self.pc.T)
+ self.C = (1-self.ccov)*self.C + self.ccov *
numpy.outer(self.pc, self.pc)
else:
self.pc = (1 - self.cc)*self.pc
- self.C = (1-self.ccov)*self.C + self.ccov *
(numpy.dot(self.pc, self.pc.T) + self.cc*(2-self.cc)*self.C)
+ self.C = (1-self.ccov)*self.C + self.ccov *
(numpy.outer(self.pc, self.pc) + self.cc*(2-self.cc)*self.C)

self.sigma = self.sigma * exp(1.0/self.d * (self.psucc -
self.ptarg)/(1.0-self.ptarg))


==============================================================================
Revision: 11d07ea12b13
Branch: dev
Author: François-Michel De Rainville <f.dera...@gmail.com>
Date: Wed Apr 9 16:40:01 2014 UTC
Log: Merge
http://code.google.com/p/deap/source/detail?r=11d07ea12b13

Deleted:
/examples/eda/fctmin.py

=======================================
--- /examples/eda/fctmin.py Fri Apr 4 14:25:20 2014 UTC
+++ /dev/null
@@ -1,93 +0,0 @@
-# This file is part of DEAP.
-#
-# DEAP is free software: you can redistribute it and/or modify
-# it under the terms of the GNU Lesser General Public License as
-# published by the Free Software Foundation, either version 3 of
-# the License, or (at your option) any later version.
-#
-# DEAP is distributed in the hope that it will be useful,
-# but WITHOUT ANY WARRANTY; without even the implied warranty of
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-# GNU Lesser General Public License for more details.
-#
-# You should have received a copy of the GNU Lesser General Public
-# License along with DEAP. If not, see <http://www.gnu.org/licenses/>.
-
-import random
-
-import numpy
-
-from operator import attrgetter
-
-from deap import algorithms
-from deap import base
-from deap import benchmarks
-from deap import creator
-from deap import tools
-
-creator.create("FitnessMin", base.Fitness, weights=(-1.0,))
-creator.create("Individual", numpy.ndarray, fitness=creator.FitnessMin)
-
-class EMNA(object):
- """Estimation of Multivariate Normal Algorithm (EMNA) as described
- by Algorithm 1 in:
-
- Fabien Teytaud and Olivier Teytaud. 2009.
- Why one must use reweighting in estimation of distribution algorithms.
- In Proceedings of the 11th Annual conference on Genetic and
- evolutionary computation (GECCO '09). ACM, New York, NY, USA, 453-460.
- """
- def __init__(self, centroid, sigma, mu, lambda_):
- self.dim = len(centroid)
- self.centroid = numpy.array(centroid)
- self.sigma = numpy.array(sigma)
- self.lambda_ = lambda_
- self.mu = mu
-
- def generate(self, ind_init):
- # Generate lambda_ individuals and put them into the provided class
- arz = self.centroid + self.sigma *
numpy.random.randn(self.lambda_, self.dim)
- return list(map(ind_init, arz))
-
- def update(self, population):
- # Sort individuals so the best is first
- sorted_pop = sorted(population, key=attrgetter("fitness"),
reverse=True)
-
- # Compute the average of the mu best individuals
- z = sorted_pop[:self.mu] - self.centroid
- avg = numpy.mean(z, axis=0)
-
- # Adjust variance of the distribution
- self.sigma = numpy.sqrt(numpy.sum(numpy.sum((z - avg)**2, axis=1))
/ (self.mu*self.dim))
- self.centroid = self.centroid + avg
-
-def main():
- N, LAMBDA = 30, 1000
- MU = int(LAMBDA/4)
- strategy = EMNA(centroid=[5.0]*N, sigma=5.0, mu=MU, lambda_=LAMBDA)
-
- toolbox = base.Toolbox()
- toolbox.register("evaluate", benchmarks.rastrigin)
- toolbox.register("generate", strategy.generate, creator.Individual)
- toolbox.register("update", strategy.update)
-
- # Numpy equality function (operators.eq) between two arrays returns the
- # equality element wise, which raises an exception in the if similar()
- # check of the hall of fame. Using a different equality function like
- # numpy.array_equal or numpy.allclose solve this issue.
- hof = tools.HallOfFame(1, similar=numpy.array_equal)
- stats = tools.Statistics(lambda ind: ind.fitness.values)
- stats.register("avg", numpy.mean)
- stats.register("std", numpy.std)
- stats.register("min", numpy.min)
- stats.register("max", numpy.max)
-
- algorithms.eaGenerateUpdate(toolbox, ngen=150, stats=stats,
halloffame=hof)
-
- return hof[0].fitness.values[0]
-
-if __name__ == "__main__":
- main()
-
-
-
Reply all
Reply to author
Forward
0 new messages