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>
34 #include <microsim/MSGlobals.h>
35 #include <microsim/MSNet.h>
39 #include <microsim/MSLane.h>
42 
43 #ifdef CHECK_MEMORY_LEAKS
44 #include <foreign/nvwa/debug_new.h>
45 #endif // CHECK_MEMORY_LEAKS
46 
47 
48 // ===========================================================================
49 // parameter defaults definitions
50 // ===========================================================================
51 
52 // if a vehicle's timeloss is below TIMELOSS_THRESHOLD, this is counted as insignificant,
53 // since this may stem from dawdling, or driving slightly slower than the detector
54 // TODO: this might be adapted to the detector-length and the vehicle's maximal speed...
55 #define TIMELOSS_THRESHOLD 1.0
56 
57 //#define DEBUG_TIMELOSS_CONTROL
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 
70  myShowDetectors = TplConvert::_2bool(getParameter("show-detectors", "false").c_str());
71  myFile = FileHelpers::checkForRelativity(getParameter("file", "NUL"), basePath);
72  myFreq = TIME2STEPS(TplConvert::_2SUMOReal(getParameter("freq", "300").c_str()));
73  myVehicleTypes = getParameter("vTypes", "");
74 }
75 
76 
77 void
80  assert(myLanes.size() > 0);
81  // change values for setting the loops and lanestate-detectors, here
82  //SUMOTime inductLoopInterval = 1; //
83  LaneVectorVector::const_iterator i2;
84  LaneVector::const_iterator i;
85  // build the induct loops
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  // Build the induct loop and set it into the container
91  std::string id = "TLS" + myID + "_" + myProgramID + "_E2DetectorOn_" + lane->getID();
92  if (myLaneDetectors.find(lane) == myLaneDetectors.end()) {
93  // TODO: allow hiding E2-detectors in the gui
94  // TODO: allow specifying a maximal detector range
95  myLaneDetectors[lane] = nb.createSingleLaneE2Detector(id, DU_TL_CONTROL, lane, 0., lane->getLength(), 1000, SUMO_const_haltingSpeed, 1000, myVehicleTypes); //, myShowDetectors);
97  }
98  }
99  }
100 }
101 
102 
104 
105 // ------------ Switching and setting current rows
106 
107 
108 SUMOTime
110 #ifdef DEBUG_TIMELOSS_CONTROL
111  std::cout << "\n" << SIMTIME << " MSDelayBasedTrafficLightLogic::proposeProlongation() for TLS '" << this->getID() << "'" << std::endl;
112 #endif
113  SUMOReal prolongationTime = 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  if (state[i] == LINKSTATE_TL_GREEN_MAJOR || state[i] == LINKSTATE_TL_GREEN_MINOR) {
118  const std::vector<MSLane*>& lanes = getLanesAt(i);
119  for (LaneVector::const_iterator j = lanes.begin(); j != lanes.end(); j++) {
120  LaneDetectorMap::iterator i = myLaneDetectors.find(*j);
121 
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 
132 #ifdef DEBUG_TIMELOSS_CONTROL
133  std::cout << "Number of current vehicles on detector: " << vehInfos.size() << std::endl;
134 #endif
135 
136  for (std::vector<MSE2Collector::VehicleInfo>::const_iterator iv = vehInfos.begin(); iv != vehInfos.end(); ++iv) {
137  if (iv->stillOnDet && iv->accumulatedTimeLoss > TIMELOSS_THRESHOLD) {
138  SUMOReal estimatedTimeToJunction = ((*j)->getLength() - iv->position) / (*j)->getSpeedLimit();
139  prolongationTime = MAX2(prolongationTime, estimatedTimeToJunction);
140 #ifdef DEBUG_TIMELOSS_CONTROL
141  std::cout << "vehicle '" << iv->id << "' with accumulated timeloss: " << iv->accumulatedTimeLoss
142  << "\nestimated passing time: " << estimatedTimeToJunction << std::endl;
143  } else {
144  std::cout << "disregarded: (vehicle '" << iv->id << "' with accumulated timeloss " << iv->accumulatedTimeLoss << ")" << std::endl;
145 #endif
146  }
147  }
148  }
149  }
150  }
151 #ifdef DEBUG_TIMELOSS_CONTROL
152  std::cout << "Proposed prolongation (maximal estimated passing time): " << prolongationTime << std::endl; // debug
153 #endif
154  return TIME2STEPS(prolongationTime);
155 }
156 
157 
158 SUMOTime
160  /* check if the actual phase should be prolonged */
161  const MSPhaseDefinition& currentPhase = getCurrentPhaseDef();
162  // time since last switch
163  const SUMOTime actDuration = MSNet::getInstance()->getCurrentTimeStep() - currentPhase.myLastSwitch;
164 
165 #ifdef DEBUG_TIMELOSS_CONTROL
166  std::cout << "last switch = " << currentPhase.myLastSwitch
167  << "\nactDuration = " << actDuration << std::endl;
168 #endif
169 
170  // flag whether to prolong or not
171  bool prolong = currentPhase.isGreenPhase() && actDuration < currentPhase.maxDuration && !MSGlobals::gUseMesoSim;
172  if (prolong) {
173  // keep this phase
174  SUMOTime proposedProlongation = proposeProlongation();
175  proposedProlongation = MAX3(SUMOTime(0), MIN2(proposedProlongation, currentPhase.maxDuration - actDuration), currentPhase.minDuration - actDuration);
176 
177 #ifdef DEBUG_TIMELOSS_CONTROL
178  std::cout << "Proposed prolongation = " << proposedProlongation << std::endl;
179 #endif
180 
181  prolong = proposedProlongation > 0;
182  if (prolong) {
183  // check again after the prolonged period (must be positive...)
184  // XXX: Can it be harmful not to return a duration of integer seconds?
185  return proposedProlongation;
186  }
187  }
188  // Don't prolong... switch to the next phase
189  myStep++;
190  assert(myStep <= (int)myPhases.size());
191  if (myStep == (int)myPhases.size()) {
192  myStep = 0;
193  }
194  MSPhaseDefinition* newPhase = myPhases[myStep];
195  //stores the time the phase started
197  // set the next event
198  return newPhase->minDuration;
199 }
200 
201 
202 /****************************************************************************/
The link has green light, may pass.
Builds detectors for microsim.
long long int SUMOTime
Definition: SUMOTime.h:43
const std::string & getState() const
Returns the state within this phase.
const LaneVector & getLanesAt(int i) const
Returns the list of lanes that are controlled by the signals at the given position.
An areal (along a single lane) detector.
Definition: MSE2Collector.h:84
std::string myProgramID
The id of the logic.
static SUMOReal _2SUMOReal(const E *const data)
converts a char-type array into the SUMOReal value described by it
Definition: TplConvert.h:297
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.
SUMOReal getLength() const
Returns the lane&#39;s length.
Definition: MSLane.h:480
Phases myPhases
The list of phases this logic uses.
SUMOTime trySwitch()
Switches to the next phase, if possible.
#define TIMELOSS_THRESHOLD
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition: MSNet.cpp:159
T MAX2(T a, T b)
Definition: StdDefs.h:70
#define TIME2STEPS(x)
Definition: SUMOTime.h:66
const std::string & getParameter(const std::string &key, const std::string &defaultValue) const
Returns the value for a given key.
SUMOTime getCurrentTimeStep() const
Returns the current simulation step.
Definition: MSNet.h:255
T MAX3(T a, T b, T c)
Definition: StdDefs.h:84
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...
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.
const std::string & getID() const
Returns the id.
Definition: Named.h:66
std::string myFile
The output file for generated detectors.
SUMOTime myLastSwitch
Stores the timestep of the last on-switched of the phase.
virtual void init(NLDetectorBuilder &nb)
Initialises the tls with information about incoming lanes.
LaneDetectorMap myLaneDetectors
A map from lanes to the corresponding lane detectors.
const MSPhaseDefinition & getCurrentPhaseDef() const
Returns the definition of the current phase.
T MIN2(T a, T b)
Definition: StdDefs.h:64
bool isGreenPhase() const
Returns whether this phase is a pure "green" phase.
SUMOTime proposeProlongation()
The returned, proposed prolongation for the green phase is oriented on the largest estimated passing ...
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:371
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: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...
const SUMOReal SUMO_const_haltingSpeed
the speed threshold at which vehicles are considered as halting
Definition: StdDefs.h:56
virtual MSE2Collector * createSingleLaneE2Detector(const std::string &id, DetectorUsage usage, MSLane *lane, SUMOReal pos, SUMOReal length, SUMOTime haltingTimeThreshold, SUMOReal haltingSpeedThreshold, SUMOReal jamDistThreshold, const std::string &vTypes)
Creates an instance of an e2 detector using the given values.
SUMOTime minDuration
The minimum duration of the phase.
void init(NLDetectorBuilder &nb)
Initializes the tls with information about incoming lanes.
#define SUMOReal
Definition: config.h:208
const std::vector< VehicleInfo > & getCurrentVehicles() const
Returns the vehicles within the area.
bool myShowDetectors
Whether the detectors shall be shown in the GUI.
SUMOTime myFreq
The frequency for aggregating detector output.
static bool gUseMesoSim
Definition: MSGlobals.h:95
The definition of a single phase of a tls logic.
Representation of a lane in the micro simulation.
Definition: MSLane.h:79