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-2017 German Aerospace Center (DLR) and others.
4 /****************************************************************************/
5 //
6 // This program and the accompanying materials
7 // are made available under the terms of the Eclipse Public License v2.0
8 // which accompanies this distribution, and is available at
9 // http://www.eclipse.org/legal/epl-v20.html
10 //
11 /****************************************************************************/
19 // The base abstract class for SOTL logics
20 /****************************************************************************/
21 #ifndef MSSOTLTrafficLightLogic_h
22 #define MSSOTLTrafficLightLogic_h
23 
24 // ===========================================================================
25 // included modules
26 // ===========================================================================
27 #ifdef _MSC_VER
28 #include <windows_config.h>
29 #else
30 #include <config.h>
31 #endif
32 
34 
35 #include <utils/common/SUMOTime.h>
36 #include "MSSOTLDefinitions.h"
38 #include "MSSOTLE2Sensors.h"
41 
42 
43 // ===========================================================================
44 // class declarations
45 // ===========================================================================
46 class MSPushButton;
47 
48 
49 // ===========================================================================
50 // class definitions
51 // ===========================================================================
52 
66 public:
67  // typedef int CTS;
68 
79  MSSOTLTrafficLightLogic(MSTLLogicControl& tlcontrol, const std::string& id,
80  const std::string& subid, const Phases& phases, int step,
81  SUMOTime delay,
82  const std::map<std::string, std::string>& parameters);
83 
95  MSSOTLTrafficLightLogic(MSTLLogicControl& tlcontrol, const std::string& id,
96  const std::string& subid, const Phases& phases, int step,
97  SUMOTime delay,
98  const std::map<std::string, std::string>& parameters,
99  MSSOTLSensors* sensors);
100 
103 
110  void init(NLDetectorBuilder& nb) throw(ProcessError);
111 
112  /*
113  * This member implements the base operations for all SOTL logics.
114  * SOTL politics are implementated through decidePhase() member
115  * @see MSTrafficLightLogic::trySwitch
116  */
118 
119 protected:
120 
121  typedef std::map<const std::string, std::vector<MSPushButton*> > PhasePushButtons;
122  PhasePushButtons m_pushButtons;
123 
124  void logStatus();
125  /*
126  * This member has to contain the switching logic for SOTL policies
127  */
128 
129  virtual int decideNextPhase();
130 
131  virtual bool canRelease() = 0;
132 
134 
135  /*
136  * Count the number of vehicles approaching the target lanes for the given phase.
137  * If the phase in not a target phase the function member will return 0.
138  * @param[in] The target phase index
139  */
140  int countVehicles(MSPhaseDefinition phase);
141 
142  /*
143  * Every target step except the one from the current chain is checked.
144  * This is because the current chain is not eligible to be directly
145  * targeted again, it would be unfair.
146  * @return True if at least a target phase has passed the threshold for input cars-timesteps
147  */
148  bool isThresholdPassed();
149 
154  bool isPushButtonPressed();
155 
156  int getThreshold() {
157  return TplConvert::_2int(getParameter("THRESHOLD", "10").c_str());
158  }
159 
160  double getSpeedThreshold() {
161  return TplConvert::_2double(getParameter("THRESHOLDSPEED", "2").c_str());
162  }
163 
165  return TplConvert::_2double(getParameter("INSENSORSLENGTH", "100").c_str());
166  }
167 
169  return TplConvert::_2double(getParameter("OUTSENSORSLENGTH", "80").c_str());
170  }
171 
172  /*
173  * Every target step except the one from the current chain is checked.
174  * This is because the current chain is not eligible to be directly
175  * targeted again, it would be unfair.
176  * @return The index of the phase with the maximum value of cars-timesteps
177  */
179 
181  return mySensors;
182  }
183 
188  return myCountSensors;
189  }
190  /*
191  * Computes how much time will pass after decideNextPhase will be executed again
192  */
194  if (getCurrentPhaseDef().isTransient()) {
195  return getCurrentPhaseDef().duration;
196 
197  }
198  return DELTA_T;
199 
200  }
201 
202 
203 private:
204  /*
205  * Pointer to the sensor logic regarding the junction controlled by this SOTLTrafficLightLogic
206  */
208 
209  /*
210  * Pointer to the sensor logic regarding the count of the vehicles that pass into a tl.
211  */
213 
214  /*
215  * When true means the class has responsibilities to intantiate and delete the SOTLSensors instance,
216  * otherwise MSSOTLTrafficLightLogic::init and MSSOTLTrafficLightLogic::~MSSOTLTrafficLightLogic have not to affect SOTLSensors instance lifecycle
217  */
219 
220  // The map to store the cars*timesteps for each target phase
221  std::map<int, SUMOTime> targetPhasesCTS;
222 
223  //The map to store the time each target phase have been checked last
224  //This helps to compute the timesteps to get the cars*timesteps value
225  std::map<int, SUMOTime> lastCheckForTargetPhase;
226 
227  //Map to store how many selection rounds have been done from the last selection of the phase
228  std::map<int, int> targetPhasesLastSelection;
229 
231  //return 2 * targetPhasesCTS.size() - 1;
232  return (int)targetPhasesCTS.size() - 1;
233  }
234 
235  /*
236  * This member keeps track which is the current steps chain, i.e.
237  * which is the last target phase executed (even if it is currently executed)
238  * (a steps chain starts always with a target phase)
239  */
241 
243  /*
244  * @brief Check for phases compliancy
245  */
246  void checkPhases();
247 
248  /*
249  * Find the first target phase and set the current step on it
250  */
251  void setToATargetPhase();
252 
253  /*
254  * This function member helps to set up the map keeping track of target phases and associated timesteps
255  */
256  void setupCTS();
257 
258  /*
259  * Updates the cars-timesteps counters for each target phase except the one belonging to the current steps chain
260  */
261  void updateCTS();
262 
263  /*
264  * To reset the cars-timesteps counter when a target phase is newly selected
265  * If phaseStep is not a target phase nothing happens
266  */
267  void resetCTS(int phaseStep);
268  /*
269  * TEST
270  */
271  void updateDecayThreshold();
272 
273  /*
274  * Traffic threshold calculation mode:
275  * 0-> cars times seconds
276  * 1-> estimated cars times seconds
277  * 2-> queue length
278  */
279  int getMode() {
280  return TplConvert::_2int(getParameter("MODE", "0").c_str());
281  }
282  /*
283  * Decay threshold that should be used in case of penetration rate != 100%
284  * 0-> not active
285  * 1-> active
286  */
288  return TplConvert::_2bool(getParameter("DECAY_THRESHOLD", "0").c_str());
289  }
290 
291  double getDecayConstant() {
292  return TplConvert::_2double(getParameter("DECAY_CONSTANT", "-0.001").c_str());
293  }
294 
295 };
296 
297 #endif
298 /****************************************************************************/
Builds detectors for microsim.
void init(NLDetectorBuilder &nb)
Initialises the tls with sensors on incoming and outgoing lanes Sensors are built in the simulation a...
static bool _2bool(const E *const data)
converts a 0-terminated char-type array into the boolean value described by it
Definition: TplConvert.h:388
std::map< const std::string, std::vector< MSPushButton * > > PhasePushButtons
SUMOTime DELTA_T
Definition: SUMOTime.cpp:39
std::map< int, SUMOTime > lastCheckForTargetPhase
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.
std::map< int, SUMOTime > targetPhasesCTS
std::vector< MSPhaseDefinition * > Phases
Definition of a list of phases, being the junction logic.
MSSOTLTrafficLightLogic(MSTLLogicControl &tlcontrol, const std::string &id, const std::string &subid, const Phases &phases, int step, SUMOTime delay, const std::map< std::string, std::string > &parameters)
Constructor without sensors passed.
static int _2int(const E *const data)
converts a char-type array into the integer value described by it
Definition: TplConvert.h:155
const MSPhaseDefinition & getCurrentPhaseDef() const
Returns the definition of the current phase.
SUMOTime trySwitch()
Switches to the next phase.
static double _2double(const E *const data)
converts a char-type array into the double value described by it
Definition: TplConvert.h:311
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
long long int SUMOTime
Definition: TraCIDefs.h:51
A fixed traffic light logic.
The definition of a single phase of a tls logic.
std::map< int, int > targetPhasesLastSelection
int countVehicles(MSPhaseDefinition phase)