Eclipse SUMO - Simulation of Urban MObility
MSSOTLTrafficLightLogic.h
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2013-2020 German Aerospace Center (DLR) and others.
4 // This program and the accompanying materials are made available under the
5 // terms of the Eclipse Public License 2.0 which is available at
6 // https://www.eclipse.org/legal/epl-2.0/
7 // This Source Code may also be made available under the following Secondary
8 // Licenses when the conditions for such availability set forth in the Eclipse
9 // Public License 2.0 are satisfied: GNU General Public License, version 2
10 // or later which is available at
11 // https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
12 // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
13 /****************************************************************************/
20 // The base abstract class for SOTL logics
21 /****************************************************************************/
22 #pragma once
23 #include <config.h>
24 
26 
27 #include <utils/common/SUMOTime.h>
28 #include "MSSOTLDefinitions.h"
30 #include "MSSOTLE2Sensors.h"
33 
34 
35 // ===========================================================================
36 // class declarations
37 // ===========================================================================
38 class MSPushButton;
39 
40 
41 // ===========================================================================
42 // class definitions
43 // ===========================================================================
44 
58 public:
59  // typedef int CTS;
60 
72  MSSOTLTrafficLightLogic(MSTLLogicControl& tlcontrol, const std::string& id,
73  const std::string& programID, const TrafficLightType logicType, const Phases& phases, int step,
74  SUMOTime delay,
75  const std::map<std::string, std::string>& parameters);
76 
89  MSSOTLTrafficLightLogic(MSTLLogicControl& tlcontrol, const std::string& id,
90  const std::string& programID, const TrafficLightType logicType, const Phases& phases, int step,
91  SUMOTime delay,
92  const std::map<std::string, std::string>& parameters,
93  MSSOTLSensors* sensors);
94 
97 
104  void init(NLDetectorBuilder& nb);
105 
106  /*
107  * This member implements the base operations for all SOTL logics.
108  * SOTL politics are implementated through decidePhase() member
109  * @see MSTrafficLightLogic::trySwitch
110  */
112 
113 protected:
114 
115  typedef std::map<const std::string, std::vector<MSPushButton*> > PhasePushButtons;
117 
118  void logStatus();
119  /*
120  * This member has to contain the switching logic for SOTL policies
121  */
122 
123  virtual int decideNextPhase();
124 
125  virtual bool canRelease() = 0;
126 
128 
129  /*
130  * Count the number of vehicles approaching the target lanes for the given phase.
131  * If the phase in not a target phase the function member will return 0.
132  * @param[in] The target phase index
133  */
134  int countVehicles(MSPhaseDefinition phase);
135 
136  /*
137  * Every target step except the one from the current chain is checked.
138  * This is because the current chain is not eligible to be directly
139  * targeted again, it would be unfair.
140  * @return True if at least a target phase has passed the threshold for input cars-timesteps
141  */
142  bool isThresholdPassed();
143 
148  bool isPushButtonPressed();
149 
150  int getThreshold() {
151  return StringUtils::toInt(getParameter("THRESHOLD", "10"));
152  }
153 
154  double getSpeedThreshold() {
155  return StringUtils::toDouble(getParameter("THRESHOLDSPEED", "2"));
156  }
157 
159  return StringUtils::toDouble(getParameter("INSENSORSLENGTH", "100"));
160  }
161 
163  return StringUtils::toDouble(getParameter("OUTSENSORSLENGTH", "80"));
164  }
165 
166  /*
167  * Every target step except the one from the current chain is checked.
168  * This is because the current chain is not eligible to be directly
169  * targeted again, it would be unfair.
170  * @return The index of the phase with the maximum value of cars-timesteps
171  */
173 
175  return mySensors;
176  }
177 
182  return myCountSensors;
183  }
184  /*
185  * Computes how much time will pass after decideNextPhase will be executed again
186  */
188  if (getCurrentPhaseDef().isTransient()) {
189  return getCurrentPhaseDef().duration;
190 
191  }
192  return DELTA_T;
193 
194  }
195 
196 
197 private:
198  /*
199  * Pointer to the sensor logic regarding the junction controlled by this SOTLTrafficLightLogic
200  */
202 
203  /*
204  * Pointer to the sensor logic regarding the count of the vehicles that pass into a tl.
205  */
207 
208  /*
209  * When true means the class has responsibilities to intantiate and delete the SOTLSensors instance,
210  * otherwise MSSOTLTrafficLightLogic::init and MSSOTLTrafficLightLogic::~MSSOTLTrafficLightLogic have not to affect SOTLSensors instance lifecycle
211  */
213 
214  // The map to store the cars*timesteps for each target phase
215  std::map<int, SUMOTime> targetPhasesCTS;
216 
217  //The map to store the time each target phase have been checked last
218  //This helps to compute the timesteps to get the cars*timesteps value
219  std::map<int, SUMOTime> lastCheckForTargetPhase;
220 
221  //Map to store how many selection rounds have been done from the last selection of the phase
222  std::map<int, int> targetPhasesLastSelection;
223 
225  //return 2 * targetPhasesCTS.size() - 1;
226  return (int)targetPhasesCTS.size() - 1;
227  }
228 
229  /*
230  * This member keeps track which is the current steps chain, i.e.
231  * which is the last target phase executed (even if it is currently executed)
232  * (a steps chain starts always with a target phase)
233  */
235 
237  /*
238  * @brief Check for phases compliancy
239  */
240  void checkPhases();
241 
242  /*
243  * Find the first target phase and set the current step on it
244  */
245  void setToATargetPhase();
246 
247  /*
248  * This function member helps to set up the map keeping track of target phases and associated timesteps
249  */
250  void setupCTS();
251 
252  /*
253  * Updates the cars-timesteps counters for each target phase except the one belonging to the current steps chain
254  */
255  void updateCTS();
256 
257  /*
258  * To reset the cars-timesteps counter when a target phase is newly selected
259  * If phaseStep is not a target phase nothing happens
260  */
261  void resetCTS(int phaseStep);
262  /*
263  * TEST
264  */
265  void updateDecayThreshold();
266 
267  /*
268  * Traffic threshold calculation mode:
269  * 0-> cars times seconds
270  * 1-> estimated cars times seconds
271  * 2-> queue length
272  */
273  int getMode() {
274  return StringUtils::toInt(getParameter("MODE", "0"));
275  }
276  /*
277  * Decay threshold that should be used in case of penetration rate != 100%
278  * 0-> not active
279  * 1-> active
280  */
282  return StringUtils::toBool(getParameter("DECAY_THRESHOLD", "0"));
283  }
284 
285  double getDecayConstant() {
286  return StringUtils::toDouble(getParameter("DECAY_CONSTANT", "-0.001"));
287  }
288 
289 };
SUMOTime DELTA_T
Definition: SUMOTime.cpp:37
long long int SUMOTime
Definition: SUMOTime.h:31
TrafficLightType
The definition of a single phase of a tls logic.
SUMOTime duration
The duration of the phase.
A fixed traffic light logic.
const MSPhaseDefinition & getCurrentPhaseDef() const
Returns the definition of the current phase.
A self-organizing traffic light logic.
std::map< int, SUMOTime > lastCheckForTargetPhase
SUMOTime trySwitch()
Switches to the next phase.
MSSOTLE2Sensors * getCountSensors()
Return the sensors that count the passage of vehicles in and out of the tl.
virtual bool canRelease()=0
std::map< const std::string, std::vector< MSPushButton * > > PhasePushButtons
int countVehicles(MSPhaseDefinition phase)
MSSOTLTrafficLightLogic(MSTLLogicControl &tlcontrol, const std::string &id, const std::string &programID, const TrafficLightType logicType, const Phases &phases, int step, SUMOTime delay, const std::map< std::string, std::string > &parameters)
Constructor without sensors passed.
std::map< int, SUMOTime > targetPhasesCTS
virtual SUMOTime computeReturnTime()
std::map< int, int > targetPhasesLastSelection
void init(NLDetectorBuilder &nb)
Initialises the tls with sensors on incoming and outgoing lanes Sensors are built in the simulation a...
A class that stores and controls tls and switching of their programs.
std::vector< MSPhaseDefinition * > Phases
Definition of a list of phases, being the junction logic.
Builds detectors for microsim.
const std::string getParameter(const std::string &key, const std::string defaultValue="") const
Returns the value for a given key.
static double toDouble(const std::string &sData)
converts a string into the double value described by it by calling the char-type converter
static int toInt(const std::string &sData)
converts a string into the integer value described by it by calling the char-type converter,...
static bool toBool(const std::string &sData)
converts a string into the bool value described by it by calling the char-type converter