Eclipse SUMO - Simulation of Urban MObility
MESegment.h
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2001-2020 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 /****************************************************************************/
18 // A single mesoscopic segment (cell)
19 /****************************************************************************/
20 #pragma once
21 #include <config.h>
22 
23 #include <vector>
24 #include <utils/common/Named.h>
25 #include <utils/common/SUMOTime.h>
26 
27 
28 // ===========================================================================
29 // class declarations
30 // ===========================================================================
31 class MSEdge;
32 class MSLink;
33 class MSMoveReminder;
35 class MSVehicleControl;
36 class MEVehicle;
37 class OutputDevice;
38 
39 
40 // ===========================================================================
41 // class definitions
42 // ===========================================================================
47 class MESegment : public Named {
48 private:
49  class Queue {
50  public:
51  Queue(const SVCPermissions permissions) : myPermissions(permissions) {}
52  inline int size() const {
53  return (int)myVehicles.size();
54  }
55  inline const std::vector<MEVehicle*>& getVehicles() const {
56  return myVehicles;
57  }
59  inline std::vector<MEVehicle*>& getModifiableVehicles() {
60  return myVehicles;
61  }
62  inline double getOccupancy() const {
63  return myOccupancy;
64  }
65  inline void setOccupancy(const double occ) {
66  myOccupancy = occ;
67  }
68  inline bool allows(SUMOVehicleClass vclass) const {
69  return (myPermissions & vclass) == vclass;
70  }
71 
73  inline SUMOTime getEntryBlockTime() const {
74  return myEntryBlockTime;
75  }
76 
78  inline void setEntryBlockTime(SUMOTime entryBlockTime) {
79  myEntryBlockTime = entryBlockTime;
80  }
81 
82  inline SUMOTime getBlockTime() const {
83  return myBlockTime;
84  }
85  inline void setBlockTime(SUMOTime t) {
86  myBlockTime = t;
87  }
88 
89  private:
92 
93  std::vector<MEVehicle*> myVehicles;
94 
96  double myOccupancy = 0.;
97 
100 
103 
104  private:
106  Queue& operator=(const Queue&) = delete;
107  };
108 
109 public:
126  MESegment(const std::string& id,
127  const MSEdge& parent, MESegment* next,
128  const double length, const double speed,
129  const int idx,
130  const bool multiQueue,
131  const MSNet::MesoEdgeType& edgeTyp);
132 
134  void initSegment(const MSNet::MesoEdgeType& edgeType, const MSEdge& parent);
135 
138 
143  void addDetector(MSMoveReminder* data);
144 
149  void removeDetector(MSMoveReminder* data);
150 
157 
166  SUMOTime hasSpaceFor(const MEVehicle* const veh, const SUMOTime entryTime, int& qIdx, const bool init = false) const;
167 
174  bool initialise(MEVehicle* veh, SUMOTime time);
175 
180  inline int getCarNumber() const {
181  return myNumVehicles;
182  }
183 
185  inline int numQueues() const {
186  return (int)myQueues.size();
187  }
191  inline const std::vector<MEVehicle*>& getQueue(int index) const {
192  assert(index < (int)myQueues.size());
193  return myQueues[index].getVehicles();
194  }
195 
200  inline int getIndex() const {
201  return myIndex;
202  }
203 
208  inline MESegment* getNextSegment() const {
209  return myNextSegment;
210  }
211 
216  inline double getLength() const {
217  return myLength;
218  }
219 
224  inline double getBruttoOccupancy() const {
225  double occ = 0.;
226  for (const Queue& q : myQueues) {
227  occ += q.getOccupancy();
228  }
229  return occ;
230  }
231 
235  inline double getRelativeOccupancy() const {
236  return getBruttoOccupancy() / myCapacity;
237  }
238 
243  inline double getRelativeJamThreshold() const {
244  return myJamThreshold / myCapacity;
245  }
246 
257  double getMeanSpeed(bool useCache) const;
258 
260  inline double getMeanSpeed() const {
261  return getMeanSpeed(true);
262  }
263 
264 
265  void writeVehicles(OutputDevice& of) const;
266 
274  MEVehicle* removeCar(MEVehicle* v, SUMOTime leaveTime, const MSMoveReminder::Notification reason);
275 
285  MSLink* getLink(const MEVehicle* veh, bool tlsPenalty = false) const;
286 
294  bool isOpen(const MEVehicle* veh) const;
295 
303  void send(MEVehicle* veh, MESegment* const next, const int nextQIdx, SUMOTime time, const MSMoveReminder::Notification reason);
304 
312  void receive(MEVehicle* veh, const int qIdx, SUMOTime time, const bool isDepart = false, const bool isTeleport = false, const bool newEdge = false);
313 
314 
320  bool vaporizeAnyCar(SUMOTime currentTime, const MSDetectorFileOutput* filter);
321 
325  inline const MSEdge& getEdge() const {
326  return myEdge;
327  }
328 
329 
334  void setSpeed(double newSpeed, SUMOTime currentTime, double jamThresh = DO_NOT_PATCH_JAM_THRESHOLD);
335 
339  SUMOTime getEventTime() const;
340 
342  inline double getEventTimeSeconds() const {
343  return STEPS2TIME(getEventTime());
344  }
345 
347  inline double getLastHeadwaySeconds() const {
348  return STEPS2TIME(myLastHeadway);
349  }
350 
352  inline double getEntryBlockTimeSeconds() const {
354  for (const Queue& q : myQueues) {
355  t = MIN2(t, q.getEntryBlockTime());
356  }
357  return STEPS2TIME(t);
358  }
359 
361  double getWaitingSeconds() const;
362 
365 
375  void saveState(OutputDevice& out) const;
376 
378  void clearState();
379 
395  void loadState(const std::vector<std::string>& vehIDs, MSVehicleControl& vc, const SUMOTime blockTime, const int queIdx);
397 
398 
401  std::vector<const MEVehicle*> getVehicles() const;
402 
406  double getFlow() const;
407 
409  static inline bool isInvalid(const MESegment* segment) {
410  return segment == nullptr || segment == &myVaporizationTarget;
411  }
412 
414  SUMOTime getNextInsertionTime(SUMOTime earliestEntry) const;
415 
417  inline int remainingVehicleCapacity(const double vehLength) const {
418  int cap = 0;
419  for (const Queue& q : myQueues) {
420  if (q.getOccupancy() == 0. && myQueueCapacity < vehLength) {
421  // even small segments can hold at least one vehicle
422  cap += 1;
423  } else {
424  cap += (int)((myQueueCapacity - q.getOccupancy()) / vehLength);
425  }
426  }
427  return cap;
428  }
429 
432  return myTau_ff;
433  }
434 
435  static const double DO_NOT_PATCH_JAM_THRESHOLD;
436 
438  void addReminders(MEVehicle* veh) const;
439 
444  SUMOTime getLinkPenalty(const MEVehicle* veh) const;
445 
446 private:
447  bool overtake();
448 
449  void setSpeedForQueue(double newSpeed, SUMOTime currentTime,
450  SUMOTime blockTime, const std::vector<MEVehicle*>& vehs);
451 
454  SUMOTime newArrival(const MEVehicle* const v, double newSpeed, SUMOTime currentTime);
455 
457  bool hasBlockedLeader() const;
458 
463  void recomputeJamThreshold(double jamThresh);
464 
466  double jamThresholdForSpeed(double speed, double jamThresh) const;
467 
469  bool limitedControlOverride(const MSLink* link) const;
470 
472  inline SUMOTime tauWithVehLength(SUMOTime tau, double lengthWithGap) const {
473  return tau + (SUMOTime)(lengthWithGap * myTau_length);
474  }
475 
476 private:
478  const MSEdge& myEdge;
479 
482 
484  const double myLength;
485 
487  const int myIndex;
488 
491 
494 
496  double myA, myB;
497 
500 
502  bool myCheckMinorPenalty; // for legacy compatibility (#7802, 7804)
504 
507 
511 
513  double myTau_length;
514 
517  const double myHeadwayCapacity;
518 
520  const double myCapacity;
521 
524 
527 
529  std::vector<MSMoveReminder*> myDetectorData;
530 
532  std::vector<Queue> myQueues;
533 
536 
538  std::map<const MSEdge*, int> myFollowerMap;
539 
542 
543  /* @brief segment for signifying vaporization. This segment has invalid
544  * data and should only be used as a unique pointer */
547 
549  mutable double myMeanSpeed;
550 
553 
554 private:
557 
560 
562  MESegment(const std::string& id);
563 };
#define STEPS2TIME(x)
Definition: SUMOTime.h:53
#define SUMOTime_MAX
Definition: SUMOTime.h:32
#define SUMOTime_MIN
Definition: SUMOTime.h:33
long long int SUMOTime
Definition: SUMOTime.h:31
SUMOVehicleClass
Definition of vehicle classes to differ between different lane usage and authority types.
int SVCPermissions
bitset where each bit declares whether a certain SVC may use this edge/lane
T MIN2(T a, T b)
Definition: StdDefs.h:73
int size() const
Definition: MESegment.h:52
void setOccupancy(const double occ)
Definition: MESegment.h:65
const SVCPermissions myPermissions
The vClass permissions for this queue.
Definition: MESegment.h:91
MEVehicle * remove(MEVehicle *v)
Definition: MESegment.cpp:69
void setBlockTime(SUMOTime t)
Definition: MESegment.h:85
SUMOTime getBlockTime() const
Definition: MESegment.h:82
double myOccupancy
The occupied space (in m) in the queue.
Definition: MESegment.h:96
bool allows(SUMOVehicleClass vclass) const
Definition: MESegment.h:68
SUMOTime myBlockTime
The block time.
Definition: MESegment.h:102
void setEntryBlockTime(SUMOTime entryBlockTime)
set the next time at which a vehicle may enter this queue
Definition: MESegment.h:78
SUMOTime myEntryBlockTime
The block time for vehicles who wish to enter this queue.
Definition: MESegment.h:99
double getOccupancy() const
Definition: MESegment.h:62
const std::vector< MEVehicle * > & getVehicles() const
Definition: MESegment.h:55
std::vector< MEVehicle * > myVehicles
Definition: MESegment.h:93
Queue & operator=(const Queue &)=delete
Invalidated assignment operator.
Queue(const SVCPermissions permissions)
Definition: MESegment.h:51
SUMOTime getEntryBlockTime() const
return the next time at which a vehicle may enter this queue
Definition: MESegment.h:73
std::vector< MEVehicle * > & getModifiableVehicles()
Definition: MESegment.h:59
A single mesoscopic segment (cell)
Definition: MESegment.h:47
void addReminders(MEVehicle *veh) const
add this lanes MoveReminders to the given vehicle
Definition: MESegment.cpp:538
double myQueueCapacity
The number of lanes represented by the queue * the length of the lane.
Definition: MESegment.h:523
bool overtake()
Definition: MESegment.cpp:532
SUMOTime myTau_ff
The time headway parameters, see the Eissfeldt thesis.
Definition: MESegment.h:493
double getEntryBlockTimeSeconds() const
get the earliest entry time in seconds
Definition: MESegment.h:352
bool initialise(MEVehicle *veh, SUMOTime time)
Inserts (emits) vehicle into the segment.
Definition: MESegment.cpp:333
std::vector< Queue > myQueues
The car queues. Vehicles are inserted in the front and removed in the back.
Definition: MESegment.h:532
double getBruttoOccupancy() const
Returns the occupany of the segment (the sum of the vehicle lengths + minGaps)
Definition: MESegment.h:224
SUMOTime myLastHeadway
the last headway
Definition: MESegment.h:541
bool limitedControlOverride(const MSLink *link) const
whether the given link may be passed because the option meso-junction-control.limited is set
Definition: MESegment.cpp:484
const std::vector< MEVehicle * > & getQueue(int index) const
Returns the cars in the queue with the given index for visualization.
Definition: MESegment.h:191
bool isOpen(const MEVehicle *veh) const
Returns whether the vehicle may use the next link.
Definition: MESegment.cpp:444
void initSegment(const MSNet::MesoEdgeType &edgeType, const MSEdge &parent)
set model parameters (may be updated from additional file after network loading is complete)
Definition: MESegment.cpp:130
double getLastHeadwaySeconds() const
get the last headway time in seconds
Definition: MESegment.h:347
double getRelativeOccupancy() const
Returns the relative occupany of the segment (percentage of road used))
Definition: MESegment.h:235
void clearState()
Remove all vehicles before quick-loading state.
Definition: MESegment.cpp:712
void receive(MEVehicle *veh, const int qIdx, SUMOTime time, const bool isDepart=false, const bool isTeleport=false, const bool newEdge=false)
Adds the vehicle to the segment, adapting its parameters.
Definition: MESegment.cpp:546
SUMOTime getLinkPenalty(const MEVehicle *veh) const
Returns the penalty time for passing a link (if using gMesoTLSPenalty > 0 or gMesoMinorPenalty > 0)
Definition: MESegment.cpp:770
void writeVehicles(OutputDevice &of) const
Definition: MESegment.cpp:375
std::map< const MSEdge *, int > myFollowerMap
The follower edge to allowed que index mapping for multi queue segments.
Definition: MESegment.h:538
MSLink * getLink(const MEVehicle *veh, bool tlsPenalty=false) const
Returns the link the given car will use when passing the next junction.
Definition: MESegment.cpp:415
const double myCapacity
The number of lanes represented by the queue * the length of the lane.
Definition: MESegment.h:520
int myNumVehicles
The cached value for the number of vehicles.
Definition: MESegment.h:535
void setSpeedForQueue(double newSpeed, SUMOTime currentTime, SUMOTime blockTime, const std::vector< MEVehicle * > &vehs)
Definition: MESegment.cpp:636
double getLength() const
Returns the length of the segment in meters.
Definition: MESegment.h:216
MESegment(const MESegment &)
Invalidated copy constructor.
SUMOTime hasSpaceFor(const MEVehicle *const veh, const SUMOTime entryTime, int &qIdx, const bool init=false) const
Returns whether the given vehicle would still fit into the segment.
Definition: MESegment.cpp:283
SUMOTime tauWithVehLength(SUMOTime tau, double lengthWithGap) const
convert net time gap (leader back to follower front) to gross time gap (leader front to follower fron...
Definition: MESegment.h:472
void removeDetector(MSMoveReminder *data)
Removes a data collector for a detector from this segment.
Definition: MESegment.cpp:255
void saveState(OutputDevice &out) const
Saves the state of this segment into the given stream.
Definition: MESegment.cpp:691
MESegment * getNextSegment() const
Returns the following segment on the same edge (0 if it is the last).
Definition: MESegment.h:208
void prepareDetectorForWriting(MSMoveReminder &data)
Updates data of a detector for all vehicle queues.
Definition: MESegment.cpp:269
static MESegment myVaporizationTarget
Definition: MESegment.h:546
int numQueues() const
return the number of queues
Definition: MESegment.h:185
double myJamThreshold
The space (in m) which needs to be occupied before the segment is considered jammed.
Definition: MESegment.h:526
const int myIndex
Running number of the segment in the edge.
Definition: MESegment.h:487
double myB
Definition: MESegment.h:496
void send(MEVehicle *veh, MESegment *const next, const int nextQIdx, SUMOTime time, const MSMoveReminder::Notification reason)
Removes the vehicle from the segment, adapting its parameters.
Definition: MESegment.cpp:497
SUMOTime myMinorPenalty
Definition: MESegment.h:503
double myMeanSpeed
the mean speed on this segment. Updated at event time or on demand
Definition: MESegment.h:549
bool myCheckMinorPenalty
penalty for minor links
Definition: MESegment.h:502
std::vector< MSMoveReminder * > myDetectorData
The data collection for all kinds of detectors.
Definition: MESegment.h:529
double jamThresholdForSpeed(double speed, double jamThresh) const
compute jam threshold for the given speed and jam-threshold option
Definition: MESegment.cpp:225
SUMOTime myLastMeanSpeedUpdate
the time at which myMeanSpeed was last updated
Definition: MESegment.h:552
SUMOTime myTau_jf
Definition: MESegment.h:493
MESegment * myNextSegment
The next segment of this edge, 0 if this is the last segment of this edge.
Definition: MESegment.h:481
double getRelativeJamThreshold() const
Returns the relative occupany of the segment (percentage of road used)) at which the segment is consi...
Definition: MESegment.h:243
const MSEdge & getEdge() const
Returns the edge this segment belongs to.
Definition: MESegment.h:325
bool hasBlockedLeader() const
whether a leader in any queue is blocked
Definition: MESegment.cpp:753
int remainingVehicleCapacity(const double vehLength) const
return the remaining physical space on this segment
Definition: MESegment.h:417
double getWaitingSeconds() const
Get the waiting time for vehicles in all queues.
Definition: MESegment.cpp:793
const double myLength
The segment's length.
Definition: MESegment.h:484
SUMOTime getMinimumHeadwayTime() const
return the minimum headway-time with which vehicles may enter or leave this segment
Definition: MESegment.h:431
SUMOTime getEventTime() const
Returns the (planned) time at which the next vehicle leaves this segment.
Definition: MESegment.cpp:676
const MSEdge & myEdge
The microsim edge this segment belongs to.
Definition: MESegment.h:478
MEVehicle * removeCar(MEVehicle *v, SUMOTime leaveTime, const MSMoveReminder::Notification reason)
Removes the given car from the edge's que.
Definition: MESegment.cpp:385
std::vector< const MEVehicle * > getVehicles() const
returns all vehicles (for debugging)
Definition: MESegment.cpp:743
void addDetector(MSMoveReminder *data)
Adds a data collector for a detector to this segment.
Definition: MESegment.cpp:244
double getEventTimeSeconds() const
Like getEventTime but returns seconds (for visualization)
Definition: MESegment.h:342
static MSEdge myDummyParent
Definition: MESegment.h:545
void recomputeJamThreshold(double jamThresh)
compute a value for myJamThreshold if jamThresh is negative, compute a value which allows free flow a...
Definition: MESegment.cpp:180
int getIndex() const
Returns the running index of the segment in the edge (0 is the most upstream).
Definition: MESegment.h:200
double myA
slope and axis offset for the jam-jam headway function
Definition: MESegment.h:496
double getMeanSpeed() const
wrapper to satisfy the FunctionBinding signature
Definition: MESegment.h:260
int getCarNumber() const
Returns the total number of cars on the segment.
Definition: MESegment.h:180
void setSpeed(double newSpeed, SUMOTime currentTime, double jamThresh=DO_NOT_PATCH_JAM_THRESHOLD)
reset mySpeed and patch the speed of all vehicles in it. Also set/recompute myJamThreshold
Definition: MESegment.cpp:664
void loadState(const std::vector< std::string > &vehIDs, MSVehicleControl &vc, const SUMOTime blockTime, const int queIdx)
Loads the state of this segment with the given parameters.
Definition: MESegment.cpp:719
const double myHeadwayCapacity
The capacity of the segment in number of cars, used only in time headway calculation This parameter h...
Definition: MESegment.h:517
double myTau_length
Headway parameter for computing gross time headyway from net time headway, length and edge speed.
Definition: MESegment.h:513
SUMOTime myTau_jj
Definition: MESegment.h:493
SUMOTime newArrival(const MEVehicle *const v, double newSpeed, SUMOTime currentTime)
compute the new arrival time when switching speed
Definition: MESegment.cpp:655
bool myJunctionControl
Whether junction control is enabled.
Definition: MESegment.h:506
bool myTLSPenalty
Whether tls penalty is enabled.
Definition: MESegment.h:499
static const double DO_NOT_PATCH_JAM_THRESHOLD
Definition: MESegment.h:435
double getFlow() const
returns flow based on headway
Definition: MESegment.cpp:764
static bool isInvalid(const MESegment *segment)
whether the given segment is 0 or encodes vaporization
Definition: MESegment.h:409
SUMOTime getNextInsertionTime(SUMOTime earliestEntry) const
return a time after earliestEntry at which a vehicle may be inserted at full speed
Definition: MESegment.cpp:398
bool myOvertaking
Whether overtaking is permitted on this segment.
Definition: MESegment.h:509
MESegment & operator=(const MESegment &)
Invalidated assignment operator.
bool vaporizeAnyCar(SUMOTime currentTime, const MSDetectorFileOutput *filter)
tries to remove any car from this segment
Definition: MESegment.cpp:619
SUMOTime myTau_fj
Definition: MESegment.h:493
MESegment(const std::string &id, const MSEdge &parent, MESegment *next, const double length, const double speed, const int idx, const bool multiQueue, const MSNet::MesoEdgeType &edgeTyp)
constructor
Definition: MESegment.cpp:89
A vehicle from the mesoscopic point of view.
Definition: MEVehicle.h:42
Base of value-generating classes (detectors)
A road/street connecting two junctions.
Definition: MSEdge.h:77
Something on a lane to be noticed about vehicle movement.
Notification
Definition of a vehicle state.
The class responsible for building and deletion of vehicles.
Base class for objects which have an id.
Definition: Named.h:53
Static storage of an output device and its base (abstract) implementation.
Definition: OutputDevice.h:60
edge type specific meso parameters
Definition: MSNet.h:117