-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSimulator.cpp
563 lines (496 loc) · 15 KB
/
Simulator.cpp
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
// main.cpp
// CSA_Simulator
// Created by Erik Sandgren on 21/12/15.
// Copyright © 2015 Erik Sandgren. All rights reserved.
#include <stdlib.h>
#include <iostream>
#include <ctime>
#include <cmath>
#include <fstream>
#include <string>
#include <vector>
#include <iomanip>
#include "Encoder.hpp"
#include "Node.hpp"
#include "Arrivals.hpp"
#include "Decoder.hpp"
#include "Parameters.hpp"
#include <assert.h>
using namespace std;
// Declaration of function to evaluate PLR for a certain load, defined further down...
void evalLoadFa(int index);
void evalLoadFs(int index);
void evalLoadSc(int index);
void writeToOutputFile();
vector<double> computeAverageOfCollectedStats (vector<vector<int> > vec);
int main () {
clock_t beginTime = clock();
if(saveDelays)
{
delays.resize(gLength, vector<int>(maximumDelay, 0));
}
if(saveAvgNumberOfIterations)
{
iterations.resize(gLength, vector<int>(maximumDelay, 0));
}
cout << "\n\n****************************************************************** \n";
switch(typeOfSimulation)
{
case FA_FNB:
cout << "* Frame Asynchronous Coded Slotted ALOHA (FA-FNB) simulation *\n";
break;
case FA_FB:
cout << "* Frame Asynchronous Coded Slotted ALOHA (FA-FB) simulation *\n";
break;
case FA_UB:
cout << "* Frame Asynchronous Coded Slotted ALOHA (FA-UB) simulation *\n";
break;
case FA_UNB:
cout << "* Frame Asynchronous Coded Slotted ALOHA (FA-UNB) simulation *\n";
break;
case FS:
cout << "* Frame Synchronous Coded Slotted ALOHA (FS) simulation *\n";
break;
case SC:
cout << "* Spatially Coupled Coded Slotted ALOHA (SC) simulation *\n";
break;
}
cout << "****************************************************************** \n\n";
cout << "n = " << n << "\n";
cout << "d = [";
for (int i = 0; i < q; i++)
{
if (i != q-1)
cout << d[i] <<", ";
else
cout << d[i];
}
cout << "]\n";
cout << "Lambda = [";
for (int i = 0; i < q; i++)
{
if (i != q-1)
cout << Lambda[i] <<", ";
else
cout << Lambda[i];
}
cout << "]\n";
cout << "Packets to simulate = " << numberOfPacketsToSimulateMax << "\n";
cout << "Please find the results in '" << fileName << "'\n\n";
cout << "**************************** Progress **************************** " << "\n\n";
cout << "Load \t \t Sent \t\t Lost \t \t PLR \n";
cout << "------------------------------------------------------------------ \n";
gIndex = gLength-1;
while(gIndex >= 0)
{
switch(typeOfSimulation)
{
case FA_FNB:
case FA_FB:
case FA_UB:
case FA_UNB:
evalLoadFa(gIndex);
break;
case FS:
evalLoadFs(gIndex);
break;
case SC:
evalLoadSc(gIndex);
break;
}
gIndex--;
}
writeToOutputFile();
clock_t endTime = clock();
double elapsedSeconds = double(endTime-beginTime)/CLOCKS_PER_SEC;
cout<< "Elapsed time is: " << elapsedSeconds << " seconds " << endl;
return 0;
}
// This function is used to evaluate the performance for the given parameters (loop is usually over g, the system load).
void evalLoadFa(int index)
{
int samp;
unsigned long int timeStep = 0;
Arrivals poiss = Arrivals(g[index]);
Encoder enc = Encoder(n, n_rx, d, Lambda,q);
Decoder dec = Decoder(numDecodingIterations, slotsBetweenDecoding, n, n_rx, maximumDelay);
vector<Node*> VN;
vector<Node*>CN(n + n_rx);
int sentPacketsCount = 0;
int lostPacketsCount = 0;
Node* tempCn;
Node* temp_VN;
// Initialize CNs
for (int i = 0; i < n_rx; i++) {
tempCn = new Node();
CN[i] = tempCn;
}
for (int i = n_rx; i < n_rx + n; i++) {
timeStep++;
tempCn = new Node(timeStep);
CN[i] = tempCn;
}
timeStep = 0;
// Running some steps without decoding in case the RX is not present at the start
if(typeOfSimulation == FA_FNB || typeOfSimulation == FA_UNB){
int timeZero = 2 * n_rx + n;
for (int i = 0; i < timeZero; i++) {
timeStep++;
// Create new CN and remove the oldest one.
tempCn = new Node(timeStep);
CN.push_back(tempCn);
tempCn = CN[0];
CN.erase(CN.begin());
tempCn -> letGoOffNeighbours();
delete tempCn;
//Create new VN and distribute its packets...
samp = poiss.sample();
for (int i = 0; i < samp; i++)
{
temp_VN = new Node(timeStep);
if(typeOfSimulation == FA_FNB || typeOfSimulation == FA_FB)
{
enc.distributeRepsFirstSlot(temp_VN, &CN);
}
else
{
enc.distributeRepsUniform(temp_VN, &CN);
}
VN.push_back(temp_VN);
}
}
// Find out which VNs are countable and which are not (of the ones joining before "i=0")
for (int i = 0; i < VN.size(); ++i)
{
temp_VN = VN[i];
int numNeighbours = temp_VN->getNumNeighbours();
for (int j = 0; j < numNeighbours; ++j)
{
tempCn = temp_VN->getNeighbour(j);
if(tempCn->getTimeOfArrival() >= timeZero - n && (!temp_VN->getCountable()))
{
temp_VN->setCountable();
}
}
}
}
unsigned long int startTime = timeStep;
while(sentPacketsCount < numberOfPacketsToSimulateMax && lostPacketsCount < numberOfPacketLossesToSimulateMax)
{
timeStep++;
int memorySize = timeStep - startTime > n_rx ? n_rx : (int)(timeStep - startTime);
// Create new CN and remove the oldest one.
tempCn = new Node(timeStep);
CN.push_back(tempCn);
tempCn = CN[0];
CN.erase(CN.begin());
tempCn -> letGoOffNeighbours();
delete tempCn;
//Create new VN and distribute its packets...
samp = poiss.sample();
for (int i = 0; i < samp; i++)
{
temp_VN = new Node(timeStep, true);
if(typeOfSimulation == FA_FNB || typeOfSimulation == FA_FB)
{
enc.distributeRepsFirstSlot(temp_VN, &CN);
}
else
{
enc.distributeRepsUniform(temp_VN, &CN);
}
VN.push_back(temp_VN);
}
dec.decode(&CN, &VN, memorySize, timeStep, index, iterations, saveAvgNumberOfIterations);
// Counting packets
if(typeOfSimulation == FA_FB || typeOfSimulation == FA_UB )
{
dec.countPacketsBoundaryEffect(&VN, timeStep);
}
else
{
dec.countPacketsNoBoundaryEffect(&VN, timeStep);
}
sentPacketsCount = dec.getSentPacketsCount();
lostPacketsCount = dec.getLostPacketsCount();
}
VN.clear();
CN.clear();
if (saveDelays)
{
delays.at(index) = dec.getDelays();
}
plr.at(index) = (double) lostPacketsCount / (double) sentPacketsCount;
streamsize normal_prec = cout.precision();
cout << setprecision(3) << fixed;
cout << g[index] << " \t \t " <<
setw((int) log10(numberOfPacketsToSimulateMax) + 1)
<< sentPacketsCount << " \t \t"
<< setw( (int) log10(numberOfPacketLossesToSimulateMax) + 1 )
<< lostPacketsCount << " \t \t " << scientific << plr.at(index) << endl;
cout << setprecision(normal_prec);
}
void evalLoadFs(int index){
n_rx = n;
int samp;
unsigned long int timeStep = 0;
Arrivals poiss = Arrivals(g[index]);
Encoder enc = Encoder(n, n_rx, d, Lambda, q);
Decoder dec = Decoder(numDecodingIterations, slotsBetweenDecoding, n, n_rx, maximumDelay);
vector<Node*> VN;
vector<Node*> VNForNextFrame;
vector<Node*>CN(2 * n);
int sentPacketsCount = 0;
int lostPacketsCount = 0;
Node* tempCN;
Node* tempVN;
// Initialize CNs
for (int i = 0; i < 2 * n; i++)
{
timeStep++;
tempCN = new Node(timeStep);
CN[i] = tempCN;
}
// time_step=;
while(sentPacketsCount < numberOfPacketsToSimulateMax && lostPacketsCount < numberOfPacketLossesToSimulateMax)
{
timeStep++;
//Create new VN and wait.
samp = poiss.sample();
for (int i = 0; i < samp; i++)
{
tempVN = new Node(timeStep);
VNForNextFrame.push_back(tempVN);
}
if(saveDelays){dec.decode(&CN, &VN, n_rx, timeStep, index, iterations, saveAvgNumberOfIterations);}
if ( (timeStep % n) == 0 )
{
if(!saveDelays){
dec.decodeFrame(&CN, &VN, timeStep);
}
dec.countPacketsFS(&VN, timeStep);
sentPacketsCount = dec.getSentPacketsCount();
lostPacketsCount = dec.getLostPacketsCount();
for (int i = 0; i < VNForNextFrame.size(); i++)
{
tempVN = VNForNextFrame.at(i);
enc.distributeRepsUniform(tempVN, &CN);
VN.push_back(tempVN);
}
VNForNextFrame.clear();
}
// Create new CN and remove the oldest one.
tempCN = new Node(timeStep);
CN.push_back(tempCN);
tempCN = CN[0];
CN.erase(CN.begin());
tempCN -> letGoOffNeighbours();
delete tempCN;
}
VN.clear();
CN.clear();
plr[index] = (double) lostPacketsCount / (double) sentPacketsCount;
if(saveDelays){
delays.at(index) = dec.getDelays();
}
streamsize normal_prec = cout.precision();
cout<<setprecision(3)<<fixed;
cout << g[index] << " \t \t "
<< setw((int)log10(numberOfPacketsToSimulateMax) + 1)
<< sentPacketsCount << " \t \t"
<< setw((int)log10(numberOfPacketLossesToSimulateMax) + 1)
<< lostPacketsCount << " \t \t "<< scientific<<plr[index] << endl;
cout << setprecision(normal_prec);
};
void evalLoadSc(int index)
{
assert(1 == sizeof(d)/sizeof(int));
int rep = d[0];
assert((n % rep) == 0);
int nSubFrame = n / rep;
n_rx = 5 * rep * nSubFrame; // makes the memory "sufficiently" large
int samp;
unsigned long int timeStep = 0;
Arrivals poiss = Arrivals(g[index]);
Encoder enc = Encoder(nSubFrame, n_rx, d, Lambda, q);
Decoder dec = Decoder(numDecodingIterations, slotsBetweenDecoding, nSubFrame, n_rx, maximumDelay);
vector<Node*> VN;
vector<Node*> VNForNextFrame;
vector<Node*> CN(rep * nSubFrame + n_rx);
int sentPacketsCount = 0;
int lostPacketsCount = 0;
Node* tempCN;
Node* tempVN;
// Initialize CNs
for (int i = 0; i < n_rx; i++)
{
tempCN = new Node();
CN[i] = tempCN;
}
for (int i = n_rx; i < n_rx + rep * nSubFrame; i++)
{
timeStep++;
tempCN = new Node(timeStep);
CN[i] = tempCN;
}
timeStep = 0;
while(sentPacketsCount < numberOfPacketsToSimulateMax && lostPacketsCount < numberOfPacketLossesToSimulateMax) {
// Arrivals at this time step, saved in the VNs_for_next_frame.
samp = poiss.sample();
for (int i = 0; i < samp; i++)
{
tempVN = new Node(timeStep);
VNForNextFrame.push_back(tempVN);
}
timeStep++;
// If its time for a new frame, we let the activated VNs distribute their 'd' replicas...
if( (timeStep % nSubFrame) == 0 )
{
for (int i = 0; i < VNForNextFrame.size();i++)
{
enc.distributeRepsSC(VNForNextFrame[i], &CN);
}
VN.insert(VN.end(), VNForNextFrame.begin(), VNForNextFrame.end());
VNForNextFrame.clear();
}
// Shifts in a new node at the back of the memory and removes the node at the front (the oldest one)
tempCN = new Node(timeStep + rep * nSubFrame);
CN.push_back(tempCN);
tempCN = CN[0];
tempCN -> letGoOffNeighbours();
CN.erase(CN.begin());
delete tempCN;
dec.decode(&CN, &VN, n_rx, timeStep, index, iterations, saveAvgNumberOfIterations);
// Counting packets: different method depending on the time_initial variable
dec.countPacketsSC(&VN, timeStep, rep);
sentPacketsCount = dec.getSentPacketsCount();
lostPacketsCount = dec.getLostPacketsCount();
}
VN.clear();
CN.clear();
if (saveDelays)
{
delays.at(index) = dec.getDelays();
}
plr .at(index) = (double) lostPacketsCount / (double) sentPacketsCount;
streamsize normal_prec = cout.precision();
cout<<setprecision(3) << fixed;
cout<< g[index] << " \t \t "<< setw( (int) log10(numberOfPacketsToSimulateMax) + 1)
<< sentPacketsCount << " \t \t"
<< setw( (int) log10(numberOfPacketLossesToSimulateMax) + 1)
<< lostPacketsCount << " \t \t "<< scientific << plr.at(index) << endl;
cout << setprecision(normal_prec);
}
void writeToOutputFile(){
///////// WRITES TO OUTPUT ////////////////////////
// where to save the output and by which name:
ofstream outputFile;
outputFile.open(fileName);
outputFile << "Lambda: ";
for (int i = 0; i < q ; i++) {
outputFile << Lambda[i] << " ";
}
outputFile << endl;
outputFile << "degrees: ";
for (int i = 0; i < q ; i++) {
outputFile << d[i] << " ";
}
outputFile << endl;
outputFile << "n: " << n << endl;
outputFile<< "n_rx: " << n_rx << endl;
outputFile<< "maximum delay: "<< maximumDelay << endl;
outputFile << "Type of simulation: ";
switch(typeOfSimulation){
case FA_FNB:
outputFile << "FA_FNB" << endl;
break;
case FA_FB:
outputFile << "FA_FB" << endl;
break;
case FA_UNB:
outputFile << "FA_UNB" << endl;
break;
case FA_UB:
outputFile << "FA_UB" << endl;
break;
case FS:
outputFile << "FS" << endl;
break;
case SC:
outputFile << "SC" << endl;
break;
}
outputFile << "Max number of packets to simulate: " << numberOfPacketsToSimulateMax << endl;
outputFile << "Max number of losses to simulate: " << numberOfPacketLossesToSimulateMax << endl;
outputFile << endl;
outputFile << endl;
outputFile << endl;
outputFile << endl;
outputFile << "Load \t PLR" << endl;
for (int i = gLength - 1; i >= 0; i--)
{
outputFile << g[i] << "\t" << plr [i] << "\\\\" << endl;
}
outputFile << endl;
outputFile << endl;
outputFile << "Delay PDF (one row per load, not normalized)" << endl;
if (saveDelays)
{
for (int i=0; i<gLength; i++)
{
for (int ii=0; ii < maximumDelay; ii++)
{
outputFile<< delays[i][ii]<<" ";
}
outputFile <<"\\\\"<<endl;
}
}
outputFile << endl;
outputFile << "Avg delay for each simulated load:" << endl;
vector<double> avgDelay = computeAverageOfCollectedStats(delays);
for (int i=0; i < avgDelay.size(); i++)
{
outputFile<< avgDelay[i]<<" ";
}
outputFile << endl;
outputFile << endl;
outputFile << endl;
outputFile << "Iterations PDF (one row per load, not normalized)" << endl;
if (saveAvgNumberOfIterations)
{
for (int i=0; i<gLength; i++)
{
for (int ii=0; ii < maximumDelay; ii++)
{
outputFile<< iterations[i][ii]<<" ";
}
outputFile <<"\\\\"<<endl;
}
}
outputFile << endl;
outputFile << "Avg number of iterations per slot for each simulated load:" << endl;
vector<double> avgIteration = computeAverageOfCollectedStats(iterations);
for (int i=0; i < avgIteration.size(); i++)
{
outputFile<< avgIteration[i]<<" ";
}
outputFile << endl;
outputFile.close();
}
vector<double> computeAverageOfCollectedStats (vector<vector<int> > vec)
{
vector<double> result;
for (int i = 0; i < vec.size() ; i++)
{
int numEntries = 0;
int total = 0;
for (int j = 0; j < vec[0].size(); j++)
{
total += vec[i][j] * j;
numEntries += vec[i][j];
}
double avg = double(total)/double(numEntries);
result.push_back(avg);
}
return result;
}