-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathEH_Random_Access.m
235 lines (189 loc) · 7.98 KB
/
EH_Random_Access.m
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
% function [throughput,n_nodes] = EH_Random_Access(n_nodes,buff_cap,batt_cap,max_retx)
clear();
clc();
%%% INITIALISATION %%%
simlength = 10000;
batt_cap = 8;
buff_cap = 10 ;
% e_arr_v = [0 1] ;
% p_arr_v = [0 1] ;
max_retx = 4 ;
n_nodes = 2;
batt = randi([0 batt_cap],1,n_nodes);
buff = zeros(1,n_nodes);
backoff = zeros(1,n_nodes);
retx= zeros(1,n_nodes);
en_prob = 0.6;
pkt_prob = 0.8;
p_burst = 5;
e_burst = 3;
e_curr = zeros(1,n_nodes);
p_curr = zeros(1,n_nodes);
arr_count = zeros(1,n_nodes) ;
collision_flag = zeros(1,n_nodes) ;
tx_count = zeros(1,n_nodes) ;
drop_count = zeros(1,n_nodes);
drop_retx = zeros(1,n_nodes);
arr_count_t = 0;
drop_count_t = 0;
drop_retx_t = 0;
tx_count_t = 0;
tx_count_c = zeros(simlength,1);
throughput = zeros(simlength,1);
collision_count = 0;
time = 0 ;
timer = zeros(simlength,1);
while (time ~= simlength)
e_arr = rand();
e_arr_p = rand(1,n_nodes);
p_arr_p = rand(1,n_nodes);
time = time + 1 ;
for i = 1:n_nodes % for all nodes transmitting simultaneousl
% modelling packet arrival with burstiness
if (p_curr(i) ==0)
if (p_arr_p >= (1/p_burst))
p_curr(i) = 1;
else
p_curr(i) = 0;
end
elseif(p_curr(i) == 1)
if (p_arr_p >= (pkt_prob/(p_burst*(1-pkt_prob))))
p_curr(i) = 0;
else
p_curr(i) = 1;
end
end
if(p_curr(i) == 1) %packet arrived
arr_count(i) = arr_count(i) + 1;
arr_count_t = arr_count_t + 1;
buff(i) = buff(i) + 1;
if(batt(i) > 0 ) % buff is not empty
if(buff(i) > 0) % battery available
if(backoff(i) == 0)
collision_flag(i) = 1; %Node 'i' can transmit
if(sum(collision_flag) == 1) %if only one node can tranmit
tx_count(i) = tx_count(i) + 1 ; %Succesful tx
tx_count_t = tx_count_t + 1;
buff(i) = buff(i) - 1;
batt(i) = batt(i) - 1;
else
collision_count = collision_count + 1;
retx(i) = retx(i) + 1;
if(retx(i) > max_retx)
retx(i) = 0;
drop_retx(i) = drop_retx(i) + 1; %dropped due to retx window
drop_retx_t = drop_retx_t + 1;
buff(i) = buff(i) - 1 ;
end
backoff(i) = randi([0 (2^retx(i) - 1)]); %backoff recalculated
batt(i) = batt(i) - 1;
end
else
collision_flag(i) = 0; %no nodes ready to tx
backoff(i) = backoff(i)-1; %decrease backoff
end
end
else
%energy exhausion/battery died
batt(i) = 0;
if buff(i) >0
if backoff(i) == 0
retx(i) = retx(i) + 1; %incr retx and recalc backoff
backoff(i) = randi([1 (2^retx(i) - 1)]) ;
if(retx(i) > max_retx) %pkt dropped due to max_retx
retx(i) = 0;
drop_retx(i) = drop_retx(i) + 1 ;
drop_retx_t = drop_retx_t + 1;
buff(i) = buff(i) - 1 ;
end
else
backoff(i) = backoff(i) - 1;
end
else
backoff(i) = backoff(i) - 1;
if backoff(i) < 0
backoff(i) = 0;
end
end
end
else
%no packet arrived
if(batt(i) > 0 ) % buff is not empty
if(buff(i) > 0) % battery available
if(backoff(i) == 0)
collision_flag(i) = 1;
if(sum(collision_flag) == 1)
tx_count(i) = tx_count(i) + 1 ;
tx_count_t = tx_count_t + 1;
buff(i) = buff(i) - 1;
batt(i) = batt(i) - 1;
else
collision_count = collision_count + 1;
retx(i) = retx(i) + 1;
if(retx(i) > max_retx)
retx(i) = 0;
drop_retx(i) = drop_retx(i) + 1;
drop_retx_t = drop_retx_t + 1;
buff(i) = buff(i) - 1 ;
end
backoff(i) = randi([0 (2^retx(i) - 1)]);
batt(i) = batt(i) - 1;
end
else
collision_flag(i) = 0;
backoff(i) = backoff(i)-1;
end
end
else
%energy exhausion/battery died
batt(i) = 0;
if buff(i) >0
if backoff(i) == 0
retx(i) = retx(i) + 1; %incr retx and recalc backoff
backoff(i) = randi([1 (2^retx(i) - 1)]) ;
if(retx(i) > max_retx) %pkt dropped due to max_retx
retx(i) = 0;
drop_retx(i) = drop_retx(i) + 1 ;
drop_retx_t = drop_retx_t + 1;
buff(i) = buff(i) - 1 ;
end
else
backoff(i) = backoff(i) - 1;
end
else
backoff(i) = backoff(i) - 1;
if backoff(i) < 0
backoff(i) = 0;
end
end
end
end
if(buff(i) > buff_cap)
buff(i)=buff(i) - 1;
drop_count(i) = drop_count(i) + 1;
drop_count_t = drop_count_t +1;
end
% 2-state markov chain for energy arrival state if (e_arr_p(i) > en_prob)
if (e_curr(i) ==0)
if (e_arr >= (1/e_burst))
e_curr(i) = 1;
else
e_curr(i) = 0;
end
elseif(e_curr(i) == 1)
if (e_arr >= (en_prob/(e_burst*(1-en_prob))))
e_curr(i) = 0;
else
e_curr(i) = 1;
end
end
if(e_curr(i) == 1)
if(batt(i) < batt_cap)
batt(i) = batt(i) + 1;
end
end
end
timer(time,1) = time;
tx_count_c (time,1) = tx_count_t;
end
% end