random_events.h Source File

CPP API: random_events.h Source File
random_events.h
Go to the documentation of this file.
1 /*
2 * Copyright (C) 2020-2026 MEmilio
3 *
4 * Authors: Daniel Abele
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 #include "abm/time.h"
22 #include <algorithm>
23 #include <array>
24 #include <numeric>
25 
26 namespace mio
27 {
28 namespace abm
29 {
30 
49 template <class RNG, class T, size_t NumTransitions>
50 T random_transition(RNG& rng, T current_state, TimeSpan dt,
51  const std::pair<T, ScalarType> (&transitions)[NumTransitions])
52 {
53  assert(std::all_of(std::begin(transitions), std::end(transitions),
54  [](auto& p) {
55  return p.second >= 0.0;
56  }) &&
57  "transition rates must be non-negative");
58 
59  //check if any transition happens using exponential distribution with the sum of all transition rates
60  auto sum = std::accumulate(std::begin(transitions), std::end(transitions), 0.0, [](auto&& a, auto&& t) {
61  return a + t.second;
62  });
63  if (sum <= 0) { //no transitions or all transitions have rate zero
64  return current_state;
65  }
66  auto v = ExponentialDistribution<ScalarType>::get_instance()(rng, sum);
67  if (v < dt.days()) {
68  //pick one of the possible transitions using discrete distribution
69  std::array<ScalarType, NumTransitions> rates;
70  std::transform(std::begin(transitions), std::end(transitions), rates.begin(), [](auto&& t) {
71  return t.second;
72  });
73  auto random_idx = DiscreteDistribution<size_t>::get_instance()(rng, rates);
74  return transitions[random_idx].first;
75  }
76 
77  return current_state;
78 }
79 
80 } // namespace abm
81 } // namespace mio
A duration of time.
Definition: time.h:36
ScalarType days() const
Length of time in days.
Definition: time.h:55
T random_transition(RNG &rng, T current_state, TimeSpan dt, const std::pair< T, ScalarType >(&transitions)[NumTransitions])
Select a random transition from a list of possible transitions from the current state to others.
Definition: random_events.h:50
A collection of classes to simplify handling of matrix shapes in meta programming.
Definition: models/abm/analyze_result.h:30
requires details::IsElementReference< M > RowMajorIterator< M, false > end(M &m)
create a non-const end iterator for the matrix m.
Definition: eigen_util.h:449
requires details::IsElementReference< M > RowMajorIterator< M, false > begin(M &m)
create a non-const iterator to first element of the matrix m.
Definition: eigen_util.h:421