DEAP / deap

Distributed Evolutionary Algorithms in Python
http://deap.readthedocs.org/
GNU Lesser General Public License v3.0
5.88k stars 1.13k forks source link

History graph not working. #649

Closed TOP1RM closed 1 year ago

TOP1RM commented 2 years ago

Hello,

I am Rémi MOCHON, a DEAP user. Here is my issue:

I intend to use the class History to plot the genealogy of the evolution, along with recording every individual whose fitness has been evaluated.

When I follow the steps in DEAP's documentation (https://deap.readthedocs.io/en/master/api/tools.html#history), I end up with a disordered graph, without numbers on the nodes (see png file attached). I am studying the code so I begin to understand how History works, but I am not sure it is adapted for my script (see py file attached). I am using genetic programming (gp module) and the eaSimple evolution algorithm (the later uses VarAnd to vary the pool of selected individuals).

Could someone provide me some working example using History in Genetic Programming (thus using the gp module), so I can produce a nice graph like in the documentation of DEAP ? Graph

Looking forward for your answer.

Best regards, Rémi MOCHON

script_and_png.zip

Yasu31 commented 1 year ago

I know I'm answering a half-year-old issue, but since I had the same question I'll post my solution for the future; at the end of your code (or the example code from https://deap.readthedocs.io/en/master/overview.html ), just draw the graph using pygraphviz (you may have to do sudo apt-get install graphviz graphviz-dev and then pip install pygraphviz) with the folliowing code (the formatting is optional, of course)

import pygraphviz as pgv

graph = pgv.AGraph(history.genealogy_tree, directed=True)
graph = graph.reverse()     # Make the grah top-down
# set attribute for each element to make it look nice
# attributes: https://graphviz.org/doc/info/attrs.html
for n in graph.nodes():
    n.attr["shape"] = "hexagon"
    n.attr["color"] = "violet"
    n.attr["fillcolor"] = "violet"
    n.attr["style"] = "bold,filled"

graph.graph_attr["bgcolor"] = "black"
graph.graph_attr["splines"] = "curved"
graph.edge_attr["color"] = "green"
graph.edge_attr["arrowhead"] = "open"

graph.layout(prog="dot")    # layout with dot
graph.draw("family_tree.png") # draw png

which will create a nice looking graph, probably what the commenter was looking for.

family_tree