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-2016 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/MSGlobals.h>
43 #include <microsim/MSNet.h>
44 #include "MSTrafficLightLogic.h"
46 #include <microsim/MSLane.h>
49 
50 #ifdef CHECK_MEMORY_LEAKS
51 #include <foreign/nvwa/debug_new.h>
52 #endif // CHECK_MEMORY_LEAKS
53 
54 
55 // ===========================================================================
56 // parameter defaults definitions
57 // ===========================================================================
58 #define DEFAULT_MAX_GAP "3.1"
59 #define DEFAULT_PASSING_TIME "1.9"
60 #define DEFAULT_DETECTOR_GAP "3.0"
61 
62 
63 // ===========================================================================
64 // method definitions
65 // ===========================================================================
67  const std::string& id, const std::string& programID,
68  const Phases& phases,
69  int step, SUMOTime delay,
70  const std::map<std::string, std::string>& parameter,
71  const std::string& basePath) :
72  MSSimpleTrafficLightLogic(tlcontrol, id, programID, phases, step, delay, parameter) {
73 
77  myShowDetectors = TplConvert::_2bool(getParameter("show-detectors", "false").c_str());
78  myFile = FileHelpers::checkForRelativity(getParameter("file", "NUL"), basePath);
79  myFreq = TIME2STEPS(TplConvert::_2SUMOReal(getParameter("freq", "300").c_str()));
80  myVehicleTypes = getParameter("vTypes", "");
81 }
82 
83 
84 void
87  assert(myLanes.size() > 0);
88  // change values for setting the loops and lanestate-detectors, here
89  //SUMOTime inductLoopInterval = 1; //
90  LaneVectorVector::const_iterator i2;
91  LaneVector::const_iterator i;
92  // build the induct loops
93  for (i2 = myLanes.begin(); i2 != myLanes.end(); ++i2) {
94  const LaneVector& lanes = *i2;
95  for (i = lanes.begin(); i != lanes.end(); i++) {
96  MSLane* lane = (*i);
97  SUMOReal length = lane->getLength();
98  SUMOReal speed = lane->getSpeedLimit();
99  SUMOReal inductLoopPosition = myDetectorGap * speed;
100  // check whether the lane is long enough
101  SUMOReal ilpos = length - inductLoopPosition;
102  if (ilpos < 0) {
103  ilpos = 0;
104  }
105  // Build the induct loop and set it into the container
106  std::string id = "TLS" + myID + "_" + myProgramID + "_InductLoopOn_" + lane->getID();
107  if (myInductLoops.find(lane) == myInductLoops.end()) {
108  myInductLoops[lane] = nb.createInductLoop(id, lane, ilpos, myVehicleTypes);
110  }
111  }
112  }
113 }
114 
115 
117 
118 
119 // ------------ Switching and setting current rows
120 SUMOTime
122  // checks if the actual phase should be continued
123  // @note any vehicles which arrived during the previous phases which are now waiting between the detector and the stop line are not
124  // considere here. RiLSA recommends to set minDuration in a way that lets all vehicles pass the detector
125  // @todo: it would be nice to warn users if (inductLoopPosition / defaultLengthWithGap * myPassingTime > minDuration)
126  const SUMOReal detectionGap = gapControl();
127  if (detectionGap < std::numeric_limits<SUMOReal>::max()) {
128  return duration(detectionGap);
129  }
130  // increment the index to the current phase
131  myStep++;
132  assert(myStep <= (int)myPhases.size());
133  if (myStep == (int)myPhases.size()) {
134  myStep = 0;
135  }
136  //stores the time the phase started
138  // set the next event
140 }
141 
142 
143 // ------------ "actuated" algorithm methods
144 SUMOTime
146  assert(getCurrentPhaseDef().isGreenPhase());
147  assert((int)myPhases.size() > myStep);
148  const SUMOTime actDuration = MSNet::getInstance()->getCurrentTimeStep() - myPhases[myStep]->myLastSwitch;
149  // ensure that minimum duration is kept
150  SUMOTime newDuration = getCurrentPhaseDef().minDuration - actDuration;
151  // try to let the last detected vehicle pass the intersection (duration must be positive)
152  newDuration = MAX3(newDuration, TIME2STEPS(myDetectorGap - detectionGap), SUMOTime(1));
153  // cut the decimal places to ensure that phases always have integer duration
154  if (newDuration % 1000 != 0) {
155  const SUMOTime totalDur = newDuration + actDuration;
156  newDuration = (totalDur / 1000 + 1) * 1000 - actDuration;
157  }
158  // ensure that the maximum duration is not exceeded
159  newDuration = MIN2(newDuration, getCurrentPhaseDef().maxDuration - actDuration);
160  return newDuration;
161 }
162 
163 
164 SUMOReal
166  //intergreen times should not be lenghtend
167  assert((int)myPhases.size() > myStep);
169  if (!getCurrentPhaseDef().isGreenPhase()) {
170  return result; // end current phase
171  }
172 
173  // Checks, if the maxDuration is kept. No phase should longer send than maxDuration.
174  SUMOTime actDuration = MSNet::getInstance()->getCurrentTimeStep() - myPhases[myStep]->myLastSwitch;
175  if (actDuration >= getCurrentPhaseDef().maxDuration) {
176  return result; // end current phase
177  }
178 
179  // now the gapcontrol starts
180  const std::string& state = getCurrentPhaseDef().getState();
181  for (int i = 0; i < (int) state.size(); i++) {
182  if (state[i] == LINKSTATE_TL_GREEN_MAJOR || state[i] == LINKSTATE_TL_GREEN_MINOR) {
183  const std::vector<MSLane*>& lanes = getLanesAt(i);
184  for (LaneVector::const_iterator j = lanes.begin(); j != lanes.end(); j++) {
185  if (myInductLoops.find(*j) == myInductLoops.end()) {
186  continue;
187  }
188  if (!MSGlobals::gUseMesoSim) {
189  const SUMOReal actualGap = static_cast<MSInductLoop*>(myInductLoops.find(*j)->second)->getTimestepsSinceLastDetection();
190  if (actualGap < myMaxGap) {
191  result = MIN2(result, actualGap);
192  }
193  }
194  }
195  }
196  }
197  return result;
198 }
199 
200 
201 
202 /****************************************************************************/
203 
The link has green light, may pass.
SUMOReal getTimestepsSinceLastDetection() const
Returns the time since the last vehicle left the detector.
SUMOReal getLength() const
Returns the lane&#39;s length.
Definition: MSLane.h:480
const std::string & getState() const
Returns the state within this phase.
Builds detectors for microsim.
long long int SUMOTime
Definition: SUMOTime.h:43
std::string myProgramID
The id of the logic.
static SUMOReal _2SUMOReal(const E *const data)
converts a char-type array into the SUMOReal value described by it
Definition: TplConvert.h:290
static bool _2bool(const E *const data)
converts a 0-terminated char-type array into the boolean value described by it
Definition: TplConvert.h:364
The link has green light, has to brake.
void init(NLDetectorBuilder &nb)
Initialises the tls with information about incoming lanes.
Phases myPhases
The list of phases this logic uses.
SUMOReal getSpeedLimit() const
Returns the lane&#39;s maximum allowed speed.
Definition: MSLane.h:472
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition: MSNet.cpp:159
std::string myFile
The output file for generated detectors.
#define DEFAULT_DETECTOR_GAP
const std::string & getID() const
Returns the id.
Definition: Named.h:66
#define TIME2STEPS(x)
Definition: SUMOTime.h:66
T MAX3(T a, T b, T c)
Definition: StdDefs.h:89
const MSPhaseDefinition & getCurrentPhaseDef() const
Returns the definition of the current phase.
A fixed traffic light logic.
LaneVectorVector myLanes
The list of LaneVectors; each vector contains the incoming lanes that belong to the same link index...
const LaneVector & getLanesAt(int i) const
Returns the list of lanes that are controlled by the signals at the given position.
A class that stores and controls tls and switching of their programs.
std::string myVehicleTypes
Whether detector output separates by vType.
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
SUMOTime getCurrentTimeStep() const
Returns the current simulation step.
Definition: MSNet.h:254
SUMOTime duration(const SUMOReal detectionGap) const
Returns the minimum duration of the current phase.
T MIN2(T a, T b)
Definition: StdDefs.h:69
virtual MSDetectorFileOutput * createInductLoop(const std::string &id, MSLane *lane, SUMOReal pos, const std::string &vTypes, bool show=true)
Creates an instance of an e1 detector using the given values.
void add(SumoXMLTag type, MSDetectorFileOutput *d, const std::string &device, SUMOTime splInterval, SUMOTime begin=-1)
Adds a detector/output combination into the containers.
MSDetectorControl & getDetectorControl()
Returns the detector control.
Definition: MSNet.h:370
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.
const std::string & getParameter(const std::string &key, const std::string &defaultValue) const
Returns the value for a given key.
SUMOReal myDetectorGap
The detector distance in seconds.
std::string myID
The name of the object.
Definition: Named.h:136
std::vector< MSLane * > LaneVector
Definition of the list of links that participate in this tl-light.
static std::string checkForRelativity(const std::string &filename, const std::string &basePath)
Returns the path from a configuration so that it is accessable from the current working directory...
SUMOTime minDuration
The minimum duration of the phase.
#define SUMOReal
Definition: config.h:213
MSActuatedTrafficLightLogic(MSTLLogicControl &tlcontrol, const std::string &id, const std::string &programID, const MSSimpleTrafficLightLogic::Phases &phases, int step, SUMOTime delay, const std::map< std::string, std::string > &parameter, const std::string &basePath)
Constructor.
static bool gUseMesoSim
Definition: MSGlobals.h:95
SUMOTime myFreq
The frequency for aggregating detector output.
Representation of a lane in the micro simulation.
Definition: MSLane.h:79
#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