Line data Source code
1 : /****************************************************************************/
2 : // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3 : // Copyright (C) 2001-2026 German Aerospace Center (DLR) and others.
4 : // This program and the accompanying materials are made available under the
5 : // terms of the Eclipse Public License 2.0 which is available at
6 : // https://www.eclipse.org/legal/epl-2.0/
7 : // This Source Code may also be made available under the following Secondary
8 : // Licenses when the conditions for such availability set forth in the Eclipse
9 : // Public License 2.0 are satisfied: GNU General Public License, version 2
10 : // or later which is available at
11 : // https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
12 : // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
13 : /****************************************************************************/
14 : /// @file MESegment.h
15 : /// @author Daniel Krajzewicz
16 : /// @date Tue, May 2005
17 : ///
18 : // A single mesoscopic segment (cell)
19 : /****************************************************************************/
20 : #pragma once
21 : #include <config.h>
22 :
23 : #include <vector>
24 : #include <cassert>
25 : #include <utils/common/SUMOVehicleClass.h>
26 : #include <utils/common/Named.h>
27 : #include <utils/common/SUMOTime.h>
28 : #include <microsim/MSMoveReminder.h>
29 :
30 :
31 : // ===========================================================================
32 : // class declarations
33 : // ===========================================================================
34 : class SUMOVehicle;
35 : class MSEdge;
36 : class MSLink;
37 : class MSDetectorFileOutput;
38 : class MSVehicleControl;
39 : class MEVehicle;
40 : class OutputDevice;
41 :
42 :
43 : // ===========================================================================
44 : // class definitions
45 : // ===========================================================================
46 : /**
47 : * @class MESegment
48 : * @brief A single mesoscopic segment (cell)
49 : */
50 : class MESegment : public Named {
51 : public:
52 : static const double DO_NOT_PATCH_JAM_THRESHOLD;
53 : static const int PARKING_QUEUE = -1;
54 : /// @brief special param value
55 : static const std::string OVERRIDE_TLS_PENALTIES;
56 :
57 : /// @brief edge type specific meso parameters
58 : struct MesoEdgeType {
59 : SUMOTime tauff;
60 : SUMOTime taufj;
61 : SUMOTime taujf;
62 : SUMOTime taujj;
63 : double jamThreshold;
64 : bool junctionControl;
65 : double tlsPenalty;
66 : double tlsFlowPenalty;
67 : SUMOTime minorPenalty;
68 : bool overtaking;
69 : double edgeLength;
70 : };
71 :
72 :
73 : private:
74 2240772 : class Queue {
75 : public:
76 735599 : Queue(const SVCPermissions permissions) : myPermissions(permissions) {}
77 : inline int size() const {
78 198614225 : return (int)myVehicles.size();
79 : }
80 : inline const std::vector<MEVehicle*>& getVehicles() const {
81 623158 : return myVehicles;
82 : }
83 : MEVehicle* remove(MEVehicle* v);
84 : inline std::vector<MEVehicle*>& getModifiableVehicles() {
85 26031030 : return myVehicles;
86 : }
87 : inline double getOccupancy() const {
88 206081596 : return myOccupancy;
89 : }
90 : inline void setOccupancy(const double occ) {
91 26025228 : myOccupancy = occ;
92 : }
93 : inline bool allows(SUMOVehicleClass vclass) const {
94 64863511 : return (myPermissions & vclass) == vclass;
95 : }
96 :
97 : /// @brief return the next time at which a vehicle may enter this queue
98 : inline SUMOTime getEntryBlockTime() const {
99 17929169 : return myEntryBlockTime;
100 : }
101 :
102 : /// @brief set the next time at which a vehicle may enter this queue
103 : inline void setEntryBlockTime(SUMOTime entryBlockTime) {
104 25312557 : myEntryBlockTime = entryBlockTime;
105 25311401 : }
106 :
107 : inline SUMOTime getBlockTime() const {
108 69729628 : return myBlockTime;
109 : }
110 : inline void setBlockTime(SUMOTime t) {
111 1156 : myBlockTime = t;
112 25326853 : }
113 :
114 : inline void setPermissions(SVCPermissions p) {
115 48 : myPermissions = p;
116 2210 : }
117 :
118 : void addDetector(MSMoveReminder* data);
119 :
120 : void addReminders(MEVehicle* veh) const;
121 :
122 : private:
123 : /// The vClass permissions for this queue
124 : SVCPermissions myPermissions;
125 :
126 : std::vector<MEVehicle*> myVehicles;
127 :
128 : /// @brief The occupied space (in m) in the queue
129 : double myOccupancy = 0.;
130 :
131 : /// @brief The block time for vehicles who wish to enter this queue
132 : SUMOTime myEntryBlockTime = SUMOTime_MIN;
133 :
134 : /// @brief The block time
135 : SUMOTime myBlockTime = -1;
136 :
137 : /// @brief The data collection for all kinds of detectors
138 : std::vector<MSMoveReminder*> myDetectorData;
139 :
140 : };
141 :
142 : public:
143 : /** @brief constructor
144 : * @param[in] id The id of this segment (currently: "<EDGEID>:<SEGMENTNO>")
145 : * @param[in] parent The edge this segment is located within
146 : * @param[in] next The following segment (belonging to the same edge)
147 : * @param[in] length The segment's length
148 : * @param[in] speed The speed allowed on this segment
149 : * @param[in] idx The running index of this segment within the segment's edge
150 : * @param[in] multiQueue whether to install multiple queues on this segment
151 : * @param[in] edgeType edge type specific meso parameters such as the different taus
152 : */
153 : MESegment(const std::string& id,
154 : const MSEdge& parent, MESegment* next,
155 : const double length, const double speed,
156 : const int idx,
157 : const bool multiQueue,
158 : const MesoEdgeType& edgeType);
159 :
160 : /// @brief set model parameters (may be updated from additional file after network loading is complete)
161 : void initSegment(const MesoEdgeType& edgeType, const MSEdge& parent, const double capacity);
162 :
163 : /// @name Measure collection
164 : /// @{
165 :
166 : /** @brief Adds a data collector for a detector to this segment
167 : *
168 : * @param[in] data The data collector to add
169 : * @param[in] queueIndex The queue (aka lane) to use, -1 means all
170 : */
171 : void addDetector(MSMoveReminder* data, int queueIndex = -1);
172 :
173 : /** @brief Removes a data collector for a detector from this segment
174 : *
175 : * @param[in] data The data collector to remove
176 : * @note: currently not used
177 : */
178 : // void removeDetector(MSMoveReminder* data);
179 :
180 : /** @brief Updates data of a detector for one or all vehicle queues
181 : *
182 : * @param[in] data The detector data to update
183 : * @param[in] queueIndex The queue (aka lane) to use, -1 means all
184 : */
185 : void prepareDetectorForWriting(MSMoveReminder& data, int queueIndex = -1);
186 : /// @}
187 :
188 : /** @brief Returns whether the given vehicle would still fit into the segment
189 : *
190 : * @param[in] veh The vehicle to check space for
191 : * @param[in] entryTime The time at which the vehicle wants to enter
192 : * @param[out] qIdx The index of the queue the vehicle should choose
193 : * @param[in] init whether the check is done at insertion time
194 : * @return the earliest time a vehicle may be added to this segment
195 : */
196 : SUMOTime hasSpaceFor(const MEVehicle* const veh, const SUMOTime entryTime, int& qIdx, const bool init = false) const;
197 :
198 : /** @brief Inserts (emits) vehicle into the segment
199 : *
200 : * @param[in] veh The vehicle to emit
201 : * @param[in] time The emission time
202 : * @return Whether the emission was successful
203 : */
204 : bool initialise(MEVehicle* veh, SUMOTime time);
205 :
206 : /** @brief Returns the total number of cars on the segment
207 : *
208 : * @return the total number of cars on the segment
209 : */
210 0 : inline int getCarNumber() const {
211 184323989 : return myNumVehicles;
212 : }
213 :
214 : /// @brief return the number of queues
215 : inline int numQueues() const {
216 833428 : return (int)myQueues.size();
217 : }
218 : /** @brief Returns the cars in the queue with the given index for visualization
219 : * @return the Queue (XXX not thread-safe!)
220 : */
221 : inline const std::vector<MEVehicle*>& getQueue(int index) const {
222 : assert(index < (int)myQueues.size());
223 621831 : return myQueues[index].getVehicles();
224 : }
225 :
226 : inline SUMOTime getQueueBlockTime(int index) const {
227 : assert(index < (int)myQueues.size());
228 : return myQueues[index].getBlockTime();
229 : }
230 :
231 : /** @brief Returns the running index of the segment in the edge (0 is the most upstream).
232 : *
233 : * @return the running index of the segment in the edge
234 : */
235 : inline int getIndex() const {
236 8585021 : return myIndex;
237 : }
238 :
239 : /** @brief Returns the following segment on the same edge (0 if it is the last).
240 : *
241 : * @return the following segment on the same edge (0 if it is the last)
242 : */
243 : inline MESegment* getNextSegment() const {
244 255235191 : return myNextSegment;
245 : }
246 :
247 : /** @brief Returns the length of the segment in meters.
248 : *
249 : * @return the length of the segment
250 : */
251 : inline double getLength() const {
252 117184825 : return myLength;
253 : }
254 :
255 : /** @brief Returns the sum of the lengths of all usable lanes of the segment in meters.
256 : *
257 : * @return the capacity of the segment
258 : */
259 : inline double getCapacity() const {
260 : return myCapacity;
261 : }
262 :
263 : /** @brief Returns the occupany of the segment (the sum of the vehicle lengths + minGaps)
264 : *
265 : * @return the occupany of the segment in meters
266 : */
267 : inline double getBruttoOccupancy() const {
268 : double occ = 0.;
269 364274 : for (const Queue& q : myQueues) {
270 183094 : occ += q.getOccupancy();
271 : }
272 : return occ;
273 : }
274 :
275 : /** @brief Returns the relative occupany of the segment (percentage of road used))
276 : * @return the occupany of the segment in percent
277 : */
278 0 : inline double getRelativeOccupancy() const {
279 0 : return getBruttoOccupancy() / myCapacity;
280 : }
281 :
282 : /** @brief Returns the relative occupany of the segment (percentage of road used))
283 : * at which the segment is considered jammed
284 : * @return the jam threshold of the segment in percent
285 : */
286 : inline double getRelativeJamThreshold() const {
287 0 : return myJamThreshold / myCapacity;
288 : }
289 :
290 : /** @brief Returns the average speed of vehicles on the segment in meters per second.
291 : * If there is no vehicle on the segment it returns the maximum allowed speed
292 : * @param[in] useCache whether to use a cached value if available
293 : * @note this value is cached in myMeanSpeed. Since caching only takes place
294 : * once every simstep there is a potential for side-influences (i.e. GUI calls to
295 : * this method, ...) For that reason the simulation logic doesn't use the cache.
296 : * This shouldn't matter much for speed since it is only used during
297 : * initializsation of vehicles onto the segment.
298 : * @return the average speed on the segment
299 : */
300 : double getMeanSpeed(bool useCache) const;
301 :
302 : /// @brief reset myLastMeanSpeedUpdate
303 : void resetCachedSpeeds();
304 :
305 : /// @brief wrapper to satisfy the FunctionBinding signature
306 0 : inline double getMeanSpeed() const {
307 25370207 : return getMeanSpeed(true);
308 : }
309 :
310 :
311 : void writeVehicles(OutputDevice& of) const;
312 :
313 : /** @brief Removes the given car from the edge's que
314 : *
315 : * @param[in] v The vehicle to remove
316 : * @param[in] leaveTime The time at which the vehicle is leaving the que
317 : * @param[in] reason The reason for removing to send to reminders
318 : * @return The next first vehicle to add to the net's que
319 : */
320 : MEVehicle* removeCar(MEVehicle* v, SUMOTime leaveTime, const MSMoveReminder::Notification reason);
321 :
322 : /** @brief Returns the link the given car will use when passing the next junction
323 : *
324 : * This returns non-zero values only for the last segment and only
325 : * if junction control is enabled.
326 : *
327 : * @param[in] veh The vehicle in question
328 : * @param[in] tlsPenalty Whether the link should be returned for computing tlsPenalty
329 : * @return The link to use or 0 without junction control
330 : */
331 : MSLink* getLink(const MEVehicle* veh, bool tlsPenalty = false) const;
332 :
333 : /** @brief Returns whether the vehicle may use the next link
334 : *
335 : * In case of disabled junction control it returns always true.
336 : *
337 : * @param[in] veh The vehicle in question
338 : * @return Whether it may pass to the next segment
339 : */
340 : bool isOpen(const MEVehicle* veh) const;
341 :
342 : /** @brief Removes the vehicle from the segment, adapting its parameters
343 : *
344 : * @param[in] veh The vehicle in question
345 : * @param[in] next The subsequent segment for delay calculation
346 : * @param[in] time the leave time
347 : * @todo Isn't always time == veh->getEventTime?
348 : */
349 : void send(MEVehicle* veh, MESegment* const next, const int nextQIdx, SUMOTime time, const MSMoveReminder::Notification reason);
350 :
351 : /** @brief Adds the vehicle to the segment, adapting its parameters
352 : *
353 : * @param[in] veh The vehicle in question
354 : * @param[in] time the leave time
355 : * @param[in] isDepart whether the vehicle just departed
356 : * @todo Isn't always time == veh->getEventTime?
357 : */
358 : void receive(MEVehicle* veh, const int qIdx, SUMOTime time, const bool isDepart = false, const bool isTeleport = false, const bool newEdge = false);
359 :
360 :
361 : /** @brief tries to remove any car from this segment
362 : *
363 : * @param[in] currentTime the current time
364 : * @return Whether vaporization was successful
365 : * @note: cars removed via this method do NOT count as arrivals */
366 : bool vaporizeAnyCar(SUMOTime currentTime, const MSDetectorFileOutput* filter);
367 :
368 : /** @brief Returns the edge this segment belongs to
369 : * @return the edge this segment belongs to
370 : */
371 : inline const MSEdge& getEdge() const {
372 87946865 : return myEdge;
373 : }
374 :
375 :
376 : /** @brief reset mySpeed and patch the speed of
377 : * all vehicles in it. Also set/recompute myJamThreshold
378 : * @param[in] jamThresh follows the semantic of option meso-jam-threshold
379 : */
380 : void setSpeed(double newSpeed, SUMOTime currentTime, double jamThresh = DO_NOT_PATCH_JAM_THRESHOLD, int qIdx = -1);
381 :
382 : /** @brief Returns the (planned) time at which the next vehicle leaves this segment
383 : * @return The time the vehicle thinks it leaves
384 : */
385 : SUMOTime getEventTime() const;
386 :
387 : /// @brief Like getEventTime but returns seconds (for visualization)
388 0 : inline double getEventTimeSeconds() const {
389 0 : return STEPS2TIME(getEventTime());
390 : }
391 :
392 : /// @brief get the last headway time in seconds
393 0 : inline double getLastHeadwaySeconds() const {
394 0 : return STEPS2TIME(myLastHeadway);
395 : }
396 :
397 : /// @brief get the earliest entry time in seconds
398 0 : inline double getEntryBlockTimeSeconds() const {
399 : SUMOTime t = SUMOTime_MAX;
400 0 : for (const Queue& q : myQueues) {
401 : t = MIN2(t, q.getEntryBlockTime());
402 : }
403 0 : return STEPS2TIME(t);
404 : }
405 :
406 : /// @brief Get the waiting time for vehicles in all queues
407 : double getWaitingSeconds() const;
408 :
409 : /// @name State saving/loading
410 : /// @{
411 :
412 : /** @brief Saves the state of this segment into the given stream
413 : *
414 : * Some internal values which must be restored are saved as well as ids of
415 : * the vehicles stored in internal queues and the last departures of connected
416 : * edges.
417 : *
418 : * @param[in, filled] out The (possibly binary) device to write the state into
419 : * @todo What about throwing an IOError?
420 : */
421 : void saveState(OutputDevice& out) const;
422 :
423 : /** @brief Remove all vehicles before quick-loading state */
424 : void clearState();
425 :
426 : /** @brief Loads the state of this segment with the given parameters
427 : *
428 : * This method is called for every internal que the segment has.
429 : * Every vehicle is retrieved from the given MSVehicleControl and added to this
430 : * segment. Then, the internal queues that store vehicles dependant to their next
431 : * edge are filled the same way. Then, the departure of last vehicles onto the next
432 : * edge are restored.
433 : *
434 : * @param[in] vehs The vehicles for the current que
435 : * @param[in] blockTime The time the last vehicle left the que
436 : * @param[in] queIdx The index of the current que
437 : * @todo What about throwing an IOError?
438 : * @todo What about throwing an error if something else fails (a vehicle can not be referenced)?
439 : */
440 : void loadState(const std::vector<SUMOVehicle*>& vehs, const SUMOTime blockTime, const SUMOTime entryBlockTime, const int queIdx);
441 : /// @}
442 :
443 :
444 : /** @brief returns all vehicles (for debugging)
445 : */
446 : std::vector<const MEVehicle*> getVehicles() const;
447 :
448 : /** @brief returns flow based on headway
449 : * @note: returns magic number 10000 when headway cannot be computed
450 : */
451 : double getFlow() const;
452 :
453 : /// @brief whether the given segment is 0 or encodes vaporization
454 : static inline bool isInvalid(const MESegment* segment) {
455 62607646 : return segment == nullptr || segment == &myVaporizationTarget;
456 : }
457 :
458 : /// @brief return a time after earliestEntry at which a vehicle may be inserted at full speed
459 : SUMOTime getNextInsertionTime(SUMOTime earliestEntry) const;
460 :
461 : /// @brief return the remaining physical space on this segment
462 : inline int remainingVehicleCapacity(const double vehLength) const {
463 : int cap = 0;
464 153230 : for (const Queue& q : myQueues) {
465 77080 : if (q.getOccupancy() == 0. && myQueueCapacity < vehLength) {
466 : // even small segments can hold at least one vehicle
467 0 : cap += 1;
468 : } else {
469 77080 : cap += (int)((myQueueCapacity - q.getOccupancy()) / vehLength);
470 : }
471 : }
472 : return cap;
473 : }
474 :
475 : /// @brief return the minimum headway-time with which vehicles may enter or leave this segment
476 : inline SUMOTime getMinimumHeadwayTime() const {
477 76150 : return myTau_ff;
478 : }
479 :
480 : /// @brief add this lanes MoveReminders to the given vehicle
481 : void addReminders(MEVehicle* veh) const;
482 :
483 : /** @brief Returns the penalty time for passing a link (if using gMesoTLSPenalty > 0 or gMesoMinorPenalty > 0)
484 : * @param[in] veh The vehicle in question
485 : * @return The time penalty
486 : */
487 : SUMOTime getLinkPenalty(const MEVehicle* veh) const;
488 :
489 : /// @brief called when permissions change due to Rerouter or TraCI
490 : void updatePermissions();
491 :
492 : /// @brief whether the traffic light should use normal junction control despite penalty options
493 : void overrideTLSPenalty() {
494 132 : myTLSPenalty = false;
495 132 : }
496 :
497 : /// @brief convert net time gap (leader back to follower front) to gross time gap (leader front to follower front)
498 : inline SUMOTime getMinTauWithVehLength(double lengthWithGap, double vehicleTau) const {
499 714297 : return (SUMOTime)((double)myTau_ff * vehicleTau + lengthWithGap * myTau_length);
500 : }
501 :
502 : private:
503 : bool overtake();
504 :
505 : void setSpeedForQueue(double newSpeed, SUMOTime currentTime,
506 : SUMOTime blockTime, const std::vector<MEVehicle*>& vehs);
507 :
508 : /** @brief compute the new arrival time when switching speed
509 : */
510 : SUMOTime newArrival(const MEVehicle* const v, double newSpeed, SUMOTime currentTime);
511 :
512 : /// @brief whether a leader in any queue is blocked
513 : bool hasBlockedLeader() const;
514 :
515 : /** @brief compute a value for myJamThreshold
516 : * if jamThresh is negative, compute a value which allows free flow at mySpeed
517 : * interpret jamThresh as the relative occupation at which jam starts
518 : */
519 : void recomputeJamThreshold(double jamThresh);
520 :
521 : /// @brief compute jam threshold for the given speed and jam-threshold option
522 : double jamThresholdForSpeed(double speed, double jamThresh) const;
523 :
524 : /// @brief whether the given link may be passed because the option meso-junction-control.limited is set
525 : bool limitedControlOverride(const MSLink* link) const;
526 :
527 : /// @brief convert net time gap (leader back to follower front) to gross time gap (leader front to follower front)
528 : inline SUMOTime tauWithVehLength(SUMOTime tau, double lengthWithGap, double vehicleTau) const {
529 210050804 : return (SUMOTime)((double)tau * vehicleTau + lengthWithGap * myTau_length);
530 : }
531 :
532 : SUMOTime getTauJJ(double nextQueueSize, double nextQueueCapacity, double nextJamThreshold) const;
533 :
534 : private:
535 : /// @brief The microsim edge this segment belongs to
536 : const MSEdge& myEdge;
537 :
538 : /// @brief The next segment of this edge, 0 if this is the last segment of this edge
539 : MESegment* myNextSegment;
540 :
541 : /// @brief The segment's length
542 : const double myLength;
543 :
544 : /// @brief Running number of the segment in the edge
545 : const int myIndex;
546 :
547 : /// @name Model constants that may be reset once via additional file
548 : /// @{
549 :
550 : /// @brief The time headway parameters, see the Eissfeldt thesis
551 : SUMOTime myTau_ff, myTau_fj, myTau_jf, myTau_jj;
552 :
553 : /// @brief Whether tls penalty is enabled
554 : bool myTLSPenalty;
555 :
556 : /// @brief penalty for minor links
557 : bool myCheckMinorPenalty; // for legacy compatibility (#7802, 7804)
558 : SUMOTime myMinorPenalty;
559 :
560 : /// @brief Whether junction control is enabled
561 : bool myJunctionControl;
562 :
563 : /// @brief Whether overtaking is permitted on this segment
564 : bool myOvertaking;
565 : /// @}
566 :
567 : /// @brief Headway parameter for computing gross time headyway from net time headway, length and edge speed
568 : double myTau_length;
569 :
570 : /// @brief The number of lanes represented by the queue * the length of the lane
571 : double myCapacity = 0.;
572 :
573 : /// @brief The number of lanes represented by the queue * the length of the lane
574 : double myQueueCapacity = 0.;
575 :
576 : /// @brief The space (in m) which needs to be occupied before the segment is considered jammed
577 : double myJamThreshold;
578 :
579 : /// @brief The car queues. Vehicles are inserted in the front and removed in the back
580 : std::vector<Queue> myQueues;
581 :
582 : /// @brief The cached value for the number of vehicles
583 : int myNumVehicles;
584 :
585 : /// @brief The follower edge to allowed que index mapping for multi queue segments
586 : std::map<const MSEdge*, int> myFollowerMap;
587 :
588 : /// @brief the last headway
589 : SUMOTime myLastHeadway;
590 :
591 : /* @brief segment for signifying vaporization. This segment has invalid
592 : * data and should only be used as a unique pointer */
593 : static MSEdge myDummyParent;
594 : static MESegment myVaporizationTarget;
595 :
596 : /// @brief the mean speed on this segment. Updated at event time or on demand
597 : mutable double myMeanSpeed;
598 :
599 : /// @brief the time at which myMeanSpeed was last updated
600 : mutable SUMOTime myLastMeanSpeedUpdate;
601 :
602 : private:
603 : /// @brief Invalidated copy constructor.
604 : MESegment(const MESegment&);
605 :
606 : /// @brief Invalidated assignment operator.
607 : MESegment& operator=(const MESegment&);
608 :
609 : /// @brief constructor for dummy segment
610 : MESegment(const std::string& id);
611 : };
|