~ubuntu-branches/ubuntu/utopic/deap/utopic-proposed

« back to all changes in this revision

Viewing changes to examples/es/cma_plotting.py

  • Committer: Package Import Robot
  • Author(s): Daniel Stender, Miriam Ruiz, Daniel Stender, Jakub Wilk
  • Date: 2014-07-06 00:03:41 UTC
  • mfrom: (1.1.2)
  • Revision ID: package-import@ubuntu.com-20140706000341-s7gij1ki3d8xz6t9
Tags: 1.0.1-1
[ Miriam Ruiz ]
* New Upstream Release. Closes: #675200
* Upgraded Standards-Version from 3.9.2 to 3.9.5
* Switched to dh_python2
* Upgraded debian/compat to 9
* Added build-arch and build-indep targets to debian/rules
* Using awk to remove connection from the documentation to google analytics
* Using jquery.js and underscore.js from libjs-jquery and libjs-underscore
* Added patches/doc.patch

[ Daniel Stender ]
* deb/control:
  + Added myself to Uploaders.
  + Added version to b-p on python-all.
  + Updated Homepage.
  + Wrapped and sorted.
* deb/copyright:
  + Changed copyright file towards DEP-5.
  + Updated Source URI (project source moved to Github).
  + Added email addresses for copyright holders.
  + Dropped license for eap/toolbox.py (not included anymore).
  + Extended copyrights to 2014.
  + Added myself to copyrights for deb/*.
  + Specified license location.
* Added watch file [initial version by Jackson Doak].

[ Jakub Wilk ]
* Use canonical URIs for Vcs-* fields.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#    This file is part of DEAP.
 
2
#
 
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.
 
7
#
 
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.
 
12
#
 
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/>.
 
15
 
 
16
import numpy
 
17
 
 
18
from deap import algorithms
 
19
from deap import base
 
20
from deap import benchmarks
 
21
from deap import cma
 
22
from deap import creator
 
23
from deap import tools
 
24
 
 
25
import matplotlib.pyplot as plt
 
26
 
 
27
# Problem size
 
28
N = 10
 
29
NGEN = 125
 
30
 
 
31
creator.create("FitnessMin", base.Fitness, weights=(-1.0,))
 
32
creator.create("Individual", list, fitness=creator.FitnessMin)
 
33
 
 
34
toolbox = base.Toolbox()
 
35
toolbox.register("evaluate", benchmarks.rastrigin)
 
36
 
 
37
def main(verbose=True):
 
38
    # The cma module uses the numpy random number generator
 
39
    numpy.random.seed(64)
 
40
 
 
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)
 
47
 
 
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)
 
54
 
 
55
    logbook = tools.Logbook()
 
56
    logbook.header = "gen", "evals", "std", "min", "avg", "max"
 
57
    
 
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))
 
65
 
 
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
 
73
        
 
74
        # Update the strategy with the evaluated individuals
 
75
        toolbox.update(population)
 
76
        
 
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)
 
82
        
 
83
        if verbose:
 
84
            print(logbook.stream)
 
85
        
 
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)
 
94
 
 
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")
 
98
    plt.figure()
 
99
    plt.subplot(2, 2, 1)
 
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")
 
106
    plt.grid(True)
 
107
    plt.title("blue: f-values, green: sigma, red: axis ratio")
 
108
 
 
109
    plt.subplot(2, 2, 2)
 
110
    plt.plot(x, best)
 
111
    plt.grid(True)
 
112
    plt.title("Object Variables")
 
113
 
 
114
    plt.subplot(2, 2, 3)
 
115
    plt.semilogy(x, diagD)
 
116
    plt.grid(True)
 
117
    plt.title("Scaling (All Main Axes)")
 
118
 
 
119
    plt.subplot(2, 2, 4)
 
120
    plt.semilogy(x, std)
 
121
    plt.grid(True)
 
122
    plt.title("Standard Deviations in All Coordinates")
 
123
    
 
124
    plt.show()
 
125
 
 
126
if __name__ == "__main__":
 
127
    main(False)