Skip to content

Instantly share code, notes, and snippets.

@cmd-ntrf
Created March 6, 2012 22:01
Show Gist options
  • Save cmd-ntrf/1989214 to your computer and use it in GitHub Desktop.
Save cmd-ntrf/1989214 to your computer and use it in GitHub Desktop.
algorithm eaGenerateUpdate
def eaGenerateUpdate(toolbox, ngen, halloffame=None, stats=None, verbose=True):
"""The CMA-ES algorithm as described in Hansen, N. (2006). *The CMA
Evolution Strategy: A Comparing Rewiew.*
:param toolbox: A :class:`~deap.base.Toolbox` that contains the evolution
operators.
:param ngen: The number of generation.
:param stats: A :class:`~deap.tools.Statistics` object that is updated
inplace, optional.
:param halloffame: A :class:`~deap.tools.HallOfFame` object that will
contain the best individuals, optional.
:param verbose: Whether or not to log the statistics.
The *population* should have been generated from a strategy, while the
toolbox should contain a reference to the update method of the chosen
strategy.
"""
if verbose:
if stats is not None:
logger = tools.EvolutionLogger(["gen", "evals"] + stats.functions.keys())
else:
tools.EvolutionLogger(["gen", "evals"])
logger.logHeader()
population = None
for gen in xrange(ngen):
population = toolbox.generate(population)
# Evaluate the individuals
fitnesses = toolbox.map(toolbox.evaluate, population)
for ind, fit in zip(population, fitnesses):
ind.fitness.values = fit
if halloffame is not None:
halloffame.update(population)
# Update the Strategy with the evaluated individuals
toolbox.update(population)
if stats is not None:
stats.update(population)
if verbose:
logger.logGeneration(evals=len(population), gen=gen, stats=stats)
return population
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment