|
| 1 | +/* |
| 2 | +* Copyright (C) 2020-2025 MEmilio |
| 3 | +* |
| 4 | +* Authors: Henrik Zunker |
| 5 | +* |
| 6 | +* Contact: Martin J. Kuehn <[email protected]> |
| 7 | +* |
| 8 | +* Licensed under the Apache License, Version 2.0 (the "License"); |
| 9 | +* you may not use this file except in compliance with the License. |
| 10 | +* You may obtain a copy of the License at |
| 11 | +* |
| 12 | +* http://www.apache.org/licenses/LICENSE-2.0 |
| 13 | +* |
| 14 | +* Unless required by applicable law or agreed to in writing, software |
| 15 | +* distributed under the License is distributed on an "AS IS" BASIS, |
| 16 | +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 17 | +* See the License for the specific language governing permissions and |
| 18 | +* limitations under the License. |
| 19 | +*/ |
| 20 | +#include "memilio/data/analyze_result.h" |
| 21 | +#include "ode_secir/model.h" |
| 22 | +#include "memilio/compartments/feedback_simulation.h" |
| 23 | +#include "memilio/mobility/metapopulation_mobility_instant.h" |
| 24 | +#include "memilio/mobility/graph_simulation.h" |
| 25 | +#include "memilio/utils/logging.h" |
| 26 | +#include <iostream> |
| 27 | + |
| 28 | +// alias for the type of the simulation with feedback |
| 29 | +using FeedbackSim = mio::FeedbackSimulation<double, mio::Simulation<double, mio::osecir::Model<double>>, |
| 30 | + mio::osecir::ContactPatterns<double>>; |
| 31 | + |
| 32 | +// helper function to initialize the model with population and parameters |
| 33 | +void initialize_model(mio::osecir::Model<double>& model, double cont_freq) |
| 34 | +{ |
| 35 | + model.parameters.set<mio::osecir::StartDay>(60); |
| 36 | + model.parameters.set<mio::osecir::Seasonality<double>>(0.2); |
| 37 | + |
| 38 | + // Mean stay times per compartment |
| 39 | + model.parameters.get<mio::osecir::TimeExposed<double>>() = 3.2; |
| 40 | + model.parameters.get<mio::osecir::TimeInfectedNoSymptoms<double>>() = 2.0; |
| 41 | + model.parameters.get<mio::osecir::TimeInfectedSymptoms<double>>() = 5.8; |
| 42 | + model.parameters.get<mio::osecir::TimeInfectedSevere<double>>() = 9.5; |
| 43 | + model.parameters.get<mio::osecir::TimeInfectedCritical<double>>() = 7.1; |
| 44 | + |
| 45 | + // Set transmission and isolation parameters |
| 46 | + model.parameters.get<mio::osecir::TransmissionProbabilityOnContact<double>>() = 0.05; |
| 47 | + model.parameters.get<mio::osecir::RelativeTransmissionNoSymptoms<double>>() = 0.7; |
| 48 | + model.parameters.get<mio::osecir::RecoveredPerInfectedNoSymptoms<double>>() = 0.09; |
| 49 | + model.parameters.get<mio::osecir::RiskOfInfectionFromSymptomatic<double>>() = 0.25; |
| 50 | + model.parameters.get<mio::osecir::MaxRiskOfInfectionFromSymptomatic<double>>() = 0.45; |
| 51 | + model.parameters.get<mio::osecir::TestAndTraceCapacity<double>>() = 35; |
| 52 | + model.parameters.get<mio::osecir::SeverePerInfectedSymptoms<double>>() = 0.2; |
| 53 | + model.parameters.get<mio::osecir::CriticalPerSevere<double>>() = 0.25; |
| 54 | + model.parameters.get<mio::osecir::DeathsPerCritical<double>>() = 0.3; |
| 55 | + |
| 56 | + // contact matrix |
| 57 | + mio::ContactMatrixGroup& contact_matrix = model.parameters.get<mio::osecir::ContactPatterns<double>>(); |
| 58 | + contact_matrix[0] = mio::ContactMatrix(Eigen::MatrixXd::Constant(1, 1, cont_freq)); |
| 59 | +} |
| 60 | + |
| 61 | +// helper function to initialize the feedback mechanism parameters for a simulation |
| 62 | +void initialize_feedback(FeedbackSim& feedback_simulation) |
| 63 | +{ |
| 64 | + // nominal ICU capacity |
| 65 | + feedback_simulation.get_parameters().template get<mio::NominalICUCapacity<double>>() = 10; |
| 66 | + |
| 67 | + // ICU occupancy in the past for memory kernel |
| 68 | + auto& icu_occupancy = feedback_simulation.get_parameters().template get<mio::ICUOccupancyHistory<double>>(); |
| 69 | + Eigen::VectorXd icu_day = Eigen::VectorXd::Constant(1, 1); |
| 70 | + const auto cutoff = static_cast<int>(feedback_simulation.get_parameters().template get<mio::GammaCutOff>()); |
| 71 | + for (int t = -cutoff; t <= 0; ++t) { |
| 72 | + icu_occupancy.add_time_point(t, icu_day); |
| 73 | + } |
| 74 | + |
| 75 | + // bounds for contact reduction measures |
| 76 | + feedback_simulation.get_parameters().template get<mio::ContactReductionMin<double>>() = {0.1}; |
| 77 | + feedback_simulation.get_parameters().template get<mio::ContactReductionMax<double>>() = {0.8}; |
| 78 | + |
| 79 | + // Set blending factors. The global blending factor is implicitly defined as 1 - local - regional. |
| 80 | + feedback_simulation.get_parameters().template get<mio::BlendingFactorLocal<double>>() = 0.5; |
| 81 | + feedback_simulation.get_parameters().template get<mio::BlendingFactorRegional<double>>() = 0.3; |
| 82 | +} |
| 83 | + |
| 84 | +// helper function to create the graph with nodes and edges |
| 85 | +mio::Graph<mio::SimulationNode<FeedbackSim>, mio::MobilityEdge<double>> |
| 86 | +create_graph(int num_nodes, int total_population, double cont_freq) |
| 87 | +{ |
| 88 | + // Create a graph for the metapopulation simulation |
| 89 | + mio::Graph<mio::SimulationNode<FeedbackSim>, mio::MobilityEdge<double>> g; |
| 90 | + |
| 91 | + // Create models and add nodes to the graph |
| 92 | + for (int i = 0; i < num_nodes; ++i) { |
| 93 | + mio::osecir::Model<double> model(1); |
| 94 | + initialize_model(model, cont_freq); |
| 95 | + |
| 96 | + // Set initial populations (infection starts in the first node) |
| 97 | + if (i == 0) { |
| 98 | + model.populations[{mio::AgeGroup(0), mio::osecir::InfectionState::Exposed}] = total_population * 0.1; |
| 99 | + model.populations[{mio::AgeGroup(0), mio::osecir::InfectionState::InfectedNoSymptoms}] = |
| 100 | + total_population * 0.1; |
| 101 | + model.populations[{mio::AgeGroup(0), mio::osecir::InfectionState::InfectedSymptoms}] = |
| 102 | + total_population * 0.05; |
| 103 | + model.populations[{mio::AgeGroup(0), mio::osecir::InfectionState::InfectedSevere}] = |
| 104 | + total_population * 0.02; |
| 105 | + model.populations[{mio::AgeGroup(0), mio::osecir::InfectionState::InfectedCritical}] = |
| 106 | + total_population * 0.01; |
| 107 | + model.populations[{mio::AgeGroup(0), mio::osecir::InfectionState::Recovered}] = 0; |
| 108 | + model.populations.set_difference_from_total({mio::AgeGroup(0), mio::osecir::InfectionState::Susceptible}, |
| 109 | + total_population); |
| 110 | + } |
| 111 | + else { |
| 112 | + model.populations[{mio::AgeGroup(0), mio::osecir::InfectionState::Susceptible}] = total_population; |
| 113 | + } |
| 114 | + model.apply_constraints(); |
| 115 | + |
| 116 | + // Determine the index for the ICU state (InfectedCritical) for the feedback mechanism |
| 117 | + auto icu_index = std::vector<size_t>{ |
| 118 | + model.populations.get_flat_index({mio::AgeGroup(0), mio::osecir::InfectionState::InfectedCritical})}; |
| 119 | + |
| 120 | + // Create feedback simulation |
| 121 | + auto feedback_sim = FeedbackSim(mio::Simulation<double, mio::osecir::Model<double>>(model), icu_index); |
| 122 | + initialize_feedback(feedback_sim); |
| 123 | + |
| 124 | + // Node-ID-Logic: 1001-1005, 2001-2005, ... |
| 125 | + const int region_id = i / 5; |
| 126 | + const int local_id = i % 5; |
| 127 | + const int node_id = (region_id + 1) * 1000 + (local_id + 1); |
| 128 | + g.add_node(node_id, std::move(feedback_sim)); |
| 129 | + } |
| 130 | + |
| 131 | + // Define complete graph, i.e. each node is connected to every other node |
| 132 | + std::vector<std::vector<size_t>> mobile_compartments(2); |
| 133 | + for (size_t i = 0; i < g.nodes().size(); ++i) { |
| 134 | + for (size_t j = 0; j < g.nodes().size(); ++j) { |
| 135 | + if (i != j) { |
| 136 | + g.add_edge(i, j, Eigen::VectorXd::Constant((size_t)mio::osecir::InfectionState::Count, 0.01)); |
| 137 | + } |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + return g; |
| 142 | +} |
| 143 | + |
| 144 | +int main() |
| 145 | +{ |
| 146 | + // This example demonstrates the implementation of a feedback mechanism for a ODE SECIR model in a graph. |
| 147 | + // It shows how the perceived risk dynamically impacts contact reduction measures in different regions (nodes). |
| 148 | + mio::set_log_level(mio::LogLevel::err); |
| 149 | + |
| 150 | + const auto t0 = 0.; |
| 151 | + const auto tmax = 10.; |
| 152 | + const auto dt = 0.5; |
| 153 | + const int total_population = 1000; |
| 154 | + const double cont_freq = 2.7; |
| 155 | + const int num_nodes = 10; |
| 156 | + |
| 157 | + // Create the graph |
| 158 | + auto g = create_graph(num_nodes, total_population, cont_freq); |
| 159 | + |
| 160 | + // Create and run the simulation |
| 161 | + using Graph = decltype(g); |
| 162 | + auto sim = mio::FeedbackGraphSimulation<double, Graph>(std::move(g), t0, dt); |
| 163 | + sim.advance(tmax); |
| 164 | + |
| 165 | + // The output shows the compartments sizes for a node without any initial infections. |
| 166 | + auto& node = sim.get_graph().nodes()[1]; |
| 167 | + auto& results_node = node.property.get_simulation().get_result(); |
| 168 | + // interpolate results |
| 169 | + auto interpolated_results_node = mio::interpolate_simulation_result(results_node); |
| 170 | + |
| 171 | + // print result with print_table |
| 172 | + std::cout << "Node ID: " << node.id << "\n"; |
| 173 | + std::vector<std::string> cols = {"S", "E", "C", "C_confirmed", "I", "I_confirmed", "H", "U", "R", "D"}; |
| 174 | + interpolated_results_node.print_table(cols); |
| 175 | + |
| 176 | + return 0; |
| 177 | +} |
0 commit comments