1
# This file is part of DEAP.
3
# DEAP 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.
8
# DEAP 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.
13
# You should have received a copy of the GNU Lesser General Public
14
# License along with DEAP. If not, see <http://www.gnu.org/licenses/>.
21
from deap import algorithms
23
from deap import creator
24
from deap import tools
26
creator.create("FitnessMax", base.Fitness, weights=(1.0,))
27
creator.create("Individual", array.array, typecode='b', fitness=creator.FitnessMax)
29
toolbox = base.Toolbox()
32
toolbox.register("attr_bool", random.randint, 0, 1)
34
# Structure initializers
35
toolbox.register("individual", tools.initRepeat, creator.Individual, toolbox.attr_bool, 100)
36
toolbox.register("population", tools.initRepeat, list, toolbox.individual)
38
def evalOneMax(individual):
39
return sum(individual),
41
toolbox.register("evaluate", evalOneMax)
42
toolbox.register("mate", tools.cxTwoPoint)
43
toolbox.register("mutate", tools.mutFlipBit, indpb=0.05)
44
toolbox.register("select", tools.selTournament, tournsize=3)
49
pop = toolbox.population(n=300)
50
hof = tools.HallOfFame(1)
51
stats = tools.Statistics(lambda ind: ind.fitness.values)
52
stats.register("avg", numpy.mean)
53
stats.register("std", numpy.std)
54
stats.register("min", numpy.min)
55
stats.register("max", numpy.max)
57
pop, log = algorithms.eaSimple(pop, toolbox, cxpb=0.5, mutpb=0.2, ngen=40,
58
stats=stats, halloffame=hof, verbose=True)
62
if __name__ == "__main__":