uncertain_matrix.h Source File

CPP API: uncertain_matrix.h Source File
uncertain_matrix.h
Go to the documentation of this file.
1 /*
2 * Copyright (C) 2020-2026 MEmilio
3 *
4 * Authors: Daniel Abele, Martin J. Kuehn
5 *
6 * Contact: Martin J. Kuehn <Martin.Kuehn@DLR.de>
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 #ifndef MIO_EPI_ODE_UNCERTAINMATRIX_H
21 #define MIO_EPI_ODE_UNCERTAINMATRIX_H
22 
25 
26 #include <vector>
27 
28 namespace mio
29 {
30 
41 template <typename FP>
43 {
44 public:
45  UncertainContactMatrix(size_t num_matrices = 1, Eigen::Index num_groups = 1)
46  : UncertainContactMatrix<FP>(ContactMatrixGroup<FP>(num_matrices, num_groups))
47  {
48  }
49 
51  : m_cont_freq(cont_freq)
52  , m_dampings()
53  , m_school_holiday_damping(mio::UncertainValue<FP>(0.0), mio::DampingLevel(0), mio::DampingType(0),
54  mio::SimulationTime<FP>(0.0), {},
55  Eigen::VectorX<FP>::Zero(cont_freq.get_num_groups()))
57  {
58  }
59 
64  operator ContactMatrixGroup<FP> const&() const
65  {
66  return m_cont_freq;
67  }
68 
74  {
75  return m_cont_freq;
76  }
77 
83  {
84  m_cont_freq = cont_freq;
85  return *this;
86  }
87 
93  {
94  return m_cont_freq;
95  }
96 
102  {
103  return m_cont_freq;
104  }
105 
111  const std::vector<DampingSampling<FP>>& get_dampings() const
112  {
113  return m_dampings;
114  }
115  std::vector<DampingSampling<FP>>& get_dampings()
116  {
117  return m_dampings;
118  }
127  {
129  }
131  {
133  }
141  std::vector<std::pair<SimulationTime<FP>, SimulationTime<FP>>>& get_school_holidays()
142  {
143  return m_school_holidays;
144  }
145  const std::vector<std::pair<SimulationTime<FP>, SimulationTime<FP>>>& get_school_holidays() const
146  {
147  return m_school_holidays;
148  }
157  {
159  return make_matrix(accum);
160  }
161 
166  {
167  for (auto& d : m_dampings) {
168  d.draw_sample();
169  }
170  m_school_holiday_damping.draw_sample();
171  }
172 
179  {
180  if (!accum) {
181  m_cont_freq.clear_dampings();
182  }
183 
184  auto make_matrix = [](auto&& v) {
186  };
188 
189  for (auto h : m_school_holidays) {
190  //enable damping at the start of the period
191  auto damping = m_school_holiday_damping;
192  damping.set_time(h.first);
193  mio::apply_dampings(m_cont_freq, Range(&damping, &damping + 1), make_matrix);
194 
195  //disable damping at the end of the period
196  damping.get_value() = 0.0;
197  damping.set_time(h.second);
198  mio::apply_dampings(m_cont_freq, Range(&damping, &damping + 1), make_matrix);
199  }
200 
201  return m_cont_freq;
202  }
203 
208  template <class IOContext>
209  void serialize(IOContext& io) const
210  {
211  auto obj = io.create_object("UncertainContactMatrix");
212  obj.add_element("ContactMatrix", m_cont_freq);
213  if (!(io.flags() & IOF_OmitDistributions)) {
214  obj.add_element("SchoolHolidayDamping", m_school_holiday_damping);
215  obj.add_list("SchoolHolidays", m_school_holidays.begin(), m_school_holidays.end());
216  obj.add_list("Dampings", m_dampings.begin(), m_dampings.end());
217  }
218  }
219 
224  template <class IOContext>
226  {
227  auto obj = io.expect_object("UncertainContactMatrix");
228  if (!(io.flags() & IOF_OmitDistributions)) {
229  auto c = obj.expect_element("ContactMatrix", Tag<ContactMatrixGroup<FP>>{});
230  auto e = obj.expect_element("SchoolHolidayDamping", Tag<DampingSampling<FP>>{});
231  auto f = obj.expect_list("SchoolHolidays", Tag<std::pair<SimulationTime<FP>, SimulationTime<FP>>>{});
232  auto d = obj.expect_list("Dampings", Tag<DampingSampling<FP>>{});
233  return apply(
234  io,
235  [](auto&& c_, auto&& d_, auto&& e_, auto&& f_) {
236  auto m = UncertainContactMatrix<FP>{c_};
237  m.get_dampings() = d_;
238  m.get_school_holiday_damping() = e_;
239  m.get_school_holidays() = f_;
240  return m;
241  },
242  c, d, e, f);
243  }
244  else {
245  auto c = obj.expect_element("ContactMatrix", Tag<ContactMatrixGroup<FP>>{});
246  return apply(
247  io,
248  [](auto&& c_) {
249  return UncertainContactMatrix<FP>{c_};
250  },
251  c);
252  }
253  }
254 
255 private:
257  std::vector<DampingSampling<FP>> m_dampings;
259  std::vector<std::pair<SimulationTime<FP>, SimulationTime<FP>>> m_school_holidays;
260 };
261 
262 } // namespace mio
263 
264 #endif // MIO_EPI_ODE_UNCERTAINMATRIX_H
represents a collection of contact frequency matrices that whose sum is the total number of contacts.
Definition: contact_matrix.h:536
randomly sample dampings for e.g.
Definition: damping_sampling.h:38
double simulation time.
Definition: damping.h:58
The UncertainContactMatrix class consists of a ContactMatrix with fixed baseline and uncertain Dampin...
Definition: uncertain_matrix.h:43
void draw_sample_dampings()
draw sample of all dampings.
Definition: uncertain_matrix.h:165
const DampingSampling< FP > & get_school_holiday_damping() const
Damping that is active during school holiday periods.
Definition: uncertain_matrix.h:126
static IOResult< UncertainContactMatrix< FP > > deserialize(IOContext &io)
deserialize an object of this class.
Definition: uncertain_matrix.h:225
ContactMatrixGroup< FP > make_matrix(bool accum=false)
create the contact matrix using the sampled dampings.
Definition: uncertain_matrix.h:178
UncertainContactMatrix< FP > & operator=(const ContactMatrixGroup< FP > &cont_freq)
Set an UncertainContactMatrix from a ContactMatrix, all distributions remain unchanged.
Definition: uncertain_matrix.h:82
std::vector< DampingSampling< FP > > & get_dampings()
Get a list of uncertain Dampings that are sampled and added to the contact matrix.
Definition: uncertain_matrix.h:115
ContactMatrixGroup< FP > const & get_cont_freq_mat() const
Returns the const ContactMatrix reference of the UncertainContactMatrix object.
Definition: uncertain_matrix.h:101
ContactMatrixGroup< FP > m_cont_freq
Definition: uncertain_matrix.h:256
std::vector< std::pair< SimulationTime< FP >, SimulationTime< FP > > > m_school_holidays
Definition: uncertain_matrix.h:259
const std::vector< DampingSampling< FP > > & get_dampings() const
Get a list of uncertain Dampings that are sampled and added to the contact matrix.
Definition: uncertain_matrix.h:111
DampingSampling< FP > m_school_holiday_damping
Definition: uncertain_matrix.h:258
void serialize(IOContext &io) const
serialize this.
Definition: uncertain_matrix.h:209
DampingSampling< FP > & get_school_holiday_damping()
Damping that is active during school holiday periods.
Definition: uncertain_matrix.h:130
UncertainContactMatrix(size_t num_matrices=1, Eigen::Index num_groups=1)
Definition: uncertain_matrix.h:45
ContactMatrixGroup< FP > & get_cont_freq_mat()
Returns the ContactMatrix reference of the UncertainContactMatrix object.
Definition: uncertain_matrix.h:92
const std::vector< std::pair< SimulationTime< FP >, SimulationTime< FP > > > & get_school_holidays() const
list of school holiday periods.
Definition: uncertain_matrix.h:145
std::vector< DampingSampling< FP > > m_dampings
Definition: uncertain_matrix.h:257
std::vector< std::pair< SimulationTime< FP >, SimulationTime< FP > > > & get_school_holidays()
list of school holiday periods.
Definition: uncertain_matrix.h:141
UncertainContactMatrix(const ContactMatrixGroup< FP > &cont_freq)
Definition: uncertain_matrix.h:50
ContactMatrixGroup< FP > draw_sample(bool accum=false)
Samples dampings and adds them to the contact matrix.
Definition: uncertain_matrix.h:156
The UncertainValue class consists of a scalar value and a Distribution object.
Definition: uncertain_value.h:46
A collection of classes to simplify handling of matrix shapes in meta programming.
Definition: models/abm/analyze_result.h:30
Range(std::pair< I, S > iterator_pair) -> Range< I, S >
Deduction guide to correctly deduce range type when constructed from a pair.
void apply_dampings(DampingExpression &damping_expression, const DampingSamplings &dampings, F make_matrix)
add sampled dampings to a damping expression.
Definition: damping_sampling.h:256
boost::outcome_v2::in_place_type_t< T > Tag
Type that is used for overload resolution.
Definition: io.h:407
details::ApplyResultT< F, T... > apply(IOContext &io, F f, const IOResult< T > &... rs)
Evaluate a function with zero or more unpacked IOResults as arguments.
Definition: io.h:481
auto make_contact_damping_matrix(V &&groups)
Make a contact damping matrix from dampings by group.
Definition: damping_sampling.h:282
@ IOF_OmitDistributions
Don't serialize distributions for types that contain both a specific value and a distribution from wh...
Definition: io.h:76
boost::outcome_v2::unchecked< T, IOStatus > IOResult
Value-or-error type for operations that return a value but can fail.
Definition: io.h:353