2020#ifndef PARAMETER_STUDIES_H
2121#define PARAMETER_STUDIES_H
2222
23- #include " secir/secir.h"
24- #include " secir/parameter_space.h"
2523#include " memilio/utils/time_series.h"
2624#include " memilio/mobility/mobility.h"
2725#include " memilio/compartments/simulation.h"
@@ -89,33 +87,18 @@ class ParameterStudy
8987 m_graph.add_node (0 , model);
9088 }
9189
92- /* *
93- * @brief create study for single compartment model with normal distributions.
94- * Sets all parameters to normal distribution with specified relative deviation.
95- * @param params SecirParams object
96- * @param t0 start time of simulations
97- * @param tmax end time of simulations
98- * @param dev_rel relative deviation of parameters distributions
99- * @param num_runs number of runs in ensemble run
100- */
101- ParameterStudy (typename Simulation::Model const & model, double t0, double tmax, double dev_rel, size_t num_runs)
102- : ParameterStudy(model, t0, tmax, num_runs)
103- {
104- set_params_distributions_normal (m_graph.nodes ()[0 ].property , t0, tmax, dev_rel);
105- }
106-
10790 /*
10891 * @brief Carry out all simulations in the parameter study.
10992 * Save memory and enable more runs by immediately processing and/or discarding the result.
11093 * @param result_processing_function Processing function for simulation results, e.g., output function.
11194 * Receives the result after each run is completed.
11295 */
113- template < class HandleSimulationResultFunction >
114- void run (HandleSimulationResultFunction result_processing_function)
96+ template < class SampleGraphFunction , class HandleSimulationResultFunction >
97+ void run (SampleGraphFunction sample_graph, HandleSimulationResultFunction result_processing_function)
11598 {
11699 // Iterate over all parameters in the parameter space
117100 for (size_t i = 0 ; i < m_num_runs; i++) {
118- auto sim = create_sampled_simulation ();
101+ auto sim = create_sampled_simulation (sample_graph );
119102 sim.advance (m_tmax);
120103
121104 result_processing_function (std::move (sim).get_graph ());
@@ -124,15 +107,16 @@ class ParameterStudy
124107
125108 /*
126109 * @brief Carry out all simulations in the parameter study.
127- * Convinience function for a few number of runs, but uses a lot of memory.
110+ * Convenience function for a few number of runs, but uses a lot of memory.
128111 * @return vector of results of each run.
129112 */
130- std::vector<mio::Graph<mio::SimulationNode<Simulation>, mio::MigrationEdge>> run ()
113+ template <class SampleGraphFunction >
114+ std::vector<mio::Graph<mio::SimulationNode<Simulation>, mio::MigrationEdge>> run (SampleGraphFunction sample_graph)
131115 {
132116 std::vector<mio::Graph<mio::SimulationNode<Simulation>, mio::MigrationEdge>> ensemble_result;
133117 ensemble_result.reserve (m_num_runs);
134118
135- run ([&ensemble_result](auto && r) {
119+ run (sample_graph, [&ensemble_result](auto && r) {
136120 ensemble_result.emplace_back (std::move (r));
137121 });
138122
@@ -219,47 +203,18 @@ class ParameterStudy
219203
220204private:
221205 // sample parameters and create simulation
222- mio::GraphSimulation<mio::Graph<mio::SimulationNode<Simulation>, mio::MigrationEdge>> create_sampled_simulation ()
206+ template <class SampleGraphFunction >
207+ mio::GraphSimulation<mio::Graph<mio::SimulationNode<Simulation>, mio::MigrationEdge>>
208+ create_sampled_simulation (SampleGraphFunction sample_graph)
223209 {
224210 mio::Graph<mio::SimulationNode<Simulation>, mio::MigrationEdge> sim_graph;
225211
226- // sample global parameters
227- auto & shared_params_model = m_graph.nodes ()[0 ].property ;
228- draw_sample_infection (shared_params_model);
229- auto & shared_contacts = shared_params_model.parameters .template get <mio::ContactPatterns>();
230- shared_contacts.draw_sample_dampings ();
231- auto & shared_dynamic_npis = shared_params_model.parameters .template get <DynamicNPIsInfected>();
232- shared_dynamic_npis.draw_sample ();
233-
234- for (auto & params_node : m_graph.nodes ()) {
235- auto & node_model = params_node.property ;
236-
237- // sample local parameters
238- draw_sample_demographics (params_node.property );
239-
240- // copy global parameters
241- // save demographic parameters so they aren't overwritten
242- auto local_icu_capacity = node_model.parameters .template get <ICUCapacity>();
243- auto local_tnt_capacity = node_model.parameters .template get <TestAndTraceCapacity>();
244- auto local_holidays = node_model.parameters .template get <ContactPatterns>().get_school_holidays ();
245- node_model.parameters = shared_params_model.parameters ;
246- node_model.parameters .template get <ICUCapacity>() = local_icu_capacity;
247- node_model.parameters .template get <TestAndTraceCapacity>() = local_tnt_capacity;
248- node_model.parameters .template get <ContactPatterns>().get_school_holidays () = local_holidays;
249-
250- node_model.parameters .template get <ContactPatterns>().make_matrix ();
251- node_model.apply_constraints ();
252-
253- sim_graph.add_node (params_node.id , node_model, m_t0, m_dt_integration);
212+ auto sampled_graph = sample_graph (m_graph);
213+ for (auto && node : sampled_graph.nodes ()) {
214+ sim_graph.add_node (node.id , node.property , m_t0, m_dt_integration);
254215 }
255-
256- for (auto & edge : m_graph.edges ()) {
257- auto edge_params = edge.property ;
258- apply_dampings (edge_params.get_coefficients (), shared_contacts.get_dampings (), [&edge_params](auto & v) {
259- return make_migration_damping_vector (edge_params.get_coefficients ().get_shape (), v);
260- });
261- edge_params.set_dynamic_npis_infected (shared_dynamic_npis);
262- sim_graph.add_edge (edge.start_node_idx , edge.end_node_idx , edge_params);
216+ for (auto && edge : sampled_graph.edges ()) {
217+ sim_graph.add_edge (edge.start_node_idx , edge.end_node_idx , edge.property );
263218 }
264219
265220 return make_migration_sim (m_t0, m_dt_graph_sim, std::move (sim_graph));
0 commit comments