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"
29 #include "memilio/mobility/graph.h"
30 
31 #include <algorithm>
32 #include <utility>
33 
34 namespace mio
35 {
39 template <class... History>
41 {
42 
43 public:
45 
46  template <class... Args, typename = std::enable_if_t<std::is_constructible<Sim, Args...>::value, void>>
47  ABMSimulationNode(std::tuple<History...>&& history, Args&&... args)
48  : m_simulation(std::forward<Args>(args)...)
49  , m_history(history)
50  {
51  }
52 
57  {
58  return m_simulation;
59  }
60  const Sim& get_simulation() const
61  {
62  return m_simulation;
63  }
64 
68  std::tuple<History...>& get_history()
69  {
70  return m_history;
71  }
72  const std::tuple<History...>& get_history() const
73  {
74  return m_history;
75  }
76 
85  {
86  m_simulation.advance(t + dt, std::get<0>(m_history));
87  }
88 
89 private:
91  std::tuple<History...> m_history;
92 };
93 
97 template <class... History>
99 {
100 
101 public:
110  abm::TimePoint /*t*/)
111  {
112  auto& model_from = node_from.get_simulation().get_model();
113  auto& model_to = node_to.get_simulation().get_model();
114  auto& persons_to_change = model_from.get_person_buffer();
115  //sort vector such that we start removing the persons from the bottom
116  std::sort(persons_to_change.begin(), persons_to_change.end());
117  //iterate over all persons that change from node_from
118  for (int i = int(persons_to_change.size()) - 1; i >= 0; --i) {
119  auto& person = model_from.get_persons()[persons_to_change[i]];
120  auto target_type = person.get_location_type();
121  if (target_type == abm::LocationType::Invalid) {
122  target_type = model_to.get_location(person.get_location()).get_type();
123  }
124  //check if Person uses this edge
125  if (person.get_assigned_location_model_id(target_type) == model_to.get_id()) {
126 
127  auto target_id = person.get_assigned_location(target_type);
128  //set correct location for person
129  person.set_location(target_type, target_id, model_to.get_id());
130  //add person to model_to
131  model_to.add_person(std::move(person));
132  //remove person from model_from
133  model_from.remove_person(persons_to_change[i]);
134  //correct indices in persons buffer from node_from
135  //here it is required that the vector is sorted
136  for (size_t j = i + 1; j < persons_to_change.size(); ++j) {
137  persons_to_change[j]--;
138  }
139  //delete current index from list
140  persons_to_change.erase(persons_to_change.begin() + i);
141  }
142  }
143  }
144 };
145 
156 template <class... History>
159 {
160  edge.apply_mobility(node_from, node_to, t);
161 }
162 
170 template <class... History>
172 {
173  node.advance(t, dt);
174 }
175 
184 template <class... History>
185 GraphSimulation<ScalarType, Graph<ABMSimulationNode<History...>, ABMMobilityEdge<History...>>, abm::TimePoint,
186  abm::TimeSpan,
188  mio::ABMSimulationNode<History...>&, mio::ABMSimulationNode<History...>&),
192 {
193  return make_graph_sim<ScalarType>(t0, dt, std::move(graph), &advance_model<History...>,
194  &apply_mobility<History...>);
195 }
196 
197 } // namespace mio
198 
199 #endif // MIO_ABM_GRAPH_MOBILITY_H
Represents the mobility between two nodes.
Definition: graph_abm_mobility.h:99
void apply_mobility(ABMSimulationNode< History... > &node_from, ABMSimulationNode< History... > &node_to, abm::TimePoint)
Exchanges persons via the edge.
Definition: graph_abm_mobility.h:109
Represents the ABM simulation in one node of the ABM graph model.
Definition: graph_abm_mobility.h:41
ABMSimulationNode(std::tuple< History... > &&history, Args &&... args)
Definition: graph_abm_mobility.h:47
abm::Simulation< abm::GraphABModel > Sim
Definition: graph_abm_mobility.h:44
Sim & get_simulation()
Get abm simulation in this node.
Definition: graph_abm_mobility.h:56
const std::tuple< History... > & get_history() const
Definition: graph_abm_mobility.h:72
std::tuple< History... > m_history
Definition: graph_abm_mobility.h:91
std::tuple< History... > & get_history()
Get history object(s) in this node.
Definition: graph_abm_mobility.h:68
Sim m_simulation
ABM Simulation of the node.
Definition: graph_abm_mobility.h:90
const Sim & get_simulation() const
Definition: graph_abm_mobility.h:60
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:84
generic graph structure
Definition: graph.h:148
History class that handles writers and loggers.
Definition: history.h:70
std::vector< size_t > & get_person_buffer()
Get person buffer.
Definition: graph_abmodel.h:57
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:190
auto i
Definition: io.h:810
void advance_model(abm::TimePoint t, abm::TimeSpan dt, ABMSimulationNode< History... > &node)
Node functor for abm graph simulation.
Definition: graph_abm_mobility.h:171
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:157
Definition: io.h:95