SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
MSActuatedTrafficLightLogic.cpp
Go to the documentation of this file.
1 /****************************************************************************/
11 // An actuated (adaptive) traffic light logic
12 /****************************************************************************/
13 // SUMO, Simulation of Urban MObility; see http://sumo-sim.org/
14 // Copyright (C) 2001-2013 DLR (http://www.dlr.de/) and contributors
15 /****************************************************************************/
16 //
17 // This file is part of SUMO.
18 // SUMO is free software: you can redistribute it and/or modify
19 // it under the terms of the GNU General Public License as published by
20 // the Free Software Foundation, either version 3 of the License, or
21 // (at your option) any later version.
22 //
23 /****************************************************************************/
24 
25 
26 // ===========================================================================
27 // included modules
28 // ===========================================================================
29 #ifdef _MSC_VER
30 #include <windows_config.h>
31 #else
32 #include <config.h>
33 #endif
34 
35 #include <cassert>
36 #include <utility>
37 #include <vector>
38 #include <bitset>
41 #include <microsim/MSNet.h>
42 #include "MSTrafficLightLogic.h"
44 #include <microsim/MSLane.h>
47 
48 #ifdef CHECK_MEMORY_LEAKS
49 #include <foreign/nvwa/debug_new.h>
50 #endif // CHECK_MEMORY_LEAKS
51 
52 
53 // ===========================================================================
54 // parameter defaults definitions
55 // ===========================================================================
56 #define DEFAULT_MAX_GAP "3.1"
57 #define DEFAULT_PASSING_TIME "1.9"
58 #define DEFAULT_DETECTOR_GAP "3.0"
59 
60 
61 // ===========================================================================
62 // method definitions
63 // ===========================================================================
65  const std::string& id, const std::string& programID,
66  const Phases& phases,
67  unsigned int step, SUMOTime delay,
68  const std::map<std::string, std::string>& parameter) :
69  MSSimpleTrafficLightLogic(tlcontrol, id, programID, phases, step, delay, parameter),
70  myContinue(false) {
71 
75 }
76 
77 
78 void
80  assert(myLanes.size() > 0);
81  // change values for setting the loops and lanestate-detectors, here
82  //SUMOTime inductLoopInterval = 1; //
83  LaneVectorVector::const_iterator i2;
84  LaneVector::const_iterator i;
85  // build the induct loops
86  for (i2 = myLanes.begin(); i2 != myLanes.end(); ++i2) {
87  const LaneVector& lanes = *i2;
88  for (i = lanes.begin(); i != lanes.end(); i++) {
89  MSLane* lane = (*i);
90  SUMOReal length = lane->getLength();
91  SUMOReal speed = lane->getSpeedLimit();
92  SUMOReal inductLoopPosition = myDetectorGap * speed;
93  // check whether the lane is long enough
94  SUMOReal ilpos = length - inductLoopPosition;
95  if (ilpos < 0) {
96  ilpos = 0;
97  }
98  // Build the induct loop and set it into the container
99  std::string id = "TLS" + myID + "_" + myProgramID + "_InductLoopOn_" + lane->getID();
100  if (myInductLoops.find(lane) == myInductLoops.end()) {
101  myInductLoops[lane] = dynamic_cast<MSInductLoop*>(nb.createInductLoop(id, lane, ilpos, false));
102  assert(myInductLoops[lane] != 0);
103  }
104  }
105  }
106 }
107 
108 
110  for (InductLoopMap::iterator i = myInductLoops.begin(); i != myInductLoops.end(); ++i) {
111  delete(*i).second;
112  }
113 }
114 
115 
116 // ------------ Switching and setting current rows
117 SUMOTime
119  // checks if the actual phase should be continued
120  gapControl();
121  if (myContinue) {
122  return duration();
123  }
124  // increment the index to the current phase
125  myStep++;
126  assert(myStep <= myPhases.size());
127  if (myStep == myPhases.size()) {
128  myStep = 0;
129  }
130  //stores the time the phase started
132  // set the next event
134 }
135 
136 
137 // ------------ "actuated" algorithm methods
138 SUMOTime
140  assert(myContinue);
141  assert(getCurrentPhaseDef().isGreenPhase());
142  assert(myPhases.size() > myStep);
143  // define the duration depending from the number of waiting vehicles of the actual phase
144  int newduration = (int) getCurrentPhaseDef().minDuration;
145  const std::string& state = getCurrentPhaseDef().getState();
146  for (unsigned int i = 0; i < (unsigned int) state.size(); i++) {
147  if (state[i] == LINKSTATE_TL_GREEN_MAJOR || state[i] == LINKSTATE_TL_GREEN_MINOR) {
148  const std::vector<MSLane*>& lanes = getLanesAt(i);
149  if (lanes.empty()) {
150  break;
151  }
152  for (LaneVector::const_iterator j = lanes.begin(); j != lanes.end(); j++) {
153  InductLoopMap::const_iterator k = myInductLoops.find(*j);
154  assert(k != myInductLoops.end());
155  SUMOReal waiting = (SUMOReal)(*k).second->getCurrentPassedNumber();
156  SUMOReal tmpdur = myPassingTime * waiting;
157  if (tmpdur > newduration) {
158  // here we cut the decimal places, because we have to return an integer
159  newduration = (int) tmpdur;
160  }
161  if (newduration > (int) getCurrentPhaseDef().maxDuration) {
163  }
164  }
165  }
166  }
167  return newduration;
168 }
169 
170 
171 void
173  //intergreen times should not be lenghtend
174  assert(myPhases.size() > myStep);
175  if (!getCurrentPhaseDef().isGreenPhase()) {
176  myContinue = false;
177  return;
178  }
179 
180  // Checks, if the maxDuration is kept. No phase should longer send than maxDuration.
181  SUMOTime actDuration = MSNet::getInstance()->getCurrentTimeStep() - myPhases[myStep]->myLastSwitch;
182  if (actDuration >= getCurrentPhaseDef().maxDuration) {
183  myContinue = false;
184  return;
185  }
186 
187  // now the gapcontrol starts
188  const std::string& state = getCurrentPhaseDef().getState();
189  for (unsigned int i = 0; i < (unsigned int) state.size(); i++) {
190  if (state[i] == LINKSTATE_TL_GREEN_MAJOR || state[i] == LINKSTATE_TL_GREEN_MINOR) {
191  const std::vector<MSLane*>& lanes = getLanesAt(i);
192  if (lanes.empty()) {
193  break;
194  }
195  for (LaneVector::const_iterator j = lanes.begin(); j != lanes.end(); j++) {
196  if (myInductLoops.find(*j) == myInductLoops.end()) {
197  continue;
198  }
199  SUMOReal actualGap =
200  myInductLoops.find(*j)->second->getTimestepsSinceLastDetection();
201  if (actualGap < myMaxGap) {
202  myContinue = true;
203  return;
204  }
205  }
206  }
207  }
208  myContinue = false;
209 }
210 
211 
212 
213 /****************************************************************************/
214