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 
The link has green light, may pass.
virtual MSDetectorFileOutput * createInductLoop(const std::string &id, MSLane *lane, SUMOReal pos, bool splitByType)
Creates an instance of an e1 detector using the given values.
Builds detectors for microsim.
const std::string & getState() const
Returns the state within this phase.
unsigned int myStep
The current step.
std::string myProgramID
The id of the logic.
static SUMOReal _2SUMOReal(const E *const data)
Definition: TplConvert.h:223
The link has green light, has to brake.
SUMOReal getLength() const
Returns the lane's length.
Definition: MSLane.h:370
void init(NLDetectorBuilder &nb)
Initialises the tls with information about incoming lanes.
Phases myPhases
The list of phases this logic uses.
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition: MSNet.cpp:154
#define DEFAULT_DETECTOR_GAP
const std::string & getParameter(const std::string &key, const std::string &defaultValue) const
Returns the value for a given key.
SUMOTime getCurrentTimeStep() const
Returns the current simulation step.
Definition: MSNet.h:210
A fixed traffic light logic.
LaneVectorVector myLanes
The list of links which do participate in this traffic light.
A class that stores and controls tls and switching of their programs.
SUMOTime trySwitch(bool isActive)
Switches to the next phase.
const std::string & getID() const
Returns the id.
Definition: Named.h:60
SUMOReal myMaxGap
The maximum gap to check.
SUMOTime duration() const
Returns the duration of the given step.
SUMOReal myPassingTime
The passing time used.
virtual void init(NLDetectorBuilder &nb)
Initialises the tls with information about incoming lanes.
#define DEFAULT_PASSING_TIME
const LaneVector & getLanesAt(unsigned int i) const
Returns the list of lanes that are controlled by the signals at the given position.
const MSPhaseDefinition & getCurrentPhaseDef() const
Returns the definition of the current phase.
SUMOReal getSpeedLimit() const
Returns the lane's maximum allowed speed.
Definition: MSLane.h:362
bool isGreenPhase() const
Returns whether this phase is a pure "green" phase.
InductLoopMap myInductLoops
A map from lanes to induct loops lying on them.
std::vector< MSPhaseDefinition * > Phases
Definition of a list of phases, being the junction logic.
SUMOReal myDetectorGap
The detector distance.
std::string myID
The name of the object.
Definition: Named.h:128
std::vector< MSLane * > LaneVector
Definition of the list of links that participate in this tl-light.
SUMOTime maxDuration
The maximum duration of the phase.
bool myContinue
information whether the current phase should be lenghtend
int SUMOTime
Definition: SUMOTime.h:43
SUMOTime minDuration
The minimum duration of the phase.
MSActuatedTrafficLightLogic(MSTLLogicControl &tlcontrol, const std::string &id, const std::string &programID, const MSSimpleTrafficLightLogic::Phases &phases, unsigned int step, SUMOTime delay, const std::map< std::string, std::string > &parameter)
Constructor.
#define SUMOReal
Definition: config.h:215
void gapControl()
Decides, whether a phase should be continued by checking the gaps of vehicles having green...
Representation of a lane in the micro simulation.
Definition: MSLane.h:77
#define DEFAULT_MAX_GAP
An unextended detector measuring at a fixed position on a fixed lane.
Definition: MSInductLoop.h:70