-
Notifications
You must be signed in to change notification settings - Fork 4
/
playground.py
207 lines (180 loc) · 8.78 KB
/
playground.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
import numpy as np
from matplotlib import pyplot as plt
from scipy.interpolate import interp1d
from algo.intervals import Intervals
from simulator.coverage import CoverageLatencyInference, CoverageLatencyInference4AlternationBroadcast, \
AlternationBroadcastConfig, CLIFABL
from simulator.determined import BruteForceLossAdvdelaySimulator
import random
from simulator.sampler import PureBleSimulator, AlternationBroadcastSampler
import utils.plotter as pt
from utils import Log
def determined_test():
for _ in range(10):
scan_interval = random.randint(1000, 5000)
adv_interval = random.randint(200, scan_interval - 500)
scan_window = random.randint(30, scan_interval // 3 * 2)
loss_rate = random.randint(0, 65)
config = f"scan_interval: {scan_interval}, scan_window: {scan_window}," \
f" adv_interval: {adv_interval}, loss: {loss_rate}%"
print(config)
blender = BruteForceLossAdvdelaySimulator(adv_interval=adv_interval,
scan_interval=scan_interval,
scan_window=scan_window,
end_time=50000,
loss_rate=loss_rate,
max_advdelay=15)
sampler = PureBleSimulator(adv_interval=adv_interval,
scan_interval=scan_interval,
scan_window=scan_window,
end_time=50000,
loss_rate=loss_rate,
max_advdelay=15)
try:
blender_result_cdf = blender.simulate_all(to_cdf=True)
sampler_result_values = sampler.get_latency_n_times(n=50000)
x = np.asarray([i for i in range(len(blender_result_cdf))])
ax = pt.plot_cdf(x, blender_result_cdf, lc='b')
pt.plot_values_as_cdf(sampler_result_values, ax=ax)
plt.title(config)
plt.show()
except NotImplementedError as e:
Log.E('Playground', e)
def coverage_test():
for _ in range(10):
# scan_interval = random.randint(1000, 5000)
# adv_interval = random.randint(200, scan_interval - 500)
# scan_window =random.randint(30, scan_interval // 3 * 2)
scan_interval = 3685
adv_interval = 3651
scan_window = 1120
loss_rate = random.randint(0, 65)
config = f"scan_interval: {scan_interval}, scan_window: {scan_window}," \
f" adv_interval: {adv_interval}"
blender = CoverageLatencyInference(adv_interval=adv_interval,
scan_interval=scan_interval,
scan_window=scan_window,
end_time=50000)
sampler = PureBleSimulator(adv_interval=adv_interval,
scan_interval=scan_interval,
scan_window=scan_window,
end_time=50000,
loss_rate=0,
max_advdelay=0)
try:
blender_result_cdf = blender.simulate_all(to_cdf=True)
print(np.argmax(blender_result_cdf))
sampler_result_values = sampler.get_latency_n_times(n=50000)
print(max(sampler_result_values))
x = np.asarray([i for i in range(len(blender_result_cdf))])
ax = pt.plot_cdf(x, blender_result_cdf, lc='b')
pt.plot_values_as_cdf(sampler_result_values, ax=ax)
plt.title(config)
plt.show()
except NotImplementedError as e:
Log.E('Playground', e)
def abp_test():
for _ in range(1):
# scan_interval = random.randint(1000, 6000)
# abp_config = AlternationBroadcastConfig(random.sample(range(1000, 6000), random.randint(1, 70)))
# scan_window =random.randint(30, scan_interval // 3 * 2)
scan_interval = 3685
abp_config = AlternationBroadcastConfig([3651])
scan_window = 1120
config = f"scan_interval: {scan_interval}, scan_window: {scan_window}," \
f" abp_config: {abp_config}"
blender = CoverageLatencyInference4AlternationBroadcast(abp_config=abp_config,
scan_interval=scan_interval,
scan_window=scan_window,
end_time=50000)
sampler = AlternationBroadcastSampler(abp_config=abp_config,
scan_interval=scan_interval,
scan_window=scan_window,
end_time=50000,
loss_rate=0)
try:
blender_result_cdf = blender.simulate_all(to_cdf=True)
sampler_result_values = sampler.get_latency_n_times(n=5000)
print(max(sampler_result_values))
x = np.asarray([i for i in range(len(blender_result_cdf))])
ax = pt.plot_cdf(x, blender_result_cdf, lc='b')
pt.plot_values_as_cdf(sampler_result_values, ax=ax)
plt.title(config)
plt.show()
except NotImplementedError as e:
Log.E('Playground', e)
def abp_loss_test():
for _ in range(20):
scan_interval = random.randint(1000, 6000)
abp_config = AlternationBroadcastConfig(random.sample(range(1000, 6000), random.randint(1, 70)))
scan_window =random.randint(30, scan_interval // 3 * 2)
loss_rate = random.randint(10, 70) / 100
# scan_interval = 3685
# abp_config = AlternationBroadcastConfig([3651])
# scan_window = 1120
# loss_rate = 0.0
config = f"scan_interval: {scan_interval}, scan_window: {scan_window}," \
f" abp: {abp_config}, loss rate: {loss_rate}"
print(config)
blender = CLIFABL(abp_config=abp_config,
scan_interval=scan_interval,
scan_window=scan_window,
end_time=50000,
fail_rate=loss_rate)
sampler = AlternationBroadcastSampler(abp_config=abp_config,
scan_interval=scan_interval,
scan_window=scan_window,
end_time=50000,
loss_rate=round(loss_rate * 100))
try:
blender_result_cdf = blender.simulate_all(to_cdf=True)
sampler_result_values = sampler.get_latency_n_times(n=5000)
print(max(sampler_result_values))
x = np.asarray([i for i in range(len(blender_result_cdf))])
ax = pt.plot_cdf(x, blender_result_cdf, lc='b')
pt.plot_values_as_cdf(sampler_result_values, ax=ax)
plt.title(config)
plt.show()
except NotImplementedError as e:
Log.E('Playground', e)
def abp_compare():
"""
TODO: set up at least 9 different parameter sets and run alternation broadcasting.
Then take 200-300 random samples in realistic emulation to form a distribution.
Calculate the RMSE between the distribution generated by simulation and emulation.
RMSE between 0.05-0.1 is desired
"""
# Sample Configuration
scan_interval = 5120
scan_window = 512
abp_config = AlternationBroadcastConfig([1860, 1860, 1860, 1860, 1860, 2140, 2140, 2140, 2140, 2140, 2140, 2140])
loss_rate = 0.3 # May need 2-3 different loss rates regarding different environments
end_time = 40000
config = f"scan_interval: {scan_interval}, scan_window: {scan_window}," \
f" abp: {abp_config}, loss rate: {loss_rate}"
print(config)
# the Alternation Broadcast Simulator
blender = CLIFABL(abp_config=abp_config,
scan_interval=scan_interval,
scan_window=scan_window,
end_time=end_time,
fail_rate=loss_rate)
blender_result_cdf = blender.simulate_all(to_cdf=True) # A numpy array of length (end_time + 1), index is latency and value is probability
"""
*********YOUR CODE HERE***********
"""
# Read Emulation latency distribution
import h5py
f = h5py.File('yourdata.mat', 'r')
data = f.get('data/variable1')
emulation_data = np.array(data) # For converting to a NumPy array
# OR export Blender result
np.save(blender_result_cdf, "blender_temp.npy")
# Calculate RMSE
"""
*********END OF YOUR CODE***********
"""
if __name__ == '__main__':
Log.debug()
# abp_loss_test()
abp_compare()