Line data Source code
1 : /*
2 : * Copyright (C) 2020-2025 MEmilio
3 : *
4 : * Authors: Elisabeth Kluth, Khoa Nguyen
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_ABM_TRIP_LIST_H
21 : #define MIO_ABM_TRIP_LIST_H
22 :
23 : #include "abm/location_id.h"
24 : #include "abm/mobility_data.h"
25 : #include "abm/person.h"
26 : #include "abm/person_id.h"
27 : #include "abm/time.h"
28 : #include "abm/location_type.h"
29 : #include "memilio/io/io.h"
30 : #include "memilio/io/default_serialize.h"
31 : #include <cstdint>
32 : #include <vector>
33 :
34 : namespace mio
35 : {
36 : namespace abm
37 : {
38 :
39 : /**
40 : * @brief A trip describes a change of Location from one Location to another Location.
41 : */
42 : struct Trip {
43 : //TODO: Origin is currently not used for the trips. Should we delete it then?
44 : PersonId person_id; /**< Person that makes the trip and corresponds to the index into the structure m_persons from
45 : Model, where all Person%s are saved.*/
46 : TimePoint time; ///< Daytime at which a Person changes the Location.
47 : LocationId destination; ///< Location where the Person changes to.
48 : int destination_model_id; ///< Model id of destination Location.
49 : LocationId origin; ///< Location where the Person starts the Trip.
50 : int origin_model_id; ///< Model id of origin Location.
51 : std::vector<uint32_t> cells; /**< If destination consists of different Cell%s, this gives the index of the
52 : Cell%s the Person changes to.*/
53 : TransportMode
54 : trip_mode; ///< Mode of transportation. 1:Bike, 2:Car (Driver), 3:Car (Co-Driver)), 4:Public Transport, 5:Walking, 6:Other/Unknown
55 : LocationType destination_type; ///< Type of destination Location.
56 :
57 : /**
58 : * @brief Construct a new Trip.
59 : * @param[in] id ID of the Person that makes the Trip.
60 : * @param[in] time_new Time at which a Person changes the Location this currently cant be set for s specific day just a timepoint in a day.
61 : * @param[in] destination Location where the Person changes to.
62 : * @param[in] destination_model_id Model the Person changes to.
63 : * @param[in] origin Location where the person starts the Trip.
64 : * @param[in] origin_model_id Model the Person starts the Trip.
65 : * @param[in] input_cells The index of the Cell%s the Person changes to.
66 : */
67 3 : Trip(PersonId id, TimePoint time_new, LocationId dest, int dest_model_id, LocationId orig, int orig_model_id,
68 : TransportMode mode_of_transport, LocationType type_of_activity, const std::vector<uint32_t>& input_cells = {})
69 3 : : person_id(id)
70 3 : , time(mio::abm::TimePoint(time_new.time_since_midnight().seconds()))
71 3 : , destination(dest)
72 3 : , destination_model_id(dest_model_id)
73 3 : , origin(orig)
74 3 : , origin_model_id(orig_model_id)
75 3 : , cells(input_cells)
76 3 : , trip_mode(mode_of_transport)
77 3 : , destination_type(type_of_activity)
78 : {
79 3 : }
80 :
81 198 : Trip(PersonId id, TimePoint time_new, LocationId dest, LocationId orig, TransportMode mode_of_transport,
82 : LocationType type_of_activity, const std::vector<uint32_t>& input_cells = {})
83 198 : : person_id(id)
84 198 : , time(mio::abm::TimePoint(time_new.time_since_midnight().seconds()))
85 198 : , destination(dest)
86 198 : , destination_model_id(0)
87 198 : , origin(orig)
88 198 : , origin_model_id(0)
89 198 : , cells(input_cells)
90 198 : , trip_mode(mode_of_transport)
91 198 : , destination_type(type_of_activity)
92 : {
93 198 : }
94 :
95 189 : Trip(PersonId id, TimePoint time_new, LocationId dest, LocationId orig, LocationType type_of_activity,
96 : const std::vector<uint32_t>& input_cells = {})
97 189 : : Trip(id, time_new, dest, orig, mio::abm::TransportMode::Unknown, type_of_activity, input_cells)
98 : {
99 189 : }
100 :
101 9 : Trip(PersonId id, TimePoint time_new, LocationId dest, LocationType type_of_activity,
102 : const std::vector<uint32_t>& input_cells = {})
103 9 : : Trip(id, time_new, dest, dest, mio::abm::TransportMode::Unknown, type_of_activity, input_cells)
104 : {
105 9 : }
106 :
107 : /**
108 : * @brief Compare two Trip%s.
109 : */
110 : bool operator==(const Trip& other) const
111 : {
112 : return (person_id == other.person_id) && (time == other.time) && (destination == other.destination) &&
113 : (origin == other.origin);
114 : }
115 :
116 18 : auto default_serialize()
117 : {
118 36 : return Members("Trip")
119 54 : .add("person_id", person_id)
120 54 : .add("time", time)
121 54 : .add("destination", destination)
122 54 : .add("origin", origin);
123 : }
124 : };
125 :
126 : /**
127 : * @brief A list of Trip%s a Person follows.
128 : */
129 : class TripList
130 : {
131 : public:
132 : /**
133 : * @brief Construct empty TripList.
134 : */
135 : TripList();
136 :
137 : /**
138 : * @brief Get the next Trip.
139 : * @param weekend Whether the Trip%s during the week or on the weekend are used.
140 : */
141 : const Trip& get_next_trip(bool weekend) const;
142 :
143 : /**
144 : * @brief Get the time at which the next Trip will happen.
145 : * @param weekend Whether the Trip%s during the week or on the weekend are used.
146 : */
147 : TimePoint get_next_trip_time(bool weekend) const;
148 :
149 : /**
150 : * @brief Add a Trip to the trip list.
151 : * @param[in] trip The Trip to be added.
152 : * @param[in] weekend If the Trip is made on a weekend day.
153 : */
154 : void add_trip(Trip trip, bool weekend = false);
155 :
156 : /**
157 : * @brief Use the same TripList for weekend and weekday.
158 : */
159 : void use_weekday_trips_on_weekend();
160 :
161 : /**
162 : * @brief Increment the current index to select the next Trip.
163 : */
164 193 : void increase_index()
165 : {
166 193 : m_current_index++;
167 193 : }
168 :
169 : /**
170 : * @brief Reset the current index to 0.
171 : */
172 83 : void reset_index()
173 : {
174 83 : m_current_index = 0;
175 83 : }
176 :
177 : /**
178 : * @brief Get the length of the TripList.
179 : * @param weekend Whether the Trip%s during the week or on the weekend are used.
180 : */
181 1063 : size_t num_trips(bool weekend = false) const
182 : {
183 1063 : return weekend ? m_trips_weekend.size() : m_trips_weekday.size();
184 : }
185 :
186 : /**
187 : * @brief Get the current index.
188 : */
189 1256 : uint32_t get_current_index() const
190 : {
191 1256 : return m_current_index;
192 : }
193 :
194 : /// This method is used by the default serialization feature.
195 18 : auto default_serialize()
196 : {
197 36 : return Members("TestingScheme")
198 54 : .add("trips_weekday", m_trips_weekday)
199 54 : .add("trips_weekend", m_trips_weekend)
200 54 : .add("index", m_current_index);
201 : }
202 :
203 : private:
204 : std::vector<Trip> m_trips_weekday; ///< The list of Trip%s a Person makes on a weekday.
205 : std::vector<Trip> m_trips_weekend; ///< The list of Trip%s a Person makes on a weekend day.
206 : uint32_t m_current_index; ///< The index of the Trip a Person makes next.
207 : };
208 :
209 : } // namespace abm
210 :
211 : /// @brief Creates an instance of abm::Trip for default serialization.
212 : template <>
213 : struct DefaultFactory<abm::Trip> {
214 9 : static abm::Trip create()
215 : {
216 9 : return abm::Trip{abm::PersonId{}, abm::TimePoint{}, abm::LocationId{}, abm::LocationType{}};
217 : }
218 : };
219 :
220 : } // namespace mio
221 :
222 : #endif
|