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.dlr.de/
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 
74 }
75 
76 
77 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  // @note any vehicles which arrived during the previous phases which are now waiting between the detector and the stop line are not
121  // considere here. RiLSA recommends to set minDuration in a way that lets all vehicles pass the detector
122  // @todo: it would be nice to warn users if (inductLoopPosition / defaultLengthWithGap * myPassingTime > minDuration)
123  const SUMOReal detectionGap = gapControl();
124  if (detectionGap < std::numeric_limits<SUMOReal>::max()) {
125  return duration(detectionGap);
126  }
127  // increment the index to the current phase
128  myStep++;
129  assert(myStep <= myPhases.size());
130  if (myStep == myPhases.size()) {
131  myStep = 0;
132  }
133  //stores the time the phase started
135  // set the next event
137 }
138 
139 
140 // ------------ "actuated" algorithm methods
141 SUMOTime
143  assert(getCurrentPhaseDef().isGreenPhase());
144  assert(myPhases.size() > myStep);
145  const SUMOTime actDuration = MSNet::getInstance()->getCurrentTimeStep() - myPhases[myStep]->myLastSwitch;
146  // ensure that minimum duration is kept
147  SUMOTime newDuration = getCurrentPhaseDef().minDuration - actDuration;
148  // try to let the last detected vehicle pass the intersection (duration must be positive)
149  newDuration = MAX3(newDuration, TIME2STEPS(myDetectorGap - detectionGap), 1);
150  // cut the decimal places to ensure that phases always have integer duration
151  if (newDuration % 1000 != 0) {
152  const SUMOTime totalDur = newDuration + actDuration;
153  newDuration = (totalDur / 1000 + 1) * 1000 - actDuration;
154  }
155  // ensure that the maximum duration is not exceeded
156  newDuration = MIN2(newDuration, getCurrentPhaseDef().maxDuration - actDuration);
157  return newDuration;
158 }
159 
160 
161 SUMOReal
163  //intergreen times should not be lenghtend
164  assert(myPhases.size() > myStep);
166  if (!getCurrentPhaseDef().isGreenPhase()) {
167  return result; // end current phase
168  }
169 
170  // Checks, if the maxDuration is kept. No phase should longer send than maxDuration.
171  SUMOTime actDuration = MSNet::getInstance()->getCurrentTimeStep() - myPhases[myStep]->myLastSwitch;
172  if (actDuration >= getCurrentPhaseDef().maxDuration) {
173  return result; // end current phase
174  }
175 
176  // now the gapcontrol starts
177  const std::string& state = getCurrentPhaseDef().getState();
178  for (unsigned int i = 0; i < (unsigned int) state.size(); i++) {
179  if (state[i] == LINKSTATE_TL_GREEN_MAJOR || state[i] == LINKSTATE_TL_GREEN_MINOR) {
180  const std::vector<MSLane*>& lanes = getLanesAt(i);
181  for (LaneVector::const_iterator j = lanes.begin(); j != lanes.end(); j++) {
182  if (myInductLoops.find(*j) == myInductLoops.end()) {
183  continue;
184  }
185  SUMOReal actualGap =
186  myInductLoops.find(*j)->second->getTimestepsSinceLastDetection();
187  if (actualGap < myMaxGap) {
188  result = MIN2(result, actualGap);
189  }
190  }
191  }
192  }
193  return result;
194 }
195 
196 
197 
198 /****************************************************************************/
199 
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&#39;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:156
#define DEFAULT_DETECTOR_GAP
#define TIME2STEPS(x)
Definition: SUMOTime.h:66
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:230
T MAX3(T a, T b, T c)
Definition: StdDefs.h:88
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 in seconds.
#define max(a, b)
Definition: polyfonts.c:65
SUMOReal myPassingTime
The passing time used in seconds.
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.
T MIN2(T a, T b)
Definition: StdDefs.h:68
SUMOReal getSpeedLimit() const
Returns the lane&#39;s maximum allowed speed.
Definition: MSLane.h:362
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 in seconds.
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 duration(const SUMOReal detectionGap) const
Returns the minimum duration of the current phase.
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:218
Representation of a lane in the micro simulation.
Definition: MSLane.h:77
#define DEFAULT_MAX_GAP
SUMOReal gapControl()
Return the minimum detection gap of all detectors if the current phase should be extended and SUMORea...
An unextended detector measuring at a fixed position on a fixed lane.
Definition: MSInductLoop.h:71