| 324 | } |
| 325 | |
| 326 | int calcSIR( |
| 327 | const vector<vector<double>>& graph, |
| 328 | int real_size, |
| 329 | std::vector<int> sources, |
| 330 | float infectious_rate, |
| 331 | float recovery_rate, |
| 332 | int timestep, |
| 333 | const std::string& filename) |
| 334 | { |
| 335 | srand((int)time(0)); |
| 336 | |
| 337 | vector<int> state; |
| 338 | state.resize(real_size); |
| 339 | memset(state.data(), 0, sizeof(int) * real_size); |
| 340 | for (auto &i : sources) |
| 341 | { |
| 342 | if (i >= real_size) |
| 343 | { |
| 344 | cerr << "SIR source index error!" << endl; |
| 345 | return -1; |
| 346 | } |
| 347 | state[i] = 1; |
| 348 | } |
| 349 | |
| 350 | auto tmp_state = state; |
| 351 | vector<vector<int>> time_states; |
| 352 | for (int t = 0; t < timestep; t++) |
| 353 | { |
| 354 | for (int i = 0; i < real_size; i++) |
| 355 | { |
| 356 | if (state[i] == 1) { |
| 357 | double r1 = 1.0 * rand() / RAND_MAX; |
| 358 | if (r1 < recovery_rate) |
| 359 | { |
| 360 | tmp_state[i] = 2; |
| 361 | } |
| 362 | |
| 363 | for (int j = 0; j < real_size; j++) { |
| 364 | double r2 = 1.0 * rand() / RAND_MAX; |
| 365 | if (graph[i][j] > 1e-3 |
| 366 | && r2 < infectious_rate |
| 367 | && state[j] == 0) |
| 368 | { |
| 369 | tmp_state[j] = 1; |
| 370 | } |
| 371 | } |
| 372 | } |
| 373 | } |
| 374 | |
| 375 | time_states.push_back(tmp_state); |
| 376 | state = tmp_state; |
| 377 | } |
| 378 | |
| 379 | OriginCollection collection(filename, false); |
| 380 | collection = { "time_step", "node_state" }; |
| 381 | for (int i = 0; i < time_states.size(); i++) |
| 382 | { |
| 383 | collection.insertValue(i, time_states[i]); |