SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
MS_E2_ZS_CollectorOverLanes.cpp
Go to the documentation of this file.
1 /****************************************************************************/
9 // A detector which joins E2Collectors over consecutive lanes (backward)
10 /****************************************************************************/
11 // SUMO, Simulation of Urban MObility; see http://sumo.dlr.de/
12 // Copyright (C) 2003-2015 DLR (http://www.dlr.de/) and contributors
13 /****************************************************************************/
14 //
15 // This file is part of SUMO.
16 // SUMO is free software: you can redistribute it and/or modify
17 // it under the terms of the GNU General Public License as published by
18 // the Free Software Foundation, either version 3 of the License, or
19 // (at your option) any later version.
20 //
21 /****************************************************************************/
22 
23 
24 // ===========================================================================
25 // included modules
26 // ===========================================================================
27 #ifdef _MSC_VER
28 #include <windows_config.h>
29 #else
30 #include <config.h>
31 #endif
32 
33 #include <cassert>
34 #include <microsim/MSEdge.h>
35 #include <microsim/MSLane.h>
37 #include "MSE2Collector.h"
38 
39 #ifdef CHECK_MEMORY_LEAKS
40 #include <foreign/nvwa/debug_new.h>
41 #endif // CHECK_MEMORY_LEAKS
42 
43 
44 // ===========================================================================
45 // method definitions
46 // ===========================================================================
48  DetectorUsage usage,
49  MSLane* lane,
50  SUMOReal startPos,
51  SUMOTime haltingTimeThreshold,
52  SUMOReal haltingSpeedThreshold,
53  SUMOReal jamDistThreshold)
55  startPosM(startPos), haltingTimeThresholdM(haltingTimeThreshold),
56  haltingSpeedThresholdM(haltingSpeedThreshold), jamDistThresholdM(jamDistThreshold),
57  myStartLaneID(lane->getID()), myUsage(usage) {}
58 
59 
60 void
62  myLength = detLength;
63  if (startPosM == 0) {
64  startPosM = (SUMOReal) 0.1;
65  }
66  SUMOReal length = lane->getLength() - startPosM - (SUMOReal) 0.1;
67  SUMOReal dlength = detLength;
68  if (length > dlength) {
69  length = dlength;
70  }
71  myLengths.push_back(length);
72  myLaneCombinations.push_back(LaneVector());
73  myLaneCombinations[0].push_back(lane);
75  MSE2Collector* c =
76  buildCollector(0, 0, lane, startPosM, length);
77  myDetectorCombinations[0].push_back(c);
78  myAlreadyBuild[lane] = c;
79  extendTo(detLength);
80 }
81 
82 
84 
85 
86 void
88  bool done = false;
89  while (!done) {
90  done = true;
91  LengthVector::iterator leni = myLengths.begin();
92  LaneVectorVector::iterator lanei = myLaneCombinations.begin();
93  DetectorVectorVector::iterator deti = myDetectorCombinations.begin();
94  for (; leni != myLengths.end(); leni++, lanei++, deti++) {
95  if ((*leni) < length) {
96  done = false;
97  // copy current values
98  LaneVector lv = *lanei;
99  DetectorVector dv = *deti;
100  SUMOReal clength = *leni;
101  assert(lv.size() > 0);
102  assert(dv.size() > 0);
103  // erase previous elements
104  assert(leni != myLengths.end());
105  myLengths.erase(leni);
106  myLaneCombinations.erase(lanei);
107  myDetectorCombinations.erase(deti);
108  // get the lane to look before
109  MSLane* toExtend = lv.back();
110  // and her predecessors
111  std::vector<MSLane*> predeccessors = getLanePredeccessorLanes(toExtend);
112  if (predeccessors.size() == 0) {
113  int off = 1;
114  MSEdge& e = toExtend->getEdge();
115  const std::vector<MSLane*>& lanes = e.getLanes();
116  int idx = (int) distance(lanes.begin(), find(lanes.begin(), lanes.end(), toExtend));
117  while (predeccessors.size() == 0) {
118  if (idx - off >= 0) {
119  MSLane* tryMe = lanes[idx - off];
120  predeccessors = getLanePredeccessorLanes(tryMe);
121  }
122  if (predeccessors.size() == 0 && idx + off < (int) lanes.size()) {
123  MSLane* tryMe = lanes[idx + off];
124  predeccessors = getLanePredeccessorLanes(tryMe);
125  }
126  off++;
127  }
128  }
129 
130  /* LaneContinuations::const_iterator conts =
131  laneContinuations.find(toExtend->id());
132  assert(conts!=laneContinuations.end());
133  const std::vector<std::string> &predeccessors =
134  (*conts).second;*/
135  // go through the predeccessors and extend the detector
136  for (std::vector<MSLane*>::const_iterator i = predeccessors.begin(); i != predeccessors.end(); i++) {
137  // get the lane
138  MSLane* l = *i;
139  // compute detector length
140  SUMOReal lanelen = length - clength;
141  if (lanelen > l->getLength()) {
142  lanelen = l->getLength() - (SUMOReal) 0.2;
143  }
144  // build new info
145  LaneVector nlv = lv;
146  nlv.push_back(l);
147  DetectorVector ndv = dv;
148  MSE2Collector* coll = 0;
149  if (myAlreadyBuild.find(l) == myAlreadyBuild.end()) {
150  coll = buildCollector(0, 0, l, (SUMOReal) 0.1, lanelen);
151  } else {
152  coll = myAlreadyBuild.find(l)->second;
153  }
154  myAlreadyBuild[l] = coll;
155  ndv.push_back(coll);
156  // store new info
157  myLaneCombinations.push_back(nlv);
158  myDetectorCombinations.push_back(ndv);
159  myLengths.push_back(clength + lanelen);
160  }
161  // restart
162  leni = myLengths.end() - 1;
163  }
164  }
165  }
166 }
167 
168 
169 std::vector<MSLane*>
171  std::string eid = l->getEdge().getID();
172  // get predecessing edges
173  const MSEdgeVector& predEdges = l->getEdge().getIncomingEdges();
174  std::vector<MSLane*> ret;
175  // find predecessing lanes
176  MSEdgeVector::const_iterator i = predEdges.begin();
177  for (; i != predEdges.end(); ++i) {
178  MSEdge* e = *i;
179  assert(e != 0);
180  typedef std::vector<MSLane*> LaneVector;
181  const LaneVector* cl = e->allowedLanes(l->getEdge(), SVC_IGNORING);
182  bool fastAbort = false;
183  if (cl != 0) {
184  for (LaneVector::const_iterator j = cl->begin(); !fastAbort && j != cl->end(); j++) {
185  const MSLinkCont& lc = (*j)->getLinkCont();
186  for (MSLinkCont::const_iterator k = lc.begin(); !fastAbort && k != lc.end(); k++) {
187  if ((*k)->getLane() == l) {
188  ret.push_back(*j);
189  fastAbort = true;
190  }
191  }
192  }
193  }
194  }
195  return ret;
196 }
197 
198 
201  SUMOReal start, SUMOReal end) {
202  std::string id = makeID(l->getID(), c, r);
203  if (start + end < l->getLength()) {
204  start = l->getLength() - end - (SUMOReal) 0.1;
205  }
206  return new MSE2Collector(id, myUsage,
207  l, start, end, haltingTimeThresholdM,
209 }
210 
211 
212 void
214  SUMOTime /*startTime*/, SUMOTime /*stopTime*/) {
215  /*
216  dev<<"<interval begin=\""<<time2string(startTime)<<"\" end=\""<<
217  time2string(stopTime)<<"\" "<<"id=\""<<myID<<"\" ";
218  if (hasDetector(E2::QUEUE_LENGTH_AHEAD_OF_TRAFFIC_LIGHTS_IN_VEHICLES)) {
219  dev<<"collQueueLengthAheadOfTrafficLightsInVehiclesMax=\"";
220  dev<<toString(getCurrent(E2::QUEUE_LENGTH_AHEAD_OF_TRAFFIC_LIGHTS_IN_VEHICLES));
221  dev<<"\" ";
222  resetQueueLengthAheadOfTrafficLights();
223  }
224  myDetectorCombinations[0][0]->writeXMLOutput(dev, startTime, stopTime);
225  dev<<"/>\n";
226  */
227 }
228 
229 
230 void
232  dev.writeXMLHeader("detector");
233 }
234 
235 
236 size_t bla = 0;
237 
238 std::string
239 MS_E2_ZS_CollectorOverLanes::makeID(const std::string& baseID ,
240  size_t /*col*/, size_t /*row*/) const {
241  std::string add;
242  switch (myUsage) {
243  case DU_USER_DEFINED:
244  add = "(u)";
245  break;
246  case DU_SUMO_INTERNAL:
247  add = "(i)";
248  break;
249  case DU_TL_CONTROL:
250  add = "(c)";
251  break;
252  default:
253  break;
254  }
255  std::string ret = baseID + add + toString<size_t>(bla++);
256  return ret;
257 }
258 
259 
260 const std::string&
262  return myStartLaneID;
263 }
264 
265 
266 /****************************************************************************/
267 
SUMOReal haltingSpeedThresholdM
Describes how slow a vehicle must be before being assigned to a jam.
MSEdge & getEdge() const
Returns the lane's edge.
Definition: MSLane.h:455
std::vector< MSE2Collector * > DetectorVector
Definition of a detector storage.
const std::string & getStartLaneID() const
Returns the id of the lane this detector starts at.
An areal (along a single lane) detector.
Definition: MSE2Collector.h:79
const std::vector< MSLane * > & getLanes() const
Returns this edge's lanes.
Definition: MSEdge.h:186
SUMOTime haltingTimeThresholdM
Describes how long a vehicle shall stay before being assigned to a jam.
SUMOReal getLength() const
Returns the lane's length.
Definition: MSLane.h:370
LengthVector myLengths
Storage for length combinations.
std::vector< MSLane * > getLanePredeccessorLanes(MSLane *l)
Returns the list of lanes predecessing the given one.
const MSEdgeVector & getIncomingEdges() const
Returns the list of edges from which this edge may be reached.
Definition: MSEdge.h:300
std::vector< MSLane * > LaneVector
Definition of a lane storage.
std::string makeID(const std::string &baseID, size_t c, size_t r) const
Builds an id for one of the E2 collectors this detector uses.
bool writeXMLHeader(const std::string &rootElement, const std::string &attrs="", const std::string &comment="")
Writes an XML header with optional configuration.
DetectorUsage myUsage
Information about how this detector is used.
virtual MSE2Collector * buildCollector(size_t c, size_t r, MSLane *l, SUMOReal start, SUMOReal end)
Builds a single collector.
const std::string & getID() const
Returns the id.
Definition: Named.h:60
A road/street connecting two junctions.
Definition: MSEdge.h:81
SUMOReal jamDistThresholdM
Describes how long a jam must be before being recognized.
std::string myStartLaneID
The id of the lane this detector starts at.
MS_E2_ZS_CollectorOverLanes(const std::string &id, DetectorUsage usage, MSLane *lane, SUMOReal startPos, SUMOTime haltingTimeThreshold, SUMOReal haltingSpeedThreshold, SUMOReal jamDistThreshold)
Constructor.
SUMOReal myLength
The length of the collector.
virtual ~MS_E2_ZS_CollectorOverLanes()
Destructor.
SUMOReal startPosM
The position the collector starts at.
DetectorVectorVector myDetectorCombinations
Storage for detector combinations.
void extendTo(SUMOReal length)
This method extends the current length up to the given.
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:217
void init(MSLane *lane, SUMOReal detLength)
Builds the consecutive E2 detectors.
void writeXMLOutput(OutputDevice &dev, SUMOTime startTime, SUMOTime stopTime)
Writes collected values into the given stream.
int SUMOTime
Definition: SUMOTime.h:43
LaneDetMap myAlreadyBuild
Storage for detectors which already have been build for a single lane.
Static storage of an output device and its base (abstract) implementation.
Definition: OutputDevice.h:71
#define SUMOReal
Definition: config.h:218
std::vector< MSEdge * > MSEdgeVector
Definition: MSEdge.h:78
LaneVectorVector myLaneCombinations
Storage for lane combinations.
void writeXMLDetectorProlog(OutputDevice &dev) const
Opens the XML-output using "detector" as root element.
Representation of a lane in the micro simulation.
Definition: MSLane.h:77
vehicles ignoring classes
Base of value-generating classes (detectors)
SUMOReal getLength() const
Returns this detector's length [m].