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 /****************************************************************************/
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", toString(OptionsCont::getOptions().getFloat("tls.delay_based.detector-range"))));
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 
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:107
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:399
MSTrafficLightLogic::myLanes
LaneVectorVector myLanes
The list of LaneVectors; each vector contains the incoming lanes that belong to the same link index.
Definition: MSTrafficLightLogic.h:418
MIN2
T MIN2(T a, T b)
Definition: StdDefs.h:74
MSPhaseDefinition::myLastSwitch
SUMOTime myLastSwitch
Stores the timestep of the last on-switched of the phase.
Definition: MSPhaseDefinition.h:83
MSNet::getDetectorControl
MSDetectorControl & getDetectorControl()
Returns the detector control.
Definition: MSNet.h:400
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:342
MSNet.h
MSLane
Representation of a lane in the micro simulation.
Definition: MSLane.h:83
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:313
MSTrafficLightLogic::Phases
std::vector< MSPhaseDefinition * > Phases
Definition of a list of phases, being the junction logic.
Definition: MSTrafficLightLogic.h:62
MSDelayBasedTrafficLightLogic::trySwitch
SUMOTime trySwitch()
Switches to the next phase, if possible.
Definition: MSDelayBasedTrafficLightLogic.cpp:183
MSLane::getPermissions
SVCPermissions getPermissions() const
Returns the vehicle class permissions for this lane.
Definition: MSLane.h:549
MSDelayBasedTrafficLightLogic::myShowDetectors
bool myShowDetectors
Whether the detectors shall be shown in the GUI.
Definition: MSDelayBasedTrafficLightLogic.h:122
SUMOTime
long long int SUMOTime
Definition: SUMOTime.h:35
MSSimpleTrafficLightLogic::getCurrentPhaseDef
const MSPhaseDefinition & getCurrentPhaseDef() const
Returns the definition of the current phase.
Definition: MSSimpleTrafficLightLogic.cpp:130
MSDelayBasedTrafficLightLogic::myTimeLossThreshold
double myTimeLossThreshold
Definition: MSDelayBasedTrafficLightLogic.h:130
OptionsCont::getOptions
static OptionsCont & getOptions()
Retrieves the options.
Definition: OptionsCont.cpp:58
LINKSTATE_TL_GREEN_MINOR
The link has green light, has to brake.
Definition: SUMOXMLDefinitions.h:1136
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:143
MSGlobals::gUseMesoSim
static bool gUseMesoSim
Definition: MSGlobals.h:91
MSE2Collector::getCurrentVehicles
std::vector< VehicleInfo * > getCurrentVehicles() const
Returns the VehicleInfos for the vehicles currently on the detector.
Definition: MSE2Collector.cpp:1413
MAX3
T MAX3(T a, T b, T c)
Definition: StdDefs.h:94
LINKSTATE_TL_GREEN_MAJOR
The link has green light, may pass.
Definition: SUMOXMLDefinitions.h:1134
MSPhaseDefinition::maxDuration
SUMOTime maxDuration
The maximum duration of the phase.
Definition: MSPhaseDefinition.h:80
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:71
MSDelayBasedTrafficLightLogic::~MSDelayBasedTrafficLightLogic
~MSDelayBasedTrafficLightLogic()
Destructor.
Definition: MSDelayBasedTrafficLightLogic.cpp:101
MSDelayBasedTrafficLightLogic::myFile
std::string myFile
The output file for generated detectors.
Definition: MSDelayBasedTrafficLightLogic.h:133
MAX2
T MAX2(T a, T b)
Definition: StdDefs.h:80
INVALID_POSITION
#define INVALID_POSITION
Definition: MSDelayBasedTrafficLightLogic.cpp:35
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:71
SIMTIME
#define SIMTIME
Definition: SUMOTime.h:64
TIME2STEPS
#define TIME2STEPS(x)
Definition: SUMOTime.h:59
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:46
MSPhaseDefinition::getState
const std::string & getState() const
Returns the state within this phase.
Definition: MSPhaseDefinition.h:200
MSNet::getCurrentTimeStep
SUMOTime getCurrentTimeStep() const
Returns the current simulation step.
Definition: MSNet.h:284
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:541
NLDetectorBuilder.h
MSSimpleTrafficLightLogic
A fixed traffic light logic.
Definition: MSSimpleTrafficLightLogic.h:55
MSPhaseDefinition::isGreenPhase
bool isGreenPhase() const
Returns whether this phase is a pure "green" phase.
Definition: MSPhaseDefinition.h:231
MSGlobals.h
MSDelayBasedTrafficLightLogic::myFreq
SUMOTime myFreq
The frequency for aggregating detector output.
Definition: MSDelayBasedTrafficLightLogic.h:136
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:125
MSTrafficLightLogic::myProgramID
const std::string myProgramID
The id of the logic.
Definition: MSTrafficLightLogic.h:409
DU_TL_CONTROL
Definition: MSDetectorFileOutput.h:54
toString
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition: ToString.h:48
StringUtils.h
MSNet::getInstance
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition: MSNet.cpp:168
TLTYPE_DELAYBASED
Definition: SUMOXMLDefinitions.h:1197
MSDelayBasedTrafficLightLogic::init
void init(NLDetectorBuilder &nb)
Initializes the tls with information about incoming lanes.
Definition: MSDelayBasedTrafficLightLogic.cpp:75
MSDelayBasedTrafficLightLogic::myVehicleTypes
std::string myVehicleTypes
Whether detector output separates by vType.
Definition: MSDelayBasedTrafficLightLogic.h:139
MSSimpleTrafficLightLogic::myPhases
Phases myPhases
The list of phases this logic uses.
Definition: MSSimpleTrafficLightLogic.h:199
noVehicles
bool noVehicles(SVCPermissions permissions)
Returns whether an edge with the given permission forbids vehicles.
Definition: SUMOVehicleClass.cpp:388
MSDelayBasedTrafficLightLogic::myLaneDetectors
LaneDetectorMap myLaneDetectors
A map from lanes to the corresponding lane detectors.
Definition: MSDelayBasedTrafficLightLogic.h:119
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:64
config.h
MSTLLogicControl
A class that stores and controls tls and switching of their programs.
Definition: MSTLLogicControl.h:60
MSLane.h
MSPhaseDefinition
The definition of a single phase of a tls logic.
Definition: MSPhaseDefinition.h:52
MSSimpleTrafficLightLogic::myStep
int myStep
The current step.
Definition: MSSimpleTrafficLightLogic.h:202
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:134
Named::getID
const std::string & getID() const
Returns the id.
Definition: Named.h:77
SUMO_TAG_E2DETECTOR
an e2 detector
Definition: SUMOXMLDefinitions.h:68
NLDetectorBuilder
Builds detectors for microsim.
Definition: NLDetectorBuilder.h:56
MSPhaseDefinition::minDuration
SUMOTime minDuration
The minimum duration of the phase.
Definition: MSPhaseDefinition.h:77
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:189