Skip to content

Instantly share code, notes, and snippets.

@cmd-ntrf
Created March 6, 2012 22:03
Show Gist options
  • Save cmd-ntrf/1989254 to your computer and use it in GitHub Desktop.
Save cmd-ntrf/1989254 to your computer and use it in GitHub Desktop.
CMA-ES generate update example
import numpy
from deap import algorithms
from deap import base
from deap import benchmarks
from deap import cma
from deap import creator
from deap import tools
# Problem size
N=30
creator.create("FitnessMin", base.Fitness, weights=(-1.0,))
creator.create("Individual", list, fitness=creator.FitnessMin)
def main():
# The cma module uses the numpy random number generator
numpy.random.seed(128)
# The CMA-ES algorithm takes a population of one individual as argument
# The centroid is set to a vector of 5.0 see http://www.lri.fr/~hansen/cmaes_inmatlab.html
# for more details about the rastrigin and other tests for CMA-ES
strategy = cma.Strategy(centroid=[5.0]*N, sigma=5.0, lambda_=20*N)
strategy.register("evaluate", benchmarks.rastrigin)
strategy.register("individual", creator.Individual)
stats = tools.Statistics(lambda ind: ind.fitness.values)
stats.register("avg", tools.mean)
stats.register("std", tools.std)
stats.register("min", min)
stats.register("max", max)
#logger = tools.EvolutionLogger(stats.functions.keys())
# The CMA-ES algorithm converge with good probability with those settings
algorithms.eaGenerateUpdate(strategy, ngen=250, stats=stats)
# print "Best individual is %s, %s" % (hof[0], hof[0].fitness.values)
# return hof[0].fitness.values[0]
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment