DEAP / deap

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

Correction to code examples - Genetic Programming graph #541

Open aymericvie opened 3 years ago

aymericvie commented 3 years ago

Hi, I was playing with the genetic programming commands, notably gp.graph. It looks to me like the example commands provided in the documentation and notebook do not work.

One modified code that worked for me to plot the genetic programming trees works as follows: import matplotlib.pyplot as plt import networkx import pygraphviz as pgv nodes, edges, labels = gp.graph(expr) graph = networkx.Graph() graph.add_nodes_from(nodes) graph.add_edges_from(edges) pos = graphviz_layout(graph, prog='dot') plt.figure(figsize=(7,7)) networkx.draw_networkx_nodes(graph, pos, node_size=900, node_color="w") networkx.draw_networkx_edges(graph, pos) networkx.draw_networkx_labels(graph, pos, labels) plt.axis("off") plt.show()

It seems that installing both networkx and pygraphviz is necessary. Main changes in my workaround: 1) using gp.graph instead of graph, and 2) graphviz_layout instead of networkx.graphviz_layout.

Hope this can be of help. Best, Aymeric

aymericvie commented 1 year ago

Closing.