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.cpp
15 : /// @author Daniel Krajzewicz
16 : /// @date Tue, May 2005
17 : ///
18 : // A single mesoscopic segment (cell)
19 : /****************************************************************************/
20 : #include <config.h>
21 :
22 : #include <algorithm>
23 : #include <limits>
24 : #include <utils/common/StdDefs.h>
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>
31 : #include <microsim/MSMoveReminder.h>
32 : #include <microsim/traffic_lights/MSTrafficLightLogic.h>
33 : #include <microsim/traffic_lights/MSDriveWay.h>
34 : #include <microsim/traffic_lights/MSRailSignalControl.h>
35 : #include <microsim/output/MSXMLRawOut.h>
36 : #include <microsim/output/MSDetectorFileOutput.h>
37 : #include <microsim/MSVehicleControl.h>
38 : #include <microsim/devices/MSDevice.h>
39 : #include <utils/common/FileHelpers.h>
40 : #include <utils/common/MsgHandler.h>
41 : #include <utils/iodevices/OutputDevice.h>
42 : #include <utils/common/RandHelper.h>
43 : #include "MEVehicle.h"
44 : #include "MELoop.h"
45 : #include "MESegment.h"
46 :
47 : #define DEFAULT_VEH_LENGTH_WITH_GAP (SUMOVTypeParameter::getDefault().length + SUMOVTypeParameter::getDefault().minGap)
48 : // avoid division by zero when driving very slowly
49 : #define MESO_MIN_SPEED (0.05)
50 :
51 : //#define DEBUG_OPENED
52 : //#define DEBUG_JAMTHRESHOLD
53 : //#define DEBUG_COND (getID() == "blocker")
54 : //#define DEBUG_COND (true)
55 : #define DEBUG_COND (myEdge.isSelected())
56 : #define DEBUG_COND2(obj) ((obj != 0 && (obj)->isSelected()))
57 :
58 :
59 : // ===========================================================================
60 : // static member definition
61 : // ===========================================================================
62 : MSEdge MESegment::myDummyParent("MESegmentDummyParent", -1, SumoXMLEdgeFunc::UNKNOWN, "", "", "", -1, 0);
63 : MESegment MESegment::myVaporizationTarget("vaporizationTarget");
64 : const double MESegment::DO_NOT_PATCH_JAM_THRESHOLD(std::numeric_limits<double>::max());
65 : const std::string MESegment::OVERRIDE_TLS_PENALTIES("meso.tls.control");
66 :
67 :
68 : // ===========================================================================
69 : // MESegment::Queue method definitions
70 : // ===========================================================================
71 : MEVehicle*
72 26001365 : MESegment::Queue::remove(MEVehicle* v) {
73 26001365 : myOccupancy -= v->getVehicleType().getLengthWithGap();
74 : assert(std::find(myVehicles.begin(), myVehicles.end(), v) != myVehicles.end());
75 26001365 : if (v == myVehicles.back()) {
76 : myVehicles.pop_back();
77 25995086 : if (myVehicles.empty()) {
78 8272861 : myOccupancy = 0.;
79 : } else {
80 17722225 : return myVehicles.back();
81 : }
82 : } else {
83 6279 : myVehicles.erase(std::find(myVehicles.begin(), myVehicles.end(), v));
84 : }
85 : return nullptr;
86 : }
87 :
88 : void
89 128532 : MESegment::Queue::addDetector(MSMoveReminder* data) {
90 128532 : myDetectorData.push_back(data);
91 134280 : for (MEVehicle* const v : myVehicles) {
92 5748 : v->addReminder(data);
93 : }
94 128532 : }
95 :
96 : void
97 26042340 : MESegment::Queue::addReminders(MEVehicle* veh) const {
98 35502006 : for (MSMoveReminder* rem : myDetectorData) {
99 9459666 : veh->addReminder(rem);
100 : }
101 26042340 : }
102 :
103 : // ===========================================================================
104 : // MESegment method definitions
105 : // ===========================================================================
106 704463 : MESegment::MESegment(const std::string& id,
107 : const MSEdge& parent, MESegment* next,
108 : const double length, const double speed,
109 : const int idx,
110 : const bool multiQueue,
111 704463 : const MesoEdgeType& edgeType):
112 704463 : Named(id), myEdge(parent), myNextSegment(next),
113 704463 : myLength(length), myIndex(idx),
114 704463 : myTau_length(TIME2STEPS(1) / MAX2(MESO_MIN_SPEED, speed)),
115 704463 : myNumVehicles(0),
116 704463 : myLastHeadway(TIME2STEPS(-1)),
117 704463 : myMeanSpeed(speed),
118 1408599 : myLastMeanSpeedUpdate(SUMOTime_MIN) {
119 :
120 : const std::vector<MSLane*>& lanes = parent.getLanes();
121 : int usableLanes = 0;
122 1531436 : for (MSLane* const l : lanes) {
123 826973 : const SVCPermissions allow = MSEdge::getMesoPermissions(l->getPermissions());
124 826973 : if (multiQueue) {
125 108530 : myQueues.push_back(Queue(allow));
126 : }
127 826973 : if (allow != 0) {
128 774182 : usableLanes++;
129 : }
130 : }
131 704463 : if (usableLanes == 0) {
132 : // cars won't drive here. Give sensible tau values capacity for the ignored classes
133 : usableLanes = 1;
134 : }
135 704463 : if (multiQueue) {
136 23129 : if (next == nullptr) {
137 107707 : for (const MSEdge* const edge : parent.getSuccessors()) {
138 84923 : if (edge->isTazConnector()) {
139 284 : continue;
140 : }
141 84639 : const std::vector<MSLane*>* const allowed = parent.allowedLanes(*edge);
142 : assert(allowed != nullptr);
143 : assert(allowed->size() > 0);
144 177062 : for (MSLane* const l : *allowed) {
145 92423 : std::vector<MSLane*>::const_iterator it = std::find(lanes.begin(), lanes.end(), l);
146 92423 : myFollowerMap[edge] |= (1 << distance(lanes.begin(), it));
147 : }
148 : }
149 : }
150 23129 : myQueueCapacity = length;
151 : } else {
152 1362668 : myQueues.push_back(Queue(parent.getPermissions()));
153 : }
154 :
155 704463 : initSegment(edgeType, parent, length * usableLanes);
156 704463 : }
157 :
158 : void
159 704463 : MESegment::initSegment(const MesoEdgeType& edgeType, const MSEdge& parent, const double capacity) {
160 :
161 704463 : myCapacity = capacity;
162 704463 : if (myQueues.size() == 1) {
163 681762 : const double laneScale = capacity / myLength;
164 681762 : myQueueCapacity = capacity;
165 681762 : myTau_length = TIME2STEPS(1) / MAX2(MESO_MIN_SPEED, myMeanSpeed) / laneScale;
166 : // Eissfeldt p. 90 and 151 ff.
167 681762 : myTau_ff = (SUMOTime)((double)edgeType.tauff / laneScale);
168 681762 : myTau_fj = (SUMOTime)((double)edgeType.taufj / laneScale);
169 681762 : myTau_jf = (SUMOTime)((double)edgeType.taujf / laneScale);
170 681762 : myTau_jj = (SUMOTime)((double)edgeType.taujj / laneScale);
171 : } else {
172 22701 : myTau_ff = edgeType.tauff;
173 22701 : myTau_fj = edgeType.taufj;
174 22701 : myTau_jf = edgeType.taujf;
175 22701 : myTau_jj = edgeType.taujj;
176 : }
177 :
178 704463 : myJunctionControl = myNextSegment == nullptr && (edgeType.junctionControl || MELoop::isEnteringRoundabout(parent));
179 703687 : myTLSPenalty = ((edgeType.tlsPenalty > 0 || edgeType.tlsFlowPenalty > 0) &&
180 : // only apply to the last segment of a tls-controlled edge
181 704823 : myNextSegment == nullptr && (
182 298 : parent.getToJunction()->getType() == SumoXMLNodeType::TRAFFIC_LIGHT ||
183 298 : parent.getToJunction()->getType() == SumoXMLNodeType::TRAFFIC_LIGHT_NOJUNCTION ||
184 : parent.getToJunction()->getType() == SumoXMLNodeType::TRAFFIC_LIGHT_RIGHT_ON_RED));
185 :
186 : // only apply to the last segment of an uncontrolled edge that has at least 1 minor link
187 1409246 : myCheckMinorPenalty = (edgeType.minorPenalty > 0 &&
188 320 : myNextSegment == nullptr &&
189 : parent.getToJunction()->getType() != SumoXMLNodeType::TRAFFIC_LIGHT &&
190 : parent.getToJunction()->getType() != SumoXMLNodeType::TRAFFIC_LIGHT_NOJUNCTION &&
191 704641 : parent.getToJunction()->getType() != SumoXMLNodeType::TRAFFIC_LIGHT_RIGHT_ON_RED &&
192 178 : parent.hasMinorLink());
193 704463 : myMinorPenalty = edgeType.minorPenalty;
194 704463 : myOvertaking = edgeType.overtaking && myCapacity > myLength;
195 :
196 : //std::cout << getID() << " myMinorPenalty=" << myMinorPenalty << " myTLSPenalty=" << myTLSPenalty << " myJunctionControl=" << myJunctionControl << " myOvertaking=" << myOvertaking << "\n";
197 :
198 704463 : recomputeJamThreshold(edgeType.jamThreshold);
199 704463 : }
200 :
201 45118 : MESegment::MESegment(const std::string& id):
202 : Named(id),
203 45118 : myEdge(myDummyParent), // arbitrary edge needed to supply the needed reference
204 45118 : myNextSegment(nullptr), myLength(0), myIndex(0),
205 45118 : myTau_ff(0), myTau_fj(0), myTau_jf(0), myTau_jj(0),
206 45118 : myTLSPenalty(false),
207 45118 : myCheckMinorPenalty(false),
208 45118 : myMinorPenalty(0),
209 45118 : myJunctionControl(false),
210 45118 : myOvertaking(false),
211 45118 : myTau_length(1) {
212 45118 : }
213 :
214 :
215 : void
216 2226 : MESegment::updatePermissions() {
217 2226 : if (myQueues.size() > 1) {
218 64 : for (MSLane* lane : myEdge.getLanes()) {
219 48 : myQueues[lane->getIndex()].setPermissions(lane->getPermissions());
220 : }
221 : } else {
222 2210 : myQueues.back().setPermissions(myEdge.getPermissions());
223 : }
224 2226 : }
225 :
226 :
227 : void
228 705507 : MESegment::recomputeJamThreshold(double jamThresh) {
229 705507 : if (jamThresh == DO_NOT_PATCH_JAM_THRESHOLD) {
230 : return;
231 : }
232 705507 : if (jamThresh < 0) {
233 : // compute based on speed
234 705495 : myJamThreshold = jamThresholdForSpeed(myEdge.getSpeedLimit(), jamThresh);
235 : } else {
236 : // compute based on specified percentage
237 12 : myJamThreshold = jamThresh * myCapacity;
238 : }
239 : }
240 :
241 :
242 : double
243 4796324 : MESegment::jamThresholdForSpeed(double speed, double jamThresh) const {
244 : // vehicles driving freely at maximum speed should not jam
245 : // we compute how many vehicles could possible enter the segment until the first vehicle leaves
246 : // and multiply by the space these vehicles would occupy
247 : // the jamThresh parameter is scale the resulting value
248 4796324 : if (speed == 0) {
249 : return std::numeric_limits<double>::max(); // never jam. Irrelevant at speed 0 anyway
250 : }
251 : #ifdef DEBUG_JAMTHRESHOLD
252 : if (true || DEBUG_COND) {
253 : std::cout << "jamThresholdForSpeed seg=" << getID() << " speed=" << speed << " jamThresh=" << jamThresh << " ffVehs=" << std::ceil(myLength / (-jamThresh * speed * STEPS2TIME(tauWithVehLength(myTau_ff, DEFAULT_VEH_LENGTH_WITH_GAP)))) << " thresh=" << std::ceil(myLength / (-jamThresh * speed * STEPS2TIME(tauWithVehLength(myTau_ff, DEFAULT_VEH_LENGTH_WITH_GAP)))) * DEFAULT_VEH_LENGTH_WITH_GAP
254 : << "\n";
255 : }
256 : #endif
257 4796105 : return std::ceil(myLength / (-jamThresh * speed * STEPS2TIME(tauWithVehLength(myTau_ff, DEFAULT_VEH_LENGTH_WITH_GAP, 1.)))) * DEFAULT_VEH_LENGTH_WITH_GAP;
258 : }
259 :
260 :
261 : void
262 126796 : MESegment::addDetector(MSMoveReminder* data, int queueIndex) {
263 126796 : if (queueIndex == -1) {
264 248818 : for (Queue& q : myQueues) {
265 125277 : q.addDetector(data);
266 : }
267 : } else {
268 : assert(queueIndex < (int)myQueues.size());
269 3255 : myQueues[queueIndex].addDetector(data);
270 : }
271 126796 : }
272 :
273 :
274 : /*
275 : void
276 : MESegment::removeDetector(MSMoveReminder* data) {
277 : std::vector<MSMoveReminder*>::iterator it = std::find(myDetectorData.begin(), myDetectorData.end(), data);
278 : if (it != myDetectorData.end()) {
279 : myDetectorData.erase(it);
280 : }
281 : for (const Queue& q : myQueues) {
282 : for (MEVehicle* const v : q.getVehicles()) {
283 : v->removeReminder(data);
284 : }
285 : }
286 : }
287 : */
288 :
289 :
290 : void
291 6422677 : MESegment::prepareDetectorForWriting(MSMoveReminder& data, int queueIndex) {
292 6422677 : const SUMOTime currentTime = MSNet::getInstance()->getCurrentTimeStep();
293 6422677 : if (queueIndex == -1) {
294 12869846 : for (const Queue& q : myQueues) {
295 : SUMOTime earliestExitTime = currentTime;
296 7748804 : for (std::vector<MEVehicle*>::const_reverse_iterator i = q.getVehicles().rbegin(); i != q.getVehicles().rend(); ++i) {
297 1301035 : const SUMOTime exitTime = MAX2(earliestExitTime, (*i)->getEventTime());
298 1301035 : (*i)->updateDetectorForWriting(&data, currentTime, exitTime);
299 1301035 : earliestExitTime = exitTime + tauWithVehLength(myTau_ff, (*i)->getVehicleType().getLengthWithGap(), (*i)->getVehicleType().getCarFollowModel().getHeadwayTime());
300 : }
301 : }
302 : } else {
303 : SUMOTime earliestExitTime = currentTime;
304 612 : for (std::vector<MEVehicle*>::const_reverse_iterator i = myQueues[queueIndex].getVehicles().rbegin(); i != myQueues[queueIndex].getVehicles().rend(); ++i) {
305 12 : const SUMOTime exitTime = MAX2(earliestExitTime, (*i)->getEventTime());
306 12 : (*i)->updateDetectorForWriting(&data, currentTime, exitTime);
307 12 : earliestExitTime = exitTime + tauWithVehLength(myTau_ff, (*i)->getVehicleType().getLengthWithGap(), (*i)->getVehicleType().getCarFollowModel().getHeadwayTime());
308 : }
309 : }
310 6422677 : }
311 :
312 :
313 : SUMOTime
314 112498744 : MESegment::hasSpaceFor(const MEVehicle* const veh, const SUMOTime entryTime, int& qIdx, const bool init) const {
315 : SUMOTime earliestEntry = SUMOTime_MAX;
316 112498744 : qIdx = 0;
317 112498744 : if (myNumVehicles == 0 && myQueues.size() == 1) {
318 : // we have always space for at least one vehicle
319 24781100 : if (myQueues.front().allows(veh->getVClass())) {
320 : return entryTime;
321 : } else {
322 : return earliestEntry;
323 : }
324 : }
325 87717644 : const SUMOVehicleClass svc = veh->getVClass();
326 : int minSize = std::numeric_limits<int>::max();
327 88437255 : const MSEdge* const succ = myNextSegment == nullptr ? veh->succEdge(veh->getEdge() == &myEdge ? 1 : 2) : nullptr;
328 220643049 : for (int i = 0; i < (int)myQueues.size(); i++) {
329 132925405 : const Queue& q = myQueues[i];
330 132925405 : const double newOccupancy = q.size() == 0 ? 0. : q.getOccupancy() + veh->getVehicleType().getLengthWithGap();
331 132925405 : if (newOccupancy <= myQueueCapacity) { // we must ensure that occupancy remains below capacity
332 58143010 : if (succ == nullptr || myFollowerMap.count(succ) == 0 || ((myFollowerMap.find(succ)->second & (1 << i)) != 0)) {
333 40082411 : if (q.allows(svc) && q.size() < minSize) {
334 24877065 : if (init) {
335 : // regular insertions and initial insertions must respect different constraints:
336 : // - regular insertions must respect entryBlockTime
337 : // - initial insertions should not cause additional jamming
338 : // - inserted vehicle should be able to continue at the current speed
339 6963143 : if (veh->getInsertionChecks() == (int)InsertionCheck::NONE) {
340 44 : qIdx = i;
341 : minSize = q.size();
342 6963099 : } else if (q.getOccupancy() <= myJamThreshold && !hasBlockedLeader() && !myTLSPenalty) {
343 2872270 : if (newOccupancy <= myJamThreshold) {
344 1262886 : qIdx = i;
345 : minSize = q.size();
346 : }
347 : } else {
348 4090829 : if (newOccupancy <= jamThresholdForSpeed(getMeanSpeed(false), -1)) {
349 3340020 : qIdx = i;
350 : minSize = q.size();
351 : }
352 : }
353 17913922 : } else if (entryTime >= q.getEntryBlockTime()) {
354 17648065 : qIdx = i;
355 : minSize = q.size();
356 : } else {
357 : earliestEntry = MIN2(earliestEntry, q.getEntryBlockTime());
358 : }
359 : }
360 : }
361 : }
362 : }
363 87717644 : if (minSize == std::numeric_limits<int>::max()) {
364 : return earliestEntry;
365 : }
366 : return entryTime;
367 : }
368 :
369 :
370 : bool
371 9498464 : MESegment::initialise(MEVehicle* veh, SUMOTime time) {
372 9498464 : int qIdx = 0;
373 9498464 : if (hasSpaceFor(veh, time, qIdx, true) == time) {
374 8200379 : const bool isRail = veh->isRail();
375 : // see MSLane::isInsertionSuccess
376 7489750 : if (isRail && veh->getInsertionChecks() != (int)InsertionCheck::NONE
377 7489731 : && veh->getParameter().departProcedure != DepartDefinition::SPLIT
378 7489731 : && MSRailSignalControl::isSignalized(veh->getVClass())
379 15690101 : && isRailwayOrShared(myEdge.getPermissions())) {
380 7489718 : const MSDriveWay* dw = MSDriveWay::getDepartureDriveway(veh);
381 : MSEdgeVector occupied;
382 7489718 : if (dw->foeDriveWayOccupied(false, veh, occupied)) {
383 7487529 : myEdge.getLanes()[0]->setParameter("insertionBlocked:" + veh->getID(), dw->getID());
384 : return false;
385 : }
386 7489718 : }
387 712850 : receive(veh, qIdx, time, true);
388 712849 : if (isRail) {
389 4440 : myEdge.getLanes()[0]->unsetParameter("insertionConstraint:" + veh->getID());
390 : //unsetParameter("insertionOrder:" + veh->getID());
391 : //unsetParameter("insertionBlocked:" + veh->getID());
392 : //// rail_signal (not traffic_light) requires approach information for
393 : //// switching correctly at the start of the next simulation step
394 : //if (firstRailSignal != nullptr && firstRailSignal->getJunction()->getType() == SumoXMLNodeType::RAIL_SIGNAL) {
395 : // veh->registerInsertionApproach(firstRailSignal, firstRailSignalDist);
396 : //}
397 : }
398 : // we can check only after insertion because insertion may change the route via devices
399 : std::string msg;
400 1424962 : if (MSGlobals::gCheckRoutes && !veh->hasValidRoute(msg)) {
401 6 : throw ProcessError(TLF("Vehicle '%' has no valid route. %", veh->getID(), msg));
402 : }
403 : return true;
404 : }
405 : return false;
406 : }
407 :
408 :
409 : double
410 29461036 : MESegment::getMeanSpeed(bool useCached) const {
411 29461036 : const SUMOTime currentTime = MSNet::getInstance()->getCurrentTimeStep();
412 29461036 : if (currentTime != myLastMeanSpeedUpdate || !useCached) {
413 29362951 : myLastMeanSpeedUpdate = currentTime;
414 : double v = 0;
415 : int count = 0;
416 60152740 : for (const Queue& q : myQueues) {
417 30789789 : const SUMOTime tau = q.getOccupancy() < myJamThreshold ? myTau_ff : myTau_jf;
418 30789789 : SUMOTime earliestExitTime = currentTime;
419 30789789 : count += q.size();
420 166230269 : for (std::vector<MEVehicle*>::const_reverse_iterator veh = q.getVehicles().rbegin(); veh != q.getVehicles().rend(); ++veh) {
421 135440480 : v += (*veh)->getConservativeSpeed(earliestExitTime); // earliestExitTime is updated!
422 135440480 : earliestExitTime += tauWithVehLength(tau, (*veh)->getVehicleType().getLengthWithGap(), (*veh)->getVehicleType().getCarFollowModel().getHeadwayTime());
423 : }
424 : }
425 29362951 : if (count == 0) {
426 30 : myMeanSpeed = myEdge.getSpeedLimit();
427 : } else {
428 29362921 : myMeanSpeed = v / (double) count;
429 : }
430 : }
431 29461036 : return myMeanSpeed;
432 : }
433 :
434 :
435 : void
436 5578 : MESegment::resetCachedSpeeds() {
437 5578 : myLastMeanSpeedUpdate = SUMOTime_MIN;
438 5578 : }
439 :
440 : void
441 11953 : MESegment::writeVehicles(OutputDevice& of) const {
442 24082 : for (const Queue& q : myQueues) {
443 14162 : for (const MEVehicle* const veh : q.getVehicles()) {
444 2033 : MSXMLRawOut::writeVehicle(of, *veh);
445 : }
446 : }
447 11953 : }
448 :
449 :
450 : MEVehicle*
451 26001365 : MESegment::removeCar(MEVehicle* v, SUMOTime leaveTime, const MSMoveReminder::Notification reason) {
452 26001365 : Queue& q = myQueues[v->getQueIndex()];
453 : // One could be tempted to do v->setSegment(next); here but position on lane will be invalid if next == 0
454 26001365 : v->updateDetectors(leaveTime, v->getEventTime(), true, reason);
455 26001365 : myNumVehicles--;
456 26001365 : myEdge.lock();
457 26001365 : MEVehicle* nextLeader = q.remove(v);
458 26001365 : myEdge.unlock();
459 26001365 : return nextLeader;
460 : }
461 :
462 :
463 : SUMOTime
464 13655 : MESegment::getNextInsertionTime(SUMOTime earliestEntry) const {
465 : // since we do not know which queue will be used we give a conservative estimate
466 : SUMOTime earliestLeave = earliestEntry;
467 : SUMOTime latestEntry = -1;
468 27575 : for (const Queue& q : myQueues) {
469 : earliestLeave = MAX2(earliestLeave, q.getBlockTime());
470 : latestEntry = MAX2(latestEntry, q.getEntryBlockTime());
471 : }
472 13655 : if (myEdge.getSpeedLimit() == 0) {
473 12 : return MAX2(earliestEntry, latestEntry); // FIXME: This line is just an adhoc-fix to avoid division by zero (Leo)
474 : } else {
475 13643 : return MAX3(earliestEntry, earliestLeave - TIME2STEPS(myLength / myEdge.getSpeedLimit()), latestEntry);
476 : }
477 : }
478 :
479 :
480 : MSLink*
481 122389377 : MESegment::getLink(const MEVehicle* veh, bool penalty) const {
482 122389377 : if (myJunctionControl || penalty) {
483 25728565 : const MSEdge* const nextEdge = veh->succEdge(1);
484 25728565 : if (nextEdge == nullptr || veh->getQueIndex() == PARKING_QUEUE) {
485 : return nullptr;
486 : }
487 : // try to find any link leading to our next edge, start with the lane pointed to by the que index
488 24739307 : const MSLane* const bestLane = myEdge.getLanes()[veh->getQueIndex()];
489 27764152 : for (MSLink* const link : bestLane->getLinkCont()) {
490 27359068 : if (&link->getLane()->getEdge() == nextEdge) {
491 : return link;
492 : }
493 : }
494 : // this is for the non-multique case, maybe we should use caching here !!!
495 921160 : for (const MSLane* const lane : myEdge.getLanes()) {
496 904050 : if (lane != bestLane) {
497 628038 : for (MSLink* const link : lane->getLinkCont()) {
498 516718 : if (&link->getLane()->getEdge() == nextEdge) {
499 : return link;
500 : }
501 : }
502 : }
503 : }
504 : }
505 : return nullptr;
506 : }
507 :
508 :
509 : bool
510 33522909 : MESegment::isOpen(const MEVehicle* veh) const {
511 : #ifdef DEBUG_OPENED
512 : if (DEBUG_COND || DEBUG_COND2(veh)) {
513 : gDebugFlag1 = true;
514 : std::cout << SIMTIME << " opened seg=" << getID() << " veh=" << Named::getIDSecure(veh)
515 : << " tlsPenalty=" << myTLSPenalty;
516 : const MSLink* link = getLink(veh);
517 : if (link == 0) {
518 : std::cout << " link=0";
519 : } else {
520 : std::cout << " prio=" << link->havePriority()
521 : << " override=" << limitedControlOverride(link)
522 : << " isOpen=" << link->opened(veh->getEventTime(), veh->getSpeed(), veh->estimateLeaveSpeed(link),
523 : veh->getVehicleType().getLengthWithGap(), veh->getImpatience(),
524 : veh->getVehicleType().getCarFollowModel().getMaxDecel(), veh->getWaitingTime(),
525 : 0, nullptr, false, veh)
526 : << " et=" << veh->getEventTime()
527 : << " v=" << veh->getSpeed()
528 : << " vLeave=" << veh->estimateLeaveSpeed(link)
529 : << " impatience=" << veh->getImpatience()
530 : << " tWait=" << veh->getWaitingTime();
531 : }
532 : std::cout << "\n";
533 : gDebugFlag1 = false;
534 : }
535 : #endif
536 33522909 : if (myTLSPenalty) {
537 : // XXX should limited control take precedence over tls penalty?
538 : return true;
539 : }
540 33470761 : const MSLink* link = getLink(veh);
541 : return (link == nullptr
542 9887387 : || link->havePriority()
543 8492128 : || limitedControlOverride(link)
544 41962125 : || link->opened(veh->getEventTime(), veh->getSpeed(), veh->estimateLeaveSpeed(link),
545 8491364 : veh->getVehicleType().getLengthWithGap(), veh->getImpatience(),
546 8491364 : veh->getVehicleType().getCarFollowModel().getMaxDecel(), veh->getWaitingTime(),
547 : 0, nullptr, false, veh));
548 : }
549 :
550 :
551 : bool
552 8495318 : MESegment::limitedControlOverride(const MSLink* link) const {
553 : assert(link != nullptr);
554 8495318 : if (!MSGlobals::gMesoLimitedJunctionControl) {
555 : return false;
556 : }
557 : // if the target segment of this link is not saturated junction control is disabled
558 : const MSEdge& targetEdge = link->getLane()->getEdge();
559 8246 : const MESegment* target = MSGlobals::gMesoNet->getSegmentForEdge(targetEdge);
560 8246 : return (target->getBruttoOccupancy() * 2 < target->myJamThreshold) && !targetEdge.isRoundabout();
561 : }
562 :
563 :
564 : void
565 26001365 : MESegment::send(MEVehicle* veh, MESegment* const next, const int nextQIdx, SUMOTime time, const MSMoveReminder::Notification reason) {
566 26001365 : Queue& q = myQueues[veh->getQueIndex()];
567 : assert(isInvalid(next) || time >= q.getBlockTime());
568 26001365 : MSLink* const link = getLink(veh);
569 26001365 : if (link != nullptr) {
570 1701472 : link->removeApproaching(veh);
571 : }
572 26001365 : if (veh->isStopped()) {
573 8144 : veh->processStop();
574 : }
575 26001365 : MEVehicle* lc = removeCar(veh, time, reason); // new leaderCar
576 : q.setBlockTime(time);
577 : if (!isInvalid(next)) {
578 25326853 : const bool nextFree = next->myQueues[nextQIdx].getOccupancy() <= next->myJamThreshold;
579 25326853 : const SUMOTime tau = (q.getOccupancy() <= myJamThreshold
580 25326853 : ? (nextFree ? myTau_ff : myTau_fj)
581 476379 : : (nextFree ? myTau_jf : getTauJJ((double)next->myQueues[nextQIdx].size(), next->myQueueCapacity, next->myJamThreshold)));
582 : assert(tau >= 0);
583 25326853 : myLastHeadway = tauWithVehLength(tau, veh->getVehicleType().getLengthWithGap(), veh->getVehicleType().getCarFollowModel().getHeadwayTime());
584 25326853 : if (myTLSPenalty) {
585 52148 : const MSLink* const tllink = getLink(veh, true);
586 52148 : if (tllink != nullptr && tllink->isTLSControlled()) {
587 : assert(tllink->getGreenFraction() > 0);
588 49540 : myLastHeadway = (SUMOTime)((double)myLastHeadway / tllink->getGreenFraction());
589 : }
590 : }
591 25326853 : q.setBlockTime(q.getBlockTime() + myLastHeadway);
592 : }
593 26001365 : if (lc != nullptr) {
594 : lc->setEventTime(MAX2(lc->getEventTime(), q.getBlockTime()));
595 17722225 : MSGlobals::gMesoNet->addLeaderCar(lc, getLink(lc));
596 : }
597 26001365 : }
598 :
599 : SUMOTime
600 128737 : MESegment::getTauJJ(double nextQueueSize, double nextQueueCapacity, double nextJamThreshold) const {
601 : // compute coefficients for the jam-jam headway function
602 : // this function models the effect that "empty space" needs to move
603 : // backwards through the downstream segment before the upstream segment may
604 : // send annother vehicle.
605 : // this allows jams to clear and move upstream.
606 : // the headway function f(x) depends on the number of vehicles in the
607 : // downstream segment x
608 : // f is a linear function that passes through the following fixed points:
609 : // f(n_jam_threshold) = tau_jf_withLength (for continuity)
610 : // f(headwayCapacity) = myTau_jj * headwayCapacity
611 :
612 128737 : const SUMOTime tau_jf_withLength = tauWithVehLength(myTau_jf, DEFAULT_VEH_LENGTH_WITH_GAP, 1.);
613 : // number of vehicles that fit into the NEXT queue (could be larger than expected with DEFAULT_VEH_LENGTH_WITH_GAP!)
614 128737 : const double headwayCapacity = MAX2(nextQueueSize, nextQueueCapacity / DEFAULT_VEH_LENGTH_WITH_GAP);
615 : // number of vehicles above which the NEXT queue is jammed
616 128737 : const double n_jam_threshold = headwayCapacity * nextJamThreshold / nextQueueCapacity;
617 :
618 : // slope a and axis offset b for the jam-jam headway function
619 : // solving f(x) = a * x + b
620 128737 : const double a = (STEPS2TIME(myTau_jj) * headwayCapacity - STEPS2TIME(tau_jf_withLength)) / (headwayCapacity - n_jam_threshold);
621 128737 : const double b = headwayCapacity * (STEPS2TIME(myTau_jj) - a);
622 :
623 : // it is only well defined for nextQueueSize >= n_jam_threshold (which may not be the case for longer vehicles), so we take the MAX
624 128737 : return TIME2STEPS(a * MAX2(nextQueueSize, n_jam_threshold) + b);
625 : }
626 :
627 :
628 : bool
629 1171387 : MESegment::overtake() {
630 1171411 : return myOvertaking && RandHelper::rand() > (getBruttoOccupancy() / myCapacity);
631 : }
632 :
633 :
634 : void
635 26048142 : MESegment::addReminders(MEVehicle* veh) const {
636 26048142 : if (veh->getQueIndex() != PARKING_QUEUE) {
637 26042340 : myQueues[veh->getQueIndex()].addReminders(veh);
638 : }
639 26048142 : }
640 :
641 :
642 : void
643 26045882 : MESegment::receive(MEVehicle* veh, const int qIdx, SUMOTime time, const bool isDepart, const bool isTeleport, const bool newEdge) {
644 26045882 : const double speed = isDepart ? -1 : MAX2(veh->getSpeed(), MESO_MIN_SPEED); // on the previous segment
645 26045882 : veh->setSegment(this); // for arrival checking
646 : veh->setLastEntryTime(time);
647 : veh->setBlockTime(SUMOTime_MAX);
648 26045882 : if (!isDepart && (
649 : // arrival on entering a new edge
650 4756446 : (newEdge && myEdge.isNormal() && veh->moveRoutePointer())
651 : // arrival on entering a new segment
652 25332873 : || veh->hasArrived())) {
653 : // route has ended
654 15348 : veh->setEventTime(time + TIME2STEPS(myLength / speed)); // for correct arrival speed
655 15348 : addReminders(veh);
656 15348 : veh->activateReminders(MSMoveReminder::NOTIFICATION_JUNCTION);
657 30592 : veh->updateDetectors(time, veh->getEventTime(), true,
658 15348 : veh->getEdge()->isVaporizing() ? MSMoveReminder::NOTIFICATION_VAPORIZED_VAPORIZER : MSMoveReminder::NOTIFICATION_ARRIVED);
659 15348 : MSNet::getInstance()->getVehicleControl().scheduleVehicleRemoval(veh);
660 15348 : return;
661 : }
662 : assert(veh->getEdge() == &getEdge() || getEdge().isInternal());
663 : // route continues
664 26030534 : Queue& q = myQueues[qIdx];
665 26030534 : const double maxSpeedOnEdge = veh->getEdge()->getLanes()[qIdx]->getVehicleMaxSpeed(veh);
666 : const double uspeed = MAX2(maxSpeedOnEdge, MESO_MIN_SPEED);
667 : std::vector<MEVehicle*>& cars = q.getModifiableVehicles();
668 : MEVehicle* newLeader = nullptr; // first vehicle in the current queue
669 26030534 : const SUMOTime stopTime = veh->checkStop(time);
670 26030534 : SUMOTime tleave = MAX2(stopTime + TIME2STEPS(myLength / uspeed) + getLinkPenalty(veh), q.getBlockTime());
671 26030534 : if (veh->isStopped()) {
672 14589 : myEdge.addWaiting(veh);
673 : }
674 26030534 : if (veh->isParking()) {
675 : // parking stops should take at least 1ms
676 5802 : veh->setEventTime(MAX2(stopTime, veh->getEventTime() + 1));
677 5802 : veh->setSegment(this, PARKING_QUEUE);
678 5802 : myEdge.getLanes()[0]->addParking(veh); // TODO for GUI only
679 : } else {
680 26024732 : myEdge.lock();
681 26024732 : if (cars.empty()) {
682 8279606 : cars.push_back(veh);
683 : newLeader = veh;
684 : } else {
685 17745126 : SUMOTime leaderOut = cars[0]->getEventTime();
686 17745126 : if (!isDepart && leaderOut > tleave && overtake()) {
687 20 : if (cars.size() == 1) {
688 4 : MSGlobals::gMesoNet->removeLeaderCar(cars[0]);
689 : newLeader = veh;
690 : }
691 20 : cars.insert(cars.begin() + 1, veh);
692 : } else {
693 17745106 : tleave = MAX2(leaderOut + tauWithVehLength(myTau_ff, cars[0]->getVehicleType().getLengthWithGap(), cars[0]->getVehicleType().getCarFollowModel().getHeadwayTime()), tleave);
694 17745106 : cars.insert(cars.begin(), veh);
695 : }
696 : }
697 26024732 : myEdge.unlock();
698 26024732 : myNumVehicles++;
699 26024732 : if (!isDepart && !isTeleport) {
700 : // departs and teleports could take place anywhere on the edge so they should not block regular flow
701 : // the -1 facilitates interleaving of multiple streams
702 25311401 : q.setEntryBlockTime(time + tauWithVehLength(myTau_ff, veh->getVehicleType().getLengthWithGap(), veh->getVehicleType().getCarFollowModel().getHeadwayTime()) - 1);
703 : }
704 26024732 : q.setOccupancy(MIN2(myQueueCapacity, q.getOccupancy() + veh->getVehicleType().getLengthWithGap()));
705 : veh->setEventTime(tleave);
706 26024732 : veh->setSegment(this, qIdx);
707 : }
708 26030534 : addReminders(veh);
709 26030534 : if (isDepart) {
710 712850 : veh->onDepart();
711 712850 : veh->activateReminders(MSMoveReminder::NOTIFICATION_DEPARTED);
712 25317684 : } else if (newEdge) {
713 4756287 : veh->activateReminders(MSMoveReminder::NOTIFICATION_JUNCTION);
714 : } else {
715 20561397 : veh->activateReminders(MSMoveReminder::NOTIFICATION_SEGMENT);
716 : }
717 26030533 : if (veh->isParking()) {
718 5820 : MSGlobals::gMesoNet->addLeaderCar(veh, nullptr);
719 : } else {
720 26024713 : if (newLeader != nullptr) {
721 8279592 : MSGlobals::gMesoNet->addLeaderCar(newLeader, getLink(newLeader));
722 : }
723 : }
724 : }
725 :
726 :
727 : bool
728 8752 : MESegment::vaporizeAnyCar(SUMOTime currentTime, const MSDetectorFileOutput* filter) {
729 9304 : for (const Queue& q : myQueues) {
730 8778 : if (q.size() > 0) {
731 8226 : for (MEVehicle* const veh : q.getVehicles()) {
732 8226 : if (filter->vehicleApplies(*veh)) {
733 8226 : MSGlobals::gMesoNet->removeLeaderCar(veh);
734 8226 : MSGlobals::gMesoNet->changeSegment(veh, currentTime + 1, &myVaporizationTarget, MSMoveReminder::NOTIFICATION_VAPORIZED_CALIBRATOR);
735 : return true;
736 : }
737 : }
738 : }
739 : }
740 : return false;
741 : }
742 :
743 :
744 : void
745 352 : MESegment::setSpeedForQueue(double newSpeed, SUMOTime currentTime, SUMOTime blockTime, const std::vector<MEVehicle*>& vehs) {
746 352 : MEVehicle* v = vehs.back();
747 : SUMOTime oldEarliestExitTime = currentTime;
748 : const SUMOTime oldExit = MAX2(oldEarliestExitTime, v->getEventTime());
749 352 : v->updateDetectors(currentTime, oldExit, false);
750 352 : oldEarliestExitTime = oldExit + tauWithVehLength(myTau_ff, v->getVehicleType().getLengthWithGap(), v->getVehicleType().getCarFollowModel().getHeadwayTime());
751 352 : SUMOTime newEvent = MAX2(newArrival(v, newSpeed, currentTime), blockTime);
752 352 : if (v->getEventTime() != newEvent) {
753 330 : MSGlobals::gMesoNet->removeLeaderCar(v);
754 : v->setEventTime(newEvent);
755 330 : MSGlobals::gMesoNet->addLeaderCar(v, getLink(v));
756 : }
757 1075 : for (std::vector<MEVehicle*>::const_reverse_iterator i = vehs.rbegin() + 1; i != vehs.rend(); ++i) {
758 723 : const SUMOTime oldExitTime = MAX2(oldEarliestExitTime, (*i)->getEventTime());
759 723 : (*i)->updateDetectors(currentTime, oldExitTime, false);
760 723 : const SUMOTime minTau = tauWithVehLength(myTau_ff, (*i)->getVehicleType().getLengthWithGap(), (*i)->getVehicleType().getCarFollowModel().getHeadwayTime());
761 723 : oldEarliestExitTime = oldExitTime + minTau;
762 723 : newEvent = MAX2(newArrival(*i, newSpeed, currentTime), newEvent + minTau);
763 723 : (*i)->setEventTime(newEvent);
764 : }
765 352 : }
766 :
767 :
768 : SUMOTime
769 1075 : MESegment::newArrival(const MEVehicle* const v, double newSpeed, SUMOTime currentTime) {
770 : // since speed is only an upper bound, pos may be too optimistic
771 1075 : const double pos = MIN2(myLength, STEPS2TIME(currentTime - v->getLastEntryTime()) * v->getSpeed());
772 : // traveltime may not be 0
773 1075 : double tt = (myLength - pos) / MAX2(newSpeed, MESO_MIN_SPEED);
774 1075 : return currentTime + MAX2(TIME2STEPS(tt), SUMOTime(1));
775 : }
776 :
777 :
778 : void
779 1044 : MESegment::setSpeed(double newSpeed, SUMOTime currentTime, double jamThresh, int qIdx) {
780 1044 : recomputeJamThreshold(jamThresh);
781 : //myTau_length = MAX2(MESO_MIN_SPEED, newSpeed) * myEdge.getLanes().size() / TIME2STEPS(1);
782 : int i = 0;
783 2182 : for (const Queue& q : myQueues) {
784 1138 : if (q.size() != 0) {
785 386 : if (qIdx == -1 || qIdx == i) {
786 352 : setSpeedForQueue(newSpeed, currentTime, q.getBlockTime(), q.getVehicles());
787 : }
788 : }
789 1138 : i++;
790 : }
791 1044 : }
792 :
793 :
794 : SUMOTime
795 1901060 : MESegment::getEventTime() const {
796 : SUMOTime result = SUMOTime_MAX;
797 4064047 : for (const Queue& q : myQueues) {
798 2162987 : if (q.size() != 0 && q.getVehicles().back()->getEventTime() < result) {
799 : result = q.getVehicles().back()->getEventTime();
800 : }
801 : }
802 1901060 : if (result < SUMOTime_MAX) {
803 946153 : return result;
804 : }
805 : return -1;
806 : }
807 :
808 :
809 : void
810 12104 : MESegment::saveState(OutputDevice& out) const {
811 : bool write = false;
812 23422 : for (const Queue& q : myQueues) {
813 12586 : if (q.getBlockTime() != -1 || !q.getVehicles().empty()) {
814 : write = true;
815 : break;
816 : }
817 : }
818 12104 : if (write) {
819 1268 : out.openTag(SUMO_TAG_SEGMENT).writeAttr(SUMO_ATTR_ID, getID());
820 2595 : for (const Queue& q : myQueues) {
821 1327 : out.openTag(SUMO_TAG_VIEWSETTINGS_VEHICLES);
822 1327 : out.writeAttr(SUMO_ATTR_TIME, toString<SUMOTime>(q.getBlockTime()));
823 1327 : out.writeAttr(SUMO_ATTR_BLOCKTIME, toString<SUMOTime>(q.getEntryBlockTime()));
824 1327 : out.writeAttr(SUMO_ATTR_VALUE, q.getVehicles());
825 2654 : out.closeTag();
826 : }
827 2536 : out.closeTag();
828 : }
829 12104 : }
830 :
831 :
832 : void
833 754 : MESegment::clearState() {
834 1560 : for (Queue& q : myQueues) {
835 : q.getModifiableVehicles().clear();
836 : }
837 754 : }
838 :
839 : void
840 1156 : MESegment::loadState(const std::vector<SUMOVehicle*>& vehs, const SUMOTime blockTime, const SUMOTime entryBlockTime, const int queIdx) {
841 1156 : Queue& q = myQueues[queIdx];
842 1652 : for (SUMOVehicle* veh : vehs) {
843 496 : MEVehicle* v = static_cast<MEVehicle*>(veh);
844 : assert(v->getSegment() == this || myEdge.isInternal());
845 496 : if (myEdge.isInternal()) {
846 13 : v->setSegment(this, v->getQueIndex());
847 : }
848 496 : q.getModifiableVehicles().push_back(v);
849 496 : myNumVehicles++;
850 496 : q.setOccupancy(q.getOccupancy() + v->getVehicleType().getLengthWithGap());
851 496 : addReminders(v);
852 : }
853 1156 : if (q.size() != 0) {
854 : // add the last vehicle of this queue
855 : // !!! one question - what about the previously added vehicle? Is it stored twice?
856 257 : MEVehicle* veh = q.getVehicles().back();
857 257 : MSGlobals::gMesoNet->addLeaderCar(veh, getLink(veh));
858 : }
859 : q.setBlockTime(blockTime);
860 : q.setEntryBlockTime(entryBlockTime);
861 1156 : q.setOccupancy(MIN2(q.getOccupancy(), myQueueCapacity));
862 1156 : }
863 :
864 :
865 : std::vector<const MEVehicle*>
866 122 : MESegment::getVehicles() const {
867 : std::vector<const MEVehicle*> result;
868 316 : for (const Queue& q : myQueues) {
869 194 : result.insert(result.end(), q.getVehicles().begin(), q.getVehicles().end());
870 : }
871 122 : return result;
872 0 : }
873 :
874 :
875 : bool
876 2902902 : MESegment::hasBlockedLeader() const {
877 5852284 : for (const Queue& q : myQueues) {
878 2979740 : if (q.size() > 0 && q.getVehicles().back()->getWaitingTime() > 0) {
879 : return true;
880 : }
881 : }
882 : return false;
883 : }
884 :
885 :
886 : double
887 0 : MESegment::getFlow() const {
888 0 : return 3600 * getCarNumber() * getMeanSpeed() / myLength;
889 : }
890 :
891 :
892 : SUMOTime
893 26030534 : MESegment::getLinkPenalty(const MEVehicle* veh) const {
894 26030534 : const MSLink* link = getLink(veh, myTLSPenalty || myCheckMinorPenalty);
895 26030534 : if (link != nullptr) {
896 : SUMOTime result = 0;
897 1756455 : if (link->isTLSControlled() && myTLSPenalty) {
898 : result += link->getMesoTLSPenalty();
899 : }
900 : // minor tls links may get an additional penalty
901 364179 : if (!link->havePriority() &&
902 : // do not apply penalty on top of tLSPenalty
903 1756455 : !myTLSPenalty &&
904 : // do not apply penalty if limited control is active
905 328197 : (!MSGlobals::gMesoLimitedJunctionControl || limitedControlOverride(link))) {
906 325727 : result += myMinorPenalty;
907 : }
908 1756455 : return result;
909 : } else {
910 : return 0;
911 : }
912 : }
913 :
914 :
915 : double
916 9 : MESegment::getWaitingSeconds() const {
917 : double result = 0;
918 22 : for (const Queue& q : myQueues) {
919 : // @note: only the leader currently accumulates waitingTime but this might change in the future
920 16 : for (const MEVehicle* veh : q.getVehicles()) {
921 3 : result += veh->getWaitingSeconds();
922 : }
923 : }
924 9 : return result;
925 : }
926 :
927 :
928 : /****************************************************************************/
|