SUMO - Simulation of Urban MObility
MSActuatedTrafficLightLogic.cpp
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2001-2017 German Aerospace Center (DLR) and others.
4 /****************************************************************************/
5 //
6 // This program and the accompanying materials
7 // are made available under the terms of the Eclipse Public License v2.0
8 // which accompanies this distribution, and is available at
9 // http://www.eclipse.org/legal/epl-v20.html
10 //
11 /****************************************************************************/
21 // An actuated (adaptive) traffic light logic
22 /****************************************************************************/
23 
24 
25 // ===========================================================================
26 // included modules
27 // ===========================================================================
28 #ifdef _MSC_VER
29 #include <windows_config.h>
30 #else
31 #include <config.h>
32 #endif
33 
34 #include <cassert>
35 #include <utility>
36 #include <vector>
37 #include <bitset>
41 #include <microsim/MSGlobals.h>
42 #include <microsim/MSNet.h>
43 #include "MSTrafficLightLogic.h"
45 #include <microsim/MSLane.h>
48 
49 
50 // ===========================================================================
51 // parameter defaults definitions
52 // ===========================================================================
53 #define DEFAULT_MAX_GAP "3.0"
54 #define DEFAULT_PASSING_TIME "1.9"
55 #define DEFAULT_DETECTOR_GAP "2.0"
56 
57 #define DEFAULT_LENGTH_WITH_GAP 7.5
58 
59 
60 // ===========================================================================
61 // method definitions
62 // ===========================================================================
64  const std::string& id, const std::string& programID,
65  const Phases& phases,
66  int step, SUMOTime delay,
67  const std::map<std::string, std::string>& parameter,
68  const std::string& basePath) :
69  MSSimpleTrafficLightLogic(tlcontrol, id, programID, phases, step, delay, parameter) {
70 
72  myPassingTime = TplConvert::_2double(getParameter("passing-time", DEFAULT_PASSING_TIME).c_str()); // passing-time seems obsolete... (Leo)
74  myShowDetectors = TplConvert::_2bool(getParameter("show-detectors", "false").c_str());
75  myFile = FileHelpers::checkForRelativity(getParameter("file", "NUL"), basePath);
76  myFreq = TIME2STEPS(TplConvert::_2double(getParameter("freq", "300").c_str()));
77  myVehicleTypes = getParameter("vTypes", "");
78 }
79 
81 
82 void
85  assert(myLanes.size() > 0);
86  // change values for setting the loops and lanestate-detectors, here
87  //SUMOTime inductLoopInterval = 1; //
88  LaneVectorVector::const_iterator i2;
89  LaneVector::const_iterator i;
90  // build the induct loops
91  double maxDetectorGap = 0;
92  for (i2 = myLanes.begin(); i2 != myLanes.end(); ++i2) {
93  const LaneVector& lanes = *i2;
94  for (i = lanes.begin(); i != lanes.end(); i++) {
95  MSLane* lane = (*i);
96  double length = lane->getLength();
97  double speed = lane->getSpeedLimit();
98  double inductLoopPosition = myDetectorGap * speed;
99  // check whether the lane is long enough
100  double ilpos = length - inductLoopPosition;
101  if (ilpos < 0) {
102  ilpos = 0;
103  }
104  // Build the induct loop and set it into the container
105  std::string id = "TLS" + myID + "_" + myProgramID + "_InductLoopOn_" + lane->getID();
106  if (myInductLoops.find(lane) == myInductLoops.end()) {
107  myInductLoops[lane] = nb.createInductLoop(id, lane, ilpos, myVehicleTypes, myShowDetectors);
109  }
110  maxDetectorGap = MAX2(maxDetectorGap, length - ilpos);
111  }
112  }
113  // warn if the minGap is insufficient to clear vehicles between stop line and detector
114  SUMOTime minMinDur = getMinimumMinDuration();
115  if (floor(floor(maxDetectorGap / DEFAULT_LENGTH_WITH_GAP) * myPassingTime) > STEPS2TIME(minMinDur)) {
116  WRITE_WARNING("At actuated tlLogic '" + getID() + "', minDur " + time2string(minMinDur) + " is too short to short for detector gap of " + toString(maxDetectorGap) + "m.");
117  }
118 }
119 
120 
121 SUMOTime
123  SUMOTime result = SUMOTime_MAX;
124  for (auto phase : myPhases) {
125  if (phase->minDuration != phase->maxDuration) {
126  result = MIN2(result, phase->minDuration);
127  }
128  }
129  return result;
130 }
131 
132 
133 // ------------ Switching and setting current rows
134 SUMOTime
136  // checks if the actual phase should be continued
137  // @note any vehicles which arrived during the previous phases which are now waiting between the detector and the stop line are not
138  // considere here. RiLSA recommends to set minDuration in a way that lets all vehicles pass the detector
139  const double detectionGap = gapControl();
140  if (detectionGap < std::numeric_limits<double>::max()) {
141  return duration(detectionGap);
142  }
143  // increment the index to the current phase
144  myStep++;
145  assert(myStep <= (int)myPhases.size());
146  if (myStep == (int)myPhases.size()) {
147  myStep = 0;
148  }
149  //stores the time the phase started
151  // set the next event
153 }
154 
155 
156 // ------------ "actuated" algorithm methods
157 SUMOTime
158 MSActuatedTrafficLightLogic::duration(const double detectionGap) const {
159  assert(getCurrentPhaseDef().isGreenPhase());
160  assert((int)myPhases.size() > myStep);
161  const SUMOTime actDuration = MSNet::getInstance()->getCurrentTimeStep() - myPhases[myStep]->myLastSwitch;
162  // ensure that minimum duration is kept
163  SUMOTime newDuration = getCurrentPhaseDef().minDuration - actDuration;
164  // try to let the last detected vehicle pass the intersection (duration must be positive)
165  newDuration = MAX3(newDuration, TIME2STEPS(myDetectorGap - detectionGap), SUMOTime(1));
166  // cut the decimal places to ensure that phases always have integer duration
167  if (newDuration % 1000 != 0) {
168  const SUMOTime totalDur = newDuration + actDuration;
169  newDuration = (totalDur / 1000 + 1) * 1000 - actDuration;
170  }
171  // ensure that the maximum duration is not exceeded
172  newDuration = MIN2(newDuration, getCurrentPhaseDef().maxDuration - actDuration);
173  return newDuration;
174 }
175 
176 
177 double
179  //intergreen times should not be lenghtend
180  assert((int)myPhases.size() > myStep);
181  double result = std::numeric_limits<double>::max();
182  if (!getCurrentPhaseDef().isGreenPhase()) {
183  return result; // end current phase
184  }
185 
186  // Checks, if the maxDuration is kept. No phase should longer send than maxDuration.
187  SUMOTime actDuration = MSNet::getInstance()->getCurrentTimeStep() - myPhases[myStep]->myLastSwitch;
188  if (actDuration >= getCurrentPhaseDef().maxDuration) {
189  return result; // end current phase
190  }
191 
192  // now the gapcontrol starts
193  const std::string& state = getCurrentPhaseDef().getState();
194  for (int i = 0; i < (int) state.size(); i++) {
195  if (state[i] == LINKSTATE_TL_GREEN_MAJOR || state[i] == LINKSTATE_TL_GREEN_MINOR) {
196  const std::vector<MSLane*>& lanes = getLanesAt(i);
197  for (LaneVector::const_iterator j = lanes.begin(); j != lanes.end(); j++) {
198  if (myInductLoops.find(*j) == myInductLoops.end()) {
199  continue;
200  }
201  if (!MSGlobals::gUseMesoSim) { // why not check outside the loop? (Leo)
202  const double actualGap = static_cast<MSInductLoop*>(myInductLoops.find(*j)->second)->getTimeSinceLastDetection();
203  if (actualGap < myMaxGap) {
204  result = MIN2(result, actualGap);
205  }
206  }
207  }
208  }
209  }
210  return result;
211 }
212 
213 
214 
215 /****************************************************************************/
216 
The link has green light, may pass.
const std::string & getState() const
Returns the state within this phase.
Builds detectors for microsim.
alternative tag for e1 detector
std::string myProgramID
The id of the logic.
static bool _2bool(const E *const data)
converts a 0-terminated char-type array into the boolean value described by it
Definition: TplConvert.h:388
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.
std::string time2string(SUMOTime t)
Definition: SUMOTime.cpp:59
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition: MSNet.cpp:167
T MAX2(T a, T b)
Definition: StdDefs.h:73
std::string myFile
The output file for generated detectors.
double getLength() const
Returns the lane&#39;s length.
Definition: MSLane.h:497
#define DEFAULT_DETECTOR_GAP
const std::string & getID() const
Returns the id.
Definition: Named.h:65
#define TIME2STEPS(x)
Definition: SUMOTime.h:66
T MAX3(T a, T b, T c)
Definition: StdDefs.h:87
#define WRITE_WARNING(msg)
Definition: MsgHandler.h:199
SUMOTime duration(const double detectionGap) const
Returns the minimum duration of the current phase.
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.
virtual MSDetectorFileOutput * createInductLoop(const std::string &id, MSLane *lane, double pos, const std::string &vTypes, bool show=true)
Creates an instance of an e1 detector using the given values.
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition: ToString.h:55
virtual void init(NLDetectorBuilder &nb)
Initialises the tls with information about incoming lanes.
#define SUMOTime_MAX
Definition: TraCIDefs.h:52
#define DEFAULT_PASSING_TIME
double getSpeedLimit() const
Returns the lane&#39;s maximum allowed speed.
Definition: MSLane.h:489
#define STEPS2TIME(x)
Definition: SUMOTime.h:64
SUMOTime getCurrentTimeStep() const
Returns the current simulation step.
Definition: MSNet.h:253
T MIN2(T a, T b)
Definition: StdDefs.h:67
double myMaxGap
The maximum gap to check in seconds.
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:369
double getTimeSinceLastDetection() const
Returns the time since the last vehicle left the detector.
#define DEFAULT_LENGTH_WITH_GAP
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.
std::string myID
The name of the object.
Definition: Named.h:135
std::vector< MSLane * > LaneVector
Definition of the list of arrival lanes subjected to this tls.
SUMOTime getMinimumMinDuration() const
get the minimum min duration for all stretchable phases
double gapControl()
Return the minimum detection gap of all detectors if the current phase should be extended and double:...
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...
static double _2double(const E *const data)
converts a char-type array into the double value described by it
Definition: TplConvert.h:311
const std::string getParameter(const std::string &key, const std::string &defaultValue="") const
Returns the value for a given key.
SUMOTime minDuration
The minimum duration of the phase.
double myDetectorGap
The detector distance in seconds.
long long int SUMOTime
Definition: TraCIDefs.h:51
double myPassingTime
The passing time used in seconds.
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:97
SUMOTime myFreq
The frequency for aggregating detector output.
Representation of a lane in the micro simulation.
Definition: MSLane.h:77
#define DEFAULT_MAX_GAP
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:70