-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
344 lines (315 loc) · 10 KB
/
main.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
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include "timeline.h"
#include <getopt.h>
#include <iostream>
#include <fstream>
#include <sstream>
#include "world.h"
static const char *parameterNames[] = {
"population",
"healthy_infection_probability",
"immune_infection_probability",
"death_probability",
"initial_infected",
"initial_immune",
"proximity",
"world_height",
"world_width",
};
bool generateParameterFile(const std::string &filename,
const world_parameters_t *w)
{
std::ofstream file(filename);
if (!file.is_open()) {
std::cerr << "Error: Could not create file " << filename
<< std::endl;
return false;
}
file << parameterNames[0] << " " << w->populationPercent << "\n";
file << parameterNames[1] << " " << w->healthyInfectionProbability
<< "\n";
file << parameterNames[2] << " " << w->immuneInfectionProbability
<< "\n";
file << parameterNames[3] << " " << w->deathProbability << "\n";
file << parameterNames[4] << " " << w->initialInfected << "\n";
file << parameterNames[5] << " " << w->initialImmune << "\n";
file << parameterNames[6] << " " << w->proximity << "\n";
file << parameterNames[7] << " " << w->worldHeight << "\n";
file << parameterNames[8] << " " << w->worldWidth << "\n";
file.close();
std::cout << "Parameters written to " << filename << std::endl;
return true;
}
bool loadParametersFromFile(const std::string &filename, world_parameters_t *w)
{
std::ifstream file(filename);
if (!file.is_open()) {
std::cerr << "Error: Could not open file " << filename
<< std::endl;
return false;
}
std::string line;
while (std::getline(file, line)) {
std::istringstream iss(line);
std::string key;
int value;
if (iss >> key >> value) {
if (key == parameterNames[0])
w->populationPercent = value;
else if (key == parameterNames[1])
w->healthyInfectionProbability = value;
else if (key == parameterNames[2])
w->immuneInfectionProbability = value;
else if (key == parameterNames[3])
w->deathProbability = value;
else if (key == parameterNames[4])
w->initialInfected = value;
else if (key == parameterNames[5])
w->initialImmune = value;
else if (key == parameterNames[6])
w->proximity = value;
else if (key == parameterNames[7])
w->worldHeight = value;
else if (key == parameterNames[8])
w->worldWidth = value;
else {
std::cerr << "Warning: Unknown key '" << key
<< "' in file " << filename
<< std::endl;
}
}
}
file.close();
return true;
}
void printUsage(const world_parameters_t *w)
{
std::cout
<< "Usage: Plague Simulator [options]\n"
<< "Options:\n"
<< " -p, --population <value> Percent of population (default: "
<< w->populationPercent << "%)\n"
<< " -m, --population-immune <value> Number of initial immune people (default: "
<< w->initialImmune << "%)\n"
<< " -e, --healthy-infection <value> Probability to infect a healthy person (default: "
<< w->healthyInfectionProbability << "%)\n"
<< " -n, --immune-infection <value> Probability to infect an immune person (default: "
<< w->immuneInfectionProbability << "%)\n"
<< " -d, --death-probability <value> Probability of death (default: "
<< w->deathProbability << "%)\n"
<< " -i, --initial-infected <value> Number of initial infected (default: "
<< w->initialInfected << ")\n"
<< " -y, --proximity <value> Proximity of infection (default: "
<< w->proximity << ")\n"
<< " -h, --world-height <value> Height of the grid (default: "
<< w->worldHeight << ")\n"
<< " -w, --world-width <value> Width of the grid (default: "
<< w->worldWidth << ")\n"
<< " -r, --rounds <value> Max Rounds (default: No limit)\n"
<< " -f, --file <file> Load parameters from file\n"
<< " -g, --generate-file Generate a parameter file\n"
<< " --help Display this information\n";
}
int main(int argc, char *argv[])
{
size_t total_rounds = 0;
world_parameters_t params = {
.worldHeight = 10,
.worldWidth = 10,
.populationPercent = 50,
.initialInfected = 1,
.initialImmune = 0,
.deathProbability = 10,
.infectionDuration = 10,
.healthyInfectionProbability = 10,
.immuneInfectionProbability = 10,
.proximity = 2,
};
const char *shortOptions = "p:e:n:d:r:i:m:y:h:w:f:g";
const struct option longOptions[] = {
{ "population", required_argument, nullptr, 'p' },
{ "healthy-infection-probability", required_argument, nullptr,
'e' },
{ "immune-infection-probability", required_argument, nullptr,
'n' },
{ "death-probability", required_argument, nullptr, 'd' },
{ "rounds", required_argument, nullptr, 'r' },
{ "initial-infected", required_argument, nullptr, 'i' },
{ "initial-immune", required_argument, nullptr, 'm' },
{ "proximity", required_argument, nullptr, 'y' },
{ "world-height", required_argument, nullptr, 'h' },
{ "world-width", required_argument, nullptr, 'w' },
{ "file", required_argument, nullptr, 'f' },
{ "generate-file", no_argument, nullptr,
'g' }, // Nouvelle option
{ "help", no_argument, nullptr, ' ' },
{ nullptr, 0, nullptr, 0 }
};
// args parsing
int opt;
while ((opt = getopt_long(argc, argv, shortOptions, longOptions,
nullptr)) != -1) {
switch (opt) {
case 'p':
params.populationPercent = atoi(optarg);
if (params.populationPercent > 100) {
std::cerr
<< "Error: Population must be > 0% and <=100%.\n";
return 1;
}
break;
case 'e':
params.healthyInfectionProbability = atoi(optarg);
break;
case 'n':
params.immuneInfectionProbability = atoi(optarg);
break;
case 'd':
params.deathProbability = atoi(optarg);
break;
case 'r':
total_rounds = atoi(optarg);
break;
case 'i':
params.initialInfected = atoi(optarg);
break;
case 'm':
params.initialImmune = atoi(optarg);
break;
case 'y':
params.proximity = atoi(optarg);
break;
case 'h':
params.worldHeight = atoi(optarg);
break;
case 'w':
params.worldWidth = atoi(optarg);
break;
case 'f': {
std::string filename = optarg;
if (!loadParametersFromFile(filename, ¶ms)) {
return 1;
}
break;
}
case 'g': {
std::string filename = "parameters.txt";
if (!generateParameterFile(filename, ¶ms)) {
return 1;
}
exit(0);
}
case '?':
case ':':
default:
printUsage(¶ms);
return 1;
}
}
world_t world;
int ret = world_init(&world, ¶ms);
if (ret < 0) {
return ret;
}
std::cout << "-----------------------------------\n";
std::cout << " Plague Simulator\n";
std::cout << "-----------------------------------\n";
std::cout << "------------------------------------\n";
std::cout << "Parameters :\n";
std::cout << "------------------------------------\n";
std::cout
<< "Population : " << params.populationPercent
<< "%\n"
<< "World height : " << params.worldHeight
<< "\n"
<< "World Width : " << params.worldWidth << "\n"
<< "World size : "
<< params.worldHeight * params.worldWidth << "\n"
<< "Proximity : " << params.proximity << "\n"
<< "Infection duration : " << params.infectionDuration
<< " turns\n"
<< "Healty infection probability: "
<< params.healthyInfectionProbability << " % \n"
<< "Immune infection probability: "
<< params.immuneInfectionProbability << " % \n"
<< "Death probability : " << params.deathProbability
<< "%\n"
<< "Initial infected : " << params.initialInfected
<< "\n"
<< "Population immunized : " << params.initialImmune
<< "%\n";
std::cout << "\n";
int initialImmune = world_get_immune(&world);
std::cout << "------------------------------------\n";
std::cout << "Initial world :\n";
std::cout << "------------------------------------\n";
std::cout << "Number of healty people : " << world_get_healthy(&world)
<< '\n';
std::cout
<< "Number of infected people : " << world_get_infected(&world)
<< '\n';
std::cout << "Number of immunized people: " << initialImmune << '\n';
std::cout << "\n";
std::cout << "------------------------------------\n";
std::cout << "Simulation\n";
std::cout << "------------------------------------\n";
std::cout << "Simulation started\n";
struct timespec start, finish;
int err;
double total_elapsed = 0;
timeline_t tl;
ret = timeline_init(&tl, &world.params, "timeline.bin");
if (ret < 0) {
std::cerr << "Failed to allocate memory to store world state"
<< '\n';
return 1;
}
while (world_get_infected(&world) > 0) {
if (total_rounds > 0 && tl.nb_rounds >= total_rounds) {
break;
}
if (tl.nb_rounds % 100 == 0) {
std::cout << "Round" << tl.nb_rounds << '\n';
}
void *ret = world_prepare_update(&world);
if (!ret) {
std::cerr << "Failed to Update World" << '\n';
break;
}
clock_gettime(CLOCK_MONOTONIC, &start);
world_update(&world, ret);
clock_gettime(CLOCK_MONOTONIC, &finish);
err = timeline_push_round(&tl, (int *)world.grid);
if (err < 0) {
std::cerr << "Failed to save last round\n";
break;
}
double round_elapsed = (finish.tv_sec - start.tv_sec);
round_elapsed += (finish.tv_nsec - start.tv_nsec) / 1e9;
total_elapsed += round_elapsed;
}
std::cout << "------------------------------------\n";
std::cout << "Saving Timeline\n";
err = timeline_save(&tl);
if (err < 0) {
std::cout << "Failed to create timeline\n";
} else {
std::cout << "Timeline created\n";
}
std::cout << "Simulation took : " << total_elapsed << " s\n";
std::cout << "Number of turns : " << tl.nb_rounds << '\n';
std::cout << "Number of healty people : " << world_get_healthy(&world)
<< '\n';
std::cout << "Number of immunized people: " << world_get_immune(&world)
<< '\n';
std::cout << "Number of survivor : "
<< world_get_immune(&world) + world_get_healthy(&world)
<< '\n';
std::cout << "Number of dead people : " << world_get_dead(&world)
<< '\n';
std::cout << "\n";
world_destroy(&world);
return 0;
}