-
Notifications
You must be signed in to change notification settings - Fork 3
/
Full_model_padding.py
314 lines (267 loc) · 12.2 KB
/
Full_model_padding.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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
import torch
from torch import nn, optim
from torch.autograd import Variable
import torch.utils.data as data_utils
from torch.utils.data.dataset import Dataset
from sklearn.preprocessing import StandardScaler
import pandas as pd
import numpy as np
import re
from gurobipy import *
import datetime
from sklearn.preprocessing import OneHotEncoder
from sklearn.preprocessing import MinMaxScaler
from scipy.special import softmax, log_softmax
dtype = torch.float
device = torch.device("cpu")
import logging
from MarkovModel import*
from VRP_model import *
from VRP_LSTMmodel import *
from Util import VRPGurobi, VRPsolutiontoList, eval_ad, eval_sd
reluop = nn.ReLU()
def pred0(output, target):
# reluop = nn.ReLU()
n_cols = target.shape[1]
output_ = output.clone()
for i in range(len(target)):
n_ones =int( target[i].sum().item())
# print(n_ones)
v = torch.kthvalue(output_[i], n_cols- n_ones+1).values
# print(v)
output_[i][output_[i] >= v]=1
output_[i][output_[i] < v]= 0
# return reluop(target - output_).sum()
return output_ #torch.mean((target - output_)**2)
def predother(output, target):
output_ = output.clone()
v = torch.topk(output,1).values
output_[output_>=v]= 1
output_[output_<v]= 0
# return reluop(target - output).sum()
return output_ #torch.mean((target - output)**2)
class TwoStageVRP_padding:
def __init__(self,net,lookback_period =30,
weekly=False,reverse=False,nnodes=74,
embedding_size =6, nweekdays=7,n_features=2,
optimizer=optim.Adam,epochs=20,stop_embedding=False,stop_embedding_size =10,
**kwargs):
self.net = net
self.lookback_period = lookback_period
self.weekly = weekly
self.reverse = reverse
self.nnodes = nnodes
self.embedding_size = embedding_size
self.epochs = epochs
self.kwargs = kwargs
self.stop_embedding = stop_embedding
self.stop_embedding_size = stop_embedding_size
self.n_features = n_features
optim_args= [k for k, v in signature(optimizer).parameters.items()]
self.optim_dict = {k: kwargs.pop(k) for k in dict(kwargs) if k in optim_args}
self.optimizer = optimizer
def fit_predict(self,distance_mat,stops_list,weekday,n_vehicleslist,
trgt, active_days,omega=1):
'''
past are extracted from the data,
the data of day [t] is unused.
this module also fits the models!
'''
trgt_past = trgt[:-1]
stops_list_past = stops_list[:-1]
weekday_past = weekday[:-1]
n_vehicleslist_past = n_vehicleslist[:-1]
activeindices = stops_list[-1]
act = trgt[-1,:,:]
criterion = nn.BCELoss()
###
proba_mat = np.zeros((74,74))
self.training_loss =0
training_loss = []
for test_stop in activeindices:
model_stop = self.net(self.embedding_size,
self.lookback_period,
stop_embedding_size =self.stop_embedding_size, n_features=self.n_features)
optimizer_stop = self.optimizer(model_stop.parameters(),
**self.optim_dict)
stop_active_days = active_days[test_stop]
till_day = np.searchsorted(stop_active_days ,len(trgt_past))
stop_active_tilldays = stop_active_days[:till_day]
if self.reverse:
till_day = np.searchsorted(stop_active_days ,201-len(trgt_past))
stop_active_tilldays = np.array(list(reversed(200 - stop_active_days[till_day:])))
training_days = len(stop_active_tilldays)
x_dist = log_softmax(distance_mat[test_stop]).astype(np.float32)
y_train = np.zeros((training_days,
self.nnodes)).astype(np.float32)
x_train = np.zeros((training_days,
self.lookback_period,self.nnodes)).astype(np.float32)
x_mask = np.ones((training_days,self.nnodes)).astype(np.float32)
x_vehicle = np.zeros((training_days,1)).astype(np.float32)
x_stops = np.zeros((training_days,1)).astype(np.float32)
x_week = np.zeros(training_days).astype(np.int64)
x_markov = np.zeros((training_days,
self.nnodes)).astype(np.float32)
active_stops =[]
for t in range(training_days):
d = stop_active_tilldays[t]
y_train[t] = trgt_past[d ][test_stop]
lacti = stops_list_past[d ]
x_vehicle[t] = n_vehicleslist_past[d]
x_stops[t] = len(lacti)
x_week[t] = weekday_past[d ]
x_mask[t][lacti] = 0
active_stops.append(lacti)
if t >= self.lookback_period:
x_train[t] = trgt_past[stop_active_tilldays[(t-self.lookback_period):t],
test_stop]
else:
x_train[t] = np.pad( trgt_past[stop_active_tilldays[:t],
test_stop],(( self.lookback_period - t,0),
(0,0)), 'constant', constant_values=(1/self.nnodes,0))
markovmodel = MarkovCounter(test_stop,
exp=0.7,beta=1,smoothing_value= 0.1,weekly= True)
markovmodel.fit(trgt_past[stop_active_tilldays[:t]],
weekday_past[stop_active_tilldays[:t]])
x_markov[t] = markovmodel.predict(distance_mat,
stops_list_past[[d]], weekday_past[[d]],
n_vehicleslist_past[[d]],trgt_past[stop_active_tilldays[:d]])
x_dist = torch.from_numpy(x_dist).to(device)
x_markov = torch.from_numpy(x_markov).to(device)
x_week = torch.from_numpy(x_week).to(device)
x_train = torch.from_numpy(x_train).to(device)
scaler = MinMaxScaler()
x_stops = scaler.fit_transform(x_stops)
if self.stop_embedding:
x_features = x_vehicle
else:
x_features = np.concatenate((x_stops, x_vehicle), axis=1)
x_features = torch.from_numpy(x_features).to(device)
x_mask = torch.from_numpy(x_mask).to(device)
y_train = torch.from_numpy(y_train).to(device)
# if test_stop ==0:
# criterion = my_loss0
# else:
# criterion = my_loss_other
# criterion = nn.NLLLoss()
for ep in range(self.epochs):
optimizer_stop.zero_grad()
if self.stop_embedding:
op = model_stop(active_stops, x_train,
x_dist, x_features,x_markov, x_week,x_mask)
else:
op = model_stop(x_train,
x_dist, x_features,x_markov, x_week,x_mask)
# loss = criterion( torch.log(op), torch.argmax(y_train, dim=1))
if test_stop ==0:
y_pred = pred0(torch.exp(op),y_train)
else:
y_pred = predother(torch.exp(op),y_train)
# loss = -(op * reluop(y_train -y_pred )).sum()/len(y_train)
loss = -(op * y_train).sum()/len(y_train)
loss.backward()
# print("Epochs: {} Loss: {}".format(ep,loss.item()))
optimizer_stop.step()
training_loss.append(loss.item())
#### Training cplt
### Prediction phase
model_stop.eval()
eval_days= 1
x_evaluation = np.zeros((eval_days,
self.lookback_period,self.nnodes)).astype(np.float32)
x_mask = np.ones((eval_days,self.nnodes)).astype(np.float32)
x_vehicle = np.zeros((eval_days,1)).astype(np.float32)
x_stops = np.zeros((eval_days,1)).astype(np.float32)
x_week = np.zeros(eval_days).astype(np.int64)
x_markov = np.zeros((eval_days,self.nnodes)).astype(np.float32)
active_stops = []
t = 0
lacti = activeindices
active_stops.append(lacti)
x_vehicle[t] = n_vehicleslist[-1]
x_stops[t] = len(lacti)
x_week[t] = weekday[-1 ]
x_mask[t][activeindices] = 0
if len(stop_active_tilldays) >= self.lookback_period:
x_evaluation[t] = trgt[stop_active_tilldays[-(self.lookback_period):],
test_stop]
else:
x_evaluation[t] = np.pad( trgt[stop_active_tilldays[-(self.lookback_period):],
test_stop],(( self.lookback_period - len(stop_active_tilldays),0),
(0,0)), 'constant', constant_values=(1/self.nnodes,0))
x_markov[t] = markovmodel.predict(distance_mat,
stops_list[[-1]], weekday[[-1]],
n_vehicleslist[[-1]],trgt_past)
x_markov = torch.from_numpy(x_markov).to(device)
x_week = torch.from_numpy(x_week).to(device)
x_evaluation = torch.from_numpy(x_evaluation).to(device)
x_stops = scaler.transform(x_stops)
if self.stop_embedding:
x_features = x_vehicle
else:
x_features = np.concatenate((x_stops, x_vehicle), axis=1)
x_features = torch.from_numpy(x_features).to(device)
x_mask = torch.from_numpy(x_mask).to(device)
if self.stop_embedding:
predicted_proba = model_stop(active_stops, x_evaluation,
x_dist, x_features,x_markov, x_week,x_mask).detach().numpy()
else:
predicted_proba = model_stop (x_evaluation,
x_dist, x_features,x_markov, x_week,x_mask).detach().numpy()
if omega==0:
proba_mat[test_stop] = markovmodel.predict(distance_mat,
stops_list[[-1]], weekday[[-1]],
n_vehicleslist[[-1]],trgt_past)
elif omega<1:
exp = omega*np.exp(predicted_proba) + (1-omega)*np.exp(x_markov[t].detach().numpy())
proba_mat[test_stop] = np.log(exp)
else:
proba_mat[test_stop] = predicted_proba
model_stop.train()
self.training_loss = np.mean(training_loss)
return proba_mat
def evaluation(self,distance_mat,stops_list,weekday,n_vehicleslist,
trgt, active_days,demands, capacities, capacitated =True,omega=1):
'''
demands and capacities are of day t
rest are till day t
this will be fed directly to predict,
which takes care of extracting the past
'''
trgt_past = trgt[:-1]
# stops_list_past = stops_list[:-1]
# weekday_past = weekday[:-1]
# n_vehicleslist_past = n_vehicleslist[:-1]
activeindices = stops_list[-1]
act = trgt[-1,:,:]
proba_mat = self.fit_predict(distance_mat,stops_list,weekday,n_vehicleslist,
trgt, active_days,omega)
criterion = nn.NLLLoss() #nn.BCELoss()
bceloss = criterion( torch.from_numpy(proba_mat[activeindices,:][:,activeindices]),
torch.from_numpy( np.argmax(act[activeindices,:][:,activeindices],axis=1)) ).item()
proba_mat = -(proba_mat)
'''
the zeros come becuase of masking in the solution
log of zero make it infinity, make this infinity with any scaler
doesn't matter because the constraint specifies only active
stops t be considered
'''
if capacitated:
qcapacity = demands
Q = capacities[0]
else:
qcapacity = np.ones(74)
Q = len(activeindices)
solved,cmnt, sol,u = VRPGurobi(proba_mat,qcapacity,Q,
n_vehicleslist[-1] ,activeindices)
if solved:
sol = np.rint(sol)
P = VRPsolutiontoList(sol)
A = VRPsolutiontoList(act)
# diff = act - sol
# print(" Arc Difference {} {} Route Difference {}".format(np.sum( diff* (diff >0) ),
# eval_ad (P,A), eval_sd(P,A)))
else:
raise Exception("VRP not solved for day {}".format(len(trgt_past)))
return eval_ad (P,A), eval_sd(P,A),bceloss,\
self.training_loss,np.sum(distance_mat*sol), cmnt