Skip to content

Commit

Permalink
Blank line fixes (PEP8 messages E301, E302, E303, W391).
Browse files Browse the repository at this point in the history
  • Loading branch information
bje- committed May 4, 2015
1 parent 70409df commit 2886c7f
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 7 deletions.
8 changes: 6 additions & 2 deletions deap/algorithms.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

import tools


def varAnd(population, toolbox, cxpb, mutpb):
"""Part of an evolutionary algorithm applying only the variation part
(crossover **and** mutation). The modified individuals have their
Expand Down Expand Up @@ -79,6 +80,7 @@ def varAnd(population, toolbox, cxpb, mutpb):

return offspring


def eaSimple(population, toolbox, cxpb, mutpb, ngen, stats=None,
halloffame=None, verbose=__debug__):
"""This algorithm reproduce the simplest evolutionary algorithm as
Expand Down Expand Up @@ -185,6 +187,7 @@ def eaSimple(population, toolbox, cxpb, mutpb, ngen, stats=None,

return population, logbook


def varOr(population, toolbox, lambda_, cxpb, mutpb):
"""Part of an evolutionary algorithm applying only the variation part
(crossover, mutation **or** reproduction). The modified individuals have
Expand Down Expand Up @@ -241,6 +244,7 @@ def varOr(population, toolbox, lambda_, cxpb, mutpb):

return offspring


def eaMuPlusLambda(population, toolbox, mu, lambda_, cxpb, mutpb, ngen,
stats=None, halloffame=None, verbose=__debug__):
"""This is the :math:`(\mu + \lambda)` evolutionary algorithm.
Expand Down Expand Up @@ -332,6 +336,7 @@ def eaMuPlusLambda(population, toolbox, mu, lambda_, cxpb, mutpb, ngen,

return population, logbook


def eaMuCommaLambda(population, toolbox, mu, lambda_, cxpb, mutpb, ngen,
stats=None, halloffame=None, verbose=__debug__):
"""This is the :math:`(\mu~,~\lambda)` evolutionary algorithm.
Expand Down Expand Up @@ -429,10 +434,9 @@ def eaMuCommaLambda(population, toolbox, mu, lambda_, cxpb, mutpb, ngen,
logbook.record(gen=gen, nevals=len(invalid_ind), **record)
if verbose:
print logbook.stream


return population, logbook


def eaGenerateUpdate(toolbox, ngen, halloffame=None, stats=None,
verbose=__debug__):
"""This is algorithm implements the ask-tell model proposed in
Expand Down
2 changes: 1 addition & 1 deletion deap/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class used as base class, for the fitness member of any individual. """
from functools import partial
from operator import mul, truediv


class Toolbox(object):
"""A toolbox for evolution that contains the evolutionary operators. At
first the toolbox contains a :meth:`~deap.toolbox.clone` method that
Expand Down Expand Up @@ -261,4 +262,3 @@ def __repr__(self):
"""Return the Python code to build a copy of the object."""
return "%s.%s(%r)" % (self.__module__, self.__class__.__name__,
self.values if self.valid else tuple())

6 changes: 3 additions & 3 deletions deap/cma.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

import tools


class Strategy(object):
"""
A strategy that will keep track of the basic parameters of the CMA-ES
Expand Down Expand Up @@ -154,7 +155,6 @@ def update(self, population):
+ self.ccovmu * numpy.dot((self.weights * artmp.T), artmp) \
/ self.sigma ** 2


self.sigma *= numpy.exp((numpy.linalg.norm(self.ps) / self.chiN - 1.) \
* self.cs / self.damps)

Expand Down Expand Up @@ -201,6 +201,7 @@ def computeParams(self, params):
(self.dim + 1.)) - 1.) + self.cs
self.damps = params.get("damps", self.damps)


class StrategyOnePlusLambda(object):
"""
A CMA-ES strategy that uses the :math:`1 + \lambda` paradigme.
Expand Down Expand Up @@ -316,6 +317,7 @@ def update(self, population):
# to compute covariance matrix inverse in the step-size evolutionary path computation.
self.A = numpy.linalg.cholesky(self.C)


class StrategyMultiObjective(object):
"""Multiobjective CMA-ES strategy based on the paper [Voss2010]_. It
is used similarly as the standard CMA-ES strategy with a generate-update
Expand Down Expand Up @@ -518,7 +520,6 @@ def update(self, population):
self.psucc[p_idx] = (1.0 - cp) * self.psucc[p_idx] + cp
self.sigmas[p_idx] = self.sigmas[p_idx] * exp((self.psucc[p_idx] - ptarg) / (d * (1.0 - ptarg)))


# It is unnecessary to update the entire parameter set for not chosen individuals
# Their parameters will not make it to the next generation
for ind in not_chosen:
Expand All @@ -529,7 +530,6 @@ def update(self, population):
self.psucc[p_idx] = (1.0 - cp) * self.psucc[p_idx]
self.sigmas[p_idx] = self.sigmas[p_idx] * exp((self.psucc[p_idx] - ptarg) / (d * (1.0 - ptarg)))


# Make a copy of the internal parameters
# The parameter is in the temporary variable for offspring and in the original one for parents
self.parents = chosen
Expand Down
2 changes: 2 additions & 0 deletions deap/creator.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ def __reduce__(self):

class_replacers[numpy.ndarray] = _numpy_array


class _array(array.array):
@staticmethod
def __new__(cls, seq=()):
Expand All @@ -91,6 +92,7 @@ def __reduce__(self):
return (self.__class__, (list(self),), self.__dict__)
class_replacers[array.array] = _array


def create(name, base, **kargs):
"""Creates a new class named *name* inheriting from *base* in the
:mod:`~deap.creator` module. The new class can have attributes defined by
Expand Down
18 changes: 17 additions & 1 deletion deap/gp.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
# Define the name of type for any types.
__type__ = object


class PrimitiveTree(list):
"""Tree specifically formatted for optimization of genetic
programming operations. The tree is represented with a
Expand Down Expand Up @@ -189,6 +190,7 @@ class Primitive(object):
'mul(1, 2)'
"""
__slots__ = ('name', 'arity', 'args', 'ret', 'seq')

def __init__(self, name, args, ret):
self.name = name
self.arity = len(args)
Expand All @@ -207,11 +209,13 @@ def __eq__(self, other):
else:
return NotImplemented


class Terminal(object):
"""Class that encapsulates terminal primitive in expression. Terminals can
be values or 0-arity functions.
"""
__slots__ = ('name', 'value', 'ret', 'conv_fct')

def __init__(self, terminal, symbolic, ret):
self.ret = ret
self.value = terminal
Expand All @@ -232,6 +236,7 @@ def __eq__(self, other):
else:
return NotImplemented


class Ephemeral(Terminal):
"""Class that encapsulates a terminal which value is set when the
object is created. To mutate the value, a new object has to be
Expand All @@ -247,6 +252,7 @@ def func():
"""
raise NotImplementedError


class PrimitiveSetTyped(object):
"""Class that contains the primitives that can be used to solve a
Strongly Typed GP problem. The set also defined the researched
Expand Down Expand Up @@ -417,6 +423,7 @@ def terminalRatio(self):
"""
return self.terms_count / float(self.terms_count + self.prims_count)


class PrimitiveSet(PrimitiveSetTyped):
"""Class same as :class:`~deap.gp.PrimitiveSetTyped`, except there is no
definition of type.
Expand Down Expand Up @@ -473,6 +480,7 @@ def compile(expr, pset):
"operators. See the DEAP documentation for more information. "
"DEAP will now abort."), traceback


def compileADF(expr, psets):
"""Compile the expression represented by a list of trees. The first
element of the list is the main tree, and the following elements are
Expand All @@ -498,6 +506,7 @@ def compileADF(expr, psets):
adfdict.update({pset.name: func})
return func


######################################
# GP Program generation functions #
######################################
Expand All @@ -518,6 +527,7 @@ def condition(height, depth):
return depth == height
return generate(pset, min_, max_, condition, type_)


def genGrow(pset, min_, max_, type_=None):
"""Generate an expression where each leaf might have a different depth
between *min* and *max*.
Expand All @@ -538,6 +548,7 @@ def condition(height, depth):
(depth >= min_ and random.random() < pset.terminalRatio)
return generate(pset, min_, max_, condition, type_)


def genHalfAndHalf(pset, min_, max_, type_=None):
"""Generate an expression with a PrimitiveSet *pset*.
Half the time, the expression is generated with :func:`~deap.gp.genGrow`,
Expand All @@ -554,6 +565,7 @@ def genHalfAndHalf(pset, min_, max_, type_=None):
method = random.choice((genGrow, genFull))
return method(pset, min_, max_, type_)


def genRamped(pset, min_, max_, type_=None):
"""
.. deprecated:: 1.0
Expand All @@ -563,6 +575,7 @@ def genRamped(pset, min_, max_, type_=None):
FutureWarning)
return genHalfAndHalf(pset, min_, max_, type_)


def generate(pset, min_, max_, condition, type_=None):
"""Generate a Tree as a list of list. The tree is build
from the root to the leaves, and it stop growing when the
Expand Down Expand Up @@ -756,6 +769,7 @@ def mutNodeReplacement(individual, pset):

return individual,


def mutEphemeral(individual, mode):
"""This operator works on the constants of the tree *individual*. In
*mode* ``"one"``, it will change the value of one of the individual
Expand Down Expand Up @@ -783,6 +797,7 @@ def mutEphemeral(individual, mode):

return individual,


def mutInsert(individual, pset):
"""Inserts a new branch at a random position in *individual*. The subtree
at the chosen position is used as child node of the created subtree, in
Expand Down Expand Up @@ -822,6 +837,7 @@ def mutInsert(individual, pset):
individual[slice_] = new_subtree
return individual,


def mutShrink(individual):
"""This operator shrinks the *individual* by chosing randomly a branch and
replacing it with one of the branch's arguments (also randomly chosen).
Expand Down Expand Up @@ -856,6 +872,7 @@ def mutShrink(individual):
# GP bloat control decorators #
######################################


def staticLimit(key, max_value):
"""Implement a static limit on some measurement on a GP tree, as defined
by Koza in [Koza1989]. It may be used to decorate both crossover and
Expand Down Expand Up @@ -1007,7 +1024,6 @@ def _genpop(n, pickfrom=[], acceptfunc=lambda s: True, producesizes=False):
else:
return producedpop


halflifefunc = lambda x: (x * float(alpha) + beta)
if nbrindsmodel == -1:
nbrindsmodel = max(2000, len(population))
Expand Down

0 comments on commit 2886c7f

Please sign in to comment.