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