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
logging.basicConfig(level=logging.DEBUG, stream=sys.stdout)
23
from deap import algorithms
25
from deap import creator
26
from deap import tools
28
creator.create("FitnessMax", base.Fitness, weights=(1.0,))
29
creator.create("Individual", numpy.ndarray, fitness=creator.FitnessMax)
31
toolbox = base.Toolbox()
33
toolbox.register("attr_bool", random.randint, 0, 1)
34
toolbox.register("individual", tools.initRepeat, creator.Individual, toolbox.attr_bool, n=100)
35
toolbox.register("population", tools.initRepeat, list, toolbox.individual)
37
def evalOneMax(individual):
38
return numpy.sum(individual),
40
toolbox.register("evaluate", evalOneMax)
41
toolbox.register("mate", tools.cxTwoPoints)
42
toolbox.register("mutate", tools.mutFlipBit, indpb=0.05)
43
toolbox.register("select", tools.selTournament, tournsize=3)
48
pop = toolbox.population(n=300)
49
hof = tools.HallOfFame(1)
50
stats = tools.Statistics(lambda ind: ind.fitness.values)
51
stats.register("Avg", tools.mean)
52
stats.register("Std", tools.std)
53
stats.register("Min", min)
54
stats.register("Max", max)
56
algorithms.eaSimple(toolbox, pop, cxpb=0.5, mutpb=0.2, ngen=40, stats=stats, halloffame=hof)
58
return pop, stats, hof
60
if __name__ == "__main__":