SUMO - Simulation of Urban MObility
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-2015 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>
42 #include <microsim/MSNet.h>
43 #include "MSTrafficLightLogic.h"
45 #include <microsim/MSLane.h>
48 
49 #ifdef CHECK_MEMORY_LEAKS
50 #include <foreign/nvwa/debug_new.h>
51 #endif // CHECK_MEMORY_LEAKS
52 
53 
54 // ===========================================================================
55 // parameter defaults definitions
56 // ===========================================================================
57 #define DEFAULT_MAX_GAP "3.1"
58 #define DEFAULT_PASSING_TIME "1.9"
59 #define DEFAULT_DETECTOR_GAP "3.0"
60 
61 
62 // ===========================================================================
63 // method definitions
64 // ===========================================================================
66  const std::string& id, const std::string& programID,
67  const Phases& phases,
68  unsigned int step, SUMOTime delay,
69  const std::map<std::string, std::string>& parameter) :
70  MSSimpleTrafficLightLogic(tlcontrol, id, programID, phases, step, delay, parameter) {
71 
75  myShowDetectors = TplConvert::_2bool(getParameter("show-detectors", "false").c_str());
76 }
77 
78 
79 void
82  assert(myLanes.size() > 0);
83  // change values for setting the loops and lanestate-detectors, here
84  //SUMOTime inductLoopInterval = 1; //
85  LaneVectorVector::const_iterator i2;
86  LaneVector::const_iterator i;
87  // build the induct loops
88  for (i2 = myLanes.begin(); i2 != myLanes.end(); ++i2) {
89  const LaneVector& lanes = *i2;
90  for (i = lanes.begin(); i != lanes.end(); i++) {
91  MSLane* lane = (*i);
92  SUMOReal length = lane->getLength();
93  SUMOReal speed = lane->getSpeedLimit();
94  SUMOReal inductLoopPosition = myDetectorGap * speed;
95  // check whether the lane is long enough
96  SUMOReal ilpos = length - inductLoopPosition;
97  if (ilpos < 0) {
98  ilpos = 0;
99  }
100  // Build the induct loop and set it into the container
101  std::string id = "TLS" + myID + "_" + myProgramID + "_InductLoopOn_" + lane->getID();
102  if (myInductLoops.find(lane) == myInductLoops.end()) {
103  myInductLoops[lane] = dynamic_cast<MSInductLoop*>(nb.createInductLoop(id, lane, ilpos, false));
104  assert(myInductLoops[lane] != 0);
105  if (myShowDetectors) {
107  }
108  }
109  }
110  }
111 }
112 
113 
115  if (!myShowDetectors) {
116  for (InductLoopMap::iterator i = myInductLoops.begin(); i != myInductLoops.end(); ++i) {
117  delete(*i).second;
118  }
119  }
120 }
121 
122 
123 // ------------ Switching and setting current rows
124 SUMOTime
126  // checks if the actual phase should be continued
127  // @note any vehicles which arrived during the previous phases which are now waiting between the detector and the stop line are not
128  // considere here. RiLSA recommends to set minDuration in a way that lets all vehicles pass the detector
129  // @todo: it would be nice to warn users if (inductLoopPosition / defaultLengthWithGap * myPassingTime > minDuration)
130  const SUMOReal detectionGap = gapControl();
131  if (detectionGap < std::numeric_limits<SUMOReal>::max()) {
132  return duration(detectionGap);
133  }
134  // increment the index to the current phase
135  myStep++;
136  assert(myStep <= myPhases.size());
137  if (myStep == myPhases.size()) {
138  myStep = 0;
139  }
140  //stores the time the phase started
142  // set the next event
144 }
145 
146 
147 // ------------ "actuated" algorithm methods
148 SUMOTime
150  assert(getCurrentPhaseDef().isGreenPhase());
151  assert(myPhases.size() > myStep);
152  const SUMOTime actDuration = MSNet::getInstance()->getCurrentTimeStep() - myPhases[myStep]->myLastSwitch;
153  // ensure that minimum duration is kept
154  SUMOTime newDuration = getCurrentPhaseDef().minDuration - actDuration;
155  // try to let the last detected vehicle pass the intersection (duration must be positive)
156  newDuration = MAX3(newDuration, TIME2STEPS(myDetectorGap - detectionGap), 1);
157  // cut the decimal places to ensure that phases always have integer duration
158  if (newDuration % 1000 != 0) {
159  const SUMOTime totalDur = newDuration + actDuration;
160  newDuration = (totalDur / 1000 + 1) * 1000 - actDuration;
161  }
162  // ensure that the maximum duration is not exceeded
163  newDuration = MIN2(newDuration, getCurrentPhaseDef().maxDuration - actDuration);
164  return newDuration;
165 }
166 
167 
168 SUMOReal
170  //intergreen times should not be lenghtend
171  assert(myPhases.size() > myStep);
173  if (!getCurrentPhaseDef().isGreenPhase()) {
174  return result; // end current phase
175  }
176 
177  // Checks, if the maxDuration is kept. No phase should longer send than maxDuration.
178  SUMOTime actDuration = MSNet::getInstance()->getCurrentTimeStep() - myPhases[myStep]->myLastSwitch;
179  if (actDuration >= getCurrentPhaseDef().maxDuration) {
180  return result; // end current phase
181  }
182 
183  // now the gapcontrol starts
184  const std::string& state = getCurrentPhaseDef().getState();
185  for (unsigned int i = 0; i < (unsigned int) state.size(); i++) {
186  if (state[i] == LINKSTATE_TL_GREEN_MAJOR || state[i] == LINKSTATE_TL_GREEN_MINOR) {
187  const std::vector<MSLane*>& lanes = getLanesAt(i);
188  for (LaneVector::const_iterator j = lanes.begin(); j != lanes.end(); j++) {
189  if (myInductLoops.find(*j) == myInductLoops.end()) {
190  continue;
191  }
192  SUMOReal actualGap =
193  myInductLoops.find(*j)->second->getTimestepsSinceLastDetection();
194  if (actualGap < myMaxGap) {
195  result = MIN2(result, actualGap);
196  }
197  }
198  }
199  }
200  return result;
201 }
202 
203 
204 
205 /****************************************************************************/
206 
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
static bool _2bool(const E *const data)
Definition: TplConvert.h:282
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:159
#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:235
T MAX3(T a, T b, T c)
Definition: StdDefs.h:88
A fixed traffic light logic.
LaneVectorVector myLanes
The list of LaneVectors; each vector contains the incoming lanes that belong to the same link index...
A class that stores and controls tls and switching of their programs.
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's maximum allowed speed.
Definition: MSLane.h:362
MSDetectorControl & getDetectorControl()
Returns the detector control.
Definition: MSNet.h:339
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.
void add(SumoXMLTag type, MSDetectorFileOutput *d, const std::string &device, int splInterval, SUMOTime begin=-1)
Adds a detector/output combination into the containers.
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...
bool myShowDetectors
Whether the detectors shall be shown in the GUI.
SUMOTime trySwitch()
Switches to the next phase.
An unextended detector measuring at a fixed position on a fixed lane.
Definition: MSInductLoop.h:71