SUMO - Simulation of Urban MObility
MSSOTLTrafficLightLogic.h
Go to the documentation of this file.
1 /****************************************************************************/
9 // The base abstract class for SOTL logics
10 /****************************************************************************/
11 // SUMO, Simulation of Urban MObility; see http://sumo.dlr.de/
12 // Copyright 2001-2013 DLR (http://www.dlr.de/) and contributors
13 /****************************************************************************/
14 //
15 // This file is part of SUMO.
16 // SUMO is free software: you can redistribute it and/or modify
17 // it under the terms of the GNU General Public License as published by
18 // the Free Software Foundation, either version 3 of the License, or
19 // (at your option) any later version.
20 //
21 /****************************************************************************/
22 #ifndef MSSOTLTrafficLightLogic_h
23 #define MSSOTLTrafficLightLogic_h
24 
25 // ===========================================================================
26 // included modules
27 // ===========================================================================
28 #ifdef _MSC_VER
29 #include <windows_config.h>
30 #else
31 #include <config.h>
32 #endif
33 
35 
36 #include <utils/common/SUMOTime.h>
37 #include "MSSOTLDefinitions.h"
39 #include "MSSOTLE2Sensors.h"
42 
43 
44 // ===========================================================================
45 // class declarations
46 // ===========================================================================
47 class MSPushButton;
48 
49 
50 // ===========================================================================
51 // class definitions
52 // ===========================================================================
53 
67 public:
68  // typedef int CTS;
69 
80  MSSOTLTrafficLightLogic(MSTLLogicControl& tlcontrol, const std::string& id,
81  const std::string& subid, const Phases& phases, int step,
82  SUMOTime delay,
83  const std::map<std::string, std::string>& parameters);
84 
96  MSSOTLTrafficLightLogic(MSTLLogicControl& tlcontrol, const std::string& id,
97  const std::string& subid, const Phases& phases, int step,
98  SUMOTime delay,
99  const std::map<std::string, std::string>& parameters,
100  MSSOTLSensors* sensors);
101 
104 
111  void init(NLDetectorBuilder& nb) throw(ProcessError);
112 
113  /*
114  * This member implements the base operations for all SOTL logics.
115  * SOTL politics are implementated through decidePhase() member
116  * @see MSTrafficLightLogic::trySwitch
117  */
119 
120 protected:
121 
122  typedef std::map<const std::string, std::vector<MSPushButton*> > PhasePushButtons;
123  PhasePushButtons m_pushButtons;
124 
125  void logStatus();
126  /*
127  * This member has to contain the switching logic for SOTL policies
128  */
129 
130  virtual int decideNextPhase();
131 
132  virtual bool canRelease() = 0;
133 
135 
136  /*
137  * Count the number of vehicles approaching the target lanes for the given phase.
138  * If the phase in not a target phase the function member will return 0.
139  * @param[in] The target phase index
140  */
141  int countVehicles(MSPhaseDefinition phase);
142 
143  /*
144  * Every target step except the one from the current chain is checked.
145  * This is because the current chain is not eligible to be directly
146  * targeted again, it would be unfair.
147  * @return True if at least a target phase has passed the threshold for input cars-timesteps
148  */
149  bool isThresholdPassed();
150 
155  bool isPushButtonPressed();
156 
157  int getThreshold() {
158  return TplConvert::_2int(getParameter("THRESHOLD", "10").c_str());
159  }
160 
162  return TplConvert::_2SUMOReal(getParameter("THRESHOLDSPEED", "2").c_str());
163  }
164 
166  return TplConvert::_2SUMOReal(getParameter("INSENSORSLENGTH", "100").c_str());
167  }
168 
170  return TplConvert::_2SUMOReal(getParameter("OUTSENSORSLENGTH", "80").c_str());
171  }
172 
173  /*
174  * Every target step except the one from the current chain is checked.
175  * This is because the current chain is not eligible to be directly
176  * targeted again, it would be unfair.
177  * @return The index of the phase with the maximum value of cars-timesteps
178  */
180 
182  return mySensors;
183  }
184 
189  return myCountSensors;
190  }
191  /*
192  * Computes how much time will pass after decideNextPhase will be executed again
193  */
195  if (getCurrentPhaseDef().isTransient()) {
196  return getCurrentPhaseDef().duration;
197 
198  }
199  return DELTA_T;
200 
201  }
202 
203 
204 private:
205  /*
206  * Pointer to the sensor logic regarding the junction controlled by this SOTLTrafficLightLogic
207  */
209 
210  /*
211  * Pointer to the sensor logic regarding the count of the vehicles that pass into a tl.
212  */
214 
215  /*
216  * When true means the class has responsibilities to intantiate and delete the SOTLSensors instance,
217  * otherwise MSSOTLTrafficLightLogic::init and MSSOTLTrafficLightLogic::~MSSOTLTrafficLightLogic have not to affect SOTLSensors instance lifecycle
218  */
220 
221  // The map to store the cars*timesteps for each target phase
222  std::map<int, SUMOTime> targetPhasesCTS;
223 
224  //The map to store the time each target phase have been checked last
225  //This helps to compute the timesteps to get the cars*timesteps value
226  std::map<int, SUMOTime> lastCheckForTargetPhase;
227 
228  //Map to store how many selection rounds have been done from the last selection of the phase
229  std::map<int, int> targetPhasesLastSelection;
230 
232  //return 2 * targetPhasesCTS.size() - 1;
233  return (int)targetPhasesCTS.size() - 1;
234  }
235 
236  /*
237  * This member keeps track which is the current steps chain, i.e.
238  * which is the last target phase executed (even if it is currently executed)
239  * (a steps chain starts always with a target phase)
240  */
242 
244  /*
245  * @brief Check for phases compliancy
246  */
247  void checkPhases();
248 
249  /*
250  * Find the first target phase and set the current step on it
251  */
252  void setToATargetPhase();
253 
254  /*
255  * This function member helps to set up the map keeping track of target phases and associated timesteps
256  */
257  void setupCTS();
258 
259  /*
260  * Updates the cars-timesteps counters for each target phase except the one belonging to the current steps chain
261  */
262  void updateCTS();
263 
264  /*
265  * To reset the cars-timesteps counter when a target phase is newly selected
266  * If phaseStep is not a target phase nothing happens
267  */
268  void resetCTS(int phaseStep);
269  /*
270  * TEST
271  */
272  void updateDecayThreshold();
273 
274  /*
275  * Traffic threshold calculation mode:
276  * 0-> cars times seconds
277  * 1-> estimated cars times seconds
278  * 2-> queue length
279  */
280  int getMode() {
281  return TplConvert::_2int(getParameter("MODE", "0").c_str());
282  }
283  /*
284  * Decay threshold that should be used in case of penetration rate != 100%
285  * 0-> not active
286  * 1-> active
287  */
289  return TplConvert::_2bool(getParameter("DECAY_THRESHOLD", "0").c_str());
290  }
291 
293  return TplConvert::_2SUMOReal(getParameter("DECAY_CONSTANT", "-0.001").c_str());
294  }
295 
296 };
297 
298 #endif
299 /****************************************************************************/
Builds detectors for microsim.
long long int SUMOTime
Definition: SUMOTime.h:43
void init(NLDetectorBuilder &nb)
Initialises the tls with sensors on incoming and outgoing lanes Sensors are built in the simulation a...
static SUMOReal _2SUMOReal(const E *const data)
converts a char-type array into the SUMOReal value described by it
Definition: TplConvert.h:290
static bool _2bool(const E *const data)
converts a 0-terminated char-type array into the boolean value described by it
Definition: TplConvert.h:364
std::map< const std::string, std::vector< MSPushButton * > > PhasePushButtons
SUMOTime DELTA_T
Definition: SUMOTime.cpp:39
std::map< int, SUMOTime > lastCheckForTargetPhase
const std::string & getParameter(const std::string &key, const std::string &defaultValue) const
Returns the value for a given key.
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:149
SUMOTime trySwitch()
Switches to the next phase.
virtual SUMOTime computeReturnTime()
#define SUMOReal
Definition: config.h:213
virtual bool canRelease()=0
A fixed traffic light logic.
The definition of a single phase of a tls logic.
const MSPhaseDefinition & getCurrentPhaseDef() const
Returns the definition of the current phase.
std::map< int, int > targetPhasesLastSelection
int countVehicles(MSPhaseDefinition phase)