Last active
May 2, 2019 13:35
-
-
Save fmder/5505431 to your computer and use it in GitHub Desktop.
History example with dot layout
This file contains hidden or 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/>. | |
import random | |
from deap import algorithms | |
from deap import base | |
from deap import creator | |
from deap import tools | |
import numpy | |
creator.create("FitnessMax", base.Fitness, weights=(1.0,)) | |
creator.create("Individual", list, 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) | |
history = tools.History() | |
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) | |
algorithms.eaSimple(pop, toolbox, cxpb=0.5, mutpb=0.2, ngen=5, stats=stats, | |
halloffame=hof, verbose=True) | |
import matplotlib.pyplot as plt | |
import networkx | |
graph = networkx.DiGraph(history.genealogy_tree) | |
graph = graph.reverse() # Make the grah top-down | |
colors = [toolbox.evaluate(history.genealogy_history[i])[0] for i in graph] | |
positions = networkx.graphviz_layout(graph, prog="dot") | |
networkx.draw(graph, positions, node_color=colors) | |
plt.show() | |
return pop, stats, hof | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Is there any reason why the graph initialised using:
graph = networkx.DiGraph(history.genealogy_tree)
could be of different length than the genealogy_tree?
len(history.genealogy_tree) != len(graph)