Eclipse SUMO - Simulation of Urban MObility
Loading...
Searching...
No Matches
MELSegment.cpp
Go to the documentation of this file.
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/****************************************************************************/
18// A LTM (LIFT)-model segment
19/****************************************************************************/
20#include <config.h>
21
22#include <algorithm>
23#include <limits>
25#include <microsim/MSGlobals.h>
26#include <microsim/MSEdge.h>
27#include <microsim/MSJunction.h>
28#include <microsim/MSNet.h>
29#include <microsim/MSLane.h>
30#include <microsim/MSLink.h>
46#include "MEVehicle.h"
47#include "MELoop.h"
48#include "MELSegment.h"
49
50#define DEFAULT_VEH_LENGTH_WITH_GAP (SUMOVTypeParameter::getDefault().length + SUMOVTypeParameter::getDefault().minGap)
51
52//#define DEBUG_OPENED
53//#define DEBUG_JAMTHRESHOLD
54//#define DEBUG_COND (getID() == "blocker")
55//#define DEBUG_COND (true)
56#define DEBUG_COND (myEdge.isSelected())
57#define DEBUG_COND2(obj) ((obj != 0 && (obj)->isSelected()))
58
59
60// ===========================================================================
61// static member definition
62// ===========================================================================
63
64
65// ===========================================================================
66// MELSegment method definitions
67// ===========================================================================
68MELSegment::MELSegment(const std::string& id,
69 const MSEdge& parent, MESegment* next,
70 const double length, const double speed,
71 const int idx,
72 const bool multiQueue,
73 const MesoEdgeType& edgeType):
74 MESegment(id, parent, next, length, speed, idx, multiQueue, edgeType),
75 // the gap only has to pass the vehicles that fit into the segment (the remaining length cannot be occupied)
76 myJamLength(MAX2(1., floor(length / DEFAULT_VEH_LENGTH_WITH_GAP)) * DEFAULT_VEH_LENGTH_WITH_GAP) {
77 myGapTimes.resize(myQueues.size());
78 myJamGapTimes.resize(myQueues.size(), -1);
79}
80
81
83MELSegment::computeHeadway(Queue& /*q*/, const Queue& /*qNext*/, const MESegment* const /*next*/, const MEVehicle* veh) const {
84 // @note: it might seem sensible to check veh->getQueuingTimeLoss() here but
85 // then the queing state would be "sticky" because queuingTimeLoss is computed with tau_ff
86 // and every leader car that uses tau_jf applies queing timeLoss to it's followers
87 const SUMOTime waiting = veh->getWaitingTime();
88 SUMOTime headway = tauWithVehLength(waiting > 0 ? myTau_ff + MIN2(waiting, myTau_jf - myTau_ff) : myTau_ff,
91 if (myTLSPenalty) {
92 const MSLink* const tllink = getLink(veh, true);
93 if (tllink != nullptr && tllink->isTLSControlled()) {
94 assert(tllink->getGreenFraction() > 0);
95 headway = (SUMOTime)((double)headway / tllink->getGreenFraction());
96 }
97 }
98 return headway;
99}
100
101
102void
103MELSegment::send(MEVehicle* veh, MESegment* const next, const int nextQIdx, SUMOTime time, const MSMoveReminder::Notification reason) {
104 GapTimes& gapTimes = myGapTimes[veh->getQueIndex()];
105 // record time when the gap vacated by ego will reach the upstream end of the segment
106 // gaps travel quickly in free flow (the number of gaps and vehicles stays below the segment capacity)
107 // but they travel more slowly when queued (startupDelay)
108 const SUMOTime waveTime = (SUMOTime)(myJamLength * (double)(veh->getQueuingTimeLoss() > 0 ? myTau_jj : myTau_ff) / DEFAULT_VEH_LENGTH_WITH_GAP);
109 // the entering vehicle needs this long to drive through the length that cannot be occupied
110 const SUMOTime remainderTime = TIME2STEPS(MAX2(0., myLength - myJamLength) / myEdge.getSpeedLimit());
111 gapTimes.insert(gapTimes.begin(), time + MAX2((SUMOTime)0, waveTime - remainderTime));
112 if (veh->getQueuingTimeLoss() > 0) {
113 // gaps are inserted in order of departure, so this gap is the last one left by a queued vehicle
114 myJamGapTimes[veh->getQueIndex()] = gapTimes.front();
115 }
116 MESegment::send(veh, next, nextQIdx, time, reason);
117}
118
119
120void
122 for (int qIdx = 0; qIdx < (int)myQueues.size(); qIdx++) {
123 Queue& q = myQueues[qIdx];
124 int numGaps = numTravelingGaps(qIdx, time);
125 if (numGaps == 0) {
126 continue;
127 }
128 // gap times are sorted in descending order, the gap which reaches the upstream end first is at the back
129 const GapTimes& gapTimes = myGapTimes[qIdx];
130 SUMOTime unblockTime = gapTimes.back();
131 if (myJamGapTimes[qIdx] > time) {
132 // While a jam discharges, the entering vehicle needs space in addition to the vehicles and the traveling gaps.
133 numGaps++;
134 } else {
135 // In free flow the constraint is exactly binding at capacity (the free-flow headway equals the gap travel
136 // time per vehicle). Since a gap only frees space once it has reached the upstream end, rounding would block
137 // vehicles and cause phantom jams (#18684). The next vehicle cannot follow the entering vehicle before tau_ff
138 // so gaps that arrive within this time are counted as arrived
139 auto it = gapTimes.rbegin();
140 while (it != gapTimes.rend() && *it <= time + myTau_ff) {
141 ++it;
142 numGaps--;
143 }
144 if (it == gapTimes.rend()) {
145 continue;
146 }
147 unblockTime = *it - myTau_ff;
148 }
149 if (q.getOccupancy() + (double)numGaps * DEFAULT_VEH_LENGTH_WITH_GAP > myQueueCapacity) {
150 // segment is jammed because the empty spaces have not yet reached the upstream end.
151 q.setEntryBlockTime(MAX2(q.getEntryBlockTime(), unblockTime));
152 }
153 }
154}
155
156
157bool
158MELSegment::hasSpaceForInsertion(const Queue& /*q*/, int qIdx, double newOccupancy, SUMOTime entryTime) const {
159 return newOccupancy + (double)numTravelingGaps(qIdx, entryTime) * DEFAULT_VEH_LENGTH_WITH_GAP <= myQueueCapacity;
160}
161
162
163bool
165 return numTravelingGaps(qIdx, time) > 0;
166}
167
168
169int
171 GapTimes& gapTimes = const_cast<GapTimes&>(myGapTimes[qIdx]);
172 while (!gapTimes.empty() && gapTimes.back() <= time) {
173 gapTimes.pop_back();
174 }
175 return (int)gapTimes.size();
176}
177
178
179std::string
181 const GapTimes& gapTimes = myGapTimes[qIdx];
182 std::string state = joinToString(gapTimes, " ");
183 if (!gapTimes.empty() && myJamGapTimes[qIdx] >= gapTimes.back()) {
184 // a gap left by a queued vehicle is still traveling
185 state += " j" + toString(myJamGapTimes[qIdx]);
186 }
187 return state;
188}
189
190
191void
192MELSegment::loadQueueExtraState(int qIdx, const std::string& state, SUMOTime offset) {
193 GapTimes& gapTimes = myGapTimes[qIdx];
194 gapTimes.clear();
195 myJamGapTimes[qIdx] = -1;
196 for (const std::string& t : StringTokenizer(state).getVector()) {
197 if (t[0] == 'j') {
198 myJamGapTimes[qIdx] = StringUtils::toLong(t.substr(1)) - offset;
199 } else {
200 gapTimes.push_back(StringUtils::toLong(t) - offset);
201 }
202 }
203}
204
205
206/****************************************************************************/
long long int SUMOTime
Definition GUI.h:36
#define DEFAULT_VEH_LENGTH_WITH_GAP
constexpr SUMOTime TIME2STEPS(double x)
Definition SUMOTime.h:60
T MIN2(T a, T b)
Definition StdDefs.h:80
T MAX2(T a, T b)
Definition StdDefs.h:86
std::string joinToString(const std::vector< T > &v, const T_BETWEEN &between, std::streamsize accuracy=gPrecision)
Definition ToString.h:322
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition ToString.h:57
void loadQueueExtraState(int qIdx, const std::string &state, SUMOTime offset) override
void send(MEVehicle *veh, MESegment *const next, const int nextQIdx, SUMOTime time, const MSMoveReminder::Notification reason) override
Removes the vehicle from the segment, adapting its parameters.
void updateEntryBlockTime(SUMOTime time) override
update entry blockTime for all queues
bool hasSpaceForInsertion(const Queue &q, int qIdx, double newOccupancy, SUMOTime entryTime) const override
check jam-avoidance constraints during insertion
std::vector< GapTimes > myGapTimes
Definition MELSegment.h:96
bool hasTravelingGaps(int qIdx, SUMOTime time) const override
whether space vacated by vehicles that left the given queue has not yet reached the upstream end
int numTravelingGaps(int qIdx, SUMOTime time) const
discard gaps that reached the upstream end until the given time and return the number of remaining ga...
MELSegment(const std::string &id, const MSEdge &parent, MESegment *next, const double length, const double speed, const int idx, const bool multiQueue, const MesoEdgeType &edgeType)
constructor
std::vector< SUMOTime > myJamGapTimes
upstream arrival time of the latest gap left by a queued vehicle (for each queue, -1 if there is none...
Definition MELSegment.h:99
std::vector< SUMOTime > GapTimes
upstream arrival times of traveling gaps between cars (for each queue)
Definition MELSegment.h:95
const double myJamLength
the length of a segment filled with jammed (default) vehicles
Definition MELSegment.h:91
std::string getQueueExtraState(int qIdx) const override
the gap times of the given queue for saving / loading
SUMOTime computeHeadway(Queue &q, const Queue &qNext, const MESegment *const next, const MEVehicle *veh) const override
compute current headway depending on traffic state
void setEntryBlockTime(SUMOTime entryBlockTime)
set the next time at which a vehicle may enter this queue
Definition MESegment.h:117
double getOccupancy() const
Definition MESegment.h:101
SUMOTime getEntryBlockTime() const
return the next time at which a vehicle may enter this queue
Definition MESegment.h:112
A single mesoscopic segment (cell)
Definition MESegment.h:50
double myQueueCapacity
The number of lanes represented by the queue * the length of the lane.
Definition MESegment.h:657
SUMOTime tauWithVehLength(SUMOTime tau, double lengthWithGap, double vehicleTau) const
convert net time gap (leader back to follower front) to gross time gap (leader front to follower fron...
Definition MESegment.h:584
SUMOTime myTau_ff
The time headway parameters, see the Eissfeldt thesis.
Definition MESegment.h:631
std::vector< Queue > myQueues
The car queues. Vehicles are inserted in the front and removed in the back.
Definition MESegment.h:663
MSLink * getLink(const MEVehicle *veh, bool tlsPenalty=false) const
Returns the link the given car will use when passing the next junction.
virtual 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.
SUMOTime myTau_jf
Definition MESegment.h:631
const double myLength
The segment's length.
Definition MESegment.h:622
const MSEdge & myEdge
The microsim edge this segment belongs to.
Definition MESegment.h:616
SUMOTime myTau_jj
Definition MESegment.h:631
bool myTLSPenalty
Whether tls penalty is enabled.
Definition MESegment.h:634
A vehicle from the mesoscopic point of view.
Definition MEVehicle.h:42
SUMOTime getWaitingTime(const bool accumulated=false) const
Returns the duration for which the vehicle was blocked.
Definition MEVehicle.h:347
SUMOTime getQueuingTimeLoss() const
Returns the time lost compare to free flow.
Definition MEVehicle.h:353
int getQueIndex() const
Returns the index of the que the vehicle is in.
Definition MEVehicle.h:275
const MSVehicleType & getVehicleType() const
Returns the vehicle's type definition.
virtual double getHeadwayTime() const
Get the driver's desired headway [s].
Definition MSCFModel.h:355
A road/street connecting two junctions.
Definition MSEdge.h:78
double getSpeedLimit() const
Returns the speed limit of the edge @caution The speed limit of the first lane is returned; should pr...
Definition MSEdge.cpp:1235
Notification
Definition of a vehicle state.
double getLengthWithGap() const
Get vehicle's length including the minimum gap [m].
const MSCFModel & getCarFollowModel() const
Returns the vehicle type's car following model definition (const version)
std::vector< std::string > getVector()
return vector of strings
static long long int toLong(const std::string &sData)
converts a string into the long value described by it by calling the char-type converter,...
edge type specific meso parameters
Definition MESegment.h:58