-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdcf_delay.py
More file actions
223 lines (199 loc) · 7.53 KB
/
dcf_delay.py
File metadata and controls
223 lines (199 loc) · 7.53 KB
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
"""
Function: caculate the bound of the packet service time on IEEE 802.11 DCF
and get the delay_bound curve graph
"""
from get_prob import *
get_B_first_moment = get_total
def get_B_second_moment(pc, k, m, cw):
"""
Calculate the second moments of the sum of backoff intervals(M_B^2)
input:
pc: the collision probability
k : maximum retransmission
m : maximum content window grow times
cw: initial content window size
return: M_B^2
"""
s1 = 0
s2 = 0
for i in range(k + 1):
s1 += (get_stage_average(i, k, m, cw) ** 2 +
get_stage_variance(i, k, m, cw)) * pc ** i
if i > 0:
s3 = 0
for j in range(i - 1):
s3 += get_stage_average(j, k, m, cw)
s2 += get_stage_average(i, k, m, cw) * \
(pc ** k) * s3
return s1 + 2 * s2
def get_X_first_moment(n, pc, sigma, ts, tc):
"""
Calculate the first moments of 𝑋𝑖
(𝑋𝑖 denote the duration that the backoff counter decrements by 1)
input:
n: number of nodes in the same communication range
pc:the collision probability
sigma:one slot tine
ts: channel busy slot number because of success transmission
tc: channel busy slot number because of collision
return: M_X1
"""
p_suc = get_success_prob(n, get_trans_prob(n, pc))
return ((1 - pc) + p_suc * (ts - tc) + pc * tc) * sigma
def get_X_second_moment(n, pc, sigma, ts, tc):
"""
Calculate the second moments of 𝑋𝑖
input:
n: number of nodes in the same communication range
pc:the collision probability
sigma:one slot tine
ts: channel busy slot number because of success transmission
tc: channel busy slot number because of collision
return: M_X2
"""
p_suc = get_success_prob(n, get_trans_prob(n, pc))
return ((1 - pc) +
p_suc * (ts * ts - tc * tc) +
pc * tc * tc) * sigma * sigma
def get_BI_first_moment(n, pc, k, m, cw, sigma, ts, tc):
"""
Calculate the first moments of a compound random variable(specific explanation in ReadMe.md)
input:
n: number of nodes in the same communication range
pc:the collision probability
k: maximum retransmission
m: maximum content window grow times
cw: initial content window size
sigma:one slot tine
ts: channel busy slot number because of success transmission
tc: channel busy slot number because of collision
return: the first moments of a compound random variable(M_B+I_1)
(see it in ReadMe.md)
"""
mb = get_B_first_moment(pc, k, m, cw)
mx = get_X_first_moment(n, pc, sigma, ts, tc)
return mb * mx
def get_BI_second_moment(n, pc, k, m, cw, sigma, ts, tc):
"""
Calculate the second moments of a compound random variable(specific explanation in ReadMe.md)
input:
n: number of nodes in the same communication range
pc:the collision probability
k: maximum retransmission
m: maximum content window grow times
cw: initial content window size
sigma:one slot tine
ts: channel busy slot number because of success transmission
tc: channel busy slot number because of collision
return: the second moments of a compound random variable(M_B+I_2)
(see it in ReadMe.md)
"""
mb1 = get_B_first_moment(pc, k, m, cw)
mx1 = get_X_first_moment(n, pc, sigma, ts, tc)
mb2 = get_B_second_moment(pc, k, m, cw)
mx2 = get_X_second_moment(n, pc, sigma, ts, tc)
return mb1 * mx2 + (mb2 - mb1) * mx1 * mx1
def get_C_first_moment(k, pc, tc):
"""
Calculate the first moments of the sum of collision
input:
k: maximum retransmission
pc:the collision probability
tc: channel busy slot number because of collision
return: the first moments of the sum of collision(M_C1)
"""
return tc * sum([pc ** i for i in range(1, k+1)])
def get_C_second_moment(k, pc, tc):
"""
Calculate the second moments of the sum of collision
input:
k: maximum retransmission
pc:the collision probability
tc: channel busy slot number because of collision
return: the second moments of the sum of collision(M_C1)
"""
return tc * tc * sum([(2 * i - 1) * pc ** i for i in range(1, k + 1)])
def get_service_first_moment(n, pc, k, m, cw, sigma, ts, tc):
"""
Calculate the first moment of the service time 𝛿
input:
n: number of nodes in the same communication range
pc:the collision probability
k: maximum retransmission
m: maximum content window grow times
cw: initial content window size
sigma:one slot tine
ts: channel busy slot number because of success transmission
tc: channel busy slot number because of collision
return: the first moment of the service time 𝛿(M_ 𝛿1)
"""
mbi = get_BI_first_moment(n, pc, k, m, cw, sigma, ts, tc)
mc = get_C_first_moment(k, pc, tc)
return mbi + mc + ts
def get_total_first_moment(n, pc, k, m, cw, sigma, ts, tc):
"""
Calculate M_Delta^1(Delta = C + B*sigma + I)
input:
n: number of nodes in the same communication range
pc:the collision probability
k: maximum retransmission
m: maximum content window grow times
cw: initial content window size
sigma:one slot tine
ts: channel busy slot number because of success transmission
tc: channel busy slot number because of collision
return: M_Delta^1
"""
mbi = get_BI_first_moment(n, pc, k, m, cw, sigma, ts, tc)
mc = get_C_first_moment(k, pc, tc)
return mbi + mc
def get_total_second_moment(n, pc, k, m, cw, sigma, ts, tc):
"""
Calculate M_Delta^2(Delta = C + B*sigma + I)
input:
n: number of nodes in the same communication range
pc:the collision probability
k: maximum retransmission
m: maximum content window grow times
cw: initial content window size
sigma:one slot tine
ts: channel busy slot number because of success transmission
tc: channel busy slot number because of collision
return: M_Delta^2
"""
mbi1 = get_BI_first_moment(n, pc, k, m, cw, sigma, ts, tc)
mc1 = get_C_first_moment(k, pc, tc)
mbi2 = get_BI_second_moment(n, pc, k, m, cw, sigma, ts, tc)
mc2 = get_C_second_moment(k, pc, tc)
return mc2 + mbi2 + 2 * mc1 * mbi1
def dcf_delay_bound(x, n, k, m, cw, ts, tc, sigma):
"""
Caculate the bound of the packet service time on IEEE 802.11 DCF
input:
x: delay(service time equals to delay for single packet)
n: number of nodes in the same communication range
k: maximum transmission
m: maximum content window grow times
cw: initial content window size
ts: channel busy slot number because of success send
tc: channel busy slot number because of collision
sigma: one solt time
return: the bound of the packet service time
"""
if x <= ts:
return
_, pc = get_prob(n, k, m, cw, 1e-6)
m1 = get_total_first_moment(n, pc, k, m, cw, sigma, ts, tc)
m2 = get_total_second_moment(n, pc, k, m, cw, sigma, ts, tc)
return min(m1 / (x - ts), m2 / (x - ts) / (x - ts), 1)
if __name__ == "__main__":
k = 6
m = 4
cw = 32
n = 5
ts = 256
tc = 5
sigma = 0.0001
xs = range(10000, 20000, 1000)
p = [Dcf_bound(x, n, k, m, cw, ts, tc, sigma) for x in xs]
print(list(zip([sigma * x for x in xs], p)))