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-2016 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,
54  const std::string& vTypes)
55  : MSDetectorFileOutput(id, vTypes),
56  startPosM(startPos), haltingTimeThresholdM(haltingTimeThreshold),
57  haltingSpeedThresholdM(haltingSpeedThreshold), jamDistThresholdM(jamDistThreshold),
58  myStartLaneID(lane->getID()), myUsage(usage) {}
59 
60 
61 void
63  myLength = detLength;
64  if (startPosM == 0) {
65  startPosM = (SUMOReal) 0.1;
66  }
67  SUMOReal length = lane->getLength() - startPosM - (SUMOReal) 0.1;
68  SUMOReal dlength = detLength;
69  if (length > dlength) {
70  length = dlength;
71  }
72  myLengths.push_back(length);
73  myLaneCombinations.push_back(LaneVector());
74  myLaneCombinations[0].push_back(lane);
76  MSE2Collector* c =
77  buildCollector(0, 0, lane, startPosM, length, myVehicleTypes);
78  myDetectorCombinations[0].push_back(c);
79  myAlreadyBuild[lane] = c;
80  extendTo(detLength);
81 }
82 
83 
85 
86 
87 void
89  bool done = false;
90  while (!done) {
91  done = true;
92  LengthVector::iterator leni = myLengths.begin();
93  LaneVectorVector::iterator lanei = myLaneCombinations.begin();
94  DetectorVectorVector::iterator deti = myDetectorCombinations.begin();
95  for (; leni != myLengths.end(); leni++, lanei++, deti++) {
96  if ((*leni) < length) {
97  done = false;
98  // copy current values
99  LaneVector lv = *lanei;
100  DetectorVector dv = *deti;
101  SUMOReal clength = *leni;
102  assert(lv.size() > 0);
103  assert(dv.size() > 0);
104  // erase previous elements
105  assert(leni != myLengths.end());
106  myLengths.erase(leni);
107  myLaneCombinations.erase(lanei);
108  myDetectorCombinations.erase(deti);
109  // get the lane to look before
110  MSLane* toExtend = lv.back();
111  // and her predecessors
112  std::vector<MSLane*> predeccessors = getLanePredeccessorLanes(toExtend);
113  if (predeccessors.size() == 0) {
114  int off = 1;
115  MSEdge& e = toExtend->getEdge();
116  const std::vector<MSLane*>& lanes = e.getLanes();
117  int idx = (int) distance(lanes.begin(), find(lanes.begin(), lanes.end(), toExtend));
118  while (predeccessors.size() == 0) {
119  if (idx - off >= 0) {
120  MSLane* tryMe = lanes[idx - off];
121  predeccessors = getLanePredeccessorLanes(tryMe);
122  }
123  if (predeccessors.size() == 0 && idx + off < (int) lanes.size()) {
124  MSLane* tryMe = lanes[idx + off];
125  predeccessors = getLanePredeccessorLanes(tryMe);
126  }
127  off++;
128  }
129  }
130 
131  /* LaneContinuations::const_iterator conts =
132  laneContinuations.find(toExtend->id());
133  assert(conts!=laneContinuations.end());
134  const std::vector<std::string> &predeccessors =
135  (*conts).second;*/
136  // go through the predeccessors and extend the detector
137  for (std::vector<MSLane*>::const_iterator i = predeccessors.begin(); i != predeccessors.end(); i++) {
138  // get the lane
139  MSLane* l = *i;
140  // compute detector length
141  SUMOReal lanelen = length - clength;
142  if (lanelen > l->getLength()) {
143  lanelen = l->getLength() - (SUMOReal) 0.2;
144  }
145  // build new info
146  LaneVector nlv = lv;
147  nlv.push_back(l);
148  DetectorVector ndv = dv;
149  MSE2Collector* coll = 0;
150  if (myAlreadyBuild.find(l) == myAlreadyBuild.end()) {
151  coll = buildCollector(0, 0, l, (SUMOReal) 0.1, lanelen, myVehicleTypes);
152  } else {
153  coll = myAlreadyBuild.find(l)->second;
154  }
155  myAlreadyBuild[l] = coll;
156  ndv.push_back(coll);
157  // store new info
158  myLaneCombinations.push_back(nlv);
159  myDetectorCombinations.push_back(ndv);
160  myLengths.push_back(clength + lanelen);
161  }
162  // restart
163  leni = myLengths.end() - 1;
164  }
165  }
166  }
167 }
168 
169 
170 std::vector<MSLane*>
172  std::string eid = l->getEdge().getID();
173  // get predecessing edges
174  const MSEdgeVector& predEdges = l->getEdge().getIncomingEdges();
175  std::vector<MSLane*> ret;
176  // find predecessing lanes
177  MSEdgeVector::const_iterator i = predEdges.begin();
178  for (; i != predEdges.end(); ++i) {
179  MSEdge* e = *i;
180  assert(e != 0);
181  typedef std::vector<MSLane*> LaneVector;
182  const LaneVector* cl = e->allowedLanes(l->getEdge(), SVC_IGNORING);
183  bool fastAbort = false;
184  if (cl != 0) {
185  for (LaneVector::const_iterator j = cl->begin(); !fastAbort && j != cl->end(); j++) {
186  const MSLinkCont& lc = (*j)->getLinkCont();
187  for (MSLinkCont::const_iterator k = lc.begin(); !fastAbort && k != lc.end(); k++) {
188  if ((*k)->getLane() == l) {
189  ret.push_back(*j);
190  fastAbort = true;
191  }
192  }
193  }
194  }
195  }
196  return ret;
197 }
198 
199 
202  SUMOReal start, SUMOReal end,
203  const std::set<std::string>& vTypes) {
204  std::string id = makeID(l->getID(), c, r);
205  if (start + end < l->getLength()) {
206  start = l->getLength() - end - (SUMOReal) 0.1;
207  }
208  return new MSE2Collector(id, myUsage,
209  l, start, end, haltingTimeThresholdM,
211 }
212 
213 
214 void
216  SUMOTime /*startTime*/, SUMOTime /*stopTime*/) {
217  /*
218  dev<<"<interval begin=\""<<time2string(startTime)<<"\" end=\""<<
219  time2string(stopTime)<<"\" "<<"id=\""<<myID<<"\" ";
220  if (hasDetector(E2::QUEUE_LENGTH_AHEAD_OF_TRAFFIC_LIGHTS_IN_VEHICLES)) {
221  dev<<"collQueueLengthAheadOfTrafficLightsInVehiclesMax=\"";
222  dev<<toString(getCurrent(E2::QUEUE_LENGTH_AHEAD_OF_TRAFFIC_LIGHTS_IN_VEHICLES));
223  dev<<"\" ";
224  resetQueueLengthAheadOfTrafficLights();
225  }
226  myDetectorCombinations[0][0]->writeXMLOutput(dev, startTime, stopTime);
227  dev<<"/>\n";
228  */
229 }
230 
231 
232 void
234  dev.writeXMLHeader("detector");
235 }
236 
237 
238 int bla = 0;
239 
240 std::string
241 MS_E2_ZS_CollectorOverLanes::makeID(const std::string& baseID ,
242  int /*col*/, int /*row*/) const {
243  std::string add;
244  switch (myUsage) {
245  case DU_USER_DEFINED:
246  add = "(u)";
247  break;
248  case DU_SUMO_INTERNAL:
249  add = "(i)";
250  break;
251  case DU_TL_CONTROL:
252  add = "(c)";
253  break;
254  default:
255  break;
256  }
257  std::string ret = baseID + add + toString<int>(bla++);
258  return ret;
259 }
260 
261 
262 const std::string&
264  return myStartLaneID;
265 }
266 
267 
268 /****************************************************************************/
269 
MS_E2_ZS_CollectorOverLanes(const std::string &id, DetectorUsage usage, MSLane *lane, SUMOReal startPos, SUMOTime haltingTimeThreshold, SUMOReal haltingSpeedThreshold, SUMOReal jamDistThreshold, const std::string &vTypes)
Constructor.
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:571
std::vector< MSE2Collector * > DetectorVector
Definition of a detector storage.
long long int SUMOTime
Definition: SUMOTime.h:43
const std::string & getStartLaneID() const
Returns the id of the lane this detector starts at.
virtual MSE2Collector * buildCollector(int c, int r, MSLane *l, SUMOReal start, SUMOReal end, const std::set< std::string > &vTypes)
Builds a single collector.
An areal (along a single lane) detector.
Definition: MSE2Collector.h:83
const std::vector< MSLane * > & getLanes() const
Returns this edge's lanes.
Definition: MSEdge.h:192
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:480
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:332
std::vector< MSLane * > LaneVector
Definition of a lane storage.
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.
const std::string & getID() const
Returns the id.
Definition: Named.h:66
A road/street connecting two junctions.
Definition: MSEdge.h:80
std::string makeID(const std::string &baseID, int c, int r) const
Builds an id for one of the E2 collectors this detector uses.
SUMOReal jamDistThresholdM
Describes how long a jam must be before being recognized.
std::string myStartLaneID
The id of the lane this detector starts at.
std::set< std::string > myVehicleTypes
The vehicle types to look for (empty means all)
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:300
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:71
#define SUMOReal
Definition: config.h:214
std::vector< MSEdge * > MSEdgeVector
Definition: MSEdge.h:77
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:79
vehicles ignoring classes
Base of value-generating classes (detectors)
SUMOReal getLength() const
Returns this detector's length [m].