graph_abm_mobility.h Source File

CPP API: graph_abm_mobility.h Source File
graph_abm_mobility.h
Go to the documentation of this file.
1 /*
2 * Copyright (C) 2020-2026 MEmilio
3 *
4 * Authors: Julia Bicker
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 
21 #ifndef MIO_ABM_GRAPH_MOBILITY_H
22 #define MIO_ABM_GRAPH_MOBILITY_H
23 
24 #include "abm/simulation.h"
25 #include "abm/time.h"
26 #include "abm/location_type.h"
27 #include "abm/parameters.h"
28 #include "abm/person.h"
29 #include "abm/person_id.h"
30 #include "abm/model_functions.h"
33 #include "memilio/mobility/graph.h"
35 #include <algorithm>
36 #include <cstddef>
37 #include <iostream>
38 #include <utility>
39 #include <vector>
40 
41 namespace mio
42 {
46 template <class... History>
48 {
49 
50 public:
52 
53  template <class... Args, typename = std::enable_if_t<std::is_constructible<Sim, Args...>::value, void>>
54  ABMSimulationNode(std::tuple<History...>&& history, Args&&... args)
55  : m_simulation(std::forward<Args>(args)...)
56  , m_history(history)
57  {
58  }
59 
64  {
65  return m_simulation;
66  }
67  const Sim& get_simulation() const
68  {
69  return m_simulation;
70  }
71 
75  std::tuple<History...>& get_history()
76  {
77  return m_history;
78  }
79  const std::tuple<History...>& get_history() const
80  {
81  return m_history;
82  }
83 
92  {
93  m_simulation.advance(t + dt, std::get<0>(m_history));
94  }
95 
96 private:
98  std::tuple<History...> m_history;
99 };
100 
104 template <class... History>
106 {
107 
108 public:
117  abm::TimePoint /*t*/)
118  {
119  auto& model_from = node_from.get_simulation().get_model();
120  auto& model_to = node_to.get_simulation().get_model();
121  auto& persons_to_change = model_from.get_person_buffer();
122  //sort vector such that we start removing the persons from the bottom
123  std::sort(persons_to_change.begin(), persons_to_change.end());
124  //iterate over all persons that change from node_from
125  for (int i = int(persons_to_change.size()) - 1; i >= 0; --i) {
126  auto& person = model_from.get_persons()[persons_to_change[i]];
127  auto target_type = person.get_location_type();
128  if (target_type == abm::LocationType::Invalid) {
129  target_type = model_to.get_location(person.get_location()).get_type();
130  }
131  //check if Person uses this edge
132  if (person.get_assigned_location_model_id(target_type) == model_to.get_id()) {
133 
134  auto target_id = person.get_assigned_location(target_type);
135  //set correct location for person
136  person.set_location(target_type, target_id, model_to.get_id());
137  //add person to model_to
138  model_to.add_person(std::move(person));
139  //remove person from model_from
140  model_from.remove_person(persons_to_change[i]);
141  //correct indices in persons buffer from node_from
142  //here it is required that the vector is sorted
143  for (size_t j = i + 1; j < persons_to_change.size(); ++j) {
144  persons_to_change[j]--;
145  }
146  //delete current index from list
147  persons_to_change.erase(persons_to_change.begin() + i);
148  }
149  }
150  }
151 };
152 
163 template <class... History>
166 {
167  edge.apply_mobility(node_from, node_to, t);
168 }
169 
177 template <class... History>
179 {
180  node.advance(t, dt);
181 }
182 
191 template <class... History>
192 GraphSimulation<ScalarType, Graph<ABMSimulationNode<History...>, ABMMobilityEdge<History...>>, abm::TimePoint,
193  abm::TimeSpan,
195  mio::ABMSimulationNode<History...>&, mio::ABMSimulationNode<History...>&),
199 {
200  return make_graph_sim<ScalarType>(t0, dt, std::move(graph), &advance_model<History...>,
201  &apply_mobility<History...>);
202 }
203 
204 } // namespace mio
205 
206 #endif // MIO_ABM_GRAPH_MOBILITY_H
Represents the mobility between two nodes.
Definition: graph_abm_mobility.h:106
void apply_mobility(ABMSimulationNode< History... > &node_from, ABMSimulationNode< History... > &node_to, abm::TimePoint)
Exchanges persons via the edge.
Definition: graph_abm_mobility.h:116
Represents the ABM simulation in one node of the ABM graph model.
Definition: graph_abm_mobility.h:48
ABMSimulationNode(std::tuple< History... > &&history, Args &&... args)
Definition: graph_abm_mobility.h:54
Sim & get_simulation()
Get abm simulation in this node.
Definition: graph_abm_mobility.h:63
const std::tuple< History... > & get_history() const
Definition: graph_abm_mobility.h:79
std::tuple< History... > m_history
Definition: graph_abm_mobility.h:98
std::tuple< History... > & get_history()
Get history object(s) in this node.
Definition: graph_abm_mobility.h:75
Sim m_simulation
ABM Simulation of the node.
Definition: graph_abm_mobility.h:97
const Sim & get_simulation() const
Definition: graph_abm_mobility.h:67
void advance(mio::abm::TimePoint t, mio::abm::TimeSpan dt)
advances the simulation in this node by t+dt and logs information in History object(s)
Definition: graph_abm_mobility.h:91
abm::Simulation< GraphABModel > Sim
Definition: graph_abm_mobility.h:51
std::vector< size_t > & get_person_buffer()
Get person buffer.
Definition: graph_abmodel.h:60
generic graph structure
Definition: graph.h:153
History class that handles writers and loggers.
Definition: history.h:70
Model & get_model()
Get the Model that this Simulation evolves.
Definition: models/abm/simulation.h:91
void advance(TimePoint tmax, History &... history)
Run the Simulation from the current time to tmax.
Definition: models/abm/simulation.h:70
Represents a point in time.
Definition: time.h:175
A duration of time.
Definition: time.h:36
double ScalarType
Configuration of memilio library.
Definition: memilio/config.h:30
trait_value< T >::RETURN_TYPE & value(T &x)
Definition: ad.hpp:3308
A collection of classes to simplify handling of matrix shapes in meta programming.
Definition: models/abm/analyze_result.h:30
GraphSimulation< ScalarType, Graph< ABMSimulationNode< History... >, ABMMobilityEdge< History... > >, abm::TimePoint, abm::TimeSpan, void(*)(mio::abm::TimePoint, mio::abm::TimeSpan, mio::ABMMobilityEdge< History... > &, mio::ABMSimulationNode< History... > &, mio::ABMSimulationNode< History... > &), void(*)(mio::abm::TimePoint, mio::abm::TimeSpan, mio::ABMSimulationNode< History... > &)> make_abm_graph_sim(abm::TimePoint t0, abm::TimeSpan dt, Graph< ABMSimulationNode< History... >, ABMMobilityEdge< History... >> &&graph)
Creates an abm graph simulation.
Definition: graph_abm_mobility.h:197
auto i
Definition: io.h:809
void advance_model(abm::TimePoint t, abm::TimeSpan dt, ABMSimulationNode< History... > &node)
Node functor for abm graph simulation.
Definition: graph_abm_mobility.h:178
void apply_mobility(abm::TimePoint t, abm::TimeSpan, ABMMobilityEdge< History... > &edge, ABMSimulationNode< History... > &node_from, ABMSimulationNode< History... > &node_to)
Edge functor for abm graph simulation.
Definition: graph_abm_mobility.h:164
Definition: io.h:94