SUMO - Simulation of Urban MObility
MESegment.cpp
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2001-2017 German Aerospace Center (DLR) and others.
4 /****************************************************************************/
5 //
6 // This program and the accompanying materials
7 // are made available under the terms of the Eclipse Public License v2.0
8 // which accompanies this distribution, and is available at
9 // http://www.eclipse.org/legal/epl-v20.html
10 //
11 /****************************************************************************/
17 // A single mesoscopic segment (cell)
18 /****************************************************************************/
19 
20 
21 // ===========================================================================
22 // included modules
23 // ===========================================================================
24 #ifdef _MSC_VER
25 #include <windows_config.h>
26 #else
27 #include <config.h>
28 #endif
29 
30 #include <algorithm>
31 #include <limits>
32 #include <utils/common/StdDefs.h>
33 #include <microsim/MSGlobals.h>
34 #include <microsim/MSEdge.h>
35 #include <microsim/MSJunction.h>
36 #include <microsim/MSNet.h>
37 #include <microsim/MSLane.h>
38 #include <microsim/MSLinkCont.h>
39 #include <microsim/MSVehicle.h>
48 #include "MEVehicle.h"
49 #include "MELoop.h"
50 #include "MESegment.h"
51 
52 #define DEFAULT_VEH_LENGHT_WITH_GAP (SUMOVTypeParameter::getDefault().length + SUMOVTypeParameter::getDefault().minGap)
53 // avoid division by zero when driving very slowly
54 #define MESO_MIN_SPEED (0.05)
55 
56 // ===========================================================================
57 // static member defintion
58 // ===========================================================================
59 MSEdge MESegment::myDummyParent("MESegmentDummyParent", -1, EDGEFUNC_UNKNOWN, "", "", -1);
60 MESegment MESegment::myVaporizationTarget("vaporizationTarget");
61 const double MESegment::DO_NOT_PATCH_JAM_THRESHOLD(std::numeric_limits<double>::max());
62 
63 // ===========================================================================
64 // method definitions
65 // ===========================================================================
66 MESegment::MESegment(const std::string& id,
67  const MSEdge& parent, MESegment* next,
68  double length, double speed,
69  int idx,
70  SUMOTime tauff, SUMOTime taufj,
71  SUMOTime taujf, SUMOTime taujj,
72  double jamThresh, bool multiQueue, bool junctionControl) :
73  Named(id), myEdge(parent), myNextSegment(next),
74  myLength(length), myIndex(idx),
75  myTau_ff((SUMOTime)(tauff / parent.getLanes().size())),
76  myTau_fj((SUMOTime)(taufj / parent.getLanes().size())), // Eissfeldt p. 90 and 151 ff.
77  myTau_jf((SUMOTime)(taujf / parent.getLanes().size())),
78  myTau_jj((SUMOTime)(taujj / parent.getLanes().size())),
79  myTau_length(MAX2(MESO_MIN_SPEED, speed) * parent.getLanes().size() / TIME2STEPS(1)),
80  myHeadwayCapacity(length / DEFAULT_VEH_LENGHT_WITH_GAP * parent.getLanes().size())/* Eissfeldt p. 69 */,
81  myCapacity(length * parent.getLanes().size()),
82  myOccupancy(0.f),
83  myJunctionControl(junctionControl),
84  myTLSPenalty(MSGlobals::gMesoTLSPenalty > 0 &&
85  // only apply to the last segment of a tls-controlled edge
86  myNextSegment == 0 && (
87  parent.getToJunction()->getType() == NODETYPE_TRAFFIC_LIGHT ||
88  parent.getToJunction()->getType() == NODETYPE_TRAFFIC_LIGHT_NOJUNCTION ||
89  parent.getToJunction()->getType() == NODETYPE_TRAFFIC_LIGHT_RIGHT_ON_RED)),
90  myMinorPenalty(MSGlobals::gMesoMinorPenalty > 0 &&
91  // only apply to the last segment of an uncontrolled edge that has at least 1 minor link
92  myNextSegment == 0 &&
93  parent.getToJunction()->getType() != NODETYPE_TRAFFIC_LIGHT &&
94  parent.getToJunction()->getType() != NODETYPE_TRAFFIC_LIGHT_NOJUNCTION &&
95  parent.getToJunction()->getType() != NODETYPE_TRAFFIC_LIGHT_RIGHT_ON_RED &&
96  parent.hasMinorLink()),
97  myEntryBlockTime(SUMOTime_MIN),
98  myLastHeadway(TIME2STEPS(-1)),
99  myMeanSpeed(speed),
100  myLastMeanSpeedUpdate(SUMOTime_MIN) {
101  myCarQues.push_back(std::vector<MEVehicle*>());
102  myBlockTimes.push_back(-1);
103  if (useMultiQueue(multiQueue, parent)) {
104  const std::vector<MSLane*>& lanes = parent.getLanes();
105  while (myCarQues.size() < lanes.size()) {
106  myCarQues.push_back(std::vector<MEVehicle*>());
107  myBlockTimes.push_back(-1);
108  }
109  for (int i = 0; i < (int)parent.getNumSuccessors(); ++i) {
110  const MSEdge* const edge = parent.getSuccessors()[i];
111  const std::vector<MSLane*>* const allowed = parent.allowedLanes(*edge);
112  assert(allowed != 0);
113  assert(allowed->size() > 0);
114  for (std::vector<MSLane*>::const_iterator j = allowed->begin(); j != allowed->end(); ++j) {
115  std::vector<MSLane*>::const_iterator it = find(lanes.begin(), lanes.end(), *j);
116  myFollowerMap[edge].push_back((int)distance(lanes.begin(), it));
117  }
118  }
119  }
120  recomputeJamThreshold(jamThresh);
121 }
122 
123 
124 MESegment::MESegment(const std::string& id):
125  Named(id),
126  myEdge(myDummyParent), // arbitrary edge needed to supply the needed reference
127  myNextSegment(0), myLength(0), myIndex(0),
128  myTau_ff(0), myTau_fj(0), myTau_jf(0), myTau_jj(0), myTau_length(1),
130  myTLSPenalty(false),
131  myMinorPenalty(false) {
132 }
133 
134 
135 bool
136 MESegment::useMultiQueue(bool multiQueue, const MSEdge& parent) {
137  return multiQueue && parent.getLanes().size() > 1 && parent.getNumSuccessors() > 1;
138 }
139 
140 void
142  if (jamThresh == DO_NOT_PATCH_JAM_THRESHOLD) {
143  return;
144  }
145  if (jamThresh < 0) {
146  // compute based on speed
147  double speed = myEdge.getSpeedLimit();
148  if (myTLSPenalty || myMinorPenalty) {
149  double travelTime = myLength / MAX2(speed, NUMERICAL_EPS) + getMaxPenaltySeconds();
150  speed = myLength / travelTime;
151  }
152  myJamThreshold = jamThresholdForSpeed(speed, jamThresh);
153  } else {
154  // compute based on specified percentage
155  myJamThreshold = jamThresh * myCapacity;
156  }
157 
158  // update coefficients for the jam-jam headway function
159  // this function models the effect that "empty space" needs to move
160  // backwards through the downstream segment before the upstream segment may
161  // send annother vehicle.
162  // this allows jams to clear and move upstream.
163  // the headway function f(x) depends on the number of vehicles in the
164  // downstream segment x
165  // f is a linear function that passes through the following fixed points:
166  // f(n_jam_threshold) = tau_jf_withLength (for continuity)
167  // f(myHeadwayCapacity) = myTau_jj * myHeadwayCapacity
168 
170  if (myJamThreshold < myCapacity) {
171  // jamming is possible
172  const double n_jam_threshold = myHeadwayCapacity * myJamThreshold / myCapacity; // number of vehicles above which the segment is jammed
173  // solving f(x) = a * x + b
174  myA = (STEPS2TIME(myTau_jj) * myHeadwayCapacity - STEPS2TIME(tau_jf_withLength)) / (myHeadwayCapacity - n_jam_threshold);
176 
177  // note that the original Eissfeldt model (p. 69) used different fixed points
178  // f(n_jam_threshold) = n_jam_threshold * myTau_jj
179  // f(myHeadwayCapacity) = myTau_jf * myHeadwayCapacity
180  //
181  // However, this systematically underestimates the backpropagation speed of the jam front (see #2244)
182  } else {
183  // dummy values. Should not be used
184  myA = 0;
185  myB = STEPS2TIME(tau_jf_withLength);
186  }
187 }
188 
189 
190 double
191 MESegment::jamThresholdForSpeed(double speed, double jamThresh) const {
192  // vehicles driving freely at maximum speed should not jam
193  // we compute how many vehicles could possible enter the segment until the first vehicle leaves
194  // and multiply by the space these vehicles would occupy
195  // the jamThresh parameter is scale the resulting value
196  if (speed == 0) {
197  return std::numeric_limits<double>::max(); // never jam. Irrelevant at speed 0 anyway
198  }
200 }
201 
202 
203 void
205  myDetectorData.push_back(data);
206  for (Queues::const_iterator k = myCarQues.begin(); k != myCarQues.end(); ++k) {
207  for (std::vector<MEVehicle*>::const_reverse_iterator i = k->rbegin(); i != k->rend(); ++i) {
208  (*i)->addReminder(data);
209  }
210  }
211 }
212 
213 
214 void
216  std::vector<MSMoveReminder*>::iterator it = find(
217  myDetectorData.begin(), myDetectorData.end(), data);
218  if (it != myDetectorData.end()) {
219  myDetectorData.erase(it);
220  }
221  for (Queues::const_iterator k = myCarQues.begin(); k != myCarQues.end(); ++k) {
222  for (std::vector<MEVehicle*>::const_reverse_iterator i = k->rbegin(); i != k->rend(); ++i) {
223  (*i)->removeReminder(data);
224  }
225  }
226 }
227 
228 
229 void
232  if (next == 0) {
234  } else if (next == &myVaporizationTarget) {
236  } else if (myNextSegment == 0) {
238  } else {
240  }
241  v->updateDetectors(currentTime, true, reason);
242 }
243 
244 
245 void
247  const SUMOTime currentTime = MSNet::getInstance()->getCurrentTimeStep();
248  for (Queues::const_iterator k = myCarQues.begin(); k != myCarQues.end(); ++k) {
249  SUMOTime earliestExitTime = currentTime;
250  for (std::vector<MEVehicle*>::const_reverse_iterator i = k->rbegin(); i != k->rend(); ++i) {
251  const SUMOTime exitTime = MAX2(earliestExitTime, (*i)->getEventTime());
252  (*i)->updateDetectorForWriting(&data, currentTime, exitTime);
253  earliestExitTime = exitTime + tauWithVehLength(myTau_ff, (*i)->getVehicleType().getLengthWithGap());
254  }
255  }
256 }
257 
258 
259 bool
260 MESegment::hasSpaceFor(const MEVehicle* veh, SUMOTime entryTime, bool init) const {
261  if (myOccupancy == 0.) {
262  // we have always space for at least one vehicle
263  return true;
264  }
265  const double newOccupancy = myOccupancy + veh->getVehicleType().getLengthWithGap();
266  if (newOccupancy > myCapacity) {
267  // we must ensure that occupancy remains below capacity
268  return false;
269  }
270  // regular insertions and initial insertions must respect different constraints:
271  // - regular insertions must respect entryBlockTime
272  // - initial insertions should not cause additional jamming
273  // - inserted vehicle should be able to continue at the current speed
274  if (init) {
275  if (free() && !hasBlockedLeader()) {
276  return newOccupancy <= myJamThreshold;
277  } else {
278  return newOccupancy <= jamThresholdForSpeed(getMeanSpeed(false), -1);
279  }
280  }
281  // maintain propper spacing between inflow from different lanes
282  return entryTime >= myEntryBlockTime;
283 }
284 
285 
286 bool
288  if (hasSpaceFor(veh, time, true)) {
289  receive(veh, time, true);
290  // we can check only after insertion because insertion may change the route via devices
291  std::string msg;
292  if (MSGlobals::gCheckRoutes && !veh->hasValidRoute(msg)) {
293  throw ProcessError("Vehicle '" + veh->getID() + "' has no valid route. " + msg);
294  }
295  return true;
296  }
297  return false;
298 }
299 
300 
301 int
303  int total = 0;
304  for (Queues::const_iterator k = myCarQues.begin(); k != myCarQues.end(); ++k) {
305  total += (int)k->size();
306  }
307  return total;
308 }
309 
310 
311 double
312 MESegment::getMeanSpeed(bool useCached) const {
313  const SUMOTime currentTime = MSNet::getInstance()->getCurrentTimeStep();
314  if (currentTime != myLastMeanSpeedUpdate || !useCached) {
315  myLastMeanSpeedUpdate = currentTime;
316  const SUMOTime tau = free() ? myTau_ff : myTau_jf;
317  double v = 0;
318  int count = 0;
319  for (Queues::const_iterator k = myCarQues.begin(); k != myCarQues.end(); ++k) {
320  SUMOTime earliestExitTime = currentTime;
321  count += (int)k->size();
322  for (std::vector<MEVehicle*>::const_reverse_iterator veh = k->rbegin(); veh != k->rend(); ++veh) {
323  v += (*veh)->getConservativeSpeed(earliestExitTime); // earliestExitTime is updated!
324  earliestExitTime += tauWithVehLength(tau, (*veh)->getVehicleType().getLengthWithGap());
325  }
326  }
327  if (count == 0) {
329  } else {
330  myMeanSpeed = v / (double) count;
331  }
332  }
333  return myMeanSpeed;
334 }
335 
336 
337 void
339  for (Queues::const_iterator k = myCarQues.begin(); k != myCarQues.end(); ++k) {
340  for (std::vector<MEVehicle*>::const_iterator veh = k->begin(); veh != k->end(); ++veh) {
341  MSXMLRawOut::writeVehicle(of, *(*veh));
342  }
343  }
344 }
345 
346 
347 MEVehicle*
350  std::vector<MEVehicle*>& cars = myCarQues[v->getQueIndex()];
351  assert(std::find(cars.begin(), cars.end(), v) != cars.end());
352  // One could be tempted to do v->setSegment(next); here but position on lane will be invalid if next == 0
353  updateDetectorsOnLeave(v, leaveTime, next);
354  myEdge.lock();
355  if (v == cars.back()) {
356  cars.pop_back();
357  if (!cars.empty()) {
358  myEdge.unlock();
359  return cars.back();
360  }
361  } else {
362  cars.erase(std::find(cars.begin(), cars.end(), v));
363  }
364  myEdge.unlock();
365  return 0;
366 }
367 
368 
369 SUMOTime
371  const SUMOTime tau = (pred->free()
372  ? (free() ? myTau_ff : myTau_fj)
373  : (free() ? myTau_jf : TIME2STEPS(myA * getCarNumber() + myB)));
374  return (SUMOTime)(tauWithVehLength(tau, veh->getVehicleType().getLengthWithGap()) / pred->getTLSCapacity(veh));
375 }
376 
377 
378 SUMOTime
380  // since we do not know which queue will be used we give a conservative estimate
381  SUMOTime earliestLeave = earliestEntry;
382  for (int i = 0; i < (int)myCarQues.size(); ++i) {
383  earliestLeave = MAX2(earliestLeave, myBlockTimes[i]);
384  }
385  if (myEdge.getSpeedLimit() == 0) {
386  return MAX2(earliestEntry, myEntryBlockTime); // FIXME: This line is just an adhoc-fix to avoid division by zero (Leo)
387  } else {
388  return MAX3(earliestEntry, earliestLeave - TIME2STEPS(myLength / myEdge.getSpeedLimit()), myEntryBlockTime);
389  }
390 }
391 
392 
393 MSLink*
394 MESegment::getLink(const MEVehicle* veh, bool penalty) const {
395  if (myJunctionControl || penalty) {
396  const MSEdge* const nextEdge = veh->succEdge(1);
397  if (nextEdge == 0) {
398  return 0;
399  }
400  // try to find any link leading to our next edge, start with the lane pointed to by the que index
401  const MSLane* const bestLane = myEdge.getLanes()[veh->getQueIndex()];
402  const MSLinkCont& links = bestLane->getLinkCont();
403  for (std::vector<MSLink*>::const_iterator j = links.begin(); j != links.end(); ++j) {
404  if (&(*j)->getLane()->getEdge() == nextEdge) {
405  return *j;
406  }
407  }
408  // this is for the non-multique case, maybe we should use caching here !!!
409  for (std::vector<MSLane*>::const_iterator l = myEdge.getLanes().begin(); l != myEdge.getLanes().end(); ++l) {
410  if ((*l) != bestLane) {
411  const MSLinkCont& links = (*l)->getLinkCont();
412  for (std::vector<MSLink*>::const_iterator j = links.begin(); j != links.end(); ++j) {
413  if (&(*j)->getLane()->getEdge() == nextEdge) {
414  return *j;
415  }
416  }
417  }
418  }
419  }
420  return 0;
421 }
422 
423 
424 bool
425 MESegment::isOpen(const MEVehicle* veh) const {
426  if (myTLSPenalty) {
427  // XXX should limited control take precedence over tls penalty?
428  return true;
429  }
430  const MSLink* link = getLink(veh);
431  return (link == 0
432  || link->havePriority()
433  || limitedControlOverride(link)
434  || link->opened(veh->getEventTime(), veh->getSpeed(), veh->estimateLeaveSpeed(link),
437 }
438 
439 
440 bool
442  assert(link != 0);
444  return false;
445  }
446  // if the target segment of this link is not saturated junction control is disabled
447  const MSEdge& targetEdge = link->getLane()->getEdge();
448  const MESegment* target = MSGlobals::gMesoNet->getSegmentForEdge(targetEdge);
449  return target->myOccupancy * 2 < target->myJamThreshold;
450 }
451 
452 
453 void
455  assert(isInvalid(next) || time >= myBlockTimes[veh->getQueIndex()]);
456  MSLink* link = getLink(veh);
457  if (link != 0) {
458  link->removeApproaching(veh);
459  }
460  MEVehicle* lc = removeCar(veh, time, next); // new leaderCar
461  myBlockTimes[veh->getQueIndex()] = time;
462  if (!isInvalid(next)) {
463  myLastHeadway = next->getTimeHeadway(this, veh);
465  }
466  if (lc != 0) {
469  }
470 }
471 
472 bool
475 }
476 
477 
478 void
480  for (std::vector<MSMoveReminder*>::const_iterator i = myDetectorData.begin(); i != myDetectorData.end(); ++i) {
481  veh->addReminder(*i);
482  }
483 }
484 
485 void
486 MESegment::receive(MEVehicle* veh, SUMOTime time, bool isDepart, bool afterTeleport) {
487  const double speed = isDepart ? -1 : MAX2(veh->getSpeed(), MESO_MIN_SPEED); // on the previous segment
488  veh->setSegment(this); // for arrival checking
489  veh->setLastEntryTime(time);
491  if (!isDepart && (
492  // arrival on entering a new edge
493  ((myIndex == 0 || afterTeleport) && veh->moveRoutePointer())
494  // arrival on entering a new segment
495  || veh->hasArrived())) {
496  // route has ended
497  veh->setEventTime(time + TIME2STEPS(myLength / speed)); // for correct arrival speed
498  addReminders(veh);
500  updateDetectorsOnLeave(veh, time, 0);
502  return;
503  }
504  // route continues
505  const double maxSpeedOnEdge = veh->getEdge()->getVehicleMaxSpeed(veh);
506  const double uspeed = MAX2(maxSpeedOnEdge, MESO_MIN_SPEED);
507  int nextQueIndex = 0;
508  if (myCarQues.size() > 1) {
509  const MSEdge* succ = veh->succEdge(1);
510  // succ may be invalid if called from initialise() with an invalid route
511  if (succ != 0 && myFollowerMap.count(succ) > 0) {
512  const std::vector<int>& indices = myFollowerMap[succ];
513  nextQueIndex = indices[0];
514  for (std::vector<int>::const_iterator i = indices.begin() + 1; i != indices.end(); ++i) {
515  if (myCarQues[*i].size() < myCarQues[nextQueIndex].size()) {
516  nextQueIndex = *i;
517  }
518  }
519  }
520  }
521  std::vector<MEVehicle*>& cars = myCarQues[nextQueIndex];
522  MEVehicle* newLeader = 0; // first vehicle in the current queue
523  SUMOTime tleave = MAX2(time + TIME2STEPS(myLength / uspeed) + veh->getStoptime(this) + getLinkPenalty(veh), myBlockTimes[nextQueIndex]);
524  myEdge.lock();
525  if (cars.empty()) {
526  cars.push_back(veh);
527  newLeader = veh;
528  } else {
529  SUMOTime leaderOut = cars[0]->getEventTime();
530  if (!isDepart && leaderOut > tleave && overtake()) {
531  if (cars.size() == 1) {
533  newLeader = veh;
534  }
535  cars.insert(cars.begin() + 1, veh);
536  } else {
537  tleave = MAX2(leaderOut + tauWithVehLength(myTau_ff, cars[0]->getVehicleType().getLengthWithGap()), tleave);
538  cars.insert(cars.begin(), veh);
539  }
540  }
541  myEdge.unlock();
542  if (!isDepart) {
543  // regular departs could take place anywhere on the edge so they should not block regular flow
544  // the -1 facilitates interleaving of multiple streams
546  }
547  veh->setEventTime(tleave);
548  veh->setSegment(this, nextQueIndex);
550  addReminders(veh);
551  if (isDepart) {
552  veh->onDepart();
554  } else if (myIndex == 0 || afterTeleport) {
556  } else {
558  }
559  if (newLeader != 0) {
560  MSGlobals::gMesoNet->addLeaderCar(newLeader, getLink(newLeader));
561  }
562 }
563 
564 
565 bool
567  MEVehicle* remove = 0;
568  for (Queues::const_iterator k = myCarQues.begin(); k != myCarQues.end(); ++k) {
569  if (!k->empty()) {
570  // remove last in queue
571  remove = k->front();
572  if (k->size() == 1) {
574  }
576  return true;
577  }
578  }
579  return false;
580 }
581 
582 
583 void
584 MESegment::setSpeedForQueue(double newSpeed, SUMOTime currentTime, SUMOTime blockTime, const std::vector<MEVehicle*>& vehs) {
585  MEVehicle* v = vehs.back();
586  v->updateDetectors(currentTime, false);
587  SUMOTime newEvent = MAX2(newArrival(v, newSpeed, currentTime), blockTime);
588  if (v->getEventTime() != newEvent) {
590  v->setEventTime(newEvent);
592  }
593  for (std::vector<MEVehicle*>::const_reverse_iterator i = vehs.rbegin() + 1; i != vehs.rend(); ++i) {
594  (*i)->updateDetectors(currentTime, false);
595  newEvent = MAX2(newArrival(*i, newSpeed, currentTime), newEvent + myTau_ff);
596  //newEvent = MAX2(newArrival(*i, newSpeed, currentTime), newEvent + myTau_ff + (SUMOTime)((*(i - 1))->getVehicleType().getLength() / myTau_length));
597  (*i)->setEventTime(newEvent);
598  }
599 }
600 
601 
602 SUMOTime
603 MESegment::newArrival(const MEVehicle* const v, double newSpeed, SUMOTime currentTime) {
604  // since speed is only an upper bound pos may be to optimistic
605  const double pos = MIN2(myLength, STEPS2TIME(currentTime - v->getLastEntryTime()) * v->getSpeed());
606  // traveltime may not be 0
607  return currentTime + MAX2(TIME2STEPS((myLength - pos) / newSpeed), SUMOTime(1));
608 }
609 
610 
611 void
612 MESegment::setSpeed(double newSpeed, SUMOTime currentTime, double jamThresh) {
613  recomputeJamThreshold(jamThresh);
614  //myTau_length = MAX2(MESO_MIN_SPEED, newSpeed) * myEdge.getLanes().size() / TIME2STEPS(1);
615  for (int i = 0; i < (int)myCarQues.size(); ++i) {
616  if (myCarQues[i].size() != 0) {
617  setSpeedForQueue(newSpeed, currentTime, myBlockTimes[i], myCarQues[i]);
618  }
619  }
620 }
621 
622 
623 SUMOTime
625  SUMOTime result = SUMOTime_MAX;
626  for (int i = 0; i < (int)myCarQues.size(); ++i) {
627  if (myCarQues[i].size() != 0 && myCarQues[i].back()->getEventTime() < result) {
628  result = myCarQues[i].back()->getEventTime();
629  }
630  }
631  if (result < SUMOTime_MAX) {
632  return result;
633  }
634  return -1;
635 }
636 
637 
638 void
641  for (int i = 0; i < (int)myCarQues.size(); ++i) {
644  out.closeTag();
645  }
646  out.closeTag();
647 }
648 
649 
650 void
651 MESegment::loadState(std::vector<std::string>& vehIds, MSVehicleControl& vc, const SUMOTime block, const int queIdx) {
652  for (std::vector<std::string>::const_iterator it = vehIds.begin(); it != vehIds.end(); ++it) {
653  MEVehicle* v = static_cast<MEVehicle*>(vc.getVehicle(*it));
654  assert(v != 0);
655  assert(v->getSegment() == this);
656  myCarQues[queIdx].push_back(v);
658  }
659  if (myCarQues[queIdx].size() != 0) {
660  // add the last vehicle of this queue
661  // !!! one question - what about the previously added vehicle? Is it stored twice?
662  MEVehicle* veh = myCarQues[queIdx].back();
664  }
665  myBlockTimes[queIdx] = block;
667 }
668 
669 
670 std::vector<const MEVehicle*>
672  std::vector<const MEVehicle*> result;
673  for (Queues::const_iterator k = myCarQues.begin(); k != myCarQues.end(); ++k) {
674  result.insert(result.end(), k->begin(), k->end());
675  }
676  return result;
677 }
678 
679 
680 bool
682  for (Queues::const_iterator k = myCarQues.begin(); k != myCarQues.end(); ++k) {
683  if (k->size() > 0 && (*k).back()->getWaitingTime() > 0) {
684  return true;
685  }
686  }
687  return false;
688 }
689 
690 
691 double
693  return 3600 * getCarNumber() * getMeanSpeed() / myLength;
694 }
695 
696 
697 SUMOTime
699  const MSLink* link = getLink(veh, myTLSPenalty || myMinorPenalty);
700  if (link != 0) {
701  SUMOTime result = 0;
702  if (link->isTLSControlled()) {
703  result += link->getMesoTLSPenalty();
704  }
705  // minor tls links may get an additional penalty
706  if (!link->havePriority() &&
707  // do not apply penalty if limited control is active
710  }
711  return result;
712  } else {
713  return 0;
714  }
715 }
716 
717 
718 double
720  if (myTLSPenalty) {
721  const MSLink* link = getLink(veh, true);
722  if (link != 0) {
723  assert(link->isTLSControlled());
724  assert(link->getGreenFraction() > 0);
725  return link->getGreenFraction();
726  }
727  }
728  return 1;
729 }
730 
731 
732 double
734  double maxPenalty = 0;
735  for (std::vector<MSLane*>::const_iterator i = myEdge.getLanes().begin(); i != myEdge.getLanes().end(); ++i) {
736  MSLane* l = *i;
737  const MSLinkCont& lc = l->getLinkCont();
738  for (MSLinkCont::const_iterator j = lc.begin(); j != lc.end(); ++j) {
739  MSLink* link = *j;
740  maxPenalty = MAX2(maxPenalty, STEPS2TIME(
741  link->getMesoTLSPenalty() + (link->havePriority() ? 0 : MSGlobals::gMesoMinorPenalty)));
742  }
743  }
744  return maxPenalty;
745 }
746 
747 /****************************************************************************/
double getLengthWithGap() const
Get vehicle&#39;s length including the minimum gap [m].
double myMeanSpeed
the mean speed on this segment. Updated at event time or on demand
Definition: MESegment.h:510
OutputDevice & writeAttr(const SumoXMLAttr attr, const T &val)
writes a named attribute
Definition: OutputDevice.h:260
bool changeSegment(MEVehicle *veh, SUMOTime leaveTime, MESegment *const toSegment, const bool ignoreLink=false)
change to the next segment this handles combinations of the following cases: (ending / continuing rou...
Definition: MELoop.cpp:87
static MESegment myVaporizationTarget
Definition: MESegment.h:507
segment of a lane
virtual void setSegment(MESegment *s, int idx=0)
Sets the current segment the vehicle is at together with its que.
Definition: MEVehicle.h:220
MSEdge & getEdge() const
Returns the lane&#39;s edge.
Definition: MSLane.h:607
bool isOpen(const MEVehicle *veh) const
Returns whether the vehicle may use the next link.
Definition: MESegment.cpp:425
bool hasValidRoute(std::string &msg, const MSRoute *route=0) const
Validates the current or given route.
double getMeanSpeed() const
wrapper to satisfy the FunctionBinding signature
Definition: MESegment.h:205
A vehicle from the mesoscopic point of view.
Definition: MEVehicle.h:51
MESegment * getSegmentForEdge(const MSEdge &e, double pos=0)
Get the segment for a given edge at a given position.
Definition: MELoop.cpp:289
const bool myMinorPenalty
Whether minor penalty is enabled.
Definition: MESegment.h:479
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:612
int getCarNumber() const
Returns the total number of cars on the segment.
Definition: MESegment.cpp:302
MEVehicle * removeCar(MEVehicle *v, SUMOTime leaveTime, MESegment *next)
Removes the given car from the edge&#39;s que.
Definition: MESegment.cpp:348
double myOccupancy
The occupied space (in m) on the segment.
Definition: MESegment.h:470
bool overtake()
Definition: MESegment.cpp:473
double estimateLeaveSpeed(const MSLink *link) const
Returns the vehicle&#39;s estimated speed after driving accross the link.
Definition: MEVehicle.cpp:125
bool initialise(MEVehicle *veh, SUMOTime time)
Inserts (emits) vehicle into the segment.
Definition: MESegment.cpp:287
SUMOTime getLastEntryTime() const
Returns the time the vehicle entered the current segment.
Definition: MEVehicle.h:253
virtual void unlock() const
release exclusive access to the mesoscopic state
Definition: MSEdge.h:653
The vehicle arrived at a junction.
SUMOTime myEntryBlockTime
Definition: MESegment.h:499
static double rand(std::mt19937 *rng=0)
Returns a random real number in [0, 1)
Definition: RandHelper.h:64
double jamThresholdForSpeed(double speed, double jamThresh) const
compute jam threshold for the given speed and jam-threshold option
Definition: MESegment.cpp:191
SUMOVehicle * getVehicle(const std::string &id) const
Returns the vehicle with the given id.
Notification
Definition of a vehicle state.
std::vector< MSMoveReminder * > myDetectorData
The data collection for all kinds of detectors.
Definition: MESegment.h:485
const std::vector< MSLane * > * allowedLanes(const MSEdge &destination, SUMOVehicleClass vclass=SVC_IGNORING) const
Get the allowed lanes to reach the destination-edge.
Definition: MSEdge.cpp:295
double getMaxPenaltySeconds() const
return the maximum tls penalty for all links from this edge
Definition: MESegment.cpp:733
const std::vector< MSLane * > & getLanes() const
Returns this edge&#39;s lanes.
Definition: MSEdge.h:167
SUMOTime getWaitingTime() const
Returns the duration for which the vehicle was blocked.
Definition: MEVehicle.h:276
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition: MSNet.cpp:167
static bool useMultiQueue(bool multiQueue, const MSEdge &parent)
whether the segment requires use of multiple queues
Definition: MESegment.cpp:136
T MAX2(T a, T b)
Definition: StdDefs.h:73
SUMOTime getEventTime() const
Returns the (planned) time at which the vehicle leaves his current cell.
Definition: MEVehicle.h:211
The vehicle got vaporized.
double getTLSCapacity(const MEVehicle *veh) const
Returns the average green time as fraction of cycle time.
Definition: MESegment.cpp:719
The vehicle changes the segment (meso only)
const double myCapacity
The number of lanes * the length.
Definition: MESegment.h:467
#define TIME2STEPS(x)
Definition: SUMOTime.h:66
bool hasBlockedLeader() const
whether a leader in any queue is blocked
Definition: MESegment.cpp:681
SUMOTime getEventTime() const
Returns the (planned) time at which the next vehicle leaves this segment.
Definition: MESegment.cpp:624
void setSpeedForQueue(double newSpeed, SUMOTime currentTime, SUMOTime blockTime, const std::vector< MEVehicle *> &vehs)
Definition: MESegment.cpp:584
static bool gMesoOvertaking
Definition: MSGlobals.h:103
double getSpeedLimit() const
Returns the speed limit of the edge The speed limit of the first lane is retured; should probably be...
Definition: MSEdge.cpp:846
T MAX3(T a, T b, T c)
Definition: StdDefs.h:87
void setBlockTime(const SUMOTime t)
Sets the time at which the vehicle was blocked.
Definition: MEVehicle.h:261
double myJamThreshold
The space (in m) which needs to be occupied before the segment is considered jammed.
Definition: MESegment.h:482
Queues myCarQues
The car queues. Vehicles are inserted in the front and removed in the back.
Definition: MESegment.h:488
int getNumSuccessors() const
Returns the number of edges that may be reached from this edge.
Definition: MSEdge.h:312
bool hasSpaceFor(const MEVehicle *veh, SUMOTime entryTime, bool init=false) const
Returns whether the given vehicle would still fit into the segment.
Definition: MESegment.cpp:260
void loadState(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:651
static const double DO_NOT_PATCH_JAM_THRESHOLD
Definition: MESegment.h:378
void writeVehicles(OutputDevice &of) const
Definition: MESegment.cpp:338
void removeLeaderCar(MEVehicle *v)
Removes the given car from the leading vehicles.
Definition: MELoop.cpp:224
#define SUMOTime_MIN
Definition: SUMOTime.h:44
A road/street connecting two junctions.
Definition: MSEdge.h:80
SUMOTime getLinkPenalty(const MEVehicle *veh) const
Returns the penalty time for passing a link (if using gMesoTLSPenalty > 0 or gMesoMinorPenalty > 0) ...
Definition: MESegment.cpp:698
void receive(MEVehicle *veh, SUMOTime time, bool isDepart=false, bool afterTeleport=false)
Adds the vehicle to the segment, adapting its parameters.
Definition: MESegment.cpp:486
const MSCFModel & getCarFollowModel() const
Returns the vehicle type&#39;s car following model definition (const version)
bool free() const
return whether this segment is considered free as opposed to jammed
Definition: MESegment.h:350
static bool gCheckRoutes
Definition: MSGlobals.h:85
SUMOTime myLastMeanSpeedUpdate
the time at which myMeanSpeed was last updated
Definition: MESegment.h:513
std::map< const MSEdge *, std::vector< int > > myFollowerMap
The follower edge to que index mapping for multi queue segments.
Definition: MESegment.h:491
const SUMOTime myTau_jf
Definition: MESegment.h:455
#define SUMOTime_MAX
Definition: TraCIDefs.h:52
double myTau_length
Headway parameter for computing gross time headyway from net time headway, length and edge speed...
Definition: MESegment.h:457
MESegment * getSegment() const
Returns the current segment the vehicle is on.
Definition: MEVehicle.h:229
const MSEdge * getEdge() const
Returns the edge the vehicle is currently at.
static bool isInvalid(const MESegment *segment)
whether the given segment is 0 or encodes vaporization
Definition: MESegment.h:341
MSVehicleControl & getVehicleControl()
Returns the vehicle control.
Definition: MSNet.h:306
static MSEdge myDummyParent
Definition: MESegment.h:506
void setLastEntryTime(SUMOTime t)
Sets the entry time for the current segment.
Definition: MEVehicle.h:245
void updateDetectorsOnLeave(MEVehicle *v, SUMOTime currentTime, MESegment *next)
Updates data of all detectors for a leaving vehicle.
Definition: MESegment.cpp:230
The vehicle arrived at its destination (is deleted)
#define STEPS2TIME(x)
Definition: SUMOTime.h:64
SUMOTime getCurrentTimeStep() const
Returns the current simulation step.
Definition: MSNet.h:253
bool hasArrived() const
Returns whether this vehicle has already arived (reached the arrivalPosition on its final edge) ...
Definition: MEVehicle.cpp:159
static SUMOTime gMesoMinorPenalty
Definition: MSGlobals.h:109
T MIN2(T a, T b)
Definition: StdDefs.h:67
void recomputeJamThreshold(double jamThresh)
compute a value for myJamThreshold if jamThresh is negative, compute a value which allows free flow a...
Definition: MESegment.cpp:141
void addDetector(MSMoveReminder *data)
Adds a data collector for a detector to this segment.
Definition: MESegment.cpp:204
double getImpatience() const
Returns this vehicles impatience.
Something on a lane to be noticed about vehicle movement.
double getMaxDecel() const
Get the vehicle type&#39;s maximal comfortable deceleration [m/s^2].
Definition: MSCFModel.h:211
int getQueIndex() const
Returns the index of the que the vehicle is in.
Definition: MEVehicle.h:237
bool vaporizeAnyCar(SUMOTime currentTime)
tries to remove any car from this segment
Definition: MESegment.cpp:566
std::vector< const MEVehicle * > getVehicles() const
returns all vehicles (for debugging)
Definition: MESegment.cpp:671
void removeDetector(MSMoveReminder *data)
Removes a data collector for a detector from this segment.
Definition: MESegment.cpp:215
Base class for objects which have an id.
Definition: Named.h:45
double getVehicleMaxSpeed(const SUMOVehicle *const veh) const
Returns the maximum speed the vehicle may use on this edge.
Definition: MSEdge.cpp:858
bool moveRoutePointer()
Update when the vehicle enters a new edge in the move step.
Definition: MEVehicle.cpp:141
const MSEdge & myEdge
The microsim edge this segment belongs to.
Definition: MESegment.h:443
SUMOTime getNextInsertionTime(SUMOTime earliestEntry) const
return a time after earliestEntry at which a vehicle may be inserted at full speed ...
Definition: MESegment.cpp:379
const SUMOTime myTau_jj
Definition: MESegment.h:455
SUMOTime getStoptime(const MESegment *const seg) const
Returns how long to stop at the given segment.
Definition: MEVehicle.cpp:253
trigger: the time of the step
The vehicle has departed (was inserted into the network)
void addReminders(MEVehicle *veh) const
add this lanes MoveReminders to the given vehicle
Definition: MESegment.cpp:479
void scheduleVehicleRemoval(SUMOVehicle *veh)
Removes a vehicle after it has ended.
const bool myTLSPenalty
Whether tls penalty is enabled.
Definition: MESegment.h:476
MESegment * myNextSegment
The next segment of this edge, 0 if this is the last segment of this edge.
Definition: MESegment.h:446
const double myHeadwayCapacity
The capacity of the segment in number of cars, used only in time headway calculation This parameter h...
Definition: MESegment.h:464
const MSVehicleType & getVehicleType() const
Returns the vehicle&#39;s type definition.
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:441
SUMOTime myLastHeadway
the last headway
Definition: MESegment.h:502
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:394
A single mesoscopic segment (cell)
Definition: MESegment.h:56
double getFlow() const
returns flow based on headway
Definition: MESegment.cpp:692
void onDepart()
Called when the vehicle is inserted into the network.
void updateDetectors(SUMOTime currentTime, const bool isLeave, const MSMoveReminder::Notification reason=MSMoveReminder::NOTIFICATION_JUNCTION)
Updates all vehicle detectors.
Definition: MEVehicle.cpp:306
virtual void activateReminders(const MSMoveReminder::Notification reason, const MSLane *enteredLane=0)
"Activates" all current move reminder
SUMOTime newArrival(const MEVehicle *const v, double newSpeed, SUMOTime currentTime)
compute the new arrival time when switching speed
Definition: MESegment.cpp:603
const MSEdgeVector & getSuccessors() const
Returns the following edges.
Definition: MSEdge.h:319
static MELoop * gMesoNet
mesoscopic simulation infrastructure
Definition: MSGlobals.h:112
const bool myJunctionControl
Whether junction control is enabled.
Definition: MESegment.h:473
void setEventTime(SUMOTime t, bool hasDelay=true)
Sets the (planned) time at which the vehicle leaves his current cell.
Definition: MEVehicle.h:199
virtual void lock() const
grant exclusive access to the mesoscopic state
Definition: MSEdge.h:650
const MSEdge * succEdge(int nSuccs) const
Returns the nSuccs&#39;th successor of edge the vehicle is currently at.
double myB
Definition: MESegment.h:460
const SUMOTime myTau_fj
Definition: MESegment.h:455
Static storage of an output device and its base (abstract) implementation.
Definition: OutputDevice.h:70
bool closeTag()
Closes the most recently opened tag.
void saveState(OutputDevice &out)
Saves the state of this segment into the given stream.
Definition: MESegment.cpp:639
long long int SUMOTime
Definition: TraCIDefs.h:51
#define NUMERICAL_EPS
Definition: config.h:151
MESegment(const std::string &id, const MSEdge &parent, MESegment *next, double length, double speed, int idx, SUMOTime tauff, SUMOTime taufj, SUMOTime taujf, SUMOTime taujj, double jamThresh, bool multiQueue, bool junctionControl)
constructor
Definition: MESegment.cpp:66
std::vector< SUMOTime > myBlockTimes
The block times.
Definition: MESegment.h:494
double getSpeed() const
Returns the vehicle&#39;s estimated speed assuming no delays.
Definition: MEVehicle.cpp:109
The class responsible for building and deletion of vehicles.
void prepareDetectorForWriting(MSMoveReminder &data)
Updates data of a detector for all vehicle queues.
Definition: MESegment.cpp:246
const MSLinkCont & getLinkCont() const
returns the container with all links !!!
Definition: MSLane.cpp:1874
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:437
static void writeVehicle(OutputDevice &of, const MSBaseVehicle &veh)
Writes the dump of the given vehicle into the given device.
SUMOTime getTimeHeadway(const MESegment *pred, const MEVehicle *veh)
Definition: MESegment.cpp:370
void send(MEVehicle *veh, MESegment *next, SUMOTime time)
Removes the vehicle from the segment, adapting its parameters.
Definition: MESegment.cpp:454
#define DEFAULT_VEH_LENGHT_WITH_GAP
Definition: MESegment.cpp:52
#define MESO_MIN_SPEED
Definition: MESegment.cpp:54
const int myIndex
Running number of the segment in the edge.
Definition: MESegment.h:452
const std::string & getID() const
Returns the name of the vehicle.
void addReminder(MSMoveReminder *rem)
Adds a MoveReminder dynamically.
Representation of a lane in the micro simulation.
Definition: MSLane.h:77
const SUMOTime myTau_ff
The time headway parameters, see the Eissfeldt thesis.
Definition: MESegment.h:455
static bool gMesoLimitedJunctionControl
Definition: MSGlobals.h:100
void addLeaderCar(MEVehicle *veh, MSLink *link)
Adds the given car to the leading vehicles.
Definition: MELoop.cpp:204
OutputDevice & openTag(const std::string &xmlElement)
Opens an XML tag.
double myA
slope and axis offset for the jam-jam headway function
Definition: MESegment.h:460
const double myLength
The segment&#39;s length.
Definition: MESegment.h:449