-
Notifications
You must be signed in to change notification settings - Fork 1
/
vrp.py
233 lines (185 loc) · 6.54 KB
/
vrp.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
#!/usr/bin/env python3
"""
=============================
VRP - Vehicle routing problem
=============================
https://en.wikipedia.org/wiki/Vehicle_routing_problem
o
o / `
| ` / `
| o - - - - o
| / |`
o | `
| `
o - - - o
Suppose we have 5 cities (C1, C2, C3, C4, C5), and 2 vehicles (A, B)
C1 C2 C3 C4 C5
+-----------------------------+
| | | | | | | | | | |
+-----------------------------+
|------ A -----|------ B -----|
Possible path 1:
+-----------------------------+
|C2|C1| |C4| | |C5| | |C3| => A: C0 -> C2 -> C1 -> C4 -> C0
+-----------------------------+ B: C0 -> C5 -> C3 -> C0
|------ A -----|------ B -----|
Usage
-----
# Run with random generated coordinates (7 cities, 2 vehicles)
python vrp.py --vehicles 2 --coors 7
# Run with `cities.txt` dataset and 2 vehicles
python vrp.py --path ../data/vrp/cities.txt --vehicles 2
# Run with `cities.txt` dataset and 3 vehicles
python vrp.py --path ../data/vrp/cities.txt --vehicles 3
Todo
-----
The search space is huge, need to prune to reduce search space
"""
from collections import defaultdict
import matplotlib.pyplot as plt
import networkx as nx
import numpy as np
import itertools
import argparse
import logging
import sys
FORMAT = '[%(levelname)s] %(asctime)s: %(message)s'
logging.basicConfig(format=FORMAT)
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
def distance(lft_coor, rgh_coor):
"""
Calculate the euclidean distance for given two coordinates
"""
return np.linalg.norm(lft_coor - rgh_coor)
def generate_coordinates(size):
"""
Generate random coordinate points
:param int size: How many points to create
:returns: An size * 2 dim np.array
"""
lat = np.random.uniform(22.0, 39.7, (size, 1))
lng = np.random.uniform(100, 119, (size, 1))
coors = np.hstack((lng, lat))
return coors
def get_node_distances(coordinates):
"""
Get distance mapping for each point in coordinates
"""
distances = defaultdict(lambda: 0)
distances.update({
(i, j): distance(coordinates[i], coordinates[j])
for i in range(coordinates.shape[0]) for j in range(coordinates.shape[0]) if i != j
})
return distances
def build_path_cost(distances):
def path_cost(plan):
"""
Calculate the path cost
"""
# [[0, 1, 2, 3, 4], [], [], []]
# Cost(orign -> 0 -> 1 -> 2 -> 3 -> 4 -> orig)
cost = 0
for path in plan:
temp_path = path[1:]
prev = path[0]
for coor in temp_path:
cost += distances[(prev, coor)]
prev = coor
return cost
return path_cost
def solve_vrp(coordinates, full_node_distances, no_vehicles):
"""
Solve the VRP problem with given coordinates and #Vehicles
(The first coordindate is the origin)
:param np.array coordinates: The city coordinate points, point = (lng, lat)
:param int no_vehicles: How many vehicles for the problem
:returns list: Return the optimal paths
"""
logger.debug('G(V, E) = G({}, {})'.format(len(coordinates), len(full_node_distances)))
no_cities = len(coordinates) - 1
def possible_paths():
"""
0 1 2 3 4
0 - (0, 0)
1 - (0, 1)
2 - (0, 2)
3 - (1, 0)
4 - (1, 1)
[[0, 1, 2], [3,4]]
"""
path_indexes = itertools.permutations(range(1, no_cities * no_vehicles), no_cities)
def build_path(p):
paths = [[0] for _ in range(no_vehicles)]
for i, j in enumerate(p):
paths[j // no_cities].append(i + 1)
for path in paths:
path.append(0)
return paths
paths = map(build_path, path_indexes)
return paths
path_cost = build_path_cost(full_node_distances)
min_path = min(possible_paths(), key=path_cost)
min_cost = path_cost(min_path)
return min_path, min_cost
def draw_optimal_path_graph(paths, coordinates, full_node_distances, vehicles):
full_node_distances = {k: round(v, 2) for k, v in full_node_distances.items()}
graph = nx.DiGraph()
# add all nodes
graph.add_nodes_from(range(len(coordinates)))
# Draw edges for all citie pairs
nx.draw_networkx_edges(graph, pos=coordinates, edgelist=full_node_distances.keys(), edge_color='#999999', arrows=False)
nx.draw_networkx_edge_labels(graph, pos=coordinates, edge_labels=full_node_distances, font_size=8, alpha=0.5)
colors = ['r', 'g', 'b', 'c', 'm', 'y', 'k', 'w']
for i, path in enumerate(optimal_path):
prev = path[0]
for index in path[1:]:
graph.add_edge(prev, index, color=colors[i % len(colors)], weight=1.5)
prev = index
# Ref: https://stackoverflow.com/questions/25639169/networkx-change-color-width-according-to-edge-attributes-inconsistent-result
edges = graph.edges()
colors = [graph[u][v]['color'] for u, v in edges]
weights = [graph[u][v]['weight'] for u, v in edges]
nx.draw(graph, pos=coordinates, with_labels=True, font_color='w', edges=edges, edge_color=colors, width=weights)
plt.title('VRP Optimal solution for {} cities and {} vehicles'.format(len(coordinates) - 1, vehicles))
plt.show()
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument(
'--path',
type=str,
help='Path for city coordinates'
)
parser.add_argument(
'--coors',
type=int,
default=7,
help='No. of city coordinates to random generate'
)
parser.add_argument(
'--vehicles',
type=int,
default=2,
help='No. of vehicles'
)
parser.add_argument(
'--verbose',
action='store_true',
help='Verbose mode'
)
args = parser.parse_args(sys.argv[1:])
if args.verbose:
logger.setLevel(logging.DEBUG)
if args.path is None:
logger.info('Creating random coordinates...')
coordinates = generate_coordinates(args.coors)
logger.info(coordinates)
else:
logger.info('Loading coordinates from {}'.format(args.path))
coordinates = np.genfromtxt(args.path, delimiter=',')
full_node_distances = get_node_distances(coordinates)
optimal_path, optimal_cost = solve_vrp(coordinates, full_node_distances, args.vehicles)
logger.info('Optimal path: {}'.format(optimal_path))
logger.info('Optimal cost: {}'.format(optimal_cost))
# Present the solution
draw_optimal_path_graph(optimal_path, coordinates, full_node_distances, args.vehicles)