Utopia  2
Framework for studying models of complex & adaptive systems.
params.hh
Go to the documentation of this file.
1 #ifndef UTOPIA_MODELS_CONTDISEASE_PARAMS_HH
2 #define UTOPIA_MODELS_CONTDISEASE_PARAMS_HH
3 
4 #include <queue>
5 
6 #include <utopia/core/types.hh>
8 
10 {
11 
12 
15  // -- Type definitions ----------------------------------------------------
17  using TimesQueue = std::queue<std::size_t>;
18 
20  using TimesValuesQueue = std::queue<std::pair<std::size_t, double>>;
21 
22  // -- Parameters ----------------------------------------------------
24  const bool enabled;
25 
27  const std::size_t num_additional_infections;
28 
31 
33 
38 
40 
44  :
45  enabled(get_as<bool>("enabled", cfg)),
47  "num_additional_infections", cfg, 0)},
48  at_times{[&](){
49  auto cont = get_as<std::vector<std::size_t>>("at_times", cfg, {});
50 
51  std::sort(cont.begin(), cont.end());
52 
53  // Copy elements into the queue
54  TimesQueue q{};
55  for (const auto& v : cont){
56  q.push(v);
57  }
58 
59  return q;
60  }()},
61  change_p_infect{[&](){
62  // Check the parameter
63  // For the default case, an empty list, a empty queue is returned.
64  if(cfg["change_p_infect"].IsSequence() and cfg["change_p_infect"].size() == 0){
65  // Key was empty; return empty queue.
66  return TimesValuesQueue{};
67  }
68  // Check if given Parameter is a Sequence
69  else if(not cfg["change_p_infect"].IsSequence()){
70  // Inform about bad type of the given configuration entry
71  throw std::invalid_argument("Parameter change_p_infect need be "
72  "a sequence of pairs, but was not a sequence! Given infection "
73  "control parameters:\n" + DataIO::to_string(cfg));
74  }
75  // Check if it is a sequence that it contains only pairs.
76  else{
77  bool pairs_only = true;
78  for(const auto pair: cfg["change_p_infect"]){
79  pairs_only &= pair.size() == 2;
80  }
81  if(not pairs_only){
82  throw std::invalid_argument("Parameter change_p_infect need be "
83  "a sequence of pairs, but contained something which was not "
84  "a pair! Given infection control parameters:\n"
85  + DataIO::to_string(cfg));
86  }
87  }
88  // Getting the parameters
89  std::vector<std::pair<std::size_t, double>> cont = [&cfg](){
90  std::vector<std::pair<int, double>> val =
91  get_as<std::vector<std::pair<int, double>>>
92  ("change_p_infect", cfg);
93 
94  for_each(val.begin(), val.end(),
95  [&cfg](auto& pair){
96  // Check for negative timesteps
97  if(pair.first < 0){
98  throw std::invalid_argument("Timesteps from parameter "
99  "change_p_infect needs to be larger zero. Given infection "
100  "control parameters:\n" + DataIO::to_string(cfg));
101  }
102  // Check for probabilites outside [0, 1]
103  if(pair.second < 0 or pair.second > 1){
104  throw std::invalid_argument("Infection chance from parameter "
105  "change_p_infect needs to be withhin [0, 1]. Given infection "
106  "control parameters:\n" + DataIO::to_string(cfg));
107  }
108  });
109  return get_as<std::vector<std::pair<std::size_t, double>>>
110  ("change_p_infect", cfg);
111  }();
112 
113  // Sort such that times low times are in the beginning of the queue
114  std::sort(cont.begin(), cont.end(),
115  [](const auto& a, const auto& b){
116  return a.first < b.first;
117  }
118  );
119 
120  // Copy elements into the queue
121  TimesValuesQueue q{};
122  for (const auto& v : cont){
123  q.push(v);
124  }
125 
126  return q;
127  }()}
128  {};
129 };
130 
131 
133 struct Params {
135  const double p_growth;
136 
139  const double p_immunity;
140 
143  mutable double p_infect;
144 
147 
149  Params(const DataIO::Config& cfg)
150  :
151  p_growth(get_as<double>("p_growth", cfg)),
152  p_immunity(get_as<double>("p_immunity", cfg)),
153  p_infect(get_as<double>("p_infect", cfg)),
154  infection_control(get_as<DataIO::Config>("infection_control", cfg))
155  {}
156 };
157 
158 } // namespace Utopia::Models::ContDisease
159 
160 #endif // UTOPIA_MODELS_CONTDISEASE_PARAMS_HH
void for_each(const Utopia::ExecPolicy policy, InputIt first, InputIt last, UnaryFunction f)
Apply a function to a range.
Definition: parallel.hh:346
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: ContDisease.hh:18
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 infection control.
Definition: params.hh:14
std::queue< std::size_t > TimesQueue
Type of the times queue.
Definition: params.hh:17
InfectionContParams(const DataIO::Config &cfg)
Configuration constructor.
Definition: params.hh:43
TimesValuesQueue change_p_infect
Change p_infect to new value at given times.
Definition: params.hh:37
std::queue< std::pair< std::size_t, double > > TimesValuesQueue
The type of the change p_infection pairs.
Definition: params.hh:20
const std::size_t num_additional_infections
The number of infections added to the default p_infect.
Definition: params.hh:27
TimesQueue at_times
Add additional infections at these time steps.
Definition: params.hh:30
const bool enabled
Whether infection control is enabled.
Definition: params.hh:24
Parameters of the ContDisease.
Definition: params.hh:133
const double p_growth
Probability per site and time step to go from state empty to tree.
Definition: params.hh:135
const double p_immunity
Definition: params.hh:139
double p_infect
Definition: params.hh:143
const InfectionContParams infection_control
Infection control parameters.
Definition: params.hh:146
Params(const DataIO::Config &cfg)
Construct the parameters from the given configuration node.
Definition: params.hh:149