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-2018 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 /****************************************************************************/
17 // The base abstract class for SOTL logics
18 /****************************************************************************/
19 #ifndef MSSOTLTrafficLightLogic_h
20 #define MSSOTLTrafficLightLogic_h
21 
22 // ===========================================================================
23 // included modules
24 // ===========================================================================
25 #include <config.h>
26 
28 
29 #include <utils/common/SUMOTime.h>
30 #include "MSSOTLDefinitions.h"
32 #include "MSSOTLE2Sensors.h"
35 
36 
37 // ===========================================================================
38 // class declarations
39 // ===========================================================================
40 class MSPushButton;
41 
42 
43 // ===========================================================================
44 // class definitions
45 // ===========================================================================
46 
60 public:
61  // typedef int CTS;
62 
74  MSSOTLTrafficLightLogic(MSTLLogicControl& tlcontrol, const std::string& id,
75  const std::string& programID, const TrafficLightType logicType, const Phases& phases, int step,
76  SUMOTime delay,
77  const std::map<std::string, std::string>& parameters);
78 
91  MSSOTLTrafficLightLogic(MSTLLogicControl& tlcontrol, const std::string& id,
92  const std::string& programID, const TrafficLightType logicType, const Phases& phases, int step,
93  SUMOTime delay,
94  const std::map<std::string, std::string>& parameters,
95  MSSOTLSensors* sensors);
96 
99 
106  void init(NLDetectorBuilder& nb);
107 
108  /*
109  * This member implements the base operations for all SOTL logics.
110  * SOTL politics are implementated through decidePhase() member
111  * @see MSTrafficLightLogic::trySwitch
112  */
114 
115 protected:
116 
117  typedef std::map<const std::string, std::vector<MSPushButton*> > PhasePushButtons;
118  PhasePushButtons m_pushButtons;
119 
120  void logStatus();
121  /*
122  * This member has to contain the switching logic for SOTL policies
123  */
124 
125  virtual int decideNextPhase();
126 
127  virtual bool canRelease() = 0;
128 
130 
131  /*
132  * Count the number of vehicles approaching the target lanes for the given phase.
133  * If the phase in not a target phase the function member will return 0.
134  * @param[in] The target phase index
135  */
136  int countVehicles(MSPhaseDefinition phase);
137 
138  /*
139  * Every target step except the one from the current chain is checked.
140  * This is because the current chain is not eligible to be directly
141  * targeted again, it would be unfair.
142  * @return True if at least a target phase has passed the threshold for input cars-timesteps
143  */
144  bool isThresholdPassed();
145 
150  bool isPushButtonPressed();
151 
152  int getThreshold() {
153  return StringUtils::toInt(getParameter("THRESHOLD", "10"));
154  }
155 
156  double getSpeedThreshold() {
157  return StringUtils::toDouble(getParameter("THRESHOLDSPEED", "2"));
158  }
159 
161  return StringUtils::toDouble(getParameter("INSENSORSLENGTH", "100"));
162  }
163 
165  return StringUtils::toDouble(getParameter("OUTSENSORSLENGTH", "80"));
166  }
167 
168  /*
169  * Every target step except the one from the current chain is checked.
170  * This is because the current chain is not eligible to be directly
171  * targeted again, it would be unfair.
172  * @return The index of the phase with the maximum value of cars-timesteps
173  */
175 
177  return mySensors;
178  }
179 
184  return myCountSensors;
185  }
186  /*
187  * Computes how much time will pass after decideNextPhase will be executed again
188  */
190  if (getCurrentPhaseDef().isTransient()) {
191  return getCurrentPhaseDef().duration;
192 
193  }
194  return DELTA_T;
195 
196  }
197 
198 
199 private:
200  /*
201  * Pointer to the sensor logic regarding the junction controlled by this SOTLTrafficLightLogic
202  */
204 
205  /*
206  * Pointer to the sensor logic regarding the count of the vehicles that pass into a tl.
207  */
209 
210  /*
211  * When true means the class has responsibilities to intantiate and delete the SOTLSensors instance,
212  * otherwise MSSOTLTrafficLightLogic::init and MSSOTLTrafficLightLogic::~MSSOTLTrafficLightLogic have not to affect SOTLSensors instance lifecycle
213  */
215 
216  // The map to store the cars*timesteps for each target phase
217  std::map<int, SUMOTime> targetPhasesCTS;
218 
219  //The map to store the time each target phase have been checked last
220  //This helps to compute the timesteps to get the cars*timesteps value
221  std::map<int, SUMOTime> lastCheckForTargetPhase;
222 
223  //Map to store how many selection rounds have been done from the last selection of the phase
224  std::map<int, int> targetPhasesLastSelection;
225 
227  //return 2 * targetPhasesCTS.size() - 1;
228  return (int)targetPhasesCTS.size() - 1;
229  }
230 
231  /*
232  * This member keeps track which is the current steps chain, i.e.
233  * which is the last target phase executed (even if it is currently executed)
234  * (a steps chain starts always with a target phase)
235  */
237 
239  /*
240  * @brief Check for phases compliancy
241  */
242  void checkPhases();
243 
244  /*
245  * Find the first target phase and set the current step on it
246  */
247  void setToATargetPhase();
248 
249  /*
250  * This function member helps to set up the map keeping track of target phases and associated timesteps
251  */
252  void setupCTS();
253 
254  /*
255  * Updates the cars-timesteps counters for each target phase except the one belonging to the current steps chain
256  */
257  void updateCTS();
258 
259  /*
260  * To reset the cars-timesteps counter when a target phase is newly selected
261  * If phaseStep is not a target phase nothing happens
262  */
263  void resetCTS(int phaseStep);
264  /*
265  * TEST
266  */
267  void updateDecayThreshold();
268 
269  /*
270  * Traffic threshold calculation mode:
271  * 0-> cars times seconds
272  * 1-> estimated cars times seconds
273  * 2-> queue length
274  */
275  int getMode() {
276  return StringUtils::toInt(getParameter("MODE", "0"));
277  }
278  /*
279  * Decay threshold that should be used in case of penetration rate != 100%
280  * 0-> not active
281  * 1-> active
282  */
284  return StringUtils::toBool(getParameter("DECAY_THRESHOLD", "0"));
285  }
286 
287  double getDecayConstant() {
288  return StringUtils::toDouble(getParameter("DECAY_CONSTANT", "-0.001"));
289  }
290 
291 };
292 
293 #endif
294 /****************************************************************************/
Builds detectors for microsim.
long long int SUMOTime
Definition: SUMOTime.h:36
void init(NLDetectorBuilder &nb)
Initialises the tls with sensors on incoming and outgoing lanes Sensors are built in the simulation a...
std::map< const std::string, std::vector< MSPushButton * > > PhasePushButtons
SUMOTime DELTA_T
Definition: SUMOTime.cpp:35
std::map< int, SUMOTime > lastCheckForTargetPhase
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.
static bool toBool(const std::string &sData)
converts a string into the bool value described by it by calling the char-type converter ...
A class that stores and controls tls and switching of their programs.
SUMOTime duration
The duration of the phase.
A self-organizing traffic light logic.
MSSOTLE2Sensors * getCountSensors()
Return the sensors that count the passage of vehicles in and out of the tl.
static double toDouble(const std::string &sData)
converts a string into the double value described by it by calling the char-type converter ...
std::map< int, SUMOTime > targetPhasesCTS
static int toInt(const std::string &sData)
converts a string into the integer value described by it by calling the char-type converter...
std::vector< MSPhaseDefinition * > Phases
Definition of a list of phases, being the junction logic.
const MSPhaseDefinition & getCurrentPhaseDef() const
Returns the definition of the current phase.
SUMOTime trySwitch()
Switches to the next phase.
const std::string getParameter(const std::string &key, const std::string &defaultValue="") const
Returns the value for a given key.
virtual SUMOTime computeReturnTime()
virtual bool canRelease()=0
A fixed traffic light logic.
The definition of a single phase of a tls logic.
std::map< int, int > targetPhasesLastSelection
TrafficLightType
int countVehicles(MSPhaseDefinition phase)