Last active
July 24, 2020 09:33
-
-
Save DMTSource/7babce7f0269390d9d827443ce2231f5 to your computer and use it in GitHub Desktop.
Examples of Deap History applied to onemax and Symbreg examples. See: https://deap.readthedocs.io/en/master/api/tools.html#deap.tools.History
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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/>. | |
# From https://github.com/DEAP/deap/blob/master/examples/ga/onemax_short.py | |
import array | |
import random | |
import numpy | |
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.cxTwoPoint) | |
toolbox.register("mutate", tools.mutFlipBit, indpb=0.05) | |
toolbox.register("select", tools.selTournament, tournsize=3) | |
# Based on https://deap.readthedocs.io/en/master/api/tools.html#deap.tools.History | |
################################################## | |
history = tools.History() | |
# Decorate the variation operators | |
toolbox.decorate("mate", history.decorator) | |
toolbox.decorate("mutate", history.decorator) | |
################################################## | |
def main(): | |
random.seed(64) | |
pop = toolbox.population(n=20) | |
hof = tools.HallOfFame(1) | |
################################################## | |
history.update(pop) | |
################################################## | |
stats = tools.Statistics(lambda ind: ind.fitness.values) | |
stats.register("avg", numpy.mean) | |
stats.register("std", numpy.std) | |
stats.register("min", numpy.min) | |
stats.register("max", numpy.max) | |
pop, log = algorithms.eaSimple(pop, toolbox, cxpb=0.5, mutpb=0.2, ngen=6, | |
stats=stats, halloffame=hof, verbose=True) | |
################################################## | |
#Useful, source for history so you can manipulate it | |
# https://github.com/DEAP/deap/blob/master/deap/tools/support.py#L21 | |
#print (history.genealogy_history) | |
################################################## | |
# Producing a weird chart...very odd...maybe my networkx is messed up? | |
################################################## | |
import matplotlib.pyplot as plt | |
import networkx | |
plt.figure(figsize=(10,10)) | |
graph = networkx.DiGraph(history.genealogy_tree) | |
graph = graph.reverse() # Make the graph top-down | |
colors = [toolbox.evaluate(history.genealogy_history[i])[0] for i in graph] | |
networkx.draw(graph, node_color=colors) | |
plt.show() | |
################################################## | |
return pop, log, hof | |
if __name__ == "__main__": | |
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This file is part of EAP. | |
# | |
# EAP 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. | |
# | |
# EAP 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 EAP. If not, see <http://www.gnu.org/licenses/>. | |
# from https://github.com/DEAP/deap/blob/master/examples/gp/symbreg.py | |
import operator | |
import math | |
import random | |
import numpy | |
from deap import algorithms | |
from deap import base | |
from deap import creator | |
from deap import tools | |
from deap import gp | |
# Define new functions | |
def protectedDiv(left, right): | |
try: | |
return left / right | |
except ZeroDivisionError: | |
return 1 | |
pset = gp.PrimitiveSet("MAIN", 1) | |
pset.addPrimitive(operator.add, 2) | |
pset.addPrimitive(operator.sub, 2) | |
pset.addPrimitive(operator.mul, 2) | |
pset.addPrimitive(protectedDiv, 2) | |
pset.addPrimitive(operator.neg, 1) | |
pset.addPrimitive(math.cos, 1) | |
pset.addPrimitive(math.sin, 1) | |
pset.addEphemeralConstant("rand101", lambda: random.randint(-1,1)) | |
pset.renameArguments(ARG0='x') | |
creator.create("FitnessMin", base.Fitness, weights=(-1.0,)) | |
creator.create("Individual", gp.PrimitiveTree, fitness=creator.FitnessMin) | |
toolbox = base.Toolbox() | |
toolbox.register("expr", gp.genHalfAndHalf, pset=pset, min_=1, max_=2) | |
toolbox.register("individual", tools.initIterate, creator.Individual, toolbox.expr) | |
toolbox.register("population", tools.initRepeat, list, toolbox.individual) | |
toolbox.register("compile", gp.compile, pset=pset) | |
def evalSymbReg(individual, points): | |
# Transform the tree expression in a callable function | |
func = toolbox.compile(expr=individual) | |
# Evaluate the mean squared error between the expression | |
# and the real function : x**4 + x**3 + x**2 + x | |
sqerrors = ((func(x) - x**4 - x**3 - x**2 - x)**2 for x in points) | |
return math.fsum(sqerrors) / len(points), | |
toolbox.register("evaluate", evalSymbReg, points=[x/10. for x in range(-10,10)]) | |
toolbox.register("select", tools.selTournament, tournsize=3) | |
toolbox.register("mate", gp.cxOnePoint) | |
toolbox.register("expr_mut", gp.genFull, min_=0, max_=2) | |
toolbox.register("mutate", gp.mutUniform, expr=toolbox.expr_mut, pset=pset) | |
toolbox.decorate("mate", gp.staticLimit(key=operator.attrgetter("height"), max_value=17)) | |
toolbox.decorate("mutate", gp.staticLimit(key=operator.attrgetter("height"), max_value=17)) | |
# Based on https://deap.readthedocs.io/en/master/api/tools.html#deap.tools.History | |
################################################## | |
history = tools.History() | |
# Decorate the variation operators | |
toolbox.decorate("mate", history.decorator) | |
toolbox.decorate("mutate", history.decorator) | |
################################################## | |
def main(): | |
random.seed(318) | |
pop = toolbox.population(n=20) | |
hof = tools.HallOfFame(1) | |
################################################## | |
history.update(pop) | |
################################################## | |
stats_fit = tools.Statistics(lambda ind: ind.fitness.values) | |
stats_size = tools.Statistics(len) | |
mstats = tools.MultiStatistics(fitness=stats_fit, size=stats_size) | |
mstats.register("avg", numpy.mean) | |
mstats.register("std", numpy.std) | |
mstats.register("min", numpy.min) | |
mstats.register("max", numpy.max) | |
pop, log = algorithms.eaSimple(pop, toolbox, 0.5, 0.1, 6, stats=mstats, | |
halloffame=hof, verbose=True) | |
################################################## | |
#Useful, source for history so you can manipulate it | |
# https://github.com/DEAP/deap/blob/master/deap/tools/support.py#L21 | |
#print (history.genealogy_history) | |
################################################## | |
################################################## | |
import matplotlib.pyplot as plt | |
import networkx | |
plt.figure(figsize=(10,10)) | |
graph = networkx.DiGraph(history.genealogy_tree) | |
graph = graph.reverse() # Make the graph top-down | |
colors = [toolbox.evaluate(history.genealogy_history[i])[0] for i in graph] | |
networkx.draw(graph, node_color=colors) | |
plt.show() | |
################################################## | |
# print log | |
return pop, log, hof | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment