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.dev/sumo
3 // Copyright (C) 2013-2024 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 
27 #include <utils/common/SUMOTime.h>
29 #include "MSSOTLDefinitions.h"
30 #include "MSSOTLE2Sensors.h"
31 
32 
33 // ===========================================================================
34 // class declarations
35 // ===========================================================================
36 class MSPushButton;
37 
38 
39 // ===========================================================================
40 // class definitions
41 // ===========================================================================
42 
56 public:
57  // typedef int CTS;
58 
70  MSSOTLTrafficLightLogic(MSTLLogicControl& tlcontrol, const std::string& id,
71  const std::string& programID, const TrafficLightType logicType, const Phases& phases, int step,
72  SUMOTime delay,
73  const Parameterised::Map& parameters);
74 
87  MSSOTLTrafficLightLogic(MSTLLogicControl& tlcontrol, const std::string& id,
88  const std::string& programID, const TrafficLightType logicType, const Phases& phases, int step,
89  SUMOTime delay,
90  const Parameterised::Map& parameters,
91  MSSOTLSensors* sensors);
92 
95 
102  void init(NLDetectorBuilder& nb);
103 
104  /*
105  * This member implements the base operations for all SOTL logics.
106  * SOTL politics are implementated through decidePhase() member
107  * @see MSTrafficLightLogic::trySwitch
108  */
110 
111 protected:
112 
113  typedef std::map<const std::string, std::vector<MSPushButton*> > PhasePushButtons;
115 
116  void logStatus();
117  /*
118  * This member has to contain the switching logic for SOTL policies
119  */
120 
121  virtual int decideNextPhase();
122 
123  virtual bool canRelease() = 0;
124 
126 
127  /*
128  * Count the number of vehicles approaching the target lanes for the given phase.
129  * If the phase in not a target phase the function member will return 0.
130  * @param[in] The target phase index
131  */
132  int countVehicles(MSPhaseDefinition phase);
133 
134  /*
135  * Every target step except the one from the current chain is checked.
136  * This is because the current chain is not eligible to be directly
137  * targeted again, it would be unfair.
138  * @return True if at least a target phase has passed the threshold for input cars-timesteps
139  */
140  bool isThresholdPassed();
141 
146  bool isPushButtonPressed();
147 
148  int getThreshold() {
149  return StringUtils::toInt(getParameter("THRESHOLD", "10"));
150  }
151 
152  double getSpeedThreshold() {
153  return StringUtils::toDouble(getParameter("THRESHOLDSPEED", "2"));
154  }
155 
157  return StringUtils::toDouble(getParameter("INSENSORSLENGTH", "100"));
158  }
159 
161  return StringUtils::toDouble(getParameter("OUTSENSORSLENGTH", "80"));
162  }
163 
164  /*
165  * Every target step except the one from the current chain is checked.
166  * This is because the current chain is not eligible to be directly
167  * targeted again, it would be unfair.
168  * @return The index of the phase with the maximum value of cars-timesteps
169  */
171 
173  return mySensors;
174  }
175 
180  return myCountSensors;
181  }
182  /*
183  * Computes how much time will pass after decideNextPhase will be executed again
184  */
186  if (getCurrentPhaseDef().isTransient()) {
187  return getCurrentPhaseDef().duration;
188 
189  }
190  return DELTA_T;
191 
192  }
193 
195  void setStep(int step);
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 };
long long int SUMOTime
Definition: GUI.h:35
SUMOTime DELTA_T
Definition: SUMOTime.cpp:38
TrafficLightType
The definition of a single phase of a tls logic.
SUMOTime duration
The duration of the phase.
A self-organizing traffic light logic.
std::map< int, SUMOTime > lastCheckForTargetPhase
void setStep(int step)
Forces a specific step.
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)
std::map< int, SUMOTime > targetPhasesCTS
virtual SUMOTime computeReturnTime()
std::map< int, int > targetPhasesLastSelection
MSSOTLTrafficLightLogic(MSTLLogicControl &tlcontrol, const std::string &id, const std::string &programID, const TrafficLightType logicType, const Phases &phases, int step, SUMOTime delay, const Parameterised::Map &parameters)
Constructor without sensors passed.
void init(NLDetectorBuilder &nb)
Initialises the tls with sensors on incoming and outgoing lanes Sensors are built in the simulation a...
A fixed traffic light logic.
virtual const std::string getParameter(const std::string &key, const std::string defaultValue="") const override
gets a parameter
const MSPhaseDefinition & getCurrentPhaseDef() const override
Returns the definition of the current phase.
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.
std::map< std::string, std::string > Map
parameters map
Definition: Parameterised.h:45
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