SUMO - Simulation of Urban MObility
MSDelayBasedTrafficLightLogic.cpp
Go to the documentation of this file.
1 /****************************************************************************/
7 // An actuated traffic light logic based on time delay of approaching vehicles
8 /****************************************************************************/
9 // SUMO, Simulation of Urban MObility; see http://sumo.dlr.de/
10 // Copyright (C) 2001-2017 DLR (http://www.dlr.de/) and contributors
11 /****************************************************************************/
12 //
13 // This file is part of SUMO.
14 // SUMO is free software: you can redistribute it and/or modify
15 // it under the terms of the GNU General Public License as published by
16 // the Free Software Foundation, either version 3 of the License, or
17 // (at your option) any later version.
18 //
19 /****************************************************************************/
20 
21 
22 // ===========================================================================
23 // included modules
24 // ===========================================================================
25 #ifdef _MSC_VER
26 #include <windows_config.h>
27 #else
28 #include <config.h>
29 #endif
30 
31 #include <cassert>
32 #include <vector>
33 #include <microsim/MSGlobals.h>
34 #include <microsim/MSNet.h>
38 #include <microsim/MSLane.h>
41 
42 #define INVALID_POSITION std::numeric_limits<double>::max()
43 
44 // ===========================================================================
45 // parameter defaults definitions
46 // ===========================================================================
47 
48 //#define DEBUG_TIMELOSS_CONTROL
49 
50 // ===========================================================================
51 // method definitions
52 // ===========================================================================
54  const std::string& id, const std::string& programID,
55  const Phases& phases,
56  int step, SUMOTime delay,
57  const std::map<std::string, std::string>& parameter,
58  const std::string& basePath) :
59  MSSimpleTrafficLightLogic(tlcontrol, id, programID, phases, step, delay, parameter) {
60 #ifdef DEBUG_TIMELOSS_CONTROL
61  std::cout << "Building delay based tls logic '" << id << "'" << std::endl;
62 #endif
63  myShowDetectors = TplConvert::_2bool(getParameter("show-detectors", "false").c_str());
64  myDetectionRange = TplConvert::_2double(getParameter("detectorRange", "-1.0").c_str());
65  myTimeLossThreshold = TplConvert::_2double(getParameter("minTimeloss", "1.0").c_str());
66  myFile = FileHelpers::checkForRelativity(getParameter("file", "NUL"), basePath);
67  myFreq = TIME2STEPS(TplConvert::_2double(getParameter("freq", "300").c_str()));
68  myVehicleTypes = getParameter("vTypes", "");
69 #ifdef DEBUG_TIMELOSS_CONTROL
70  std::cout << "show-detectors: " << myShowDetectors
71  << " detectorRange: " << myDetectionRange
72  << " minTimeLoss: " << myTimeLossThreshold
73  << " file: " << myFile
74  << " freq: " << myFreq
75  << " vTypes: " << myVehicleTypes
76  << std::endl;
77 #endif
78 }
79 
80 
81 void
84  assert(myLanes.size() > 0);
85  LaneVectorVector::const_iterator i2;
86  LaneVector::const_iterator i;
87  // build the E2 detectors
88  for (i2 = myLanes.begin(); i2 != myLanes.end(); ++i2) {
89  const LaneVector& lanes = *i2;
90  for (i = lanes.begin(); i != lanes.end(); i++) {
91  MSLane* lane = (*i);
92  // Build the detectors and register them at the detector control
93  std::string id = "TLS" + myID + "_" + myProgramID + "_E2CollectorOn_" + lane->getID();
94  if (myLaneDetectors.find(lane) == myLaneDetectors.end()) {
97  }
98  }
99  }
100 }
101 
102 
103 
105 
106 // ------------ Switching and setting current rows
107 
108 
109 SUMOTime
110 MSDelayBasedTrafficLightLogic::proposeProlongation(const SUMOTime actDuration, const SUMOTime maxDuration, bool& othersEmpty) {
111 #ifdef DEBUG_TIMELOSS_CONTROL
112  std::cout << "\n" << SIMTIME << " MSDelayBasedTrafficLightLogic::proposeProlongation() for TLS '" << this->getID() << "' (current phase = " << myStep << ")" << std::endl;
113 #endif
114  SUMOTime prolongation = 0;
115  const std::string& state = getCurrentPhaseDef().getState();
116  // iterate over green lanes, eventually increase the proposed prolongationTime to the estimated passing time for each lane.
117  for (int i = 0; i < (int) state.size(); i++) {
118  // this lane index corresponds to a non-green time
119  bool igreen = state[i] == LINKSTATE_TL_GREEN_MAJOR || state[i] == LINKSTATE_TL_GREEN_MINOR;
120  const std::vector<MSLane*>& lanes = getLanesAt(i);
121  for (LaneVector::const_iterator j = lanes.begin(); j != lanes.end(); j++) {
122  LaneDetectorMap::iterator i = myLaneDetectors.find(*j);
123 #ifdef DEBUG_TIMELOSS_CONTROL
124  if (i == myLaneDetectors.end()) {
125  // no detector for this lane!?
126  std::cout << "no detector on lane '" << (*j)->getID() << std::endl;
127  continue;
128  }
129 #endif
130  MSE2Collector* detector = static_cast<MSE2Collector* >(i->second);
131  const std::vector<MSE2Collector::VehicleInfo*> vehInfos = detector->getCurrentVehicles();
132 #ifdef DEBUG_TIMELOSS_CONTROL
133  int nrVehs = 0; // count vehicles on detector
134 #endif
135  if (igreen) {
136  // green phase
137  for (std::vector<MSE2Collector::VehicleInfo*>::const_iterator ivp = vehInfos.begin(); ivp != vehInfos.end(); ++ivp) {
138  MSE2Collector::VehicleInfo* iv = *ivp;
140  const SUMOTime estimatedTimeToJunction = TIME2STEPS((iv->distToDetectorEnd) / (*j)->getSpeedLimit());
141  if (actDuration + estimatedTimeToJunction <= maxDuration) {
142  // only prolong if vehicle has a chance to pass until max duration is reached
143  prolongation = MAX2(prolongation, estimatedTimeToJunction);
144  }
145 #ifdef DEBUG_TIMELOSS_CONTROL
146  nrVehs++;
147 #endif
148 
149 #ifdef DEBUG_TIMELOSS_CONTROL
150  std::cout << "vehicle '" << iv->id << "' with accumulated timeloss: " << iv->accumulatedTimeLoss
151  << "\nestimated passing time: " << estimatedTimeToJunction << std::endl;
152  } else {
153  std::string reason = iv->accumulatedTimeLoss <= myTimeLossThreshold ? " (time loss below threshold)" : " (front already left detector)";
154  std::cout << "disregarded: (vehicle '" << iv->id << "' with accumulated timeloss " << iv->accumulatedTimeLoss << ")" << reason << std::endl;
155 #endif
156  }
157  }
158  } else {
159  // non-green phase
160  if (vehInfos.size() > 0) {
161  // here is a car on a non-green approach
162  othersEmpty = false;
163  if (actDuration >= getCurrentPhaseDef().maxDuration) {
164 #ifdef DEBUG_TIMELOSS_CONTROL
165  std::cout << "Actual duration exceeds maxDuration and a vehicle is on concurrent approach: " << nrVehs << std::endl;
166 #endif
167  // don't prolong
168  return 0;
169  }
170  break;
171  }
172 #ifdef DEBUG_TIMELOSS_CONTROL
173  std::cout << "Number of current vehicles on detector: " << nrVehs << std::endl;
174 #endif
175  }
176  }
177  }
178 #ifdef DEBUG_TIMELOSS_CONTROL
179  std::cout << "Proposed prolongation (maximal estimated passing time): " << prolongation << std::endl; // debug
180 #endif
181  return prolongation;
182 }
183 
184 
185 SUMOTime
187  /* check if the actual phase should be prolonged */
188  const MSPhaseDefinition& currentPhase = getCurrentPhaseDef();
189  // time since last switch
190  const SUMOTime actDuration = MSNet::getInstance()->getCurrentTimeStep() - currentPhase.myLastSwitch;
191 
192 #ifdef DEBUG_TIMELOSS_CONTROL
193  std::cout << "last switch = " << currentPhase.myLastSwitch
194  << "\nactDuration = " << actDuration
195  << "\nmaxDuration = " << currentPhase.maxDuration
196  << std::endl;
197 #endif
198 
199  // flag whether to prolong or not
200  if (currentPhase.isGreenPhase() && !MSGlobals::gUseMesoSim) {
201  bool othersEmpty = true; // whether no vehicles are present on concurrent approaches
202  SUMOTime proposedProlongation = proposeProlongation(actDuration, currentPhase.maxDuration, othersEmpty);
203 
204 #ifdef DEBUG_TIMELOSS_CONTROL
205  std::cout << "othersEmpty = " << othersEmpty
206  << std::endl;
207 #endif
208 
209  // keep this phase a little longer?
210  bool prolong = othersEmpty || actDuration < currentPhase.maxDuration;
211  // assure minimal duration
212  proposedProlongation = MAX3(SUMOTime(0), proposedProlongation, currentPhase.minDuration - actDuration);
213  if (othersEmpty) {
214  // prolong by one second if no vehicles on other approaches
215  proposedProlongation = MAX2(proposedProlongation, TIME2STEPS(1.));
216  } else {
217  // vehicles are present on other approaches -> prolong no further than the max green time
218  proposedProlongation = MIN2(proposedProlongation, MAX2(SUMOTime(0), currentPhase.maxDuration - actDuration));
219  }
220 
221 #ifdef DEBUG_TIMELOSS_CONTROL
222  std::cout << "Proposed prolongation = " << proposedProlongation << std::endl;
223 #endif
224 
225  prolong = proposedProlongation > 0;
226  if (prolong) {
227  // check again after the prolonged period (must be positive...)
228  // XXX: Can it be harmful not to return a duration of integer seconds?
229  return proposedProlongation;
230  }
231  }
232  // Don't prolong... switch to the next phase
233  myStep++;
234  assert(myStep <= (int)myPhases.size());
235  if (myStep == (int)myPhases.size()) {
236  myStep = 0;
237  }
238  MSPhaseDefinition* newPhase = myPhases[myStep];
239  //stores the time the phase started
241  // set the next event
242  return newPhase->minDuration;
243 }
244 
245 
246 /****************************************************************************/
247 
The link has green light, may pass.
const std::string & getState() const
Returns the state within this phase.
Builds detectors for microsim.
An areal detector corresponding to a sequence of consecutive lanes.
Definition: MSE2Collector.h:87
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.
Phases myPhases
The list of phases this logic uses.
SUMOTime trySwitch()
Switches to the next phase, if possible.
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition: MSNet.cpp:158
T MAX2(T a, T b)
Definition: StdDefs.h:70
double getLength() const
Returns the lane&#39;s length.
Definition: MSLane.h:484
const std::string & getID() const
Returns the id.
Definition: Named.h:66
#define TIME2STEPS(x)
Definition: SUMOTime.h:66
std::vector< VehicleInfo * > getCurrentVehicles() const
Returns the VehicleInfos for the vehicles currently on the detector.
T MAX3(T a, T b, T c)
Definition: StdDefs.h:84
const MSPhaseDefinition & getCurrentPhaseDef() const
Returns the definition of the current phase.
A fixed traffic light logic.
#define SIMTIME
Definition: SUMOTime.h:70
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.
MSDelayBasedTrafficLightLogic(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.
A VehicleInfo stores values that are tracked for the individual vehicles on the detector, e.g., accumulated timeloss. These infos are stored in myVehicles. If a vehicle leaves the detector (may it be temporarily), the entry in myVehicles is discarded, i.e. all information on the vehicle is reset.
Definition: MSE2Collector.h:93
std::string myFile
The output file for generated detectors.
SUMOTime myLastSwitch
Stores the timestep of the last on-switched of the phase.
double distToDetectorEnd
Distance left till the detector end after the last integration step (may become negative if the vehic...
virtual void init(NLDetectorBuilder &nb)
Initialises the tls with information about incoming lanes.
std::string id
vehicle&#39;s ID
LaneDetectorMap myLaneDetectors
A map from lanes to the corresponding lane detectors.
SUMOTime getCurrentTimeStep() const
Returns the current simulation step.
Definition: MSNet.h:257
SUMOTime proposeProlongation(const SUMOTime actDuration, const SUMOTime maxDuration, bool &othersEmpty)
The returned, proposed prolongation for the green phase is oriented on the largest estimated passing ...
T MIN2(T a, T b)
Definition: StdDefs.h:64
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
virtual MSE2Collector * createE2Detector(const std::string &id, DetectorUsage usage, MSLane *lane, double pos, double endPos, double length, SUMOTime haltingTimeThreshold, double haltingSpeedThreshold, double jamDistThreshold, const std::string &vTypes, bool showDetector=true)
Creates a MSE2Collector instance, overridden by GUIE2Collector::createE2Detector() ...
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.
double accumulatedTimeLoss
Accumulated time loss that this vehicle suffered since it entered the detector.
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.
SUMOTime maxDuration
The maximum duration of the phase.
std::string myVehicleTypes
Whether detector output separates by vType.
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
#define INVALID_POSITION
SUMOTime minDuration
The minimum duration of the phase.
bool isGreenPhase() const
Returns whether this phase is a pure "green" phase.
void init(NLDetectorBuilder &nb)
Initializes the tls with information about incoming lanes.
long long int SUMOTime
Definition: TraCIDefs.h:52
bool myShowDetectors
Whether the detectors shall be shown in the GUI.
SUMOTime myFreq
The frequency for aggregating detector output.
double myDetectionRange
Range of the connected detector, which provides the information on approaching vehicles.
static bool gUseMesoSim
Definition: MSGlobals.h:98
The definition of a single phase of a tls logic.
Representation of a lane in the micro simulation.
Definition: MSLane.h:79