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