-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmontecarlo.m
190 lines (187 loc) · 7 KB
/
montecarlo.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
function [psi, theta] = montecarlo(L, N, lambda, epsilon, tx_method, K, p1, p2)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %
% function: montecarlo %
% author: Federico Chiariotti ([email protected]) %
% license: GPLv3 %
% %
% %
% %
% Runs a Monte Carlo simulation with the desired protocol %
% %
% Inputs: %
% -L: the number of steps to simulate [scalar] %
% -N: the number of nodes [scalar] %
% -lambda: the generation rate for each node [1 x N] %
% -epsilon: the wireless channel error probability [scalar] %
% -tx_method: the selected protocol [string] %
% -K: number of cleared slots in BT [scalar] %
% -p1: alpha for ZW/GZW/LZW [scalar] %
% -p2: beta for GZW/LZW [scalar] %
% %
% Outputs: %
% -psi: the maximum AoII for all nodes, step by step [N x L] %
% -theta: the real AoII for all nodes, step by step [N x L] %
% %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Initialize auxiliary variables
psi = zeros(N, L);
theta = zeros(N, L);
state = ones(1, N);
coll = 0;
coll_sequence = 0;
colliders = zeros(1, N);
collision_steps = zeros(1, N);
% Compute threshold
threshold = exp(K * log(1 - mean(lambda)));
for l = 1 : L
% Update public max AoII and private AoII
if (l > 1)
psi(:, l) = psi(:, l - 1) + 1;
theta(:, l) = theta(:, l - 1) + (state' == 2);
end
% Update system state (anomaly generation)
state = min(2, state + (rand(1, N) < lambda));
% Transmission
tx_ind = [];
if (strcmp(tx_method, 'zero_wait'))
% Run ZW algorithm
tx = theta(:, l) > 0;
tx = tx .* (rand(N, 1) < p1);
tx_ind = find(tx);
end
if (strcmp(tx_method, 'zero_wait_local'))
% Run LZW algorithm
tx = theta(:, l) > 0;
for n = 1 : N
p = p1;
if(colliders(n) > 0)
p = p2;
end
tx(n) = tx(n) * (rand < p);
end
tx_ind = find(tx);
end
if (strcmp(tx_method, 'zero_wait_global'))
% Run GZW algorithm
tx = theta(:, l) > 0;
p = p1;
if (coll == 2)
p = p2;
end
tx = tx .* (rand(N, 1) < p);
tx_ind = find(tx);
end
if (strcmp(tx_method, 'delta'))
% Run DELTA
if (l < 100)
% First 100 steps: random transmission (hot start)
tx = rand(1, N) < 1 / N;
if (sum(tx) == 1)
tx_ind = find(tx, 1);
end
else
if (coll == 0)
if (max(psi(:, l)) == 1)
% ZW phase
collision_steps = ones(1, N);
tx = state - 1;
else
% BT phase
p_tx = zeros(N, 1);
for n = 1 : N
if (theta(n, l) > 0)
p_tx(n) = 1;
% Compute belief
for j = 1 : N
if (j ~= n && psi(j, l) >= theta(n, l))
p_tx(n) = p_tx(n) * (1 - lambda(j)) ^ (psi(j, l) - theta(n, l) + 1);
end
end
end
end
if (sum(psi(:, l)) <= K)
% Reset to ZW phase
collision_steps = ones(1, N) * floor(K / N);
else
% Count possible collision steps
maxage = max(psi(:, l));
new_age = psi(:, l);
while (sum(psi(:, l)) - sum(new_age) < K)
maxage = maxage - 1;
new_age = min(psi(:, l), maxage);
end
collision_steps = psi(:, l) - new_age;
end
tx = p_tx > threshold;
end
else
if (coll == 1)
%CE phase
tx = colliders';
else
% CR phase
active = 1 - (1 - mean(lambda)) ^ (max(collision_steps));
tx_prob = optimize_cr(active, epsilon, N - coll_sequence, 0.0001);
tx = colliders' .* (rand(N, 1) < tx_prob);
end
end
tx_ind = find(tx);
% Correct maximum AoII
if (coll == 0)
if (sum(psi(:, l)) <= K)
psi(:, l) = zeros(N, 1);
else
maxage = max(psi(:, l));
new_age = psi(:, l);
while (sum(psi(:, l)) - sum(new_age) < K)
maxage = maxage - 1;
new_age = min(psi(:, l), maxage);
end
psi(:, l) = min(psi(:, l), maxage + 1);
end
end
end
end
if (strcmp(tx_method, 'max_age'))
% Run MAF algorithm
[~, tx] = max(psi(:, l));
tx_ind = tx(1);
end
if (strcmp(tx_method, 'round_robin'))
% Run RR algorithm
tx_ind = 1 + mod(l, N);
end
% CE successful: no transmission
if (coll == 1 && isempty(tx_ind))
coll = 0;
coll_sequence = 0;
end
% Only one transmitter
if (isscalar(tx_ind))
if (rand > epsilon)
% Successful transmission
state(tx_ind) = 1;
psi(tx_ind, l) = 0;
theta(tx_ind, l) = 0;
coll = max(0, coll - 1);
colliders(tx_ind) = 0;
coll_sequence = 0;
else
% Wireless channel error: NACK
colliders(tx_ind) = 1;
if (coll == 1)
coll_sequence = coll_sequence + 1;
end
coll = 2;
end
end
% More than one transmitter: NACK
if (length(tx_ind) > 1)
colliders(tx_ind) = 1;
if (coll == 1)
coll_sequence = coll_sequence + 1;
end
coll = 2;
end
end