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 
73  MSSOTLTrafficLightLogic(MSTLLogicControl& tlcontrol, const std::string& id,
74  const std::string& subid, const Phases& phases, int step,
75  SUMOTime delay,
76  const std::map<std::string, std::string>& parameters);
77 
89  MSSOTLTrafficLightLogic(MSTLLogicControl& tlcontrol, const std::string& id,
90  const std::string& subid, 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;
116  PhasePushButtons m_pushButtons;
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 TplConvert::_2int(getParameter("THRESHOLD", "10").c_str());
152  }
153 
154  double getSpeedThreshold() {
155  return TplConvert::_2double(getParameter("THRESHOLDSPEED", "2").c_str());
156  }
157 
159  return TplConvert::_2double(getParameter("INSENSORSLENGTH", "100").c_str());
160  }
161 
163  return TplConvert::_2double(getParameter("OUTSENSORSLENGTH", "80").c_str());
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 TplConvert::_2int(getParameter("MODE", "0").c_str());
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 TplConvert::_2bool(getParameter("DECAY_THRESHOLD", "0").c_str());
283  }
284 
285  double getDecayConstant() {
286  return TplConvert::_2double(getParameter("DECAY_CONSTANT", "-0.001").c_str());
287  }
288 
289 };
290 
291 #endif
292 /****************************************************************************/
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...
static bool _2bool(const E *const data)
converts a 0-terminated char-type array into the boolean value described by it
Definition: TplConvert.h:382
std::map< const std::string, std::vector< MSPushButton * > > PhasePushButtons
SUMOTime DELTA_T
Definition: SUMOTime.cpp:35
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:149
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:305
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
int countVehicles(MSPhaseDefinition phase)