-
Notifications
You must be signed in to change notification settings - Fork 2
/
multi_layer_net.cpp
735 lines (587 loc) · 18.4 KB
/
multi_layer_net.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
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
#include<iostream>
#include<sstream>
#include<fstream>
#include<vector>
#include<list>
#include<iterator>
#include<algorithm>
#include<numeric>
using namespace std;
enum Disease_State {
S, // Susceptible.
I // Infected.
};
enum Awareness_State {
A, // Aware.
U // Unaware.
};
template <typename Iter, typename Cont>
bool isLast(Iter iter, const Cont& cont)
{
return (iter != cont.end()) && (next(iter) == cont.end());
}
class MultiLayerNetwork {
/**
* Undirected two layer network class.
*/
private:
// Number of nodes.
int N;
// Adjacency lists.
vector<list<int>>* disease_net;
vector<list<int>>* awareness_net;
// Network's name.
string network_name;
public:
MultiLayerNetwork(int n_nodes) {
N = n_nodes;
disease_net = new vector<list<int>>(N);
awareness_net = new vector<list<int>>(N);
network_name = "Default";
}
MultiLayerNetwork(string file_name, string net_name) {
/**
* Loads network from file.
* (Both networks will have the same topology)
*
*
* Arguments:
* - file_name: network file name.
* - net_name: network name.
*/
ifstream file("networks/" + file_name + ".txt");
if (file.is_open()) {
string line;
// Read number of nodes.
getline(file, line);
istringstream iss(line);
if (!(iss >> N)) {
throw runtime_error("Error parsing file.");
}
disease_net = new vector<list<int>>(N);
awareness_net = new vector<list<int>>(N);
// Read edges.
while (getline(file, line)) {
istringstream iss(line);
int n1, n2;
if (!(iss >> n1 >> n2)) {
throw runtime_error("Error parsing file.");
}
this->addEdge(n1, n2, disease_net);
this->addEdge(n1, n2, awareness_net);
}
file.close();
}
network_name = net_name;
}
MultiLayerNetwork(string file_name_1, string file_name_2, string net_name) {
/**
* Loads network from file.
* (Different networks' topology)
*
*
* Arguments:
* - file_name_1: file name with disease network description.
* - file_name_2: file name with awareness network description.
* - net_name: network name.
*/
ifstream file("networks/" + file_name_1 + ".txt");
if (file.is_open()) {
string line;
// Read number of nodes.
getline(file, line);
istringstream iss(line);
if (!(iss >> N)) {
throw runtime_error("Error parsing file.");
}
disease_net = new vector<list<int>>(N);
// Read edges.
while (getline(file, line)) {
istringstream iss(line);
int n1, n2;
if (!(iss >> n1 >> n2)) {
throw runtime_error("Error parsing file 1.");
}
this->addEdge(n1, n2, disease_net);
}
file.close();
}
ifstream file_2("networks/" + file_name_2 + ".txt");
if (file_2.is_open()) {
string line;
// Read number of nodes.
getline(file_2, line);
istringstream iss(line);
if (!(iss >> N)) {
throw runtime_error("Error parsing file 2.");
}
awareness_net = new vector<list<int>>(N);
// Read edges.
while (getline(file_2, line)) {
istringstream iss(line);
int n1, n2;
if (!(iss >> n1 >> n2)) {
throw runtime_error("Error parsing file 2.");
}
this->addEdge(n1, n2, awareness_net);
}
file_2.close();
}
network_name = net_name;
}
~MultiLayerNetwork() {
delete disease_net;
delete awareness_net;
}
void addEdge(int n1, int n2, vector<list<int>>* adj_list) {
adj_list->at(n1).push_back(n2);
adj_list->at(n2).push_back(n1);
}
void printNetworkInfo() {
/**
* Prints network info.
* (Number of nodes and adjacency list)
*/
cout << "Network info:" << endl;
cout << "N=" << N << endl;
cout << "Disease spreading network:" << endl;
for (int i=0; i < N; i++) {
cout << "Node " << i << ": [";
list<int> :: iterator it;
for (it = disease_net->at(i).begin(); it != disease_net->at(i).end(); it++) {
if (isLast(it, disease_net->at(i))) {
cout << *it << "]";
} else {
cout << *it << ",";
}
}
cout << endl;
}
cout << "Awareness spreading network:" << endl;
for (int i=0; i < N; i++) {
cout << "Node " << i << ": [";
list<int> :: iterator it;
for (it = awareness_net->at(i).begin(); it != awareness_net->at(i).end(); it++) {
if (isLast(it, awareness_net->at(i))) {
cout << *it << "]";
} else {
cout << *it << ",";
}
}
cout << endl;
}
}
void printNodesStates(vector<Disease_State> disease_states,
vector<Awareness_State> awareness_states) {
/**
* Prints nodes state.
* (Used in the simulation context)
*/
for (int i=0; i < N; i++) {
cout << i << ":";
if (disease_states[i] == Disease_State::S) {
cout << "(S,";
} else if (disease_states[i] == Disease_State::I) {
cout << "(I,";
} else {
throw "Unknown disease state";
}
if (awareness_states[i] == Awareness_State::U) {
cout << "U) ";
} else if (awareness_states[i] == Awareness_State::A) {
cout << "A) ";
} else {
throw "Unknown awareness state";
}
}
cout << endl;
}
void simulateSI(int numSim,
int T,
int gamma,
int phi,
double beta,
double mu,
double lambda,
bool verbose,
bool writeStatesToFile,
int writeToFileStep,
bool writeAwarenessRatiosToFile) {
/**
* Simulates SI epidemic spreading process
* (two layer network scenario).
*
* Arguments:
* - numSim: the number of simulations.
*
* - T: total number of time steps.
*
* - gamma: size of initial infected population.
*
* - phi: size of initial aware population,
* excluding the initially infected nodes
* (because infected are always aware).
*
* - beta: infection rate.
*
* - mu: awareness spread rate.
*
* - lambda: Value between 0 and 1 (percentage)
* representing how much beta will decrease
* if the node is in the (S,A) state.
* I.e., for (S,A) node:
* probability of getting infected = beta*(1-lambda).
*
* - verbose: whether to print simulation.
*
* - writeStatesToFile: wheter to write states
* at each writeStatesToFileStep.
*
* - writeToFileStep: file writing step.
*
* - writeAwarenessRatiosToFile: whether to write to a file the
* averaged percentage of aware nodes
* for each timestep.
*/
int infected_counter;
int aware_counter;
list<int> :: iterator it;
// Store the disease state for each node.
vector<Disease_State> disease_states(N);
// Store the awareness state for each node.
vector<Awareness_State> awareness_states(N);
// Store nodes that will transit to infected at the next timestep.
vector<int> to_infect;
// Store nodes that will transit to aware at the next timestep.
vector<int> to_aware;
// Infected average ratio per time-step.
vector<double> infected_ratios(T, 0.0);
// Awareness average ratio per time-step.
vector<double> awareness_ratios(T, 0.0);
for (int sim=0; sim < numSim; sim++) {
// Reset all nodes to (S,U).
fill(disease_states.begin(), disease_states.end(), Disease_State::S);
fill(awareness_states.begin(), awareness_states.end(), Awareness_State::U);
// Setup initial infected population.
// Randomly pick gamma individuals to be initially infected.
// Note that infected nodes are always aware.
// Initialize (I,A) nodes.
vector<int> dummy(N);
iota(begin(dummy), end(dummy), 0);
random_shuffle(dummy.begin(), dummy.end());
for (int i=0; i < gamma; i++) {
disease_states[dummy[i]] = Disease_State::I;
awareness_states[dummy[i]] = Awareness_State::A;
}
// Setup initially aware but not infected nodes.
// Initialize (S,A) nodes.
for (int i=gamma; i < (gamma + phi); i++) {
awareness_states[dummy[i]] = Awareness_State::A;
}
for (int t=0; t < T; t++) {
if (verbose) {
// Print states.
cout << "t=" << t << endl;
this->printNodesStates(disease_states, awareness_states);
}
if (writeStatesToFile && (t % writeToFileStep == 0)) {
// Write disease states to file.
ofstream file;
file.open ("output/" + network_name + "_disease_states.csv", ios_base::app);
for (int i=0; i < (N-1); i++) {
file << disease_states[i] << ",";
}
file << disease_states[N-1];
file << "\n";
file.close();
// Write awareness states to file.
file.open ("output/" + network_name + "_awareness_states.csv", ios_base::app);
for (int i=0; i < (N-1); i++) {
file << awareness_states[i] << ",";
}
file << awareness_states[N-1];
file << "\n";
file.close();
}
infected_counter = 0;
aware_counter = 0;
for (int node=0; node < N; node++) {
if (disease_states[node] == Disease_State::I) {
infected_counter++;
}
if (awareness_states[node] == Awareness_State::A) {
aware_counter++;
}
}
infected_ratios[t] += ((float) infected_counter / (float) N) / (float) numSim;
if (writeAwarenessRatiosToFile) {
awareness_ratios[t] += ((float) aware_counter / (float) N) / (float) numSim;
}
for (int node=0; node < N; node++) {
// Disease network transitions.
if (disease_states[node] == Disease_State::S) {
for (it = disease_net->at(node).begin(); it != disease_net->at(node).end(); it++) {
if (disease_states[*it] == Disease_State::I) {
if (awareness_states[node] == Awareness_State::U) {
// (S,U) - Susceptible and unaware.
if (((double) rand() / (RAND_MAX)) < beta) {
to_infect.push_back(node);
to_aware.push_back(node);
}
break;
} else {
// (S,A) - Susceptible and aware.
if (((double) rand() / (RAND_MAX)) < (beta*(1-lambda))) {
to_infect.push_back(node);
to_aware.push_back(node);
}
break;
}
}
}
}
// Awareness network transitions.
if (awareness_states[node] == Awareness_State::U) {
// (S,U) - Susceptible and unaware.
for (it = awareness_net->at(node).begin(); it != awareness_net->at(node).end(); it++) {
if (awareness_states[*it] == Awareness_State::A) {
if (((double) rand() / (RAND_MAX)) < mu) {
to_aware.push_back(node);
}
break;
}
}
}
}
// Update states (susceptible -> infected).
for (int i=0; i < to_infect.size(); i++) {
disease_states[to_infect[i]] = Disease_State::I;
}
to_infect.clear();
// Update states (unaware -> aware).
for (int i=0; i < to_aware.size(); i++) {
awareness_states[to_aware[i]] = Awareness_State::A;
}
to_aware.clear();
}
}
// Write infected ratios to file.
ofstream file;
file.open ("output/" + network_name + "_infected_ratios.csv");
for (int i=0; i < (T-1); i++) {
file << infected_ratios[i] << ",";
}
file << infected_ratios[T-1];
file << "\n";
file.close();
if (writeAwarenessRatiosToFile) {
file.open ("output/" + network_name + "_awareness_ratios.csv");
for (int i=0; i < (T-1); i++) {
file << awareness_ratios[i] << ",";
}
file << awareness_ratios[T-1];
file << "\n";
file.close();
}
}
void simulateSIS(int numSim,
int T,
int gamma,
int phi,
double beta,
double delta,
double mu,
double omega,
double lambda,
bool verbose,
bool writeStatesToFile,
int writeToFileStep) {
/**
* Simulates SIS epidemic spreading process
* (two layer network scenario).
*
* Arguments:
* - numSim: the number of simulations.
*
* - T: total number of time steps.
*
* - gamma: size of initial infected population.
*
* - phi: size of initial aware population,
* excluding the initially infected nodes
* (because infected are always aware).
*
* - beta: infection rate.
*
* - delta: recovery rate.
*
* - mu: awareness spread rate.
*
* - omega: forgetness rate
* (how fast susceptible and aware nodes
* go back to susceptible and unaware).
*
* - lambda: Value between 0 and 1 (percentage)
* representing how much beta will decrease
* if the node is in the (S,A) state.
* I.e., for (S,A) node:
* probability of getting infected = beta*(1-lambda).
*
* - verbose: whether to print simulation.
*
* - writeStatesToFile: wheter to write states
* at each writeStatesToFileStep.
*
* - writeToFileStep: file writing step.
*/
int counter;
list<int> :: iterator it;
// Store the disease state for each node.
vector<Disease_State> disease_states(N);
// Store the awareness state for each node.
vector<Awareness_State> awareness_states(N);
// Store nodes that will transit to infected at the next timestep.
vector<int> to_infect;
// Store nodes that will transit to susceptible state at the next timestep.
vector<int> to_susceptible;
// Store nodes that will transit to aware at the next timestep.
vector<int> to_aware;
// Store nodes that will transit to unaware at the next timestep.
vector<int> to_unaware;
// Infected average ratio per time-step.
vector<double> infected_ratios(T, 0.0);
for (int sim=0; sim < numSim; sim++) {
// Reset all nodes to (S,U).
fill(disease_states.begin(), disease_states.end(), Disease_State::S);
fill(awareness_states.begin(), awareness_states.end(), Awareness_State::U);
// Setup initial infected population.
// Randomly pick gamma individuals to be initially infected.
// Note that infected nodes are always aware.
// Initialize (I,A) nodes.
vector<int> dummy(N);
iota(begin(dummy), end(dummy), 0);
random_shuffle(dummy.begin(), dummy.end());
for (int i=0; i < gamma; i++) {
disease_states[dummy[i]] = Disease_State::I;
awareness_states[dummy[i]] = Awareness_State::A;
}
// Setup initially aware but not infected nodes.
// Initialize (S,A) nodes.
for (int i=gamma; i < (gamma + phi); i++) {
awareness_states[dummy[i]] = Awareness_State::A;
}
for (int t=0; t < T; t++) {
if (verbose) {
// Print states.
cout << "t=" << t << endl;
this->printNodesStates(disease_states, awareness_states);
}
if (writeStatesToFile && (t % writeToFileStep == 0)) {
// Write disease states to file.
ofstream file;
file.open ("output/" + network_name + "_disease_states.csv", ios_base::app);
for (int i=0; i < (N-1); i++) {
file << disease_states[i] << ",";
}
file << disease_states[N-1];
file << "\n";
file.close();
// Write awareness states to file.
file.open ("output/" + network_name + "_awareness_states.csv", ios_base::app);
for (int i=0; i < (N-1); i++) {
file << awareness_states[i] << ",";
}
file << awareness_states[N-1];
file << "\n";
file.close();
}
counter = 0;
for (int node=0; node < N; node++) {
if (disease_states[node] == Disease_State::I) {
counter++;
}
}
infected_ratios[t] += ((float) counter / (float) N) / (float) numSim;
for (int node=0; node < N; node++) {
// Disease network transitions.
if (disease_states[node] == Disease_State::S) {
// Susceptible node.
for (it = disease_net->at(node).begin(); it != disease_net->at(node).end(); it++) {
if (disease_states[*it] == Disease_State::I) {
if (awareness_states[node] == Awareness_State::U) {
// (S,U) - Susceptible and unaware.
if (((double) rand() / (RAND_MAX)) < beta) {
to_infect.push_back(node);
to_aware.push_back(node);
}
break;
} else {
// (S,A) - Susceptible and aware.
if (((double) rand() / (RAND_MAX)) < (beta*(1-lambda))) {
to_infect.push_back(node);
to_aware.push_back(node);
}
break;
}
}
}
} else {
// Infected node.
if (((double) rand() / (RAND_MAX)) < delta) {
to_susceptible.push_back(node);
}
}
// Awareness network transitions.
if (awareness_states[node] == Awareness_State::U) {
// (S,U) - Susceptible and unaware.
for (it = awareness_net->at(node).begin(); it != awareness_net->at(node).end(); it++) {
if (awareness_states[*it] == Awareness_State::A) {
if (((double) rand() / (RAND_MAX)) < mu) {
to_aware.push_back(node);
}
break;
}
}
} else {
// Aware node.
if (disease_states[node] == Disease_State::S) {
// (S,A) - Susceptible and aware.
if (((double) rand() / (RAND_MAX)) < omega) {
to_unaware.push_back(node);
}
}
}
}
// Update states (susceptible -> infected).
for (int i=0; i < to_infect.size(); i++) {
disease_states[to_infect[i]] = Disease_State::I;
}
to_infect.clear();
// Update states (infected -> susceptible).
for (int i=0; i < to_susceptible.size(); i++) {
disease_states[to_susceptible[i]] = Disease_State::S;
}
to_susceptible.clear();
// Update states (unaware -> aware).
for (int i=0; i < to_aware.size(); i++) {
awareness_states[to_aware[i]] = Awareness_State::A;
}
to_aware.clear();
// Update states (aware -> unaware).
for (int i=0; i < to_unaware.size(); i++) {
awareness_states[to_unaware[i]] = Awareness_State::U;
}
to_unaware.clear();
}
}
// Write infected ratios to file.
ofstream file;
file.open ("output/" + network_name + "_infected_ratios.csv");
for (int i=0; i < (T-1); i++) {
file << infected_ratios[i] << ",";
}
file << infected_ratios[T-1];
file << "\n";
file.close();
}
};