Utopia  2
Framework for studying models of complex & adaptive systems.
params.hh
Go to the documentation of this file.
1 #ifndef UTOPIA_MODELS_SEIRD_PARAMS_HH
2 #define UTOPIA_MODELS_SEIRD_PARAMS_HH
3 
4 #include <queue>
5 #include <memory>
6 
7 #include <utopia/core/types.hh>
9 
10 #include "state.hh" // for Kind enum
11 
12 namespace Utopia::Models::SEIRD
13 {
16 {
17  // -- Type definitions ----------------------------------------------------
19  using TimesQueue = std::queue<std::size_t>;
20 
22  using TimesValuesQueue = std::queue<std::pair<std::size_t, double>>;
23 
24  // -- Parameters ----------------------------------------------------
26  const bool enabled;
27 
29  const std::size_t num_additional_exposures;
30 
33 
35 
40 
42 
46  enabled(get_as<bool>("enabled", cfg)),
48  get_as<std::size_t>("num_additional_exposures", cfg, 0)},
49  at_times {[&]() {
50  auto cont = get_as<std::vector<std::size_t>>("at_times", cfg, {});
51 
52  std::sort(cont.begin(), cont.end());
53 
54  // Copy elements into the queue
55  TimesQueue q {};
56  for (const auto& v : cont) {
57  q.push(v);
58  }
59 
60  return q;
61  }()},
62  change_p_exposed {[&]() {
63  // Check the parameter
64  if (not cfg["change_p_exposed"] or
65  not cfg["change_p_exposed"].size()) {
66  // Key did not exist or was empty; return empty queue.
67  return TimesValuesQueue {};
68  }
69  else if (not cfg["change_p_exposed"].IsSequence()) {
70  // Inform about bad type of the given configuration entry
71  throw std::invalid_argument(
72  "Parameter change_p_exposed need be "
73  "a sequence of pairs, but was not! Given infection control "
74  "parameters:\n" +
75  DataIO::to_string(cfg));
76  }
77 
78  auto cont = get_as<std::vector<std::pair<std::size_t, double>>>(
79  "change_p_exposed",
80  cfg);
81 
82  // Sort such that times low times are in the beginning of the queue
83  std::sort(cont.begin(),
84  cont.end(),
85  [](const auto& a, const auto& b) {
86  return a.first < b.first;
87  });
88 
89  // Copy elements into the queue
90  TimesValuesQueue q {};
91  for (const auto& v : cont) {
92  q.push(v);
93  }
94 
95  return q;
96  }()} {};
97 };
98 
101 {
102  // -- Type definitions ----------------------------------------------------
104  using TimesQueue = std::queue<std::size_t>;
105 
107  using TimesValuesQueue = std::queue<std::pair<std::size_t, double>>;
108 
109  // -- Parameters ----------------------------------------------------
111  const bool enabled;
112 
114  const std::size_t num_additional_immunities;
115 
118 
120 
125 
127 
131  enabled(get_as<bool>("enabled", cfg)),
132  num_additional_immunities {
133  get_as<std::size_t>("num_additional_immunities", cfg, 0)},
134  at_times {[&]() {
135  auto cont = get_as<std::vector<std::size_t>>("at_times", cfg, {});
136 
137  std::sort(cont.begin(), cont.end());
138 
139  // Copy elements into the queue
140  TimesQueue q {};
141  for (const auto& v : cont) {
142  q.push(v);
143  }
144 
145  return q;
146  }()},
147  change_p_immune {[&]() {
148  // Check the parameter
149  if (not cfg["change_p_immune"] or
150  not cfg["change_p_immune"].size()) {
151  // Key did not exist or was empty; return empty queue.
152  return TimesValuesQueue {};
153  }
154  else if (not cfg["change_p_immune"].IsSequence()) {
155  // Inform about bad type of the given configuration entry
156  throw std::invalid_argument(
157  "Parameter change_p_immune need be "
158  "a sequence of pairs, but was not! Given infection control "
159  "parameters:\n" +
160  DataIO::to_string(cfg));
161  }
162 
163  auto cont = get_as<std::vector<std::pair<std::size_t, double>>>(
164  "change_p_immune",
165  cfg);
166 
167  // Sort such that low times are in the beginning of the queue
168  std::sort(cont.begin(),
169  cont.end(),
170  [](const auto& a, const auto& b) {
171  return a.first < b.first;
172  });
173 
174  // Copy elements into the queue
175  TimesValuesQueue q {};
176  for (const auto& v : cont) {
177  q.push(v);
178  }
179 
180  return q;
181  }()} {};
182 };
183 
186 {
187  // -- Type definitions ----------------------------------------------------
189  using TimesQueue = std::queue<std::size_t>;
190 
193  std::queue<std::tuple<std::size_t, unsigned, Kind, double>>;
194 
195  // -- Parameters ----------------------------------------------------
197  const bool enabled;
198 
200 
206 
208 
212  enabled(get_as<bool>("enabled", cfg)), change_p_transmit {[&]() {
213  // Check the parameter
214  if (not cfg["change_p_transmit"] or
215  not cfg["change_p_transmit"].size()) {
216  // Key did not exist or was empty; return empty queue.
217  return TimesValuesQueue {};
218  }
219  else if (not cfg["change_p_transmit"].IsSequence()) {
220  // Inform about bad type of the given configuration entry
221  throw std::invalid_argument(
222  "Parameter change_p_transmit need be "
223  "a sequence of tuples, but was not! Given transmit control "
224  "parameters:\n" +
225  DataIO::to_string(cfg));
226  }
227 
228  // Create the container with Kind as type in the third place
229  // and fill it up with the
230  std::vector<std::tuple<std::size_t, unsigned, Kind, double>> cont;
231  for (const auto& cfg_item :
232  get_as<DataIO::Config>("change_p_transmit", cfg)) {
233  const auto time = get_as<std::size_t>("time", cfg_item);
234  const auto num_cells = get_as<unsigned>("num_cells", cfg_item);
235  const auto cell_kind =
236  kind_from_string.at(get_as<std::string>("cell_kind", cfg_item));
237  const auto p_transmit = get_as<double>("p_transmit", cfg_item);
238 
239  cont.emplace_back(time, num_cells, cell_kind, p_transmit);
240  }
241 
242  // Sort such that low times are in the beginning of the queue
243  std::sort(cont.begin(),
244  cont.end(),
245  [](const auto& a, const auto& b) {
246  return std::get<0>(a) < std::get<0>(b);
247  });
248 
249  // Copy elements into the queue
250  TimesValuesQueue q {};
251  for (const auto& v : cont) {
252  q.push(v);
253  }
254 
255  return q;
256  }()} {};
257 };
258 
260 struct Params
261 {
263  const double p_susceptible;
264 
266  mutable double p_immune;
267 
270  const double p_random_immunity;
271 
274  mutable double p_exposed;
275 
278 
281  mutable double p_infected;
282 
284  double p_recovered;
285 
287  double p_deceased;
288 
290  double p_empty;
291 
294 
297 
300 
303 
306 
309 
311  Params(const DataIO::Config& cfg) :
312  p_susceptible(get_as<double>("p_susceptible", cfg)),
313  p_immune(get_as<double>("p_immune", cfg)),
314  p_random_immunity(get_as<double>("p_random_immunity", cfg)),
315  p_exposed(get_as<double>("p_exposed", cfg)),
316  p_infected(get_as<double>("p_infected", cfg)),
317  p_recovered(get_as<double>("p_recovered", cfg)),
318  p_deceased(get_as<double>("p_deceased", cfg)),
319  p_empty(get_as<double>("p_empty", cfg)),
320  p_lose_immunity(get_as<double>("p_lose_immunity", cfg)),
321  move_away_from_infected(get_as<bool>("move_away_from_infected", cfg)),
322  p_move_randomly(get_as<double>("p_move_randomly", cfg)),
323  exposure_control(get_as<DataIO::Config>("exposure_control", cfg)),
324  immunity_control(get_as<DataIO::Config>("immunity_control", cfg)),
325  transmission_control(get_as<DataIO::Config>("transmission_control", cfg))
326  {
327  if ((p_susceptible > 1) or (p_susceptible < 0)) {
328  throw std::invalid_argument(
329  "Invalid p_susceptible! Need be a value "
330  "in range [0, 1] and specify the probability per time step "
331  "and cell with which an empty cell turns into a susceptible "
332  "one. Was: " +
333  std::to_string(p_susceptible));
334  }
335  if ((p_immune > 1) or (p_immune < 0)) {
336  throw std::invalid_argument(
337  "Invalid p_immune! Need be a value "
338  "in range [0, 1] and specify the probability per time step "
339  "and cell with which an empty cell turns into a susceptible "
340  "one. Was: " +
341  std::to_string(p_immune));
342  }
343  if ((p_random_immunity > 1) or (p_random_immunity < 0)) {
344  throw std::invalid_argument(
345  "Invalid p_random_immunity! Need be in range "
346  "[0, 1], was " +
347  std::to_string(p_random_immunity));
348  }
349  if ((p_exposed > 1) or (p_exposed < 0)) {
350  throw std::invalid_argument("Invalid p_exposed! Need be a value "
351  "in range [0, 1], was " +
352  std::to_string(p_exposed));
353  }
354  if ((p_infected > 1) or (p_infected < 0)) {
355  throw std::invalid_argument("Invalid p_infected! Need be a value "
356  "in range [0, 1], was " +
357  std::to_string(p_infected));
358  }
359  if ((p_recovered > 1) or (p_recovered < 0)) {
360  throw std::invalid_argument("Invalid p_recovered! Need be a value "
361  "in range [0, 1], was " +
362  std::to_string(p_recovered));
363  }
364  if ((p_deceased > 1) or (p_deceased < 0)) {
365  throw std::invalid_argument("Invalid p_deceased! Need be a value "
366  "in range [0, 1], was " +
367  std::to_string(p_deceased));
368  }
369  if (((p_deceased + p_recovered) > 1)) {
370  throw std::invalid_argument(
371  "Invalid p_deceased and p_recovered! The sum needs to be a value "
372  "smaller than 1, was " +
373  std::to_string(p_deceased + p_recovered));
374  }
375  if ((p_empty > 1) or (p_empty < 0)) {
376  throw std::invalid_argument("Invalid p_empty! Need be a value "
377  "in range [0, 1], was " +
378  std::to_string(p_empty));
379  }
380  if ((p_lose_immunity > 1) or (p_lose_immunity < 0)) {
381  throw std::invalid_argument(
382  "Invalid p_lose_immunity! Need be a value "
383  "in range [0, 1], was " +
384  std::to_string(p_lose_immunity));
385  }
386  if ((p_move_randomly > 1) or (p_move_randomly < 0)) {
387  throw std::invalid_argument(
388  "Invalid p_move_randomly! Need be a value "
389  "in range [0, 1], was " +
390  std::to_string(p_move_randomly));
391  }
392  }
393 };
394 
395 } // namespace Utopia::Models::SEIRD
396 
397 #endif // UTOPIA_MODELS_SEIRD_PARAMS_HH
YAML::Node Config
Type of a variadic dictionary-like data structure used throughout Utopia.
Definition: types.hh:71
ReturnType get_as(const std::string &key, const DataIO::Config &node)
This function is a wrapper around the yaml-cpp YAML::Node::as function.
Definition: cfg_utils.hh:158
std::string to_string(const Config &node)
Given a config node, returns a string representation of it.
Definition: cfg_utils.hh:110
Definition: counters.hh:10
const std::map< const std::string, Kind > kind_from_string
Map the Kind name given as a string to the actual Kind.
Definition: state.hh:42
DataIO::Config Config
Type of a variadic dictionary-like data structure used throughout Utopia.
Definition: types.hh:80
Definition: parallel.hh:235
Parameters specifying the exposure control.
Definition: params.hh:16
TimesQueue at_times
Add additional exposures at these time steps.
Definition: params.hh:32
const std::size_t num_additional_exposures
The number of exposures added to the default p_expose.
Definition: params.hh:29
const bool enabled
Whether exposure control is enabled.
Definition: params.hh:26
TimesValuesQueue change_p_exposed
Change p_expose to new value at given times.
Definition: params.hh:39
std::queue< std::pair< std::size_t, double > > TimesValuesQueue
The type of the change p_exposed pairs.
Definition: params.hh:22
ExposureContParams(const DataIO::Config &cfg)
Configuration constructor.
Definition: params.hh:45
std::queue< std::size_t > TimesQueue
Type of the times queue.
Definition: params.hh:19
Parameters specifying the immunity control.
Definition: params.hh:101
const std::size_t num_additional_immunities
The number of immunities added to the default p_expose.
Definition: params.hh:114
TimesValuesQueue change_p_immune
Change p_immune to new value at given times.
Definition: params.hh:124
std::queue< std::size_t > TimesQueue
Type of the times queue.
Definition: params.hh:104
std::queue< std::pair< std::size_t, double > > TimesValuesQueue
The type of the change p_immune pairs.
Definition: params.hh:107
TimesQueue at_times
Add additional immunities at these time steps.
Definition: params.hh:117
const bool enabled
Whether immunity control is enabled.
Definition: params.hh:111
ImmunityContParams(const DataIO::Config &cfg)
Configuration constructor.
Definition: params.hh:130
Parameters of the SEIRD.
Definition: params.hh:261
double p_deceased
Probability for a cell to desease.
Definition: params.hh:287
double p_lose_immunity
The probability to loose immunity if a cell is recovered.
Definition: params.hh:293
Params(const DataIO::Config &cfg)
Construct the parameters from the given configuration node.
Definition: params.hh:311
const double p_susceptible
Probability per site and time step to go from state empty to susceptible.
Definition: params.hh:263
const ImmunityContParams immunity_control
Immunity control parameters.
Definition: params.hh:305
bool move_away_from_infected
Whether to globally allow moving away from infected neighboring cells.
Definition: params.hh:296
double p_move_randomly
Probability to move randomly if the neighboring cell is empty.
Definition: params.hh:299
const double p_random_immunity
Definition: params.hh:270
double p_immune
Probability per transition to susceptible via p_susceptible to be immune.
Definition: params.hh:266
double p_exposed
Definition: params.hh:274
const TransmitContParams transmission_control
Transmit control parameters.
Definition: params.hh:308
double p_empty
Probability for a cell to become empty.
Definition: params.hh:290
double p_infected
Definition: params.hh:281
double p_recovered
Probability for a cell to recover.
Definition: params.hh:284
const ExposureContParams exposure_control
Exposure control parameters.
Definition: params.hh:302
Parameters specifying the transmit control.
Definition: params.hh:186
const bool enabled
Whether immunity control is enabled.
Definition: params.hh:197
TransmitContParams(const DataIO::Config &cfg)
Configuration constructor.
Definition: params.hh:211
TimesValuesQueue change_p_transmit
Change p_transmit to new value at given times.
Definition: params.hh:205
std::queue< std::size_t > TimesQueue
Type of the times queue.
Definition: params.hh:189
std::queue< std::tuple< std::size_t, unsigned, Kind, double > > TimesValuesQueue
The type of the change p_transmit tuples.
Definition: params.hh:193