# This file is part of DEAP. # # DEAP 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. # # DEAP 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 DEAP. If not, see . import array import random from deap import algorithms from deap import base from deap import benchmarks from deap import creator from deap import tools IND_SIZE = 30 MIN_VALUE = 4 MAX_VALUE = 5 MIN_STRATEGY = 0.5 MAX_STRATEGY = 3 creator.create("FitnessMin", base.Fitness, weights=(-1.0,)) creator.create("Individual", array.array, typecode="d", fitness=creator.FitnessMin, strategy=None) creator.create("Strategy", array.array, typecode="d") # Individual generator def generateES(icls, scls, size, imin, imax, smin, smax): ind = icls(random.uniform(imin, imax) for _ in range(size)) ind.strategy = scls(random.uniform(smin, smax) for _ in range(size)) return ind def checkStrategy(minstrategy): def decorator(func): def wrappper(*args, **kargs): children = func(*args, **kargs) for child in children: for i, s in enumerate(child.strategy): if s < minstrategy: child.strategy[i] = minstrategy return children return wrappper return decorator toolbox = base.Toolbox() toolbox.register("individual", generateES, creator.Individual, creator.Strategy, IND_SIZE, MIN_VALUE, MAX_VALUE, MIN_STRATEGY, MAX_STRATEGY) toolbox.register("population", tools.initRepeat, list, toolbox.individual) toolbox.register("mate", tools.cxESBlend, alpha=0.1) toolbox.register("mutate", tools.mutESLogNormal, c=1.0, indpb=0.03) toolbox.register("select", tools.selTournament, tournsize=3) toolbox.register("evaluate", benchmarks.sphere) toolbox.decorate("mate", checkStrategy(MIN_STRATEGY)) toolbox.decorate("mutate", checkStrategy(MIN_STRATEGY)) def main(): random.seed() MU, LAMBDA = 10, 100 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) algorithms.eaMuCommaLambda(pop, toolbox, mu=MU, lambda_=LAMBDA, cxpb=0.6, mutpb=0.3, ngen=500, stats=stats, halloffame=hof) return pop, stats, hof if __name__ == "__main__": main()