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