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.sourceforge.net/
11 // Copyright (C) 2001-2012 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 <microsim/MSEdge.h>
33 #include <microsim/MSLane.h>
35 #include "MSE2Collector.h"
36 
37 #ifdef CHECK_MEMORY_LEAKS
38 #include <foreign/nvwa/debug_new.h>
39 #endif // CHECK_MEMORY_LEAKS
40 
41 
42 // ===========================================================================
43 // method definitions
44 // ===========================================================================
46  DetectorUsage usage,
47  MSLane* lane,
48  SUMOReal startPos,
49  SUMOTime haltingTimeThreshold,
50  SUMOReal haltingSpeedThreshold,
51  SUMOReal jamDistThreshold)
53  startPosM(startPos), haltingTimeThresholdM(haltingTimeThreshold),
54  haltingSpeedThresholdM(haltingSpeedThreshold), jamDistThresholdM(jamDistThreshold),
55  myID(id), myStartLaneID(lane->getID()), myUsage(usage) {}
56 
57 
58 void
60  myLength = detLength;
61  if (startPosM == 0) {
62  startPosM = (SUMOReal) 0.1;
63  }
64  SUMOReal length = lane->getLength() - startPosM - (SUMOReal) 0.1;
65  SUMOReal dlength = detLength;
66  if (length > dlength) {
67  length = dlength;
68  }
69  myLengths.push_back(length);
70  myLaneCombinations.push_back(LaneVector());
71  myLaneCombinations[0].push_back(lane);
73  MSE2Collector* c =
74  buildCollector(0, 0, lane, startPosM, length);
75  myDetectorCombinations[0].push_back(c);
76  myAlreadyBuild[lane] = c;
77  extendTo(detLength);
78 }
79 
80 
82 
83 
84 void
86  bool done = false;
87  while (!done) {
88  done = true;
89  LengthVector::iterator leni = myLengths.begin();
90  LaneVectorVector::iterator lanei = myLaneCombinations.begin();
91  DetectorVectorVector::iterator deti = myDetectorCombinations.begin();
92  for (; leni != myLengths.end(); leni++, lanei++, deti++) {
93  if ((*leni) < length) {
94  done = false;
95  // copy current values
96  LaneVector lv = *lanei;
97  DetectorVector dv = *deti;
98  SUMOReal clength = *leni;
99  assert(lv.size() > 0);
100  assert(dv.size() > 0);
101  // erase previous elements
102  assert(leni != myLengths.end());
103  myLengths.erase(leni);
104  myLaneCombinations.erase(lanei);
105  myDetectorCombinations.erase(deti);
106  // get the lane to look before
107  MSLane* toExtend = lv.back();
108  // and her predecessors
109  std::vector<MSLane*> predeccessors = getLanePredeccessorLanes(toExtend);
110  if (predeccessors.size() == 0) {
111  int off = 1;
112  MSEdge& e = toExtend->getEdge();
113  const std::vector<MSLane*> &lanes = e.getLanes();
114  int idx = (int) distance(lanes.begin(), find(lanes.begin(), lanes.end(), toExtend));
115  while (predeccessors.size() == 0) {
116  if (idx - off >= 0) {
117  MSLane* tryMe = lanes[idx - off];
118  predeccessors = getLanePredeccessorLanes(tryMe);
119  }
120  if (predeccessors.size() == 0 && idx + off < (int) lanes.size()) {
121  MSLane* tryMe = lanes[idx + off];
122  predeccessors = getLanePredeccessorLanes(tryMe);
123  }
124  off++;
125  }
126  }
127 
128  /* LaneContinuations::const_iterator conts =
129  laneContinuations.find(toExtend->id());
130  assert(conts!=laneContinuations.end());
131  const std::vector<std::string> &predeccessors =
132  (*conts).second;*/
133  // go through the predeccessors and extend the detector
134  for (std::vector<MSLane*>::const_iterator i = predeccessors.begin(); i != predeccessors.end(); i++) {
135  // get the lane
136  MSLane* l = *i;
137  // compute detector length
138  SUMOReal lanelen = length - clength;
139  if (lanelen > l->getLength()) {
140  lanelen = l->getLength() - (SUMOReal) 0.2;
141  }
142  // build new info
143  LaneVector nlv = lv;
144  nlv.push_back(l);
145  DetectorVector ndv = dv;
146  MSE2Collector* coll = 0;
147  if (myAlreadyBuild.find(l) == myAlreadyBuild.end()) {
148  coll = buildCollector(0, 0, l, (SUMOReal) 0.1, lanelen);
149  } else {
150  coll = myAlreadyBuild.find(l)->second;
151  }
152  myAlreadyBuild[l] = coll;
153  ndv.push_back(coll);
154  // store new info
155  myLaneCombinations.push_back(nlv);
156  myDetectorCombinations.push_back(ndv);
157  myLengths.push_back(clength + lanelen);
158  }
159  // restart
160  leni = myLengths.end() - 1;
161  }
162  }
163  }
164 }
165 
166 
167 std::vector<MSLane*>
169  std::string eid = l->getEdge().getID();
170  // get predecessing edges
171  const std::vector<MSEdge*> &predEdges = l->getEdge().getIncomingEdges();
172  std::vector<MSLane*> ret;
173  // find predecessing lanes
174  std::vector<MSEdge*>::const_iterator i = predEdges.begin();
175  for (; i != predEdges.end(); ++i) {
176  MSEdge* e = *i;
177  assert(e != 0);
178  typedef std::vector<MSLane*> LaneVector;
179  const LaneVector* cl = e->allowedLanes(l->getEdge(), SVC_UNKNOWN);
180  bool fastAbort = false;
181  if (cl != 0) {
182  for (LaneVector::const_iterator j = cl->begin(); !fastAbort && j != cl->end(); j++) {
183  const MSLinkCont& lc = (*j)->getLinkCont();
184  for (MSLinkCont::const_iterator k = lc.begin(); !fastAbort && k != lc.end(); k++) {
185  if ((*k)->getLane() == l) {
186  ret.push_back(*j);
187  fastAbort = true;
188  }
189  }
190  }
191  }
192  }
193  return ret;
194 }
195 
196 
199  SUMOReal start, SUMOReal end) {
200  std::string id = makeID(l->getID(), c, r);
201  if (start + end < l->getLength()) {
202  start = l->getLength() - end - (SUMOReal) 0.1;
203  }
204  return new MSE2Collector(id, myUsage,
205  l, start, end, haltingTimeThresholdM,
207 }
208 
209 
210 void
212  SUMOTime /*startTime*/, SUMOTime /*stopTime*/) {
213  /*
214  dev<<"<interval begin=\""<<time2string(startTime)<<"\" end=\""<<
215  time2string(stopTime)<<"\" "<<"id=\""<<myID<<"\" ";
216  if (hasDetector(E2::QUEUE_LENGTH_AHEAD_OF_TRAFFIC_LIGHTS_IN_VEHICLES)) {
217  dev<<"collQueueLengthAheadOfTrafficLightsInVehiclesMax=\"";
218  dev<<toString(getCurrent(E2::QUEUE_LENGTH_AHEAD_OF_TRAFFIC_LIGHTS_IN_VEHICLES));
219  dev<<"\" ";
220  resetQueueLengthAheadOfTrafficLights();
221  }
222  myDetectorCombinations[0][0]->writeXMLOutput(dev, startTime, stopTime);
223  dev<<"/>\n";
224  */
225 }
226 
227 
228 void
230  dev.writeXMLHeader("detector");
231 }
232 
233 
234 size_t bla = 0;
235 
236 std::string
237 MS_E2_ZS_CollectorOverLanes::makeID(const std::string& baseID ,
238  size_t /*col*/, size_t /*row*/) const {
239  std::string add;
240  switch (myUsage) {
241  case DU_USER_DEFINED:
242  add = "(u)";
243  break;
244  case DU_SUMO_INTERNAL:
245  add = "(i)";
246  break;
247  case DU_TL_CONTROL:
248  add = "(c)";
249  break;
250  default:
251  break;
252  }
253  std::string ret = baseID + add + toString<size_t>(bla++);
254  return ret;
255 }
256 
257 
258 const std::string&
260  return myID;
261 }
262 
263 
264 const std::string&
266  return myStartLaneID;
267 }
268 
269 
270 /****************************************************************************/
271