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