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