-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathplots.py
564 lines (402 loc) · 14.7 KB
/
plots.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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
'''
Plot functions to graphically present simulation results
'''
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
from parameters import SAVE_FIGS, ONE_FIGURE
server_names = ['server 1', 'server 2', 'server 3',
'server 4', 'server 5']
color_sequence = ['#1f77b4', '#aec7e8', '#ff7f0e', '#ffbb78', '#2ca02c']
def setup_plots(suptitle):
'''
Basic setup of plots so it can be reused on plot functions
Parameters
----------
suptitle: string
Description of the plot that will appear on the top
Returns
-------
Figure and axis matplotlib structs
'''
fig, ax = plt.subplots(1, 1, figsize=(15, 12))
# fig.suptitle(suptitle)
for item in ([ax.title, ax.xaxis.label, ax.yaxis.label]):
item.set_fontsize(30)
for item in (ax.get_xticklabels() + ax.get_yticklabels()):
item.set_fontsize(26)
item.set_fontweight("bold")
font = {'weight' : 'bold'}
matplotlib.rc('font', **font)
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
# Provide tick lines across the plot to help viewers trace along
# the axis ticks.
plt.grid(True, 'major', 'y', ls='--', lw=.5, c='k', alpha=.3)
# Remove the tick marks; they are unnecessary with the tick lines we just
# plotted.
plt.tick_params(axis='both', which='both', bottom=True, top=False,
labelbottom=True, left=False, right=False, labelleft=True)
return fig, ax
def create_plot_server(result, path_name, suptitle, xlabel, ylabel, offset):
'''
Generate the plot needed
Parameters
----------
result: 2-d array
Each row is a different timeslot
Returns
-------
Plot
'''
if ONE_FIGURE == False:
fig, ax = setup_plots(suptitle)
y_positions = []
for index, row in enumerate(result):
line = plt.plot(row, lw=5, color=color_sequence[index])
# set the text to start on the y of the last value of the line
y_pos = row[-1]
server_name = server_names[index]
# move based on offset if names overlap on plot
while y_pos in y_positions:
y_pos += offset
y_positions.append(y_pos)
plt.text(len(row) + 5, y_pos, server_name, fontsize=24, color=color_sequence[index])
plt.xlabel(xlabel, fontweight='bold')
plt.ylabel(ylabel, fontweight='bold')
if SAVE_FIGS == True and ONE_FIGURE == False:
plt.savefig("plots/" + path_name + ".png")
else:
plt.show(block=False)
def plot_data_offloading_of_users(all_bytes_offloaded):
'''
Plot the data each user is offloading in each timeslot
Parameters
----------
all_bytes_offloaded: 2-d array
Contains on each row the amount of data each user is offloading. Each row is
a different timeslot
Returns
-------
Plot
'''
result = all_bytes_offloaded
# Each row on the transposed matrix contains the data the user offloads
# in each timeslot. Different rows mean different user.
result = np.transpose(result)
suptitle = "Data each user is offloading in each timeslot"
if ONE_FIGURE == False:
fig, ax = setup_plots(suptitle)
for index, row in enumerate(result):
# display only some of the users on the plot
if index%11 == 0:
line = plt.plot(row, lw=5)
# line = plt.plot(row, lw=5)
plt.xlabel('iterations', fontweight='bold')
plt.ylabel('amount of data (bytes)', fontweight='bold')
path_name = "all_bytes_offloaded"
if SAVE_FIGS == True and ONE_FIGURE == False:
plt.savefig("plots/" + path_name + ".png")
else:
plt.show(block=False)
def plot_user_utility(all_user_utility):
'''
Plot the utility each user has in each timeslot
Parameters
----------
all_user_utility: 2-d array
Contains on each row the utility value each user has. Each row is
a different timeslot
Returns
-------
Plot
'''
result = all_user_utility
# Each row on the transposed matrix contains the data the user offloads
# in each timeslot. Different rows mean different user.
result = np.transpose(result)
suptitle = "Utility each user has in each timeslot"
if ONE_FIGURE == False:
fig, ax = setup_plots(suptitle)
for index, row in enumerate(result):
line = plt.plot(row, lw=5)
plt.xlabel('iterations', fontweight='bold')
plt.ylabel('utility', fontweight='bold')
path_name = "all_user_utility"
if SAVE_FIGS == True and ONE_FIGURE == False:
plt.savefig("plots/" + path_name + ".png")
else:
plt.show(block=False)
def plot_num_of_users_on_each_server(all_server_selected, S, **params):
'''
Plot number of users on each server every timeslot
Parameters
----------
all_server_selected: 2-d array
Contains on each row the server each user has selected. Each row is
a different timeslot
S: int
Number of servers
Returns
-------
Plot
'''
# How many users each server has each timeslot
# result = np.empty((0, S), int)
# for row in all_server_selected:
# # the bincount finds how many times each server has been selected
# result = np.append(result, [np.bincount(row, minlength=S)], axis=0)
result = all_server_selected
# Each row on the transposed matrix contains how many users the server has
# in each timeslot. Different rows mean different servers.
result = np.transpose(result)
offset = np.abs(np.max(result) - np.min(result))*0.03
if offset < 0.005:
offset = 0.005 + np.abs(np.max(result))*0.005;
path_name = "all_server_selected"
suptitle = "Number of users each server has in each timeslot"
xlabel = "timeslots"
ylabel = "num of users"
create_plot_server(result, path_name, suptitle, xlabel, ylabel, offset)
def plot_pricing_of_each_server(all_prices):
'''
Plot pricing of each server on every timeslot
Parameters
----------
all_prices: 2-d array
Contains on each row the price each server has chosen. Each row is
a different timeslot
Returns
-------
Plot
'''
result = all_prices
# Each row on the transposed matrix contains the price the server has
# in each timeslot. Different rows mean different servers.
result = np.transpose(result)
offset = np.abs(np.max(result) - np.min(result))*0.03
if offset < 0.005:
offset = 0.005 + np.abs(np.max(result))*0.005;
path_name = "all_prices"
suptitle = "Price each server has selected in each timeslot"
xlabel = "timeslots"
ylabel = "price"
create_plot_server(result, path_name, suptitle, xlabel, ylabel, offset)
def plot_receiving_data_on_each_server(all_bytes_to_server):
'''
Plot the data each server is receiving in each timeslot
Parameters
----------
all_bytes_to_server: 2-d array
Contains on each row the amount of data each server is receiving. Each row is
a different timeslot
Returns
-------
Plot
'''
result = all_bytes_to_server
# Each row on the transposed matrix contains the data the server receives
# in each timeslot. Different rows mean different servers.
result = np.transpose(result)
offset = np.abs(np.max(result) - np.min(result))*0.03
if offset < 0.005:
offset = 0.005 + np.abs(np.max(result))*0.005;
path_name = "all_bytes_to_server"
suptitle = "Data each server is receiving in each timeslot"
xlabel = "timeslots"
ylabel = "amount of data (bytes)"
create_plot_server(result, path_name, suptitle, xlabel, ylabel, offset)
def plot_server_welfare(all_server_welfare):
'''
Plot the welfare of each server in each timeslot
Parameters
----------
all_server_welfare: 2-d array
Contains on each row the welfare of each server. Each row is
a different timeslot
Returns
-------
Plot
'''
result = all_server_welfare
# Each row on the transposed matrix contains the data the user offloads
# in each timeslot. Different rows mean different user.
result = np.transpose(result)
offset = np.abs(np.max(result) - np.min(result))*0.03
if offset < 0.005:
offset = 0.005 + np.abs(np.max(result))*0.005;
path_name = "all_server_welfare"
suptitle = "Welfare of the server at the end of each timeslot"
xlabel = "timeslots"
ylabel = "welfare"
create_plot_server(result, path_name, suptitle, xlabel, ylabel, offset)
def plot_server_Rs(all_Rs):
'''
Plot the competitiveness score each server has in each timeslot
Parameters
----------
all_Rs: 2-d array
Contains on each row the Rs of each server. Each row is
a different timeslot
Returns
-------
Plot
'''
result = all_Rs
# Each row on the transposed matrix contains the data the user offloads
# in each timeslot. Different rows mean different user.
result = np.transpose(result)
offset = np.abs(np.max(result) - np.min(result))*0.03
if offset < 0.005:
offset = 0.005 + np.abs(np.max(result))*0.005;
path_name = "all_Rs"
suptitle = "Rs of the server at the end of each timeslot"
xlabel = "timeslots"
ylabel = "Rs"
create_plot_server(result, path_name, suptitle, xlabel, ylabel, offset)
def plot_server_congestion(all_congestion):
'''
Plot the peak to average ratio each server has in each timeslot
Parameters
----------
all_congestion: 2-d array
Contains on each row the congestion of each server. Each row is
a different timeslot
Returns
-------
Plot
'''
result = all_congestion
# Each row on the transposed matrix contains the data the user offloads
# in each timeslot. Different rows mean different user.
result = np.transpose(result)
offset = np.abs(np.max(result) - np.min(result))*0.03
if offset < 0.005:
offset = 0.005 + np.abs(np.max(result))*0.005;
path_name = "all_congestion"
suptitle = "congestion of the server at the end of each timeslot"
xlabel = "timeslots"
ylabel = "congestion"
create_plot_server(result, path_name, suptitle, xlabel, ylabel, offset)
def plot_server_penetration(all_penetration):
'''
Plot the penetration score each server has in each timeslot
Parameters
----------
all_penetration: 2-d array
Contains on each row the penetration score of each server. Each row is
a different timeslot
Returns
-------
Plot
'''
result = all_penetration
# Each row on the transposed matrix contains the data the user offloads
# in each timeslot. Different rows mean different user.
result = np.transpose(result)
offset = np.abs(np.max(result) - np.min(result))*0.03
if offset < 0.005:
offset = 0.005 + np.abs(np.max(result))*0.005;
path_name = "all_penetration"
suptitle = "penetration of the server at the end of each timeslot"
xlabel = "timeslots"
ylabel = "penetration"
create_plot_server(result, path_name, suptitle, xlabel, ylabel, offset)
def plot_server_discount(all_fs):
'''
Plot the discount each server has in each timeslot
Parameters
----------
all_fs: 2-d array
Contains on each row the discount of each server. Each row is
a different timeslot
Returns
-------
Plot
'''
result = all_fs
# Each row on the transposed matrix contains the data the user offloads
# in each timeslot. Different rows mean different user.
result = np.transpose(result)
offset = np.abs(np.max(result) - np.min(result))*0.03
if offset < 0.005:
offset = 0.005 + np.abs(np.max(result))*0.005;
path_name = "all_fs"
suptitle = "discount of the server at the end of each timeslot"
xlabel = "timeslots"
ylabel = "discount"
create_plot_server(result, path_name, suptitle, xlabel, ylabel, offset)
def plot_server_relative_price(all_relative_price):
'''
Plot the relative price each server sets in each timeslot
Parameters
----------
all_relative_price: 2-d array
Contains on each row the relative_price of each server. Each row is
a different timeslot
Returns
-------
Plot
'''
result = all_relative_price
# Each row on the transposed matrix contains the data the user offloads
# in each timeslot. Different rows mean different user.
result = np.transpose(result)
offset = np.abs(np.max(result) - np.min(result))*0.03
if offset < 0.005:
offset = 0.005 + np.abs(np.max(result))*0.005;
path_name = "all_relative_price"
suptitle = "Relative pricing of the server at the end of each timeslot"
xlabel = "timeslots"
ylabel = "relative pricing"
create_plot_server(result, path_name, suptitle, xlabel, ylabel, offset)
def plot_server_cost(all_c):
'''
Plot the cost each server has in each timeslot
Parameters
----------
all_c: 2-d array
Contains on each row the server computing cost of each server. Each row is
a different timeslot
Returns
-------
Plot
'''
result = all_c
# Each row on the transposed matrix contains the data the user offloads
# in each timeslot. Different rows mean different user.
result = np.transpose(result)
offset = np.abs(np.max(result) - np.min(result))*0.03
if offset < 0.005:
offset = 0.005 + np.abs(np.max(result))*0.005;
path_name = "all_c"
suptitle = "computing cost of the server at the end of each timeslot"
xlabel = "timeslots"
ylabel = "computing cost"
create_plot_server(result, path_name, suptitle, xlabel, ylabel, offset)
def plot_user_probability_to_select_server(user_id, all_probabilities):
'''
Plot the probability that the user will select each server in each timeslot
Parameters
----------
all_probabilities: 3-d array
The first dimension contains the different users,
The second dimension contains the timeslots,
The third dimension contains the probabilities that the user selects the servers
a different timeslot
Returns
-------
Plot
'''
result = all_probabilities[user_id]
# Each row on the transposed matrix contains the data the user offloads
# in each timeslot. Different rows mean different user.
result = np.transpose(result)
offset = np.abs(np.max(result) - np.min(result))*0.03
if offset < 0.005:
offset = 0.005 + np.abs(np.max(result))*0.005;
path_name = "all_probabilities_user_" + str(user_id)
suptitle = "Probability that the user " + str(user_id) + " will select each server at the end of each timeslot"
xlabel = "timeslots"
ylabel = "probabilities"
create_plot_server(result, path_name, suptitle, xlabel, ylabel, offset)