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-2017 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 
51 // ===========================================================================
52 // parameter defaults definitions
53 // ===========================================================================
54 #define DEFAULT_MAX_GAP "3.0"
55 #define DEFAULT_PASSING_TIME "1.9"
56 #define DEFAULT_DETECTOR_GAP "2.0"
57 
58 #define DEFAULT_LENGTH_WITH_GAP 7.5
59 
60 
61 // ===========================================================================
62 // method definitions
63 // ===========================================================================
65  const std::string& id, const std::string& programID,
66  const Phases& phases,
67  int step, SUMOTime delay,
68  const std::map<std::string, std::string>& parameter,
69  const std::string& basePath) :
70  MSSimpleTrafficLightLogic(tlcontrol, id, programID, phases, step, delay, parameter) {
71 
73  myPassingTime = TplConvert::_2double(getParameter("passing-time", DEFAULT_PASSING_TIME).c_str()); // passing-time seems obsolete... (Leo)
75  myShowDetectors = TplConvert::_2bool(getParameter("show-detectors", "false").c_str());
76  myFile = FileHelpers::checkForRelativity(getParameter("file", "NUL"), basePath);
77  myFreq = TIME2STEPS(TplConvert::_2double(getParameter("freq", "300").c_str()));
78  myVehicleTypes = getParameter("vTypes", "");
79 }
80 
82 
83 void
86  assert(myLanes.size() > 0);
87  // change values for setting the loops and lanestate-detectors, here
88  //SUMOTime inductLoopInterval = 1; //
89  LaneVectorVector::const_iterator i2;
90  LaneVector::const_iterator i;
91  // build the induct loops
92  double maxDetectorGap = 0;
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  double length = lane->getLength();
98  double speed = lane->getSpeedLimit();
99  double inductLoopPosition = myDetectorGap * speed;
100  // check whether the lane is long enough
101  double 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, myShowDetectors);
110  }
111  maxDetectorGap = MAX2(maxDetectorGap, length - ilpos);
112  }
113  }
114  // warn if the minGap is insufficient to clear vehicles between stop line and detector
115  SUMOTime minMinDur = getMinimumMinDuration();
116  if (floor(floor(maxDetectorGap / DEFAULT_LENGTH_WITH_GAP) * myPassingTime) > STEPS2TIME(minMinDur)) {
117  WRITE_WARNING("At actuated traffic light '" + getID() + "', minDur " + time2string(minMinDur) + " is too short to short for detector gap of " + toString(maxDetectorGap) + "m.");
118  }
119 }
120 
121 
122 SUMOTime
124  SUMOTime result = SUMOTime_MAX;
125  for (auto phase : myPhases) {
126  if (phase->minDuration != phase->maxDuration) {
127  result = MIN2(result, phase->minDuration);
128  }
129  }
130  return result;
131 }
132 
133 
134 // ------------ Switching and setting current rows
135 SUMOTime
137  // checks if the actual phase should be continued
138  // @note any vehicles which arrived during the previous phases which are now waiting between the detector and the stop line are not
139  // considere here. RiLSA recommends to set minDuration in a way that lets all vehicles pass the detector
140  const double detectionGap = gapControl();
141  if (detectionGap < std::numeric_limits<double>::max()) {
142  return duration(detectionGap);
143  }
144  // increment the index to the current phase
145  myStep++;
146  assert(myStep <= (int)myPhases.size());
147  if (myStep == (int)myPhases.size()) {
148  myStep = 0;
149  }
150  //stores the time the phase started
152  // set the next event
154 }
155 
156 
157 // ------------ "actuated" algorithm methods
158 SUMOTime
159 MSActuatedTrafficLightLogic::duration(const double detectionGap) const {
160  assert(getCurrentPhaseDef().isGreenPhase());
161  assert((int)myPhases.size() > myStep);
162  const SUMOTime actDuration = MSNet::getInstance()->getCurrentTimeStep() - myPhases[myStep]->myLastSwitch;
163  // ensure that minimum duration is kept
164  SUMOTime newDuration = getCurrentPhaseDef().minDuration - actDuration;
165  // try to let the last detected vehicle pass the intersection (duration must be positive)
166  newDuration = MAX3(newDuration, TIME2STEPS(myDetectorGap - detectionGap), SUMOTime(1));
167  // cut the decimal places to ensure that phases always have integer duration
168  if (newDuration % 1000 != 0) {
169  const SUMOTime totalDur = newDuration + actDuration;
170  newDuration = (totalDur / 1000 + 1) * 1000 - actDuration;
171  }
172  // ensure that the maximum duration is not exceeded
173  newDuration = MIN2(newDuration, getCurrentPhaseDef().maxDuration - actDuration);
174  return newDuration;
175 }
176 
177 
178 double
180  //intergreen times should not be lenghtend
181  assert((int)myPhases.size() > myStep);
182  double result = std::numeric_limits<double>::max();
183  if (!getCurrentPhaseDef().isGreenPhase()) {
184  return result; // end current phase
185  }
186 
187  // Checks, if the maxDuration is kept. No phase should longer send than maxDuration.
188  SUMOTime actDuration = MSNet::getInstance()->getCurrentTimeStep() - myPhases[myStep]->myLastSwitch;
189  if (actDuration >= getCurrentPhaseDef().maxDuration) {
190  return result; // end current phase
191  }
192 
193  // now the gapcontrol starts
194  const std::string& state = getCurrentPhaseDef().getState();
195  for (int i = 0; i < (int) state.size(); i++) {
196  if (state[i] == LINKSTATE_TL_GREEN_MAJOR || state[i] == LINKSTATE_TL_GREEN_MINOR) {
197  const std::vector<MSLane*>& lanes = getLanesAt(i);
198  for (LaneVector::const_iterator j = lanes.begin(); j != lanes.end(); j++) {
199  if (myInductLoops.find(*j) == myInductLoops.end()) {
200  continue;
201  }
202  if (!MSGlobals::gUseMesoSim) { // why not check outside the loop? (Leo)
203  const double actualGap = static_cast<MSInductLoop*>(myInductLoops.find(*j)->second)->getTimeSinceLastDetection();
204  if (actualGap < myMaxGap) {
205  result = MIN2(result, actualGap);
206  }
207  }
208  }
209  }
210  }
211  return result;
212 }
213 
214 
215 
216 /****************************************************************************/
217 
The link has green light, may pass.
double getSpeedLimit() const
Returns the lane&#39;s maximum allowed speed.
Definition: MSLane.h:476
Builds detectors for microsim.
const std::string & getState() const
Returns the state within this phase.
SUMOTime duration(const double detectionGap) const
Returns the minimum duration of the current phase.
alternative tag for e1 detector
const LaneVector & getLanesAt(int i) const
Returns the list of lanes that are controlled by the signals at the given position.
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:389
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:60
double getLength() const
Returns the lane&#39;s length.
Definition: MSLane.h:484
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition: MSNet.cpp:169
T MAX2(T a, T b)
Definition: StdDefs.h:74
std::string myFile
The output file for generated detectors.
#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:252
T MAX3(T a, T b, T c)
Definition: StdDefs.h:88
#define WRITE_WARNING(msg)
Definition: MsgHandler.h:200
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:66
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.
#define max(a, b)
Definition: polyfonts.c:65
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition: ToString.h:56
double getTimeSinceLastDetection() const
Returns the time since the last vehicle left the detector.
virtual void init(NLDetectorBuilder &nb)
Initialises the tls with information about incoming lanes.
#define SUMOTime_MAX
Definition: TraCIDefs.h:53
#define DEFAULT_PASSING_TIME
#define STEPS2TIME(x)
Definition: SUMOTime.h:65
const MSPhaseDefinition & getCurrentPhaseDef() const
Returns the definition of the current phase.
T MIN2(T a, T b)
Definition: StdDefs.h:68
SUMOTime getMinimumMinDuration() const
get the minimum min duration for all stretchable phases
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:368
#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:136
std::vector< MSLane * > LaneVector
Definition of the list of arrival lanes subjected to this tls.
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:312
SUMOTime minDuration
The minimum duration of the phase.
double myDetectorGap
The detector distance in seconds.
long long int SUMOTime
Definition: TraCIDefs.h:52
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:98
SUMOTime myFreq
The frequency for aggregating detector output.
Representation of a lane in the micro simulation.
Definition: MSLane.h:79
#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:71