forked from kenny4uk/DSRC
-
Notifications
You must be signed in to change notification settings - Fork 0
/
alg_aarf.m
executable file
·188 lines (169 loc) · 7.61 KB
/
alg_aarf.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
function alg_aarf(spdavg_set,n)
global Sim App Mac Phy Rate Arf Onoe;
global Pk St Trace_time Trace_rate Trace_sc Trace_fc Trace_fail Trace_col Trace_suc Trace_per Static;
par_init;
% Simulation stops when all packets have been transmitted. Each iteration corresponds to a transmission attempt
Sim.tstart = clock;
Sim.time = 0.0;
t=0;
x_max=300;
ap(1)=150;
ap(2)=0;
spd_set=rand(n,1)*spdavg_set*0.5+spdavg_set*0.75;% This is a
while sum([Pk.suc])<=Sim.pk,
if (rem(sum([Pk.tx]),10000)==0) & 0,
deltaT = etime(clock,Sim.tstart);
disp(['Expected time to conclusion: ',num2str(round(deltaT/sum([Pk.suc])*(Sim.pk- sum([Pk.suc])))),' sec...'])
end; % if rem...
dt_temp = min(Mac.Bk_cnt); % Txnode = IDs of the nodes that attempt the transmission
t=1000*10^(-6);% Time after which each vehicle waits before transmitting
for i=1:n
for j=1:2
oldp(i,j)= rand()*300;
end
end
Txnode = find(Mac.Bk_cnt==dt_temp); % find the time of the first transmission attempt
Mac.Bk_cnt=Mac.Bk_cnt-dt_temp-1; % all backoff counters are decremented
Sim.time= Sim.time+ dt_temp*Phy.sigma; % update the simulation time accordingly
sTxnode = length(Txnode); % sTxnode = number of simultaneously transmitting nodes
% w=p_mob(Sim.time,spd_set,old_pos,x_max,Txnode);
w=range(spd_set,t,x_max,ap,n,oldp );
id=find(w);
% old_pos(id)=w(id);
oldp(id)=w(id);
Pk.tx(Txnode)=Pk.tx(Txnode)+1;
% old_pos=w;
% we distringuish two possible events at this slot time
if sTxnode>1 % if sTxnode > 1 => Collision occurs
St.fail(Txnode)=1;
St.col(Txnode)=1;
Pk.col(Txnode) = Pk.col(Txnode)+ 1; % total number of collided packets is updated;
Phy.Tc(Txnode)=(Phy.Lc_over+ 8*App.lave)./Rate.curr(Txnode);
Pk.power(Txnode)=Pk.power(Txnode)+Phy.Tc(Txnode)*Phy.power;
maxTc=max(Phy.Tc(Txnode)); % we need to know how long the collision is going to last
Sim.time= Sim.time + maxTc; % and update the simulation time subsequently
Mac.nRetry(Txnode)= Mac.nRetry(Txnode)+1; % Add a collision to the number of successive collisions experienced by colliding packets
elseif sTxnode==1
% process BER and check if pkt can be accepted due to ber.
Bper=0;
%Per_temp= Phy.snr_per(Rate.level(Txnode));
% if rand()<Per_temp; Bper=1; end;
if Bper==1
St.fail(Txnode)=1;
St.col(Txnode)=0;
St.per(Txnode)=1;
t=2000*10^(-6);
% w=p_mob(Sim.time,spd_set,old_pos,x_max,Txnode);
[w,w1,w2]=range(spd_set ,t,x_max,ap,n,oldp );
% w=range(spd_set ,t,x_max,ap,n,oldp );
id=find(w);
% old_pos(id)=w(id);
oldp(id)=w(id);
Pk.per(Txnode)=Pk.per(Txnode)+1;
% old_pos=w;
Phy.Ts(Txnode)=(Phy.Lc_over+8*App.lave)./Rate.curr(Txnode); % how long does it take to transmit it with success?
Pk.power(Txnode)=Pk.power(Txnode)+Phy.Tc(Txnode)*Phy.power;
Sim.time = Sim.time + Phy.Ts(Txnode); % update the simulation time
else % if sTxnode == 1 & Bper==0 => Successfull transmission occurs
St.fail(Txnode)=0;
St.col(Txnode)=0;
St.per(Txnode)=0;
t=3000*10^(-6);
% w=p_mob(Sim.time,spd_set,old_pos,x_max,Txnode);
w=range(spd_set ,t,x_max,ap,n,oldp );
id=find(w);
% old_pos(id)=w(id);
oldp(id)=w(id);
Pk.suc(Txnode) = Pk.suc(Txnode)+1; % update number of sent packets
% Phy.Ts=3000*10^(-6);
% w=p_mob(Phy.Ts,spd_set,old_pos,x_max,Txnode);
% id=find(w);
% old_pos(id)=w(id);
Phy.Ts(Txnode)=(Phy.Ls_over+8*App.lave)./Rate.curr(Txnode); % how long does it take to transmit it with success?
Pk.bit(Txnode)=Pk.bit(Txnode)+8*App.lave;
Pk.power(Txnode)=Pk.power(Txnode)+Phy.Ts(Txnode)*Phy.power;
Sim.time = Sim.time + Phy.Ts(Txnode); % update the simulation ti
% ws(Pksuc) = Sim.time-birthtime(Txnode); % compute the service time of this packet
App.birthtime(Txnode) = Sim.time; % and store the time this packet entered service
end; % if Bper
end % if sTxnode>1
for ii=1:sTxnode
iTx=Txnode(ii);
Rate.timer(iTx)=Rate.timer(iTx)-1;
Trace_rate(iTx).list=[Trace_rate(iTx).list Rate.level(iTx)];
Trace_sc(iTx).list=[Trace_sc(iTx).list Arf.sc(iTx)];
Trace_fc(iTx).list=[Trace_fc(iTx).list Arf.fc(iTx)];
Trace_fail(iTx).list=[Trace_fail(iTx).list St.fail(iTx)];
Trace_col(iTx).list=[Trace_col(iTx).list St.col(iTx)];
Trace_per(iTx).list=[Trace_per(iTx).list St.per(iTx)];
check_more_pk=0;
if St.fail(iTx)==0
Arf.sc(iTx)=min(Arf.sc(iTx)+1, Arf.sc_thr(iTx));
Arf.fc(iTx)=0;
if Rate.level(iTx)<Rate.level_max & (Arf.sc(iTx)==Arf.sc_thr(iTx) | Rate.timer(iTx)==0)
Rate.level(iTx)=Rate.level(iTx)+1;
Rate.curr(iTx)=Rate.set(Rate.level(iTx));
Arf.Brecover(iTx)=1;
Arf.sc(iTx)=0;
Rate.timer(iTx)=Arf.inc_timer;
else
if Arf.Brecover(iTx)==1
Arf.sc_thr(iTx)=Arf.sc_min;
end
Arf.Brecover(iTx)=0;
end % Rate.curr<Rate.max
check_more_pk=1;
else % if St_tx(Txnode...
Mac.nRetry(iTx)=Mac.nRetry(iTx)+1;
Arf.sc(iTx)=0;
Arf.fc(iTx)=min(Arf.fc(iTx)+1,Arf.fc_norm);
if Arf.Brecover(iTx)==1
Rate.level(iTx)=Rate.level(iTx)-1;
Rate.curr(iTx)=Rate.set(Rate.level(iTx));
Arf.fc(iTx)=0;
Arf.sc_thr(iTx)=min(2*Arf.sc_thr(iTx), Arf.sc_max);
Rate.timer(iTx)=Arf.inc_timer;
elseif Arf.Brecover(iTx)==0
if Rate.level(iTx)>1 & Arf.fc(iTx)==Arf.fc_norm
Rate.level(iTx)=Rate.level(iTx)-1;
Rate.curr(iTx)=Rate.set(Rate.level(iTx));
Arf.sc_thr(iTx)=Arf.sc_min;
Rate.timer(iTx)=Arf.inc_timer;
elseif Rate.timer(iTx)==0 & Rate.level(iTx)<Rate.level_max
Rate.curr(iTx)=Rate.set(Rate.level(iTx));
Arf.sc(iTx)=0;
Arf.fc(iTx)=0;
Arf.Brecover(iTx)=1;
Rate.timer(iTx)=Arf.inc_timer;
end
end % if Arf.Brecover==1
if Mac.nRetry(iTx)> Mac.nRetry_max
check_more_pk=1;
Pk.drop(iTx)=Pk.drop(iTx)+1;
else
Mac.W(iTx)=min(Mac.Wmin*2^Mac.nRetry(iTx), Mac.Wmax);
Mac.Bk_cnt(iTx)=floor(rand()* Mac.W(iTx));
end % if nRetry>Ret_thr
Arf.Brecover(iTx)=0;
end
if check_more_pk==1
if 1 % if more pk available in queue
Mac.nRetry(iTx)=0;
Mac.W(iTx)= Mac.Wmin;
Arf.fc(iTx)=0;
Mac.Bk_cnt(iTx)=floor(rand()*Mac.W(iTx));
else
Mac.Bk_cnt(iTx)=10^20;
end
end % if check_more_pk
end % for iTx
end; % while sum(Pksuc)<n*mpck,...,end
Static.pk_col = sum([Pk.col])/( sum([Pk.tx])); % collision probability
Static.pk_suc = sum([Pk.suc])/( sum([Pk.tx])); % collision probability
Static.pk_per = sum([Pk.per])/( sum([Pk.tx])); % collision probability
Static.through=sum([Pk.suc])*App.lave*8/Sim.time; % average throughput.
Static.energyeff=sum([Pk.power])/sum([Pk.bit]); % average energy efficiency.
if 0
figure(1); for ii=1:Sim.n; plot(Trace_rate(ii).list); hold on; end; hold off;
end
return;