-
Notifications
You must be signed in to change notification settings - Fork 0
/
SALSA_FCFS.m
250 lines (205 loc) · 7.68 KB
/
SALSA_FCFS.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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% © 2022 Luxembourg Institute of Science and Technology. All Rights Reserved.
% Author: Mohammad Afhamisis @LIST
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
clear all
clc
format long
tic
disp('Initializing ...')
%% declaration and initials
import java.util.TimeZone
%suppose "ToA=2.7935s","Gaurd_Time=20ms", "51Bytes","125KHz"
ToA=2.7935;
Gaurd_Time=0.020;
ToA_t=ToA+Gaurd_Time; % Total Time on Air.
Duty_Cycle=1;%
% receive the number of ED and then check to be a number
inp = input('Please enter the number of end devices:');
while (isnumeric(inp) == false)
disp('It is not a number. Wrong entry')
inp = input('Please enter the number of end devices:');
end
number_of_ed=inp;
% other initial variables
counter=zeros(number_of_ed,2);
overall_mat=zeros(25,8);
cnt=0;
id_cnt=0;
disp('Reading the files, converting to array and creating visibility time tables ...')
%% reading files, converting them to array (in utc human-readable format) and create visibility timetable (to store utc epoch format)
for i=1:number_of_ed
% creating the inputs from files
eval(['x' num2str(i) '=readtable("cdata/' num2str(i) '.txt");'])
% converting table to array
eval(['x' num2str(i) '=x' num2str(i) '{:,:};'])
% create two column table for visibility each time timetable
eval(['dt_x' num2str(i) '=zeros(size(x' num2str(i) ',1)/3,2);'])
end
disp('Converting timetables to utc epoch format ...')
%% conversion from human-readable format to utc epoch format
for j=1:number_of_ed
ex=eval(['x' num2str(j)]);
es=size(ex,1);
ed=eval(['dt_x' num2str(j)]);
for i=1:es/3
ds=datestr(ex(3*(i-1)+1,:));
dt = datetime(ds,'TimeZone','utc');
eval(['dt_x' num2str(j) '(' num2str(i) ',1)=posixtime(dt);'])
ds=datestr(ex(3*(i-1)+3,:));
dt = datetime(ds,'TimeZone','utc');
eval(['dt_x' num2str(j) '(' num2str(i) ',2)=posixtime(dt);'])
end
end
disp('Creating the main matrix ...')
%% creating the main matrix "overall_mat"
for i=1:number_of_ed
id_cnt=id_cnt+1;
eval(['s=size(dt_x' num2str(i) ',1);'])
eval(['overall_mat(cnt+1:cnt+s,1:2)=dt_x' num2str(i) ';'])
eval(['overall_mat(cnt+1:cnt+s,3)=id_cnt;'])
cnt=cnt+s;
end
disp('Sorting based on the time ...')
%% sorting based on their timing (order: see the satellite earlier)
overall_mat=sortrows(overall_mat);
for i=1:size(overall_mat,1)
overall_mat(i,4)=i;
end
disp('Implementing the scheduling technique ...')
%% scheduling technique and filling the places - main body
%%%% FCFS Policy
% initialise %%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% for the first one %%%%%%%%%%%%%%%%%%%%%%
overall_mat(1,5)=overall_mat(1,1);
overall_mat(1,6)=overall_mat(1,1)+ToA_t;
counter(overall_mat(1,3),1)=counter(overall_mat(1,3),1)+1;
ind=1; % index of last success
% for others %%%%%%%%%%%%%%%%%%%%%%%%%%%%%
for i=2:size(overall_mat,1)
if overall_mat(i-1,6)>0
if (overall_mat(i,1)>overall_mat(i-1,6))
overall_mat(i,5)=overall_mat(i,1);
overall_mat(i,6)=overall_mat(i,1)+ToA_t;
counter(overall_mat(i,3),1)=counter(overall_mat(i,3),1)+1;
ind=i;
else
if overall_mat(i,2)>(overall_mat(i-1,6)+ToA_t)
overall_mat(i,5)=overall_mat(i-1,6)+0.001;
overall_mat(i,6)=overall_mat(i,5)+ToA_t;
counter(overall_mat(i,3),1)=counter(overall_mat(i,3),1)+1;
ind=i;
else
counter(overall_mat(i,3),2)=counter(overall_mat(i,3),2)+1;
end
end
else
if (overall_mat(i,1)>overall_mat(ind,6))
overall_mat(i,5)=overall_mat(i,1);
overall_mat(i,6)=overall_mat(i,1)+ToA_t;
counter(overall_mat(i,3),1)=counter(overall_mat(i,3),1)+1;
ind=i;
else
if overall_mat(i,2)>(overall_mat(ind,6)+ToA_t)
overall_mat(i,5)=overall_mat(ind,6)+0.001;
overall_mat(i,6)=overall_mat(i,5)+ToA_t;
counter(overall_mat(i,3),1)=counter(overall_mat(i,3),1)+1;
ind=i;
else
counter(overall_mat(i,3),2)=counter(overall_mat(i,3),2)+1;
end
end
end
end
disp('Calculating the performance')
%% performance calculations- perft total and perfi individual
perft=(sum(counter(:,1))-sum(counter(:,2)))/sum(counter(:,1));
perfi=zeros(number_of_ed,1);
for i=1:number_of_ed
perfi(i)=(counter(i,1)-counter(i,2))/counter(i,1);
end
disp('Feature 1: Implementing FCFS ...')
%% FCFS
poor_ed=find(overall_mat(:,5)==0);
k=size(poor_ed,1);
for i=1:k
A=overall_mat(poor_ed(k),1);
B=overall_mat(poor_ed(k),2);
C=overall_mat(:,5);
D=overall_mat(:,6);
fp=find(B>C & B>D & A<C & A<D);
sfp=size(fp,1);
new_overall_mat=zeros(sfp,4);
for j=1:sfp
new_overall_mat(j,1:2)=overall_mat(fp(j),5:6);
new_overall_mat(j,3)=counter(overall_mat(fp(j),3),1);
new_overall_mat(j,4)=fp(j);
end
king=find(new_overall_mat(:,3)==max(new_overall_mat(:,3)>1) & new_overall_mat(:,1)==min(new_overall_mat(:,1)));
if isempty(king)==0
kind_address=new_overall_mat(king,4);
overall_mat(poor_ed(i),5:6)=overall_mat(king,5:6);
overall_mat(king,5:6)=[0,0];
counter(overall_mat(kind_address,3),1)=counter(overall_mat(kind_address,3),1)-1;
counter(overall_mat(kind_address,3),2)=counter(overall_mat(kind_address,3),2)+1;
counter(overall_mat(poor_ed(i),3),1)=counter(overall_mat(poor_ed(i),3),1)+1;
counter(overall_mat(poor_ed(i),3),2)=counter(overall_mat(poor_ed(i),3),2)-1;
end
clear new_overall_mat
end
disp('Feature 2: Increasing the chance of transmission for the 2nd or third time per satellite visibility ...')
%% Optimal Scheduling
hope_mat=zeros(number_of_ed,2);
hope_expected=zeros(1,2);
collision_found=0;
helped=0;
couldnot_help=0;
for i=1:number_of_ed
if (overall_mat(i,5)~=0)
hope_mat(i,1)=overall_mat(i,2)-overall_mat(i,5);
if (hope_mat(i,1)>((100/Duty_Cycle)+1)*ToA_t)
hope_mat(i,2)=1;
hope_expected(1,:)=[overall_mat(i,5)+100*Duty_Cycle,overall_mat(i,6)+100*Duty_Cycle];
for j=1:number_of_ed
if (j~=i)
A=hope_expected(1,1);
B=hope_expected(1,2);
C=overall_mat(j,5);
D=overall_mat(j,6);
if (A<D & B>C)==1
collision_found=1;
couldnot_help=couldnot_help+1;
end
end
end
if collision_found==0
k=size(overall_mat,1);
overall_mat(k+1,5)=A;
overall_mat(k+1,6)=B;
overall_mat(k+1,1:3)=overall_mat(i,1:3);
overall_mat(k+1,4)=overall_mat(k,4)+1;
counter(i,1)=counter(i,1)+1;
helped=helped+1;
end
end
end
end
caption=[' >>>>', num2str(helped), ' times helped to have more chances and ', num2str(couldnot_help), ' could not be helped because of collisions'];
disp(caption)
%% counting the runtime with tic in the top and toc here
toc
disp('Drawing the histogram of the output')
histogram(counter(:,1))
xlim([0 40])
%% performance calculations
disp('Calculating the performance - Number of TX per device')
disp('average')
xp1=mean(counter(:,1))
disp('minimum')
xp2=min(counter(:,1))
disp('maximum')
xp3=max(counter(:,1))
disp('Sent - Success')
sum(counter(:,1))
disp('Couldnot send')
sum(counter(:,2))