Eclipse 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-2019 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 /****************************************************************************/
14 // An actuated traffic light logic based on time delay of approaching vehicles
15 /****************************************************************************/
16 
17 
18 // ===========================================================================
19 // included modules
20 // ===========================================================================
21 #include <config.h>
22 
23 #include <cassert>
24 #include <vector>
25 #include <microsim/MSGlobals.h>
26 #include <microsim/MSNet.h>
30 #include <microsim/MSLane.h>
33 
34 #define INVALID_POSITION std::numeric_limits<double>::max()
35 
36 // ===========================================================================
37 // parameter defaults definitions
38 // ===========================================================================
39 
40 //#define DEBUG_TIMELOSS_CONTROL
41 
42 // ===========================================================================
43 // method definitions
44 // ===========================================================================
46  const std::string& id, const std::string& programID,
47  const Phases& phases,
48  int step, SUMOTime delay,
49  const std::map<std::string, std::string>& parameter,
50  const std::string& basePath) :
51  MSSimpleTrafficLightLogic(tlcontrol, id, programID, TLTYPE_DELAYBASED, phases, step, delay, parameter) {
52 #ifdef DEBUG_TIMELOSS_CONTROL
53  std::cout << "Building delay based tls logic '" << id << "'" << std::endl;
54 #endif
55  myShowDetectors = StringUtils::toBool(getParameter("show-detectors", "false"));
56  myDetectionRange = StringUtils::toDouble(getParameter("detectorRange", toString(OptionsCont::getOptions().getFloat("tls.delay_based.detector-range"))));
58  myFile = FileHelpers::checkForRelativity(getParameter("file", "NUL"), basePath);
60  myVehicleTypes = getParameter("vTypes", "");
61 #ifdef DEBUG_TIMELOSS_CONTROL
62  std::cout << "show-detectors: " << myShowDetectors
63  << " detectorRange: " << myDetectionRange
64  << " minTimeLoss: " << myTimeLossThreshold
65  << " file: " << myFile
66  << " freq: " << myFreq
67  << " vTypes: " << myVehicleTypes
68  << std::endl;
69 #endif
70 }
71 
72 
73 void
76  assert(myLanes.size() > 0);
77  LaneVectorVector::const_iterator i2;
78  LaneVector::const_iterator i;
79  // build the E2 detectors
80  for (i2 = myLanes.begin(); i2 != myLanes.end(); ++i2) {
81  const LaneVector& lanes = *i2;
82  for (i = lanes.begin(); i != lanes.end(); i++) {
83  MSLane* lane = (*i);
84  if (noVehicles(lane->getPermissions())) {
85  // do not build detectors on green verges or sidewalks
86  continue;
87  }
88  // Build the detectors and register them at the detector control
89  std::string id = "TLS" + myID + "_" + myProgramID + "_E2CollectorOn_" + lane->getID();
90  if (myLaneDetectors.find(lane) == myLaneDetectors.end()) {
93  }
94  }
95  }
96 }
97 
98 
99 
101 
102 // ------------ Switching and setting current rows
103 
104 
105 SUMOTime
106 MSDelayBasedTrafficLightLogic::proposeProlongation(const SUMOTime actDuration, const SUMOTime maxDuration, bool& othersEmpty) {
107 #ifdef DEBUG_TIMELOSS_CONTROL
108  std::cout << "\n" << SIMTIME << " MSDelayBasedTrafficLightLogic::proposeProlongation() for TLS '" << this->getID() << "' (current phase = " << myStep << ")" << std::endl;
109 #endif
110  SUMOTime prolongation = 0;
111  const std::string& state = getCurrentPhaseDef().getState();
112  // iterate over green lanes, eventually increase the proposed prolongationTime to the estimated passing time for each lane.
113  for (int i = 0; i < (int) state.size(); i++) {
114  // this lane index corresponds to a non-green time
115  bool igreen = state[i] == LINKSTATE_TL_GREEN_MAJOR || state[i] == LINKSTATE_TL_GREEN_MINOR;
116  const std::vector<MSLane*>& lanes = getLanesAt(i);
117  for (LaneVector::const_iterator j = lanes.begin(); j != lanes.end(); j++) {
118  LaneDetectorMap::iterator i = myLaneDetectors.find(*j);
119  if (i == myLaneDetectors.end()) {
120 #ifdef DEBUG_TIMELOSS_CONTROL
121  // no detector for this lane!? maybe noVehicles allowed
122  std::cout << "no detector on lane '" << (*j)->getID() << std::endl;
123 #endif
124  continue;
125  }
126  MSE2Collector* detector = static_cast<MSE2Collector* >(i->second);
127  const std::vector<MSE2Collector::VehicleInfo*> vehInfos = detector->getCurrentVehicles();
128 #ifdef DEBUG_TIMELOSS_CONTROL
129  int nrVehs = 0; // count vehicles on detector
130 #endif
131  if (igreen) {
132  // green phase
133  for (std::vector<MSE2Collector::VehicleInfo*>::const_iterator ivp = vehInfos.begin(); ivp != vehInfos.end(); ++ivp) {
134  MSE2Collector::VehicleInfo* iv = *ivp;
136  const SUMOTime estimatedTimeToJunction = TIME2STEPS((iv->distToDetectorEnd) / (*j)->getSpeedLimit());
137  if (actDuration + estimatedTimeToJunction <= maxDuration) {
138  // only prolong if vehicle has a chance to pass until max duration is reached
139  prolongation = MAX2(prolongation, estimatedTimeToJunction);
140  }
141 #ifdef DEBUG_TIMELOSS_CONTROL
142  nrVehs++;
143 #endif
144 
145 #ifdef DEBUG_TIMELOSS_CONTROL
146  std::cout << "vehicle '" << iv->id << "' with accumulated timeloss: " << iv->accumulatedTimeLoss
147  << "\nestimated passing time: " << estimatedTimeToJunction << std::endl;
148  } else {
149  std::string reason = iv->accumulatedTimeLoss <= myTimeLossThreshold ? " (time loss below threshold)" : " (front already left detector)";
150  std::cout << "disregarded: (vehicle '" << iv->id << "' with accumulated timeloss " << iv->accumulatedTimeLoss << ")" << reason << std::endl;
151 #endif
152  }
153  }
154  } else {
155  // non-green phase
156  if (vehInfos.size() > 0) {
157  // here is a car on a non-green approach
158  othersEmpty = false;
159  if (actDuration >= getCurrentPhaseDef().maxDuration) {
160 #ifdef DEBUG_TIMELOSS_CONTROL
161  std::cout << "Actual duration exceeds maxDuration and a vehicle is on concurrent approach: " << nrVehs << std::endl;
162 #endif
163  // don't prolong
164  return 0;
165  }
166  break;
167  }
168 #ifdef DEBUG_TIMELOSS_CONTROL
169  std::cout << "Number of current vehicles on detector: " << nrVehs << std::endl;
170 #endif
171  }
172  }
173  }
174 #ifdef DEBUG_TIMELOSS_CONTROL
175  std::cout << "Proposed prolongation (maximal estimated passing time): " << prolongation << std::endl; // debug
176 #endif
177  return prolongation;
178 }
179 
180 
181 SUMOTime
183  /* check if the actual phase should be prolonged */
184  const MSPhaseDefinition& currentPhase = getCurrentPhaseDef();
185  // time since last switch
186  const SUMOTime actDuration = MSNet::getInstance()->getCurrentTimeStep() - currentPhase.myLastSwitch;
187 
188 #ifdef DEBUG_TIMELOSS_CONTROL
189  std::cout << "last switch = " << currentPhase.myLastSwitch
190  << "\nactDuration = " << actDuration
191  << "\nmaxDuration = " << currentPhase.maxDuration
192  << std::endl;
193 #endif
194 
195  // flag whether to prolong or not
196  if (currentPhase.isGreenPhase() && !MSGlobals::gUseMesoSim) {
197  bool othersEmpty = true; // whether no vehicles are present on concurrent approaches
198  SUMOTime proposedProlongation = proposeProlongation(actDuration, currentPhase.maxDuration, othersEmpty);
199 
200 #ifdef DEBUG_TIMELOSS_CONTROL
201  std::cout << "othersEmpty = " << othersEmpty
202  << std::endl;
203 #endif
204 
205  // keep this phase a little longer?
206  bool prolong = othersEmpty || actDuration < currentPhase.maxDuration;
207  // assure minimal duration
208  proposedProlongation = MAX3(SUMOTime(0), proposedProlongation, currentPhase.minDuration - actDuration);
209  if (othersEmpty) {
210  // prolong by one second if no vehicles on other approaches
211  proposedProlongation = MAX2(proposedProlongation, TIME2STEPS(1.));
212  } else {
213  // vehicles are present on other approaches -> prolong no further than the max green time
214  proposedProlongation = MIN2(proposedProlongation, MAX2(SUMOTime(0), currentPhase.maxDuration - actDuration));
215  }
216 
217 #ifdef DEBUG_TIMELOSS_CONTROL
218  std::cout << "Proposed prolongation = " << proposedProlongation << std::endl;
219 #endif
220 
221  prolong = proposedProlongation > 0;
222  if (prolong) {
223  // check again after the prolonged period (must be positive...)
224  // XXX: Can it be harmful not to return a duration of integer seconds?
225  return proposedProlongation;
226  }
227  }
228  // Don't prolong... switch to the next phase
229  myStep++;
230  assert(myStep <= (int)myPhases.size());
231  if (myStep == (int)myPhases.size()) {
232  myStep = 0;
233  }
234  MSPhaseDefinition* newPhase = myPhases[myStep];
235  //stores the time the phase started
237  // set the next event
238  return newPhase->minDuration;
239 }
240 
241 
242 /****************************************************************************/
243 
MSDelayBasedTrafficLightLogic::proposeProlongation
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 ...
Definition: MSDelayBasedTrafficLightLogic.cpp:106
NLDetectorBuilder::createE2Detector
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()
Definition: NLDetectorBuilder.cpp:397
MSTrafficLightLogic::myLanes
LaneVectorVector myLanes
The list of LaneVectors; each vector contains the incoming lanes that belong to the same link index.
Definition: MSTrafficLightLogic.h:420
MIN2
T MIN2(T a, T b)
Definition: StdDefs.h:73
MSPhaseDefinition::myLastSwitch
SUMOTime myLastSwitch
Stores the timestep of the last on-switched of the phase.
Definition: MSPhaseDefinition.h:82
MSNet::getDetectorControl
MSDetectorControl & getDetectorControl()
Returns the detector control.
Definition: MSNet.h:399
StringUtils::toBool
static bool toBool(const std::string &sData)
converts a string into the bool value described by it by calling the char-type converter
Definition: StringUtils.cpp:374
MSNet.h
MSLane
Representation of a lane in the micro simulation.
Definition: MSLane.h:82
MSDetectorControl.h
StringUtils::toDouble
static double toDouble(const std::string &sData)
converts a string into the double value described by it by calling the char-type converter
Definition: StringUtils.cpp:345
MSTrafficLightLogic::Phases
std::vector< MSPhaseDefinition * > Phases
Definition of a list of phases, being the junction logic.
Definition: MSTrafficLightLogic.h:61
MSDelayBasedTrafficLightLogic::trySwitch
SUMOTime trySwitch()
Switches to the next phase, if possible.
Definition: MSDelayBasedTrafficLightLogic.cpp:182
MSLane::getPermissions
SVCPermissions getPermissions() const
Returns the vehicle class permissions for this lane.
Definition: MSLane.h:548
MSDelayBasedTrafficLightLogic::myShowDetectors
bool myShowDetectors
Whether the detectors shall be shown in the GUI.
Definition: MSDelayBasedTrafficLightLogic.h:121
SUMOTime
long long int SUMOTime
Definition: SUMOTime.h:34
MSSimpleTrafficLightLogic::getCurrentPhaseDef
const MSPhaseDefinition & getCurrentPhaseDef() const
Returns the definition of the current phase.
Definition: MSSimpleTrafficLightLogic.cpp:129
MSDelayBasedTrafficLightLogic::myTimeLossThreshold
double myTimeLossThreshold
Definition: MSDelayBasedTrafficLightLogic.h:129
OptionsCont::getOptions
static OptionsCont & getOptions()
Retrieves the options.
Definition: OptionsCont.cpp:57
LINKSTATE_TL_GREEN_MINOR
The link has green light, has to brake.
Definition: SUMOXMLDefinitions.h:1141
MSE2Collector
An areal detector corresponding to a sequence of consecutive lanes.
Definition: MSE2Collector.h:81
FileHelpers::checkForRelativity
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.
Definition: FileHelpers.cpp:152
MSGlobals::gUseMesoSim
static bool gUseMesoSim
Definition: MSGlobals.h:90
MSE2Collector::getCurrentVehicles
std::vector< VehicleInfo * > getCurrentVehicles() const
Returns the VehicleInfos for the vehicles currently on the detector.
Definition: MSE2Collector.cpp:1425
MAX3
T MAX3(T a, T b, T c)
Definition: StdDefs.h:93
LINKSTATE_TL_GREEN_MAJOR
The link has green light, may pass.
Definition: SUMOXMLDefinitions.h:1139
MSPhaseDefinition::maxDuration
SUMOTime maxDuration
The maximum duration of the phase.
Definition: MSPhaseDefinition.h:79
MSSimpleTrafficLightLogic.h
Parameterised::getParameter
const std::string getParameter(const std::string &key, const std::string &defaultValue="") const
Returns the value for a given key.
Definition: Parameterised.cpp:72
MSDelayBasedTrafficLightLogic::~MSDelayBasedTrafficLightLogic
~MSDelayBasedTrafficLightLogic()
Destructor.
Definition: MSDelayBasedTrafficLightLogic.cpp:100
MSDelayBasedTrafficLightLogic::myFile
std::string myFile
The output file for generated detectors.
Definition: MSDelayBasedTrafficLightLogic.h:132
MAX2
T MAX2(T a, T b)
Definition: StdDefs.h:79
INVALID_POSITION
#define INVALID_POSITION
Definition: MSDelayBasedTrafficLightLogic.cpp:34
MSE2Collector::VehicleInfo::id
std::string id
vehicle's ID
Definition: MSE2Collector.h:111
MSTrafficLightLogic::LaneVector
std::vector< MSLane * > LaneVector
Definition of the list of arrival lanes subjected to this tls.
Definition: MSTrafficLightLogic.h:70
SIMTIME
#define SIMTIME
Definition: SUMOTime.h:63
TIME2STEPS
#define TIME2STEPS(x)
Definition: SUMOTime.h:58
MSDelayBasedTrafficLightLogic::MSDelayBasedTrafficLightLogic
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.
Definition: MSDelayBasedTrafficLightLogic.cpp:45
MSPhaseDefinition::getState
const std::string & getState() const
Returns the state within this phase.
Definition: MSPhaseDefinition.h:199
MSNet::getCurrentTimeStep
SUMOTime getCurrentTimeStep() const
Returns the current simulation step.
Definition: MSNet.h:283
MSE2Collector::VehicleInfo
A VehicleInfo stores values that are tracked for the individual vehicles on the detector,...
Definition: MSE2Collector.h:87
MSLane::getLength
double getLength() const
Returns the lane's length.
Definition: MSLane.h:540
NLDetectorBuilder.h
MSSimpleTrafficLightLogic
A fixed traffic light logic.
Definition: MSSimpleTrafficLightLogic.h:54
MSPhaseDefinition::isGreenPhase
bool isGreenPhase() const
Returns whether this phase is a pure "green" phase.
Definition: MSPhaseDefinition.h:230
MSGlobals.h
MSDelayBasedTrafficLightLogic::myFreq
SUMOTime myFreq
The frequency for aggregating detector output.
Definition: MSDelayBasedTrafficLightLogic.h:135
MSE2Collector::VehicleInfo::accumulatedTimeLoss
double accumulatedTimeLoss
Accumulated time loss that this vehicle suffered since it entered the detector.
Definition: MSE2Collector.h:138
MSE2Collector::VehicleInfo::distToDetectorEnd
double distToDetectorEnd
Distance left till the detector end after the last integration step (may become negative if the vehic...
Definition: MSE2Collector.h:134
MSDelayBasedTrafficLightLogic.h
MSDelayBasedTrafficLightLogic::myDetectionRange
double myDetectionRange
Range of the connected detector, which provides the information on approaching vehicles.
Definition: MSDelayBasedTrafficLightLogic.h:124
MSTrafficLightLogic::myProgramID
const std::string myProgramID
The id of the logic.
Definition: MSTrafficLightLogic.h:411
DU_TL_CONTROL
Definition: MSDetectorFileOutput.h:53
toString
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition: ToString.h:47
StringUtils.h
MSNet::getInstance
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition: MSNet.cpp:167
TLTYPE_DELAYBASED
Definition: SUMOXMLDefinitions.h:1202
MSDelayBasedTrafficLightLogic::init
void init(NLDetectorBuilder &nb)
Initializes the tls with information about incoming lanes.
Definition: MSDelayBasedTrafficLightLogic.cpp:74
MSDelayBasedTrafficLightLogic::myVehicleTypes
std::string myVehicleTypes
Whether detector output separates by vType.
Definition: MSDelayBasedTrafficLightLogic.h:138
MSSimpleTrafficLightLogic::myPhases
Phases myPhases
The list of phases this logic uses.
Definition: MSSimpleTrafficLightLogic.h:198
noVehicles
bool noVehicles(SVCPermissions permissions)
Returns whether an edge with the given permission forbids vehicles.
Definition: SUMOVehicleClass.cpp:387
MSDelayBasedTrafficLightLogic::myLaneDetectors
LaneDetectorMap myLaneDetectors
A map from lanes to the corresponding lane detectors.
Definition: MSDelayBasedTrafficLightLogic.h:118
MSDetectorControl::add
void add(SumoXMLTag type, MSDetectorFileOutput *d, const std::string &device, SUMOTime splInterval, SUMOTime begin=-1)
Adds a detector/output combination into the containers.
Definition: MSDetectorControl.cpp:63
config.h
MSTLLogicControl
A class that stores and controls tls and switching of their programs.
Definition: MSTLLogicControl.h:59
MSLane.h
MSPhaseDefinition
The definition of a single phase of a tls logic.
Definition: MSPhaseDefinition.h:51
MSSimpleTrafficLightLogic::myStep
int myStep
The current step.
Definition: MSSimpleTrafficLightLogic.h:201
MSTrafficLightLogic::init
virtual void init(NLDetectorBuilder &nb)
Initialises the tls with information about incoming lanes.
Definition: MSTrafficLightLogic.cpp:115
Named::myID
std::string myID
The name of the object.
Definition: Named.h:133
Named::getID
const std::string & getID() const
Returns the id.
Definition: Named.h:76
SUMO_TAG_E2DETECTOR
an e2 detector
Definition: SUMOXMLDefinitions.h:67
NLDetectorBuilder
Builds detectors for microsim.
Definition: NLDetectorBuilder.h:55
MSPhaseDefinition::minDuration
SUMOTime minDuration
The minimum duration of the phase.
Definition: MSPhaseDefinition.h:76
MSTrafficLightLogic::getLanesAt
const LaneVector & getLanesAt(int i) const
Returns the list of lanes that are controlled by the signals at the given position.
Definition: MSTrafficLightLogic.h:191