-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathde_sphere.py
116 lines (95 loc) · 3.63 KB
/
de_sphere.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# This file is part of EAP.
#
# EAP is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as
# published by the Free Software Foundation, either version 3 of
# the License, or (at your option) any later version.
#
# EAP is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with EAP. If not, see <http://www.gnu.org/licenses/>.
import random
import array
from deap import base
from deap import benchmarks
from deap import creator
from deap import tools
# Problem dimension
NDIM = 10
creator.create("FitnessMin", base.Fitness, weights=(-1.0,))
creator.create("Individual", array.array, typecode='d', fitness=creator.FitnessMin)
def mutDE(y, a, b, c, f):
size = len(y)
for i in range(len(y)):
y[i] = a[i] + f*(b[i]-c[i])
return y
def cxBinomial(x, y, cr):
size = len(x)
index = random.randrange(size)
for i in range(size):
if i == index or random.random() < cr:
x[i] = y[i]
return x
def cxExponential(x, y, cr):
size = len(x)
index = random.randrange(size)
# Loop on the indices index -> end, then on 0 -> index
for i in range(index, size) + range(0, index):
x[i] = y[i]
if random.random() < cr:
break
return x
toolbox = base.Toolbox()
toolbox.register("attr_float", random.uniform, -3, 3)
toolbox.register("individual", tools.initRepeat, creator.Individual, toolbox.attr_float, NDIM)
toolbox.register("population", tools.initRepeat, list, toolbox.individual)
toolbox.register("mutate", mutDE, f=0.8)
toolbox.register("mate", cxExponential, cr=0.8)
toolbox.register("select", tools.selRandom, k=3)
toolbox.register("evaluate", benchmarks.griewank)
def main():
# Differential evolution parameters
MU = NDIM * 10
NGEN = 200
pop = toolbox.population(n=MU);
hof = tools.HallOfFame(1)
stats = tools.Statistics(lambda ind: ind.fitness.values)
stats.register("avg", tools.mean)
stats.register("std", tools.std)
stats.register("min", min)
stats.register("max", max)
logger = tools.EvolutionLogger(["gen", "evals"] + stats.functions.keys())
logger.logHeader()
# Evaluate the individuals
fitnesses = toolbox.map(toolbox.evaluate, pop)
for ind, fit in zip(pop, fitnesses):
ind.fitness.values = fit
stats.update(pop)
logger.logGeneration(gen=0, evals=len(pop), stats=stats)
for g in range(1, NGEN):
children = []
for agent in pop:
# We must clone everything to ensure independance
a, b, c = [toolbox.clone(ind) for ind in toolbox.select(pop)]
x = toolbox.clone(agent)
y = toolbox.clone(agent)
y = toolbox.mutate(y, a, b, c)
z = toolbox.mate(x, y)
del z.fitness.values
children.append(z)
fitnesses = toolbox.map(toolbox.evaluate, children)
for (i, ind), fit in zip(enumerate(children), fitnesses):
ind.fitness.values = fit
if ind.fitness > pop[i].fitness:
pop[i] = ind
hof.update(pop)
stats.update(pop)
logger.logGeneration(gen=g, evals=len(pop), stats=stats)
print "Best individual is ", hof[0]
print "with fitness", hof[0].fitness.values[0]
if __name__ == "__main__":
main()