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.1"
55 #define DEFAULT_PASSING_TIME "1.9"
56 #define DEFAULT_DETECTOR_GAP "3.0"
57 
58 
59 // ===========================================================================
60 // method definitions
61 // ===========================================================================
63  const std::string& id, const std::string& programID,
64  const Phases& phases,
65  int step, SUMOTime delay,
66  const std::map<std::string, std::string>& parameter,
67  const std::string& basePath) :
68  MSSimpleTrafficLightLogic(tlcontrol, id, programID, phases, step, delay, parameter) {
69 
71  myPassingTime = TplConvert::_2double(getParameter("passing-time", DEFAULT_PASSING_TIME).c_str()); // passing-time seems obsolete... (Leo)
73  myShowDetectors = TplConvert::_2bool(getParameter("show-detectors", "false").c_str());
74  myFile = FileHelpers::checkForRelativity(getParameter("file", "NUL"), basePath);
75  myFreq = TIME2STEPS(TplConvert::_2double(getParameter("freq", "300").c_str()));
76  myVehicleTypes = getParameter("vTypes", "");
77 }
78 
79 
80 void
83  assert(myLanes.size() > 0);
84  // change values for setting the loops and lanestate-detectors, here
85  //SUMOTime inductLoopInterval = 1; //
86  LaneVectorVector::const_iterator i2;
87  LaneVector::const_iterator i;
88  // build the induct loops
89  for (i2 = myLanes.begin(); i2 != myLanes.end(); ++i2) {
90  const LaneVector& lanes = *i2;
91  for (i = lanes.begin(); i != lanes.end(); i++) {
92  MSLane* lane = (*i);
93  double length = lane->getLength();
94  double speed = lane->getSpeedLimit();
95  double inductLoopPosition = myDetectorGap * speed;
96  // check whether the lane is long enough
97  double ilpos = length - inductLoopPosition;
98  if (ilpos < 0) {
99  ilpos = 0;
100  }
101  // Build the induct loop and set it into the container
102  std::string id = "TLS" + myID + "_" + myProgramID + "_InductLoopOn_" + lane->getID();
103  if (myInductLoops.find(lane) == myInductLoops.end()) {
104  myInductLoops[lane] = nb.createInductLoop(id, lane, ilpos, myVehicleTypes, myShowDetectors);
106  }
107  }
108  }
109 }
110 
111 
113 
114 
115 // ------------ Switching and setting current rows
116 SUMOTime
118  // checks if the actual phase should be continued
119  // @note any vehicles which arrived during the previous phases which are now waiting between the detector and the stop line are not
120  // considere here. RiLSA recommends to set minDuration in a way that lets all vehicles pass the detector
121  // @todo: it would be nice to warn users if (inductLoopPosition / defaultLengthWithGap * myPassingTime > minDuration)
122  const double detectionGap = gapControl();
123  if (detectionGap < std::numeric_limits<double>::max()) {
124  return duration(detectionGap);
125  }
126  // increment the index to the current phase
127  myStep++;
128  assert(myStep <= (int)myPhases.size());
129  if (myStep == (int)myPhases.size()) {
130  myStep = 0;
131  }
132  //stores the time the phase started
134  // set the next event
136 }
137 
138 
139 // ------------ "actuated" algorithm methods
140 SUMOTime
141 MSActuatedTrafficLightLogic::duration(const double detectionGap) const {
142  assert(getCurrentPhaseDef().isGreenPhase());
143  assert((int)myPhases.size() > myStep);
144  const SUMOTime actDuration = MSNet::getInstance()->getCurrentTimeStep() - myPhases[myStep]->myLastSwitch;
145  // ensure that minimum duration is kept
146  SUMOTime newDuration = getCurrentPhaseDef().minDuration - actDuration;
147  // try to let the last detected vehicle pass the intersection (duration must be positive)
148  newDuration = MAX3(newDuration, TIME2STEPS(myDetectorGap - detectionGap), SUMOTime(1));
149  // cut the decimal places to ensure that phases always have integer duration
150  if (newDuration % 1000 != 0) {
151  const SUMOTime totalDur = newDuration + actDuration;
152  newDuration = (totalDur / 1000 + 1) * 1000 - actDuration;
153  }
154  // ensure that the maximum duration is not exceeded
155  newDuration = MIN2(newDuration, getCurrentPhaseDef().maxDuration - actDuration);
156  return newDuration;
157 }
158 
159 
160 double
162  //intergreen times should not be lenghtend
163  assert((int)myPhases.size() > myStep);
164  double result = std::numeric_limits<double>::max();
165  if (!getCurrentPhaseDef().isGreenPhase()) {
166  return result; // end current phase
167  }
168 
169  // Checks, if the maxDuration is kept. No phase should longer send than maxDuration.
170  SUMOTime actDuration = MSNet::getInstance()->getCurrentTimeStep() - myPhases[myStep]->myLastSwitch;
171  if (actDuration >= getCurrentPhaseDef().maxDuration) {
172  return result; // end current phase
173  }
174 
175  // now the gapcontrol starts
176  const std::string& state = getCurrentPhaseDef().getState();
177  for (int i = 0; i < (int) state.size(); i++) {
178  if (state[i] == LINKSTATE_TL_GREEN_MAJOR || state[i] == LINKSTATE_TL_GREEN_MINOR) {
179  const std::vector<MSLane*>& lanes = getLanesAt(i);
180  for (LaneVector::const_iterator j = lanes.begin(); j != lanes.end(); j++) {
181  if (myInductLoops.find(*j) == myInductLoops.end()) {
182  continue;
183  }
184  if (!MSGlobals::gUseMesoSim) { // why not check outside the loop? (Leo)
185  const double actualGap = static_cast<MSInductLoop*>(myInductLoops.find(*j)->second)->getTimeSinceLastDetection();
186  if (actualGap < myMaxGap) {
187  result = MIN2(result, actualGap);
188  }
189  }
190  }
191  }
192  }
193  return result;
194 }
195 
196 
197 
198 /****************************************************************************/
199 
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:371
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.
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition: MSNet.cpp:158
std::string myFile
The output file for generated detectors.
double getLength() const
Returns the lane&#39;s length.
Definition: MSLane.h:484
#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:84
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.
#define max(a, b)
Definition: polyfonts.c:65
virtual void init(NLDetectorBuilder &nb)
Initialises the tls with information about incoming lanes.
#define DEFAULT_PASSING_TIME
double getSpeedLimit() const
Returns the lane&#39;s maximum allowed speed.
Definition: MSLane.h:476
SUMOTime getCurrentTimeStep() const
Returns the current simulation step.
Definition: MSNet.h:257
T MIN2(T a, T b)
Definition: StdDefs.h:64
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:373
double getTimeSinceLastDetection() const
Returns the time since the last vehicle left the detector.
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.
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:297
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