-
Notifications
You must be signed in to change notification settings - Fork 2
/
tesnnn_tmux_MNO_synthetic_pool_075.py
288 lines (200 loc) · 9.09 KB
/
tesnnn_tmux_MNO_synthetic_pool_075.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
import math
import numpy as np
from torch.utils.data import DataLoader
from data_loading import create_dataset, Dataset
from config import get_config
from trainer import TESRNNTrainer
from validator import TESRNNValidator
from tester import TESRNNTester
from model import TESRNN
from loss_modules import *
import matplotlib.pyplot as plt
import sys
import os
import json
import glob
from torch.multiprocessing import Pool, Process,set_start_method
try:
set_start_method('spawn')
except RuntimeError:
pass
#### 1 week 10080
#### 2 week 20160
#### 3 week 30240
#### 4 week 40320
#### 5 week 50400
#### 6 week 60480
#### 7 week 70560
#### 8 week 80640
#### 9 week 90720
#### 10 week 100800
# # ***TES-RNN***
# CONFIGURATION SETTINGS
device=0
torch.cuda.set_device(device)
# Number of clusters
num_clusters = 1
# Define the number of training epochs
epochs = 35
# Define the number of training batch size
batch_size= 630
# Define the number of train, validation and test samples
train_samples = 40320
val_samples = 20160
test_samples = 10080
# Define the time admission and time decision
time_admission = 120
input_size = 480
# Golden ratio for the golden search algorithm
gratio = (math.sqrt(5) + 1) / 2
# Stopping condition value for the golden search algorithm (interval length)
stop_value = 0.01
# %%
def val_level_dimension(levels,output_size):
arr2 = [None] * output_size
for i in range(len(arr2)):
arr2[i]=levels
return np.array(arr2).transpose()
# SIMULATION RUNS
num_runs = 1
####SHUFLE BATCHES
shuffle_batch=False
# Simulations over different services
def myMultiOpt(idx):
services_idx,alphas_idx,times_idx= idx
print(f'Pair: {idx}')
N_dec= round(time_admission/times_idx)
output_size = N_dec
run_id = f'Synthetic_Results_noisy/{time_admission}/{services_idx}/Alpha_{alphas_idx}/T_dec_{times_idx}_Tadm_{time_admission}'
if not glob.glob(os.path.join('Results', run_id, '*test_actuals.npy')):
# Configuration loading
config = get_config('Traffic', epochs, num_clusters, batch_size, train_samples, val_samples, test_samples, alphas_idx, input_size, output_size,time_admission,times_idx,N_dec,shuffle_batch)
# Data loading
data=f'./Syn_Dataset/Synthetic_data_noisy/{services_idx}_noisy_agg_60_s.npy'
train, val, test = create_dataset(data, config['chop_train'], config['chop_val'], config['chop_test'])
dataset = Dataset(train, val, test, config['device'])
# Maximum of single cluster traffic in the training set (for normalization)
maximum = np.max(train[0])
# Running many simulations for a given service and alpha
for i in range(1, num_runs+1):
#torch.cuda.manual_seed(123)
# Initial extremes of the interval of the Minimum Level Threshold tau (expressed as fraction of maximum)
tau_min = 0.0
tau_max = 1.0
# Current extremes of the interval of tau
c = tau_min
d = tau_max
# Iterations counter for golden search algorithm
iterations = 1
# Dictionary collecting denormalized validation loss values for a given tau
val_dict = {}
# Stopping condition for golden search algorithm
while abs(tau_max - tau_min) > stop_value:
# Determine current Minimum Level Threshold tau
if (iterations%3) > 0:
# Try tau as left extreme
if (iterations%3) == 1:
tau = c
# Try tau as right extreme
else:
tau = d
# Run actual golden search algorithm
else:
# Determine the new extreme of tau interval
if f_c < f_d:
# print("\nNew right-extreme of the interval is %f" % d)
tau_max = d
else:
# print("\nNew left-extreme of the interval is %f" % c)
tau_min = c
# print("Current length of tau interval is %f \n" % abs(tau_max - tau_min))
c = tau_max - (tau_max - tau_min) / gratio
d = tau_min + (tau_max - tau_min) / gratio
iterations = iterations + 1
continue
# Compute denormalized validation loss for current tau
f_val = val_dict.get(round(tau,6))
# print("\nSearching a threshold in the interval [%f,%f]" % (tau_min, tau_max))
# print("Threshold for this run is %f" % tau)
# Denormalized validation loss not yet calculated for current tau
if f_val == None:
# Dataloader initialization
dataloader = DataLoader(dataset, batch_size=config['series_batch'], shuffle=False)
# Model initialization
run_id = f'Synthetic_Results_noisy/{time_admission}/{services_idx}/Alpha_{alphas_idx}/T_dec_{times_idx}_Tadm_{time_admission}/Simulation_{i}'
model = TESRNN(tau = tau, maximum = maximum, num_clusters = num_clusters, config = config, run_id = run_id)
# Run model trainer
trainer = TESRNNTrainer(model, dataloader, run_id, config)
trainer.train_epochs()
# Run model validator
validator = TESRNNValidator(model, dataloader, run_id, config)
validator.validating()
# Compute denormalized validation loss
norm_preds = np.load('Results/' + run_id + '/val_predictions.npy')
norm_actuals = np.load('Results/' + run_id + '/val_actuals.npy')
levels = np.load('Results/' + run_id + '/val_levels.npy')
levels=val_level_dimension(levels,N_dec)
print('OK')
val_loss = denorm_validation_loss(norm_preds, norm_actuals, levels, alpha)
print("Denormalized validation loss for this run %f" % val_loss)
val_dict[round(tau,6)] = val_loss
file_path = os.path.join('Results',run_id, 'Epoch_validation_losses.csv')
with open(file_path, 'w') as f:
f.write('Epoch,Validation_loss\n')
# Store Validation loss of the current epoch
with open(file_path, 'a') as f:
f.write(','.join([str(iterations), str(val_loss)]) + '\n')
# Set denormalized validation loss for interval extreme
if (iterations%3) == 1:
f_c = val_loss
else:
f_d = val_loss
# Denormalized validation loss already calculated for current tau
else:
# print("Denormalized validation loss for this run %f" % f_val)
# Set denormalized validation loss for interval extreme
if (iterations%3) == 1:
f_c = f_val
else:
f_d = f_val
# Increase algorithm iterations
iterations = iterations + 1
# Get the final optimal Minimum Level Threshold tau
tau = (tau_min + tau_max) / 2
# print('\nFinally chosen threshold = %f\n' % tau)
np.save('Results/' + run_id + '/optimal_tau.npy', tau)
# Run the optimized model
# Dataloader initialization
dataloader = DataLoader(dataset, batch_size=config['series_batch'], shuffle=False)
# Model initialization
model = TESRNN(tau = tau, maximum = maximum, num_clusters = num_clusters, config = config, run_id = run_id)
# Run model trainer
trainer = TESRNNTrainer(model, dataloader, run_id, config)
trainer.train_epochs()
# Run model tester
tester = TESRNNTester(model, dataloader, run_id, config)
tester.testing()
else:
print('test realized')
# List of the services to be tested
emBB_filenames = []
mMTC_filenames = []
uRLLC_filenames = []
for i in range(0, 7):
emBB_filenames.append('emBB_'+str(i))
mMTC_filenames.append('mMTC_'+str(i))
uRLLC_filenames.append('uRLLC_'+str(i))
all_filenames = emBB_filenames + mMTC_filenames[:-1] + uRLLC_filenames
print(all_filenames)
print(len(all_filenames))
pool_number=len(all_filenames)
alphas = [0.75]
time_decision_range=[5,15,30,60,120]
pair_list_duration_OP=[]
for times in time_decision_range:
for alpha in alphas:
for service in all_filenames:
pair_list_duration_OP.append((service,alpha,times))
if __name__ == '__main__':
with Pool(pool_number) as p:
p.map(myMultiOpt,pair_list_duration_OP)