0

I am running a vehicle routing optimization model. below is the list of location coordinates (nodes), and the optimization output showing the sequence of locations to visit. I am wondering how to create a graph like below to visualize my output.

locations = \
        [(4, 4), # depot
         (2, 0), (8, 0), # row 0
         (0, 1), (1, 1),
         (5, 2), (7, 2),
         (3, 3), (6, 3),
         (5, 5), (8, 5),
         (1, 6), (2, 6),
         (3, 7), (6, 7),
         (0, 8), (7, 8)]

[Route for vehicle 0:
 0 ->  8 ->  6 ->  2 ->  5 ->  0
Distance of the route 0: 1552.0

Route for vehicle 1:
 0 ->  7 ->  1 ->  4 ->  3 ->  0
Distance of the route 1: 1552.0

Route for vehicle 2:
 0 ->  9 ->  10 ->  16 ->  14 ->  0
Distance of the route 2: 1552.0

Route for vehicle 3:
 0 ->  12 ->  11 ->  15 ->  13 ->  0
Distance of the route 3: 1552.0][1]

enter image description here

Jack
  • 1,339
  • 1
  • 12
  • 31

1 Answers1

1

I think the best tool to draw a network is the library networkx. In the code below I reproduce 1/2 of your example with just a couple of triks in order to select the color nodes properly.

import networkx as nx

locations = {
    0:(4,4),
    1:(2,8),
    2:(8,8),
    3:(0,7),
    4:(1,7),
    5:(5,6),
    6:(7,6),
    7:(3,6),
    8:(6,5),
}

edges = [
    (0, 8, {'vehicle': '0'}),
    (8, 6, {'vehicle': '0'}),
    (6, 2, {'vehicle': '0'}),
    (2, 5, {'vehicle': '0'}),
    (5, 0, {'vehicle': '0'}),
    (0, 7, {'vehicle': '1'}),
    (7, 1, {'vehicle': '1'}),
    (1, 4, {'vehicle': '1'}),
    (4, 3, {'vehicle': '1'}),
    (3, 0, {'vehicle': '1'}),
]

G=nx.DiGraph()

G.add_edges_from(edges)

plt.figure(figsize=(15,10))

#vehicle 0
temp = [e for e in edges if e[2]['vehicle'] == '0'] #temporary list that filters the path of vehicle 0
nx.draw_networkx_nodes(G, locations, nodelist=[x[0] for x in temp], node_color='b')
nx.draw_networkx_edges(G, locations, edgelist=temp,
                       width=2, edge_color='b', style='dashed')

#vehicle 1
temp = [e for e in edges if e[2]['vehicle'] == '1']
nx.draw_networkx_nodes(G, locations, nodelist=[x[0] for x in temp], node_color='r')
nx.draw_networkx_edges(G, locations, edgelist=temp,
                       width=2, edge_color='r', style='dashed')

#let's color the node 0 in black
nx.draw_networkx_nodes(G, locations, nodelist=[0], node_color='k')

# labels
nx.draw_networkx_labels(G, locations, font_color='w', font_size=12, font_family='sans-serif')

#print out the graph
plt.axis('off')
plt.show()

This is the output:

enter image description here

el_Rinaldo
  • 970
  • 9
  • 26
  • Many thanks. This is exactly what I want but I have a question. how did you have the coordinates of each position different from the original "locations" list. e.g. location [2] is [8,0] but in the new locations indexed is [8,8] – Jack Jun 06 '18 at 08:52
  • Glad it helps. The coordinates in my reply are just an example that want to replicate the appearance of your graph (i.e. technically, for positive coordinates the origin is on the bottom left of the graph, as in matplotlib. On the contrary, in your graph the origin is on the top left) – el_Rinaldo Jun 06 '18 at 09:57
  • Many thanks. Is there a systematic way to plot the graph since I have many vehicles. my final output for each vehicle is a list of nodes for visiting in order, e.g. self.my_routes for vehicle_0 is [0, 1, 4, 3, 15], self.my_routes for vehicle_1 is [0, 14, 16, 10, 2]. these are the indices .would be helpful if you update a more systematic solution – Jack Jun 06 '18 at 10:35
  • I'd suggest to make a simple for loop in order to create the dictionary of the locations. `d = dict(zip(range(len(locations)), locations))`. I'd do something similar for the edges too. Anyway, I suggest you to update your question making an extended example of the self.my_routes output in order to make it easier to code the response – el_Rinaldo Jun 07 '18 at 08:18