~ubuntu-branches/ubuntu/quantal/deap/quantal

« back to all changes in this revision

Viewing changes to examples/de_sphere.py

  • Committer: Package Import Robot
  • Author(s): Miriam Ruiz, Jakub Wilk, Miriam Ruiz
  • Date: 2011-11-17 11:53:15 UTC
  • mfrom: (1.1.1)
  • Revision ID: package-import@ubuntu.com-20111117115315-k9lkwpqcbsq8n0q7
Tags: 0.7.1-1
[ Jakub Wilk ]
* Add Vcs-* fields.

[ Miriam Ruiz ]
* New Upstream Release
* Upgraded Standards-Version from 3.9.1 to 3.9.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#    This file is part of EAP.
 
2
#
 
3
#    EAP 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
#    EAP 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 EAP. If not, see <http://www.gnu.org/licenses/>.
 
15
 
 
16
 
 
17
import random
 
18
import array
 
19
 
 
20
from deap import base
 
21
from deap import benchmarks
 
22
from deap import creator
 
23
from deap import tools
 
24
 
 
25
# Problem dimension
 
26
NDIM = 10
 
27
 
 
28
creator.create("FitnessMin", base.Fitness, weights=(-1.0,))
 
29
creator.create("Individual", array.array, typecode='d', fitness=creator.FitnessMin)
 
30
 
 
31
def mutDE(y, a, b, c, f):
 
32
    size = len(y)
 
33
    for i in range(len(y)):
 
34
        y[i] = a[i] + f*(b[i]-c[i])
 
35
    return y
 
36
 
 
37
def cxBinomial(x, y, cr):
 
38
    size = len(x)
 
39
    index = random.randrange(size)
 
40
    for i in range(size):
 
41
        if i == index or random.random() < cr:
 
42
            x[i] = y[i]
 
43
    return x
 
44
 
 
45
def cxExponential(x, y, cr):
 
46
    size = len(x)
 
47
    index = random.randrange(size)
 
48
    # Loop on the indices index -> end, then on 0 -> index
 
49
    for i in range(index, size) + range(0, index):
 
50
        x[i] = y[i]
 
51
        if random.random() < cr:
 
52
            break
 
53
    return x
 
54
 
 
55
toolbox = base.Toolbox()
 
56
toolbox.register("attr_float", random.uniform, -3, 3)
 
57
toolbox.register("individual", tools.initRepeat, creator.Individual, toolbox.attr_float, NDIM)
 
58
toolbox.register("population", tools.initRepeat, list, toolbox.individual)
 
59
toolbox.register("mutate", mutDE, f=0.8)
 
60
toolbox.register("mate", cxExponential, cr=0.8)
 
61
toolbox.register("select", tools.selRandom, k=3)
 
62
toolbox.register("evaluate", benchmarks.griewank)
 
63
 
 
64
def main():
 
65
    # Differential evolution parameters
 
66
    MU = NDIM * 10
 
67
    NGEN = 200    
 
68
    
 
69
    pop = toolbox.population(n=MU);
 
70
    hof = tools.HallOfFame(1)
 
71
    stats = tools.Statistics(lambda ind: ind.fitness.values)
 
72
    stats.register("Avg", tools.mean)
 
73
    stats.register("Std", tools.std)
 
74
    stats.register("Min", min)
 
75
    stats.register("Max", max)
 
76
    
 
77
    # Evaluate the individuals
 
78
    fitnesses = toolbox.map(toolbox.evaluate, pop)
 
79
    for ind, fit in zip(pop, fitnesses):
 
80
        ind.fitness.values = fit
 
81
    
 
82
    for g in range(NGEN):
 
83
        children = []
 
84
        for agent in pop:
 
85
            # We must clone everything to ensure independance
 
86
            a, b, c = [toolbox.clone(ind) for ind in toolbox.select(pop)]
 
87
            x = toolbox.clone(agent)
 
88
            y = toolbox.clone(agent)
 
89
            y = toolbox.mutate(y, a, b, c)
 
90
            z = toolbox.mate(x, y)
 
91
            del z.fitness.values
 
92
            children.append(z)
 
93
            
 
94
        fitnesses = toolbox.map(toolbox.evaluate, children)
 
95
        for ind, fit, i in zip(children, fitnesses, range(len(fitnesses))):
 
96
            ind.fitness.values = fit
 
97
            if ind.fitness > pop[i].fitness:
 
98
                pop[i] = ind
 
99
        
 
100
        hof.update(pop)
 
101
        stats.update(pop)
 
102
        
 
103
        print "-- Generation %i --" % g
 
104
        print stats
 
105
            
 
106
    print "Best individual is ", hof[0]
 
107
    print "with fitness", hof[0].fitness.values[0]
 
108
            
 
109
if __name__ == "__main__":
 
110
    main()