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-2014 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
81  assert(myLanes.size() > 0);
82  // change values for setting the loops and lanestate-detectors, here
83  //SUMOTime inductLoopInterval = 1; //
84  LaneVectorVector::const_iterator i2;
85  LaneVector::const_iterator i;
86  // build the induct loops
87  for (i2 = myLanes.begin(); i2 != myLanes.end(); ++i2) {
88  const LaneVector& lanes = *i2;
89  for (i = lanes.begin(); i != lanes.end(); i++) {
90  MSLane* lane = (*i);
91  SUMOReal length = lane->getLength();
92  SUMOReal speed = lane->getSpeedLimit();
93  SUMOReal inductLoopPosition = myDetectorGap * speed;
94  // check whether the lane is long enough
95  SUMOReal ilpos = length - inductLoopPosition;
96  if (ilpos < 0) {
97  ilpos = 0;
98  }
99  // Build the induct loop and set it into the container
100  std::string id = "TLS" + myID + "_" + myProgramID + "_InductLoopOn_" + lane->getID();
101  if (myInductLoops.find(lane) == myInductLoops.end()) {
102  myInductLoops[lane] = dynamic_cast<MSInductLoop*>(nb.createInductLoop(id, lane, ilpos, false));
103  assert(myInductLoops[lane] != 0);
104  }
105  }
106  }
107 }
108 
109 
111  for (InductLoopMap::iterator i = myInductLoops.begin(); i != myInductLoops.end(); ++i) {
112  delete(*i).second;
113  }
114 }
115 
116 
117 // ------------ Switching and setting current rows
118 SUMOTime
120  // checks if the actual phase should be continued
121  gapControl();
122  if (myContinue) {
123  return duration();
124  }
125  // increment the index to the current phase
126  myStep++;
127  assert(myStep <= myPhases.size());
128  if (myStep == myPhases.size()) {
129  myStep = 0;
130  }
131  //stores the time the phase started
133  // set the next event
135 }
136 
137 
138 // ------------ "actuated" algorithm methods
139 SUMOTime
141  assert(myContinue);
142  assert(getCurrentPhaseDef().isGreenPhase());
143  assert(myPhases.size() > myStep);
144  // define the duration depending from the number of waiting vehicles of the actual phase
145  int newduration = (int) getCurrentPhaseDef().minDuration;
146  const std::string& state = getCurrentPhaseDef().getState();
147  for (unsigned int i = 0; i < (unsigned int) state.size(); i++) {
148  if (state[i] == LINKSTATE_TL_GREEN_MAJOR || state[i] == LINKSTATE_TL_GREEN_MINOR) {
149  const std::vector<MSLane*>& lanes = getLanesAt(i);
150  if (lanes.empty()) {
151  break;
152  }
153  for (LaneVector::const_iterator j = lanes.begin(); j != lanes.end(); j++) {
154  InductLoopMap::const_iterator k = myInductLoops.find(*j);
155  assert(k != myInductLoops.end());
156  SUMOReal waiting = (SUMOReal)(*k).second->getCurrentPassedNumber();
157  SUMOReal tmpdur = myPassingTime * waiting;
158  if (tmpdur > newduration) {
159  // here we cut the decimal places, because we have to return an integer
160  newduration = (int) tmpdur;
161  }
162  if (newduration > (int) getCurrentPhaseDef().maxDuration) {
164  }
165  }
166  }
167  }
168  return newduration;
169 }
170 
171 
172 void
174  //intergreen times should not be lenghtend
175  assert(myPhases.size() > myStep);
176  if (!getCurrentPhaseDef().isGreenPhase()) {
177  myContinue = false;
178  return;
179  }
180 
181  // Checks, if the maxDuration is kept. No phase should longer send than maxDuration.
182  SUMOTime actDuration = MSNet::getInstance()->getCurrentTimeStep() - myPhases[myStep]->myLastSwitch;
183  if (actDuration >= getCurrentPhaseDef().maxDuration) {
184  myContinue = false;
185  return;
186  }
187 
188  // now the gapcontrol starts
189  const std::string& state = getCurrentPhaseDef().getState();
190  for (unsigned int i = 0; i < (unsigned int) state.size(); i++) {
191  if (state[i] == LINKSTATE_TL_GREEN_MAJOR || state[i] == LINKSTATE_TL_GREEN_MINOR) {
192  const std::vector<MSLane*>& lanes = getLanesAt(i);
193  if (lanes.empty()) {
194  break;
195  }
196  for (LaneVector::const_iterator j = lanes.begin(); j != lanes.end(); j++) {
197  if (myInductLoops.find(*j) == myInductLoops.end()) {
198  continue;
199  }
200  SUMOReal actualGap =
201  myInductLoops.find(*j)->second->getTimestepsSinceLastDetection();
202  if (actualGap < myMaxGap) {
203  myContinue = true;
204  return;
205  }
206  }
207  }
208  }
209  myContinue = false;
210 }
211 
212 
213 
214 /****************************************************************************/
215