SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
MSUnboundActuatedTrafficLightLogic.cpp
Go to the documentation of this file.
1 /****************************************************************************/
7 // The basic traffic light logic
8 /****************************************************************************/
9 // SUMO, Simulation of Urban MObility; see http://sumo.sourceforge.net/
10 // Copyright (C) 2001-2012 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 <utility>
32 #include <vector>
33 #include <bitset>
36 #include <microsim/MSLaneState.h>
37 #include <microsim/MSNet.h>
38 #include "MSTrafficLightLogic.h"
40 #include <microsim/MSLane.h>
42 
43 #ifdef CHECK_MEMORY_LEAKS
44 #include <foreign/nvwa/debug_new.h>
45 #endif // CHECK_MEMORY_LEAKS
46 
47 
48 // ===========================================================================
49 // method definitions
50 // ===========================================================================
52  const std::string& id,
53  const Phases& phases,
54  size_t step, size_t delay,
55  SUMOReal maxGap, SUMOReal passingTime, SUMOReal detectorGap)
56  : MSSimpleTrafficLightLogic(id, phases, step, delay),
57  myContinue(false),
58  myMaxGap(maxGap), myPassingTime(passingTime), myDetectorGap(detectorGap) {}
59 
60 
61 void
63  const std::vector<MSLane*>& lanes,
64  std::map<std::string, std::vector<std::string> >& laneContinuations,
65  SUMOReal det_offset) {
66  // change values for setting the loops and lanestate-detectors, here
67  SUMOTime inductLoopInterval = 1; //
68  // as the laneStateDetector shall end at the end of the lane, the position
69  // is calculated, not given
70  SUMOTime laneStateDetectorInterval = 1; //
71 
72  std::vector<MSLane*>::const_iterator i;
73  // build the induct loops
74  for (i = lanes.begin(); i != lanes.end(); i++) {
75  MSLane* lane = (*i);
76  SUMOReal length = lane->length();
77  SUMOReal speed = lane->maxSpeed();
78  SUMOReal inductLoopPosition = myDetectorGap * speed;
79  // check whether the lane is long enough
80  SUMOReal ilpos = length - inductLoopPosition;
81  if (ilpos < 0) {
82  ilpos = 0;
83  }
84  // Build the induct loop and set it into the container
85  std::string id = "TLS" + myId + "_InductLoopOn_" + lane->getID();
86  if (myInductLoops.find(lane) == myInductLoops.end()) {
87  myInductLoops[lane] =
88  nb.createInductLoop(id, lane, ilpos, inductLoopInterval);
89  }
90  }
91  // build the lane state-detectors
92  for (i = lanes.begin(); i != lanes.end(); i++) {
93  MSLane* lane = (*i);
94  SUMOReal length = lane->length();
95  // check whether the position is o.k. (not longer than the lane)
96  SUMOReal lslen = det_offset;
97  if (lslen > length) {
98  lslen = length;
99  }
100  SUMOReal lspos = length - lslen;
101  // Build the lane state detetcor and set it into the container
102  std::string id = "TLS" + myId + "_LaneStateOff_" + lane->getID();
103  if (myLaneStates.find(lane) == myLaneStates.end()) {
104  MSLaneState* loop =
105  new MSLaneState(id, lane, lspos, lslen,
106  laneStateDetectorInterval);
107  myLaneStates[lane] = loop;
108  }
109  }
110 }
111 
112 
113 
115 
116 
117 SUMOTime
119  if (myContinue) {
120  return 1;
121  }
122  assert(myPhases.size() > myStep);
123  if (!isGreenPhase()) {
124  return currentPhaseDef()->duration;
125  }
126  // define the duration depending from the number of waiting vehicles of the actual phase
127  int newduration = currentPhaseDef()->minDuration;
128  const std::bitset<64>& isgreen = currentPhaseDef()->getDriveMask();
129  for (size_t i = 0; i < isgreen.size(); i++) {
130  if (isgreen.test(i)) {
131  const std::vector<MSLane*>& lanes = getLanesAt(i);
132  if (lanes.empty()) {
133  break;
134  }
135  for (LaneVector::const_iterator j = lanes.begin(); j != lanes.end(); j++) {
136  LaneStateMap::const_iterator k = myLaneStates.find(*j);
137  SUMOReal waiting = (*k).second->getCurrentNumberOfWaiting();
138  SUMOReal tmpdur = myPassingTime * waiting;
139  if (tmpdur > newduration) {
140  // here we cut the decimal places, because we have to return an integer
141  newduration = (int) tmpdur;
142  }
143  if (newduration > (int) currentPhaseDef()->maxDuration) {
144  return currentPhaseDef()->maxDuration;
145  }
146  }
147  }
148  }
149  return newduration;
150 }
151 
152 
153 SUMOTime
155  // checks if the actual phase should be continued
156  gapControl();
157  if (myContinue) {
158  return duration();
159  }
160  // increment the index to the current phase
161  myStep++;
162  assert(myStep <= myPhases.size());
163  if (myStep == myPhases.size()) {
164  myStep = 0;
165  }
166  //stores the time the phase started
167  static_cast<MSActuatedPhaseDefinition*>(myPhases[myStep])->myLastSwitch =
169  // set the next event
170  return duration();
171 }
172 
173 
174 bool
176  if (currentPhaseDef()->getDriveMask().none()) {
177  return false;
178  }
179  if (currentPhaseDef()->getYellowMask().any()) {
180  return false;
181  }
182  return true;
183 }
184 
185 
186 bool
188  //intergreen times should not be lenghtend
189  assert(myPhases.size() > myStep);
190  if (!isGreenPhase()) {
191  return myContinue = false;
192  }
193 
194  // Checks, if the maxDuration is kept. No phase should longer send than maxDuration.
195  SUMOTime actDuration =
196  MSNet::getInstance()->getCurrentTimeStep() - static_cast<MSActuatedPhaseDefinition*>(myPhases[myStep])->myLastSwitch;
197  if (actDuration >= currentPhaseDef()->maxDuration) {
198  return myContinue = false;
199  }
200 
201  // now the gapcontrol starts
202  const std::bitset<64>& isgreen = currentPhaseDef()->getDriveMask();
203  for (size_t i = 0; i < isgreen.size(); i++) {
204  if (isgreen.test(i)) {
205  const std::vector<MSLane*>& lanes = getLanesAt(i);
206  if (lanes.empty()) {
207  break;
208  }
209  for (LaneVector::const_iterator j = lanes.begin(); j != lanes.end(); j++) {
210  if (myInductLoops.find(*j) == myInductLoops.end()) {
211  continue;
212  }
213  SUMOReal actualGap =
214  myInductLoops.find(*j)->second->getTimestepsSinceLastDetection();
215  if (actualGap < myMaxGap) {
216  return myContinue = true;
217  }
218  }
219  }
220  }
221  return myContinue = false;
222 }
223 
224 
225 MSActuatedPhaseDefinition*
227  assert(myPhases.size() > myStep);
228  return static_cast<MSActuatedPhaseDefinition*>(myPhases[myStep]);
229 }
230 
231 
232 
233 /****************************************************************************/
234