forked from Grufoony/SIR_Project
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdisease.cpp
More file actions
191 lines (177 loc) · 6.02 KB
/
Copy pathdisease.cpp
File metadata and controls
191 lines (177 loc) · 6.02 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
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
#include <cassert>
#include <cmath>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <limits>
#include <stdexcept>
#include <string>
#include <SFML/Graphics.hpp>
#include "disease.hpp"
////////////////////////////////////////////////////////////////////////////////////////////////////////////
// evolution of the disease
////////////////////////////////////////////////////////////////////////////////////////////////////////////
State disease::Disease::evolve_(State const &begin) {
auto end = State();
end.s = begin.s - beta_ * begin.i * begin.s;
if (end.s < 0) {
end.s = 0;
}
end.r = begin.r + gamma_ * begin.i;
if (end.r > tot_) {
end.r = tot_;
}
end.i = begin.i + beta_ * begin.i * begin.s - gamma_ * begin.i;
if (end.i > tot_) {
end.i = tot_ - end.r - end.s;
}
// I make sure the data is correct
assert(!(end.s < 0));
assert(!(end.i < 0));
assert(!(end.r < 0));
return end;
};
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
disease::Disease::Disease(std::string p, int n, double b, double y) : name_{p} {
assert(b > 0 && b < 1);
assert(y > 0 && y < 1);
assert(n > 1);
tot_ = n;
beta_ = b / tot_;
gamma_ = y * 0.1;
State state_0{(double)n - 1., 1., 0.};
state_.push_back(state_0);
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////
void disease::Disease::setBeta(double b) { beta_ = b / tot_; }
void disease::Disease::setGamma(double g) { gamma_ = g; }
void disease::Disease::evolve(int n) {
for (int i = 0; i < n; ++i) {
state_.push_back(evolve_(state_[i]));
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////
void disease::Disease::print() {
int i = 0;
auto tab = std::setw(20);
std::cout << name_ << '\n';
std::cout << "Current value of R0: " << (tot_ * beta_) / gamma_ << '\n';
std::cout << tab << "Day" << tab << "Susceptible" << tab << "Infectuos" << tab
<< "Recovered" << '\n';
for (auto const it : state_) {
std::cout << std::setprecision(10) << tab << i << tab << (int)it.s << tab
<< (int)it.i << tab << (int)it.r << '\n';
++i;
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////
void disease::Disease::f_print() {
// Sposto l'output su file
std::ofstream fp;
fp.open("report.txt");
std::cout.rdbuf(fp.rdbuf());
print();
fp.close();
}
//////////////////////////////////////////////////////////////////////////////////////////////
// DRAW THE GRAPH USING SFML
//////////////////////////////////////////////////////////////////////////////////////////////
void disease::Disease::draw(int lenght, int height, const char &c) {
sf::RenderWindow window(sf::VideoMode(lenght, height), name_,
sf::Style::Close | sf::Style::Resize);
window.setVerticalSyncEnabled(true);
sf::RectangleShape x_axis(sf::Vector2f(static_cast<float>(lenght), 1.f));
sf::RectangleShape y_axis(sf::Vector2f(1.f, static_cast<float>(height)));
x_axis.setPosition(0.f, static_cast<float>(height - EDGE));
y_axis.setPosition(EDGE, 0);
x_axis.setFillColor(sf::Color::White);
y_axis.setFillColor(sf::Color::White);
sf::RectangleShape bit(sf::Vector2f(2., 2.));
auto x_up = (lenght - EDGE) / state_.size();
auto y_scale = (height - (2 * EDGE)) / tot_;
if (x_up < 1) {
x_up = 1;
}
while (window.isOpen()) {
window.clear(sf::Color::Black);
window.draw(x_axis);
window.draw(y_axis);
sf::Event evnt;
while (window.pollEvent(evnt)) {
if (evnt.type == sf::Event::Closed) {
window.close();
}
}
double j;
switch (c) {
case 's':
case 'S':
j = EDGE;
bit.setFillColor(sf::Color::Green);
for (auto const it : state_) {
bit.setPosition(static_cast<float>(j),
static_cast<float>(height - EDGE - it.s * y_scale));
window.draw(bit);
j += x_up;
}
break;
case 'i':
case 'I':
j = EDGE;
bit.setFillColor(sf::Color::Red);
for (auto const it : state_) {
bit.setPosition(static_cast<float>(j),
static_cast<float>(height - EDGE - it.i * y_scale));
window.draw(bit);
j += x_up;
}
break;
case 'r':
case 'R':
j = EDGE;
bit.setFillColor(sf::Color::Blue);
for (auto const it : state_) {
bit.setPosition(static_cast<float>(j),
static_cast<float>(height - EDGE - it.r * y_scale));
window.draw(bit);
j += x_up;
}
break;
case 'a':
case 'A':
j = EDGE;
bit.setFillColor(sf::Color::Green);
for (auto const it : state_) {
bit.setPosition(static_cast<float>(j),
static_cast<float>(height - EDGE - it.s * y_scale));
window.draw(bit);
j += x_up;
}
j = EDGE;
bit.setFillColor(sf::Color::Red);
for (auto const it : state_) {
bit.setPosition(static_cast<float>(j),
static_cast<float>(height - EDGE - it.i * y_scale));
window.draw(bit);
j += x_up;
}
j = EDGE;
bit.setFillColor(sf::Color::Blue);
for (auto const it : state_) {
bit.setPosition(static_cast<float>(j),
static_cast<float>(height - EDGE - it.r * y_scale));
window.draw(bit);
j += x_up;
}
break;
default:
throw std::runtime_error(
"Warning: invalid character passed at the draw function\n");
}
window.display();
}
}