1111#pragma once
1212
1313#include " Itinerary.hpp"
14- #include " SparseMatrix.hpp"
15- #include " ../utility/TypeTraits/is_numeric.hpp"
1614#include " ../utility/Logger.hpp"
1715#include " ../utility/Typedef.hpp"
1816
2523
2624namespace dsm {
2725 // / @brief The Agent class represents an agent in the network.
28- // / @tparam delay_t, The type of the agent's delay. It must be a numeric type (see utility/TypeTraits/is_numeric.hpp).
29- template <typename delay_t >
30- requires (is_numeric_v<delay_t >)
3126 class Agent {
3227 private:
28+ Time m_spawnTime, m_freeTime;
3329 Id m_id;
3430 std::vector<Id> m_trip;
3531 std::optional<Id> m_streetId;
3632 std::optional<Id> m_srcNodeId;
3733 std::optional<Id> m_nextStreetId;
38- delay_t m_delay;
39- double m_speed;
40- double m_distance; // Travelled distance
41- unsigned int m_time; // Travelled time
4234 size_t m_itineraryIdx;
35+ double m_speed;
36+ double m_distance; // Travelled distance
4337
4438 public:
4539 // / @brief Construct a new Agent object
40+ // / @param spawnTime The agent's spawn time
4641 // / @param id The agent's id
4742 // / @param itineraryId Optional, The agent's destination node. If not provided, the agent is a random agent
4843 // / @param srcNodeId Optional, The id of the source node of the agent
49- Agent (Id id,
44+ Agent (Time const & spawnTime,
45+ Id id,
5046 std::optional<Id> itineraryId = std::nullopt ,
5147 std::optional<Id> srcNodeId = std::nullopt );
5248 // / @brief Construct a new Agent object
49+ // / @param spawnTime The agent's spawn time
5350 // / @param id The agent's id
5451 // / @param itineraryIds The agent's itinerary
5552 // / @param srcNodeId Optional, The id of the source node of the agent
56- Agent (Id id, std::vector<Id> const & trip, std::optional<Id> srcNodeId = std::nullopt );
53+ Agent (Time const & spawnTime,
54+ Id id,
55+ std::vector<Id> const & trip,
56+ std::optional<Id> srcNodeId = std::nullopt );
5757 // / @brief Set the street occupied by the agent
5858 // / @param streetId The id of the street currently occupied by the agent
59- void setStreetId (Id streetId);
59+ void setStreetId (std::optional<Id> streetId = std:: nullopt );
6060 // / @brief Set the id of the next street
6161 // / @param nextStreetId The id of the next street
62- void setNextStreetId (Id nextStreetId) { m_nextStreetId = nextStreetId; }
62+ void setNextStreetId (Id nextStreetId);
6363 // / @brief Set the agent's speed
6464 // / @param speed, The agent's speed
6565 // / @throw std::invalid_argument, if speed is negative
6666 void setSpeed (double speed);
67- // / @brief Increment the agent's delay by 1
68- // / @throw std::overflow_error, if delay has reached its maximum value
69- void incrementDelay ();
70- // / @brief Increment the agent's delay by a given value
71- // / @param delay The agent's delay
72- // / @throw std::overflow_error, if delay has reached its maximum value
73- void incrementDelay (delay_t const delay);
74- // / @brief Decrement the agent's delay by 1
75- // / @throw std::underflow_error, if delay has reached its minimum value
76- void decrementDelay ();
77- // / @brief Increment the agent's distance by its speed * 1 second
78- void incrementDistance () { m_distance += m_speed; }
67+ // / @brief Set the agent's free time
68+ // / @param freeTime The agent's free time
69+ void setFreeTime (Time const & freeTime);
7970 // / @brief Increment the agent's distance by a given value
8071 // / @param distance The value to increment the agent's distance byù
8172 // / @throw std::invalid_argument, if distance is negative
8273 void incrementDistance (double distance);
83- // / @brief Increment the agent's time by 1
84- // / @throw std::overflow_error, if time has reached its maximum value
85- void incrementTime ();
86- // / @brief Increment the agent's time by a given value
87- // / @param time The value to increment the agent's time by
88- // / @throw std::overflow_error, if time has reached its maximum value
89- void incrementTime (unsigned int const time);
90- // / @brief Reset the agent's time to 0
91- void resetTime () { m_time = 0 ; }
9274 // / @brief Update the agent's itinerary
9375 // / @details If possible, the agent's itinerary is updated by removing the first element
9476 // / from the itinerary's vector.
@@ -101,164 +83,40 @@ namespace dsm {
10183 // / - distance = 0
10284 // / - time = 0
10385 // / - itinerary index = 0
104- void reset ();
86+ void reset (Time const & spawnTime );
10587
88+ // / @brief Get the agent's spawn time
89+ // / @return The agent's spawn time
90+ Time const & spawnTime () const ;
91+ // / @brief Get the agent's free time
92+ // / @return The agent's free time
93+ Time const & freeTime () const ;
10694 // / @brief Get the agent's id
10795 // / @return The agent's id
108- Id id () const { return m_id; }
96+ Id id () const ;
10997 // / @brief Get the agent's itinerary
11098 // / @return The agent's itinerary
11199 Id itineraryId () const ;
112100 // / @brief Get the agent's trip
113101 // / @return The agent's trip
114- std::vector<Id> const & trip () const { return m_trip; }
102+ std::vector<Id> const & trip () const ;
115103 // / @brief Get the id of the street currently occupied by the agent
116104 // / @return The id of the street currently occupied by the agent
117- std::optional<Id> streetId () const { return m_streetId; }
105+ std::optional<Id> streetId () const ;
118106 // / @brief Get the id of the source node of the agent
119107 // / @return The id of the source node of the agent
120- std::optional<Id> srcNodeId () const { return m_srcNodeId; }
108+ std::optional<Id> srcNodeId () const ;
121109 // / @brief Get the id of the next street
122110 // / @return The id of the next street
123- std::optional<Id> nextStreetId () const { return m_nextStreetId; }
111+ std::optional<Id> nextStreetId () const ;
124112 // / @brief Get the agent's speed
125113 // / @return The agent's speed
126- double speed () const { return m_speed; }
127- // / @brief Get the agent's delay
128- // / @return The agent's delay
129- delay_t delay () const { return m_delay; }
114+ double speed () const ;
130115 // / @brief Get the agent's travelled distance
131116 // / @return The agent's travelled distance
132- double distance () const { return m_distance; }
133- // / @brief Get the agent's travel time
134- // / @return The agent's travel time
135- unsigned int time () const { return m_time; }
117+ double distance () const ;
136118 // / @brief Return true if the agent is a random agent
137119 // / @return True if the agent is a random agent, false otherwise
138- bool isRandom () const { return m_trip. empty (); }
120+ bool isRandom () const ;
139121 };
140-
141- template <typename delay_t >
142- requires (is_numeric_v<delay_t >)
143- Agent<delay_t >::Agent(Id id, std::optional<Id> itineraryId, std::optional<Id> srcNodeId)
144- : m_id{id},
145- m_trip{itineraryId.has_value () ? std::vector<Id>{itineraryId.value ()}
146- : std::vector<Id>{}},
147- m_srcNodeId{srcNodeId},
148- m_nextStreetId{std::nullopt },
149- m_delay{0 },
150- m_speed{0 .},
151- m_distance{0 .},
152- m_time{0 },
153- m_itineraryIdx{0 } {}
154-
155- template <typename delay_t >
156- requires (is_numeric_v<delay_t >)
157- Agent<delay_t >::Agent(Id id, std::vector<Id> const & trip, std::optional<Id> srcNodeId)
158- : m_id{id},
159- m_trip{trip},
160- m_srcNodeId{srcNodeId},
161- m_nextStreetId{std::nullopt },
162- m_delay{0 },
163- m_speed{0 .},
164- m_distance{0 .},
165- m_time{0 },
166- m_itineraryIdx{0 } {}
167-
168- template <typename delay_t >
169- requires (is_numeric_v<delay_t >)
170- Id Agent<delay_t >::itineraryId() const {
171- assert (m_itineraryIdx < m_trip.size ());
172- return m_trip[m_itineraryIdx];
173- }
174-
175- template <typename delay_t >
176- requires (is_numeric_v<delay_t >)
177- void Agent<delay_t >::setStreetId(Id streetId) {
178- assert (m_nextStreetId.has_value () ? streetId == m_nextStreetId.value () : true );
179- m_streetId = streetId;
180- m_nextStreetId = std::nullopt ;
181- }
182-
183- template <typename delay_t >
184- requires (is_numeric_v<delay_t >)
185- void Agent<delay_t >::setSpeed(double speed) {
186- if (speed < 0 ) {
187- Logger::error (std::format (" Speed ({}) of agent {} must be positive" , speed, m_id));
188- }
189- m_speed = speed;
190- }
191- template <typename delay_t >
192- requires (is_numeric_v<delay_t >)
193- void Agent<delay_t >::updateItinerary() {
194- if (m_itineraryIdx < m_trip.size () - 1 ) {
195- ++m_itineraryIdx;
196- }
197- }
198- template <typename delay_t >
199- requires (is_numeric_v<delay_t >)
200- void Agent<delay_t >::reset() {
201- m_streetId = std::nullopt ;
202- m_delay = 0 ;
203- m_speed = 0 .;
204- m_distance = 0 .;
205- m_time = 0 ;
206- m_itineraryIdx = 0 ;
207- }
208- template <typename delay_t >
209- requires (is_numeric_v<delay_t >)
210- void Agent<delay_t >::incrementDelay() {
211- if (m_delay == std::numeric_limits<delay_t >::max ()) {
212- throw std::overflow_error (
213- Logger::buildExceptionMessage (" delay_t has reached its maximum value" ));
214- }
215- ++m_delay;
216- }
217- template <typename delay_t >
218- requires (is_numeric_v<delay_t >)
219- void Agent<delay_t >::incrementDelay(delay_t const delay) {
220- if (m_delay + delay < m_delay) {
221- throw std::overflow_error (
222- Logger::buildExceptionMessage (" delay_t has reached its maximum value" ));
223- }
224- m_delay += delay;
225- }
226- template <typename delay_t >
227- requires (is_numeric_v<delay_t >)
228- void Agent<delay_t >::decrementDelay() {
229- if (m_delay == 0 ) {
230- throw std::underflow_error (
231- Logger::buildExceptionMessage (" delay_t has reached its minimum value" ));
232- }
233- --m_delay;
234- }
235-
236- template <typename delay_t >
237- requires (is_numeric_v<delay_t >)
238- void Agent<delay_t >::incrementDistance(double distance) {
239- if (distance < 0 ) {
240- Logger::error (std::format (
241- " Distance travelled ({}) by agent {} must be positive" , distance, m_id));
242- }
243- m_distance += distance;
244- }
245-
246- template <typename delay_t >
247- requires (is_numeric_v<delay_t >)
248- void Agent<delay_t >::incrementTime() {
249- if (m_time == std::numeric_limits<unsigned int >::max ()) {
250- throw std::overflow_error (
251- Logger::buildExceptionMessage (" Time has reached its maximum value" ));
252- }
253- ++m_time;
254- }
255- template <typename delay_t >
256- requires (is_numeric_v<delay_t >)
257- void Agent<delay_t >::incrementTime(unsigned int const time) {
258- if (m_time + time < m_time) {
259- throw std::overflow_error (
260- Logger::buildExceptionMessage (" Time has reached its maximum value" ));
261- }
262- m_time += time;
263- }
264122}; // namespace dsm
0 commit comments