I need to plot the graph for the following solution of the ACO TSP. The graph that I need to plot would be something close to this:
Any code snippets on which I could work on?
From the documentation of tsplib95:
Converting problems get_graph() creates a networkx.Graph instance from the problem data: >>> G = problem.get_graph() >>> G.nodes NodeView((1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16))
So, G is a networkx
graph, which means you can use networkx
to draw it:
import matplotlib.pyplot as plt
import networkx as nx
nx.draw_networkx(G)
plt.show()
edit: drawing the path:
pos = nx.spring_layout(G)
H = nx.DiGraph(G) # convert to directed graph s.t. the edges have arrows.
nx.draw_networkx_nodes(H, pos=pos) # draw nodes
nx.draw_networkx_edges(H, pos=pos, alpha=0.1) # draw all edges with transparency
nx.draw_networkx_edges(H, pos=pos, edgelist=tour.path, edge_color='red') # highlight the edges in the path
plt.show()
Is it possible to then draw the tour, which is the finished version of G (having the algorithm applied to G to get the shortest distance)?
probably. can you try type(tour) to see what datatype the tour has?
The tour is of class 'acopy.solvers.Solution' . I can also get the tour nodes and path using tour.nodes and tour.path.
@pepegaClap see my edit
The code seems to work, but no graph is being outputted