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