-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSIR.cpp
More file actions
74 lines (57 loc) · 2.32 KB
/
SIR.cpp
File metadata and controls
74 lines (57 loc) · 2.32 KB
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
#include "simulations.hpp"
using namespace mpark::patterns;
using namespace std;
void simulate_SIR(Graph graph, double beta, double gamma, int initial_infected,
int initial_resitant, ofstream &output_file) {
const int N = graph.nodes.size();
for (int i = 0; i < initial_infected; i++)
graph.nodes.at(i) = Node::INFECTED;
for (int i = initial_infected; i < initial_infected + initial_resitant;
i++)
graph.nodes.at(i) = Node::RESISTANT;
for (int i = initial_infected + initial_resitant; i < N; i++)
graph.nodes.at(i) = Node::SUSCEPTIBLE;
long step = 0;
int infected = initial_infected;
int resistant = initial_resitant;
int node_i_neighbors, j;
bool should_infect, should_resist;
Node *node_i, *node_j;
while (step < 100) {
// iterate through all nodes
for (int i = 0; i < N; ++i) {
node_i_neighbors = graph.adj_list.at(i).size();
if (node_i_neighbors == 0) continue;
j = graph.adj_list.at(i).at(rand() % node_i_neighbors).to;
should_infect = ((double)rand() / RAND_MAX) < beta;
should_resist = ((double)rand() / RAND_MAX) < gamma;
node_i = &graph.nodes.at(i);
node_j = &graph.nodes.at(j);
if (should_resist && *node_i == Node::INFECTED) {
*node_i = Node::RESISTANT;
resistant++;
infected--;
} else {
match(*node_i, *node_j)(
pattern(Node::SUSCEPTIBLE, Node::INFECTED) =
[&, node_i] {
WHEN(should_infect) {
*node_i = Node::INFECTED;
infected++;
};
},
pattern(Node::INFECTED, Node::SUSCEPTIBLE) =
[&, node_j] {
WHEN(should_infect) {
*node_j = Node::INFECTED;
infected++;
};
},
pattern(_, _) = [] {});
}
}
output_file << step << "," << N - infected - resistant << ","
<< infected << "," << resistant << "\n";
step++;
}
}