1
# This file is part of DEAP.
3
# DEAP is free software: you can redistribute it and/or modify
4
# it under the terms of the GNU Lesser General Public License as
5
# published by the Free Software Foundation, either version 3 of
6
# the License, or (at your option) any later version.
8
# DEAP is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
# GNU Lesser General Public License for more details.
13
# You should have received a copy of the GNU Lesser General Public
14
# License along with DEAP. If not, see <http://www.gnu.org/licenses/>.
18
from deap import algorithms
20
from deap import benchmarks
22
from deap import creator
23
from deap import tools
25
import matplotlib.pyplot as plt
31
creator.create("FitnessMin", base.Fitness, weights=(-1.0,))
32
creator.create("Individual", list, fitness=creator.FitnessMin)
34
toolbox = base.Toolbox()
35
toolbox.register("evaluate", benchmarks.rastrigin)
37
def main(verbose=True):
38
# The cma module uses the numpy random number generator
41
# The CMA-ES algorithm takes a population of one individual as argument
42
# The centroid is set to a vector of 5.0 see http://www.lri.fr/~hansen/cmaes_inmatlab.html
43
# for more details about the rastrigin and other tests for CMA-ES
44
strategy = cma.Strategy(centroid=[5.0]*N, sigma=5.0, lambda_=20*N)
45
toolbox.register("generate", strategy.generate, creator.Individual)
46
toolbox.register("update", strategy.update)
48
halloffame = tools.HallOfFame(1)
49
stats = tools.Statistics(lambda ind: ind.fitness.values)
50
stats.register("avg", numpy.mean)
51
stats.register("std", numpy.std)
52
stats.register("min", numpy.min)
53
stats.register("max", numpy.max)
55
logbook = tools.Logbook()
56
logbook.header = "gen", "evals", "std", "min", "avg", "max"
58
# Objects that will compile the data
59
sigma = numpy.ndarray((NGEN,1))
60
axis_ratio = numpy.ndarray((NGEN,1))
61
diagD = numpy.ndarray((NGEN,N))
62
fbest = numpy.ndarray((NGEN,1))
63
best = numpy.ndarray((NGEN,N))
64
std = numpy.ndarray((NGEN,N))
66
for gen in range(NGEN):
67
# Generate a new population
68
population = toolbox.generate()
69
# Evaluate the individuals
70
fitnesses = toolbox.map(toolbox.evaluate, population)
71
for ind, fit in zip(population, fitnesses):
72
ind.fitness.values = fit
74
# Update the strategy with the evaluated individuals
75
toolbox.update(population)
77
# Update the hall of fame and the statistics with the
78
# currently evaluated population
79
halloffame.update(population)
80
record = stats.compile(population)
81
logbook.record(evals=len(population), gen=gen, **record)
86
# Save more data along the evolution for latter plotting
87
# diagD is sorted and sqrooted in the update method
88
sigma[gen] = strategy.sigma
89
axis_ratio[gen] = max(strategy.diagD)**2/min(strategy.diagD)**2
90
diagD[gen, :N] = strategy.diagD**2
91
fbest[gen] = halloffame[0].fitness.values
92
best[gen, :N] = halloffame[0]
93
std[gen, :N] = numpy.std(population, axis=0)
95
# The x-axis will be the number of evaluations
96
x = list(range(0, strategy.lambda_ * NGEN, strategy.lambda_))
97
avg, max_, min_ = logbook.select("avg", "max", "min")
100
plt.semilogy(x, avg, "--b")
101
plt.semilogy(x, max_, "--b")
102
plt.semilogy(x, min_, "-b")
103
plt.semilogy(x, fbest, "-c")
104
plt.semilogy(x, sigma, "-g")
105
plt.semilogy(x, axis_ratio, "-r")
107
plt.title("blue: f-values, green: sigma, red: axis ratio")
112
plt.title("Object Variables")
115
plt.semilogy(x, diagD)
117
plt.title("Scaling (All Main Axes)")
122
plt.title("Standard Deviations in All Coordinates")
126
if __name__ == "__main__":