-
Notifications
You must be signed in to change notification settings - Fork 117
/
vrp.py
280 lines (199 loc) · 9.08 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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
"""Defines the main task for the VRP.
The VRP is defined by the following traits:
1. Each city has a demand in [1, 9], which must be serviced by the vehicle
2. Each vehicle has a capacity (depends on problem), the must visit all cities
3. When the vehicle load is 0, it __must__ return to the depot to refill
"""
import os
import numpy as np
import torch
from torch.utils.data import Dataset
from torch.autograd import Variable
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
class VehicleRoutingDataset(Dataset):
def __init__(self, num_samples, input_size, max_load=20, max_demand=9,
seed=None):
super(VehicleRoutingDataset, self).__init__()
if max_load < max_demand:
raise ValueError(':param max_load: must be > max_demand')
if seed is None:
seed = np.random.randint(1234567890)
np.random.seed(seed)
torch.manual_seed(seed)
self.num_samples = num_samples
self.max_load = max_load
self.max_demand = max_demand
# Depot location will be the first node in each
locations = torch.rand((num_samples, 2, input_size + 1))
self.static = locations
# All states will broadcast the drivers current load
# Note that we only use a load between [0, 1] to prevent large
# numbers entering the neural network
dynamic_shape = (num_samples, 1, input_size + 1)
loads = torch.full(dynamic_shape, 1.)
# All states will have their own intrinsic demand in [1, max_demand),
# then scaled by the maximum load. E.g. if load=10 and max_demand=30,
# demands will be scaled to the range (0, 3)
demands = torch.randint(1, max_demand + 1, dynamic_shape)
demands = demands / float(max_load)
demands[:, 0, 0] = 0 # depot starts with a demand of 0
self.dynamic = torch.tensor(np.concatenate((loads, demands), axis=1))
def __len__(self):
return self.num_samples
def __getitem__(self, idx):
# (static, dynamic, start_loc)
return (self.static[idx], self.dynamic[idx], self.static[idx, :, 0:1])
def update_mask(self, mask, dynamic, chosen_idx=None):
"""Updates the mask used to hide non-valid states.
Parameters
----------
dynamic: torch.autograd.Variable of size (1, num_feats, seq_len)
"""
# Convert floating point to integers for calculations
loads = dynamic.data[:, 0] # (batch_size, seq_len)
demands = dynamic.data[:, 1] # (batch_size, seq_len)
# If there is no positive demand left, we can end the tour.
# Note that the first node is the depot, which always has a negative demand
if demands.eq(0).all():
return demands * 0.
# Otherwise, we can choose to go anywhere where demand is > 0
new_mask = demands.ne(0) * demands.lt(loads)
# We should avoid traveling to the depot back-to-back
repeat_home = chosen_idx.ne(0)
if repeat_home.any():
new_mask[repeat_home.nonzero(), 0] = 1.
if (1 - repeat_home).any():
new_mask[(1 - repeat_home).nonzero(), 0] = 0.
# ... unless we're waiting for all other samples in a minibatch to finish
has_no_load = loads[:, 0].eq(0).float()
has_no_demand = demands[:, 1:].sum(1).eq(0).float()
combined = (has_no_load + has_no_demand).gt(0)
if combined.any():
new_mask[combined.nonzero(), 0] = 1.
new_mask[combined.nonzero(), 1:] = 0.
return new_mask.float()
def update_dynamic(self, dynamic, chosen_idx):
"""Updates the (load, demand) dataset values."""
# Update the dynamic elements differently for if we visit depot vs. a city
visit = chosen_idx.ne(0)
depot = chosen_idx.eq(0)
# Clone the dynamic variable so we don't mess up graph
all_loads = dynamic[:, 0].clone()
all_demands = dynamic[:, 1].clone()
load = torch.gather(all_loads, 1, chosen_idx.unsqueeze(1))
demand = torch.gather(all_demands, 1, chosen_idx.unsqueeze(1))
# Across the minibatch - if we've chosen to visit a city, try to satisfy
# as much demand as possible
if visit.any():
new_load = torch.clamp(load - demand, min=0)
new_demand = torch.clamp(demand - load, min=0)
# Broadcast the load to all nodes, but update demand seperately
visit_idx = visit.nonzero().squeeze()
all_loads[visit_idx] = new_load[visit_idx]
all_demands[visit_idx, chosen_idx[visit_idx]] = new_demand[visit_idx].view(-1)
all_demands[visit_idx, 0] = -1. + new_load[visit_idx].view(-1)
# Return to depot to fill vehicle load
if depot.any():
all_loads[depot.nonzero().squeeze()] = 1.
all_demands[depot.nonzero().squeeze(), 0] = 0.
tensor = torch.cat((all_loads.unsqueeze(1), all_demands.unsqueeze(1)), 1)
return torch.tensor(tensor.data, device=dynamic.device)
def reward(static, tour_indices):
"""
Euclidean distance between all cities / nodes given by tour_indices
"""
# Convert the indices back into a tour
idx = tour_indices.unsqueeze(1).expand(-1, static.size(1), -1)
tour = torch.gather(static.data, 2, idx).permute(0, 2, 1)
# Ensure we're always returning to the depot - note the extra concat
# won't add any extra loss, as the euclidean distance between consecutive
# points is 0
start = static.data[:, :, 0].unsqueeze(1)
y = torch.cat((start, tour, start), dim=1)
# Euclidean distance between each consecutive point
tour_len = torch.sqrt(torch.sum(torch.pow(y[:, :-1] - y[:, 1:], 2), dim=2))
return tour_len.sum(1)
def render(static, tour_indices, save_path):
"""Plots the found solution."""
plt.close('all')
num_plots = 3 if int(np.sqrt(len(tour_indices))) >= 3 else 1
_, axes = plt.subplots(nrows=num_plots, ncols=num_plots,
sharex='col', sharey='row')
if num_plots == 1:
axes = [[axes]]
axes = [a for ax in axes for a in ax]
for i, ax in enumerate(axes):
# Convert the indices back into a tour
idx = tour_indices[i]
if len(idx.size()) == 1:
idx = idx.unsqueeze(0)
idx = idx.expand(static.size(1), -1)
data = torch.gather(static[i].data, 1, idx).cpu().numpy()
start = static[i, :, 0].cpu().data.numpy()
x = np.hstack((start[0], data[0], start[0]))
y = np.hstack((start[1], data[1], start[1]))
# Assign each subtour a different colour & label in order traveled
idx = np.hstack((0, tour_indices[i].cpu().numpy().flatten(), 0))
where = np.where(idx == 0)[0]
for j in range(len(where) - 1):
low = where[j]
high = where[j + 1]
if low + 1 == high:
continue
ax.plot(x[low: high + 1], y[low: high + 1], zorder=1, label=j)
ax.legend(loc="upper right", fontsize=3, framealpha=0.5)
ax.scatter(x, y, s=4, c='r', zorder=2)
ax.scatter(x[0], y[0], s=20, c='k', marker='*', zorder=3)
ax.set_xlim(0, 1)
ax.set_ylim(0, 1)
plt.tight_layout()
plt.savefig(save_path, bbox_inches='tight', dpi=200)
'''
def render(static, tour_indices, save_path):
"""Plots the found solution."""
path = 'C:/Users/Matt/Documents/ffmpeg-3.4.2-win64-static/bin/ffmpeg.exe'
plt.rcParams['animation.ffmpeg_path'] = path
plt.close('all')
num_plots = min(int(np.sqrt(len(tour_indices))), 3)
fig, axes = plt.subplots(nrows=num_plots, ncols=num_plots,
sharex='col', sharey='row')
axes = [a for ax in axes for a in ax]
all_lines = []
all_tours = []
for i, ax in enumerate(axes):
# Convert the indices back into a tour
idx = tour_indices[i]
if len(idx.size()) == 1:
idx = idx.unsqueeze(0)
idx = idx.expand(static.size(1), -1)
data = torch.gather(static[i].data, 1, idx).cpu().numpy()
start = static[i, :, 0].cpu().data.numpy()
x = np.hstack((start[0], data[0], start[0]))
y = np.hstack((start[1], data[1], start[1]))
cur_tour = np.vstack((x, y))
all_tours.append(cur_tour)
all_lines.append(ax.plot([], [])[0])
ax.scatter(x, y, s=4, c='r', zorder=2)
ax.scatter(x[0], y[0], s=20, c='k', marker='*', zorder=3)
from matplotlib.animation import FuncAnimation
tours = all_tours
def update(idx):
for i, line in enumerate(all_lines):
if idx >= tours[i].shape[1]:
continue
data = tours[i][:, idx]
xy_data = line.get_xydata()
xy_data = np.vstack((xy_data, np.atleast_2d(data)))
line.set_data(xy_data[:, 0], xy_data[:, 1])
line.set_linewidth(0.75)
return all_lines
anim = FuncAnimation(fig, update, init_func=None,
frames=100, interval=200, blit=False,
repeat=False)
anim.save('line.mp4', dpi=160)
plt.show()
import sys
sys.exit(1)
'''