SUMO - Simulation of Urban MObility
MSDelayBasedTrafficLightLogic.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 /****************************************************************************/
15 // An actuated traffic light logic based on time delay of approaching vehicles
16 /****************************************************************************/
17 
18 
19 // ===========================================================================
20 // included modules
21 // ===========================================================================
22 #include <config.h>
23 
24 #include <cassert>
25 #include <vector>
26 #include <microsim/MSGlobals.h>
27 #include <microsim/MSNet.h>
31 #include <microsim/MSLane.h>
34 
35 #define INVALID_POSITION std::numeric_limits<double>::max()
36 
37 // ===========================================================================
38 // parameter defaults definitions
39 // ===========================================================================
40 
41 //#define DEBUG_TIMELOSS_CONTROL
42 
43 // ===========================================================================
44 // method definitions
45 // ===========================================================================
47  const std::string& id, const std::string& programID,
48  const Phases& phases,
49  int step, SUMOTime delay,
50  const std::map<std::string, std::string>& parameter,
51  const std::string& basePath) :
52  MSSimpleTrafficLightLogic(tlcontrol, id, programID, TLTYPE_DELAYBASED, phases, step, delay, parameter) {
53 #ifdef DEBUG_TIMELOSS_CONTROL
54  std::cout << "Building delay based tls logic '" << id << "'" << std::endl;
55 #endif
56  myShowDetectors = StringUtils::toBool(getParameter("show-detectors", "false"));
57  myDetectionRange = StringUtils::toDouble(getParameter("detectorRange", "-1.0"));
59  myFile = FileHelpers::checkForRelativity(getParameter("file", "NUL"), basePath);
61  myVehicleTypes = getParameter("vTypes", "");
62 #ifdef DEBUG_TIMELOSS_CONTROL
63  std::cout << "show-detectors: " << myShowDetectors
64  << " detectorRange: " << myDetectionRange
65  << " minTimeLoss: " << myTimeLossThreshold
66  << " file: " << myFile
67  << " freq: " << myFreq
68  << " vTypes: " << myVehicleTypes
69  << std::endl;
70 #endif
71 }
72 
73 
74 void
77  assert(myLanes.size() > 0);
78  LaneVectorVector::const_iterator i2;
79  LaneVector::const_iterator i;
80  // build the E2 detectors
81  for (i2 = myLanes.begin(); i2 != myLanes.end(); ++i2) {
82  const LaneVector& lanes = *i2;
83  for (i = lanes.begin(); i != lanes.end(); i++) {
84  MSLane* lane = (*i);
85  if (noVehicles(lane->getPermissions())) {
86  // do not build detectors on green verges or sidewalks
87  continue;
88  }
89  // Build the detectors and register them at the detector control
90  std::string id = "TLS" + myID + "_" + myProgramID + "_E2CollectorOn_" + lane->getID();
91  if (myLaneDetectors.find(lane) == myLaneDetectors.end()) {
94  }
95  }
96  }
97 }
98 
99 
100 
102 
103 // ------------ Switching and setting current rows
104 
105 
106 SUMOTime
107 MSDelayBasedTrafficLightLogic::proposeProlongation(const SUMOTime actDuration, const SUMOTime maxDuration, bool& othersEmpty) {
108 #ifdef DEBUG_TIMELOSS_CONTROL
109  std::cout << "\n" << SIMTIME << " MSDelayBasedTrafficLightLogic::proposeProlongation() for TLS '" << this->getID() << "' (current phase = " << myStep << ")" << std::endl;
110 #endif
111  SUMOTime prolongation = 0;
112  const std::string& state = getCurrentPhaseDef().getState();
113  // iterate over green lanes, eventually increase the proposed prolongationTime to the estimated passing time for each lane.
114  for (int i = 0; i < (int) state.size(); i++) {
115  // this lane index corresponds to a non-green time
116  bool igreen = state[i] == LINKSTATE_TL_GREEN_MAJOR || state[i] == LINKSTATE_TL_GREEN_MINOR;
117  const std::vector<MSLane*>& lanes = getLanesAt(i);
118  for (LaneVector::const_iterator j = lanes.begin(); j != lanes.end(); j++) {
119  LaneDetectorMap::iterator i = myLaneDetectors.find(*j);
120  if (i == myLaneDetectors.end()) {
121 #ifdef DEBUG_TIMELOSS_CONTROL
122  // no detector for this lane!? maybe noVehicles allowed
123  std::cout << "no detector on lane '" << (*j)->getID() << std::endl;
124 #endif
125  continue;
126  }
127  MSE2Collector* detector = static_cast<MSE2Collector* >(i->second);
128  const std::vector<MSE2Collector::VehicleInfo*> vehInfos = detector->getCurrentVehicles();
129 #ifdef DEBUG_TIMELOSS_CONTROL
130  int nrVehs = 0; // count vehicles on detector
131 #endif
132  if (igreen) {
133  // green phase
134  for (std::vector<MSE2Collector::VehicleInfo*>::const_iterator ivp = vehInfos.begin(); ivp != vehInfos.end(); ++ivp) {
135  MSE2Collector::VehicleInfo* iv = *ivp;
137  const SUMOTime estimatedTimeToJunction = TIME2STEPS((iv->distToDetectorEnd) / (*j)->getSpeedLimit());
138  if (actDuration + estimatedTimeToJunction <= maxDuration) {
139  // only prolong if vehicle has a chance to pass until max duration is reached
140  prolongation = MAX2(prolongation, estimatedTimeToJunction);
141  }
142 #ifdef DEBUG_TIMELOSS_CONTROL
143  nrVehs++;
144 #endif
145 
146 #ifdef DEBUG_TIMELOSS_CONTROL
147  std::cout << "vehicle '" << iv->id << "' with accumulated timeloss: " << iv->accumulatedTimeLoss
148  << "\nestimated passing time: " << estimatedTimeToJunction << std::endl;
149  } else {
150  std::string reason = iv->accumulatedTimeLoss <= myTimeLossThreshold ? " (time loss below threshold)" : " (front already left detector)";
151  std::cout << "disregarded: (vehicle '" << iv->id << "' with accumulated timeloss " << iv->accumulatedTimeLoss << ")" << reason << std::endl;
152 #endif
153  }
154  }
155  } else {
156  // non-green phase
157  if (vehInfos.size() > 0) {
158  // here is a car on a non-green approach
159  othersEmpty = false;
160  if (actDuration >= getCurrentPhaseDef().maxDuration) {
161 #ifdef DEBUG_TIMELOSS_CONTROL
162  std::cout << "Actual duration exceeds maxDuration and a vehicle is on concurrent approach: " << nrVehs << std::endl;
163 #endif
164  // don't prolong
165  return 0;
166  }
167  break;
168  }
169 #ifdef DEBUG_TIMELOSS_CONTROL
170  std::cout << "Number of current vehicles on detector: " << nrVehs << std::endl;
171 #endif
172  }
173  }
174  }
175 #ifdef DEBUG_TIMELOSS_CONTROL
176  std::cout << "Proposed prolongation (maximal estimated passing time): " << prolongation << std::endl; // debug
177 #endif
178  return prolongation;
179 }
180 
181 
182 SUMOTime
184  /* check if the actual phase should be prolonged */
185  const MSPhaseDefinition& currentPhase = getCurrentPhaseDef();
186  // time since last switch
187  const SUMOTime actDuration = MSNet::getInstance()->getCurrentTimeStep() - currentPhase.myLastSwitch;
188 
189 #ifdef DEBUG_TIMELOSS_CONTROL
190  std::cout << "last switch = " << currentPhase.myLastSwitch
191  << "\nactDuration = " << actDuration
192  << "\nmaxDuration = " << currentPhase.maxDuration
193  << std::endl;
194 #endif
195 
196  // flag whether to prolong or not
197  if (currentPhase.isGreenPhase() && !MSGlobals::gUseMesoSim) {
198  bool othersEmpty = true; // whether no vehicles are present on concurrent approaches
199  SUMOTime proposedProlongation = proposeProlongation(actDuration, currentPhase.maxDuration, othersEmpty);
200 
201 #ifdef DEBUG_TIMELOSS_CONTROL
202  std::cout << "othersEmpty = " << othersEmpty
203  << std::endl;
204 #endif
205 
206  // keep this phase a little longer?
207  bool prolong = othersEmpty || actDuration < currentPhase.maxDuration;
208  // assure minimal duration
209  proposedProlongation = MAX3(SUMOTime(0), proposedProlongation, currentPhase.minDuration - actDuration);
210  if (othersEmpty) {
211  // prolong by one second if no vehicles on other approaches
212  proposedProlongation = MAX2(proposedProlongation, TIME2STEPS(1.));
213  } else {
214  // vehicles are present on other approaches -> prolong no further than the max green time
215  proposedProlongation = MIN2(proposedProlongation, MAX2(SUMOTime(0), currentPhase.maxDuration - actDuration));
216  }
217 
218 #ifdef DEBUG_TIMELOSS_CONTROL
219  std::cout << "Proposed prolongation = " << proposedProlongation << std::endl;
220 #endif
221 
222  prolong = proposedProlongation > 0;
223  if (prolong) {
224  // check again after the prolonged period (must be positive...)
225  // XXX: Can it be harmful not to return a duration of integer seconds?
226  return proposedProlongation;
227  }
228  }
229  // Don't prolong... switch to the next phase
230  myStep++;
231  assert(myStep <= (int)myPhases.size());
232  if (myStep == (int)myPhases.size()) {
233  myStep = 0;
234  }
235  MSPhaseDefinition* newPhase = myPhases[myStep];
236  //stores the time the phase started
238  // set the next event
239  return newPhase->minDuration;
240 }
241 
242 
243 /****************************************************************************/
244 
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
An areal detector corresponding to a sequence of consecutive lanes.
Definition: MSE2Collector.h:80
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:165
T MAX2(T a, T b)
Definition: StdDefs.h:76
double getLength() const
Returns the lane&#39;s length.
Definition: MSLane.h:514
const std::string & getID() const
Returns the id.
Definition: Named.h:78
#define TIME2STEPS(x)
Definition: SUMOTime.h:60
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:90
const MSPhaseDefinition & getCurrentPhaseDef() const
Returns the definition of the current phase.
A fixed traffic light logic.
#define SIMTIME
Definition: SUMOTime.h:65
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.
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:86
std::string myFile
The output file for generated detectors.
SUMOTime myLastSwitch
Stores the timestep of the last on-switched of the phase.
static double toDouble(const std::string &sData)
converts a string into the double value described by it by calling the char-type converter ...
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
SVCPermissions getPermissions() const
Returns the vehicle class permissions for this lane.
Definition: MSLane.h:522
LaneDetectorMap myLaneDetectors
A map from lanes to the corresponding lane detectors.
SUMOTime getCurrentTimeStep() const
Returns the current simulation step.
Definition: MSNet.h:263
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:70
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
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() ...
const std::string myProgramID
The id of the logic.
std::vector< MSPhaseDefinition * > Phases
Definition of a list of phases, being the junction logic.
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:130
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...
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.
#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.
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:91
The definition of a single phase of a tls logic.
Representation of a lane in the micro simulation.
Definition: MSLane.h:78