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

« back to all changes in this revision

Viewing changes to examples/es_onefifth.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
 
 
2
#    This file is part of DEAP.
 
3
#
 
4
#    DEAP is free software: you can redistribute it and/or modify
 
5
#    it under the terms of the GNU Lesser General Public License as
 
6
#    published by the Free Software Foundation, either version 3 of
 
7
#    the License, or (at your option) any later version.
 
8
#
 
9
#    DEAP is distributed in the hope that it will be useful,
 
10
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 
12
#    GNU Lesser General Public License for more details.
 
13
#
 
14
#    You should have received a copy of the GNU Lesser General Public
 
15
#    License along with DEAP. If not, see <http://www.gnu.org/licenses/>.
 
16
 
 
17
import array
 
18
import random
 
19
 
 
20
from deap import base
 
21
from deap import creator
 
22
 
 
23
from deap import benchmarks
 
24
 
 
25
IND_SIZE = 10
 
26
 
 
27
tools = base.Toolbox()
 
28
 
 
29
creator.create("FitnessMin", base.Fitness, weights=(-1.0,))
 
30
creator.create("Individual", array.array, typecode='d', fitness=creator.FitnessMin)
 
31
 
 
32
def update(ind, mu, std):
 
33
    for i, mu_i in enumerate(mu):
 
34
        ind[i] = random.gauss(mu_i,std)
 
35
                   
 
36
tools.register("update", update)
 
37
tools.register("evaluate", benchmarks.sphere)
 
38
 
 
39
def main():
 
40
    """Implements the One-Fifth rule algorithm as expressed in :
 
41
    Kern, S., S.D. Muller, N. Hansen, D. Buche, J. Ocenasek and P. Koumoutsakos (2004). 
 
42
    Learning Probability Distributions in Continuous Evolutionary Algorithms - 
 
43
    A Comparative Review. Natural Computing, 3(1), pp. 77-112.
 
44
 
 
45
    However instead of parent and offspring the algorithm is expressed in terms of
 
46
    best and worst. Best is equivalent to the parent, and worst to the offspring.
 
47
    Instead of producing a new individual each time, we have defined a function which
 
48
    updates the worst individual using the best one as the mean of the gaussian and 
 
49
    the sigma computed as the standard deviation.
 
50
    """
 
51
    random.seed(64)
 
52
 
 
53
    interval = (-3,7)
 
54
    mu = (random.uniform(interval[0], interval[1]) for _ in xrange(IND_SIZE))
 
55
    sigma = (interval[1] - interval[0])/2.0
 
56
    alpha = 2.0**(1.0/IND_SIZE)
 
57
 
 
58
    best = creator.Individual(mu)
 
59
    best.fitness.values = tools.evaluate(best)
 
60
    worst = creator.Individual((0.0,)*IND_SIZE)
 
61
 
 
62
    NGEN = 1500
 
63
    for g in xrange(NGEN):
 
64
        tools.update(worst, best, sigma) 
 
65
        worst.fitness.values = tools.evaluate(worst)
 
66
        if best.fitness <= worst.fitness:
 
67
            sigma = sigma * alpha
 
68
            best, worst = worst, best
 
69
        else:
 
70
            sigma = sigma * alpha**(-0.25)
 
71
 
 
72
        print "Generation", g, "- Fitness:", best.fitness.values[0]
 
73
 
 
74
    print "Best individual is ", best, best.fitness.values[0]
 
75
    
 
76
    return best
 
77
    
 
78
if __name__ == "__main__":
 
79
    main()