2
# This file is part of DEAP.
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.
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.
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/>.
21
from deap import creator
23
from deap import benchmarks
27
tools = base.Toolbox()
29
creator.create("FitnessMin", base.Fitness, weights=(-1.0,))
30
creator.create("Individual", array.array, typecode='d', fitness=creator.FitnessMin)
32
def update(ind, mu, std):
33
for i, mu_i in enumerate(mu):
34
ind[i] = random.gauss(mu_i,std)
36
tools.register("update", update)
37
tools.register("evaluate", benchmarks.sphere)
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.
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.
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)
58
best = creator.Individual(mu)
59
best.fitness.values = tools.evaluate(best)
60
worst = creator.Individual((0.0,)*IND_SIZE)
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:
68
best, worst = worst, best
70
sigma = sigma * alpha**(-0.25)
72
print "Generation", g, "- Fitness:", best.fitness.values[0]
74
print "Best individual is ", best, best.fitness.values[0]
78
if __name__ == "__main__":