Skip to content

Instantly share code, notes, and snippets.

@cmd-ntrf
Created December 15, 2012 22:05
Show Gist options
  • Save cmd-ntrf/4299724 to your computer and use it in GitHub Desktop.
Save cmd-ntrf/4299724 to your computer and use it in GitHub Desktop.
Mutation rate adaptation and loggin of hall-of-fame best with onemax problem.
# 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 <http://www.gnu.org/licenses/>.
import array
import random
from deap import algorithms
from deap import base
from deap import creator
from deap import tools
creator.create("FitnessMax", base.Fitness, weights=(1.0,))
creator.create("Individual", array.array, typecode='b', fitness=creator.FitnessMax)
toolbox = base.Toolbox()
# Attribute generator
toolbox.register("attr_bool", random.randint, 0, 1)
# Structure initializers
toolbox.register("individual", tools.initRepeat, creator.Individual,
toolbox.attr_bool, 100)
toolbox.register("population", tools.initRepeat, list, toolbox.individual)
def evalOneMax(individual):
return sum(individual),
toolbox.register("evaluate", evalOneMax)
toolbox.register("mate", tools.cxTwoPoints)
toolbox.register("mutate", tools.mutFlipBit, indpb=0.05)
toolbox.register("select", tools.selTournament, tournsize=3)
def main(seed=None):
random.seed(seed)
MU = 300
NGEN = 40
cxpb = 0.5
mutpb = 0.2
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", "best", "mutpb"] + stats.functions.keys())
for ind in pop:
ind.fitness.values = toolbox.evaluate(ind)
stats.update(pop)
hof.update(pop)
logger.logHeader()
logger.logGeneration(gen=0, evals=len(pop), stats=stats,
best=hof[0].fitness.values, mutpb=mutpb)
for gen in range(1, NGEN):
pop = toolbox.select(pop, len(pop))
pop = algorithms.varAnd(pop, toolbox, cxpb=cxpb, mutpb=mutpb)
invalid_ind = [ind for ind in pop if not ind.fitness.valid]
for ind in invalid_ind:
ind.fitness.values = toolbox.evaluate(ind)
stats.update(pop)
hof.update(pop)
logger.logGeneration(gen=gen, evals=len(invalid_ind),
stats=stats, best=hof[0].fitness.values,
mutpb=mutpb)
# Adjust mutation rate (ad-hoc formula)
if stats.data["std"][0][-1][0] < 3.0:
mutpb *= 1.2
elif stats.data["std"][0][-1][0]> 5.0:
mutpb /= 1.2
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment