Eclipse SUMO - Simulation of Urban MObility
MSActuatedTrafficLightLogic.h
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2002-2022 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 // An actuated (adaptive) traffic light logic
21 /****************************************************************************/
22 #pragma once
23 #include <config.h>
24 
25 #include <utility>
26 #include <vector>
27 #include <bitset>
28 #include <map>
34 
35 
36 // ===========================================================================
37 // class declarations
38 // ===========================================================================
39 class NLDetectorBuilder;
40 
41 // ===========================================================================
42 // class definitions
43 // ===========================================================================
49 public:
60  const std::string& id, const std::string& programID,
61  const SUMOTime offset,
63  int step, SUMOTime delay,
64  const std::map<std::string, std::string>& parameter,
65  const std::string& basePath,
66  const std::map<std::string, std::string>& conditions = std::map<std::string, std::string>());
67 
68 
73  void init(NLDetectorBuilder& nb);
74 
75 
78 
79 
80 
83 
90 
93 
101  void changeStepAndDuration(MSTLLogicControl& tlcontrol, SUMOTime simStep,
102  int step, SUMOTime stepDuration);
103 
105  void activateProgram();
106  void deactivateProgram();
107 
108  bool showDetectors() const {
109  return myShowDetectors;
110  }
111 
112  void setShowDetectors(bool show);
113 
115  void setParameter(const std::string& key, const std::string& value);
116 
118  std::map<std::string, double> getDetectorStates() const;
119 
121  std::map<std::string, double> getConditions() const;
122 
123 protected:
125  void initSwitchingRules();
126 
127  struct InductLoopInfo {
128  InductLoopInfo(MSInductLoop* _loop, int numPhases, double _maxGap):
129  loop(_loop),
130  servedPhase(numPhases, false),
131  maxGap(_maxGap)
132  {}
135  std::vector<bool> servedPhase;
136  double maxGap;
137  };
138 
140  typedef std::vector<std::vector<InductLoopInfo*> > InductLoopMap;
141 
144 
149  SUMOTime duration(const double detectionGap) const;
150 
153 
156  double gapControl();
157 
158 
160  bool hasMajor(const std::string& state, const LaneVector& lanes) const;
162 
164  int decideNextPhase();
165 
167  int decideNextPhaseCustom(bool mustSwitch);
168 
170  double evalExpression(const std::string& condition) const;
171 
173  double evalTernaryExpression(double a, const std::string& o, double b, const std::string& condition) const;
174 
176  double evalAtomicExpression(const std::string& expr) const;
177 
178  int getDetectorPriority(const InductLoopInfo& loopInfo) const;
179 
181  int getPhasePriority(int step) const;
182 
184  int getTarget(int step);
185 
187  bool maxLinkDurationReached();
188 
190  bool canExtendLinkGreen(int target);
191 
193  SUMOTime getLinkMinDuration(int target) const;
194 
195  template<typename T, SumoXMLTag Tag>
196  const T* retrieveDetExpression(const std::string& arg, const std::string& expr, bool tryPrefix) const {
197  const T* det = dynamic_cast<const T*>(
199  (tryPrefix ? myDetectorPrefix : "") + arg));
200  if (det == nullptr) {
201  if (tryPrefix) {
202  // try again without prefix
203  return retrieveDetExpression<T, Tag>(arg, expr, false);
204  } else {
205  throw ProcessError("Unknown detector '" + arg + "' in expression '" + expr + "'");
206  }
207  } else {
208  return det;
209  }
210  }
211 
212 
213 protected:
216 
217  std::vector<InductLoopInfo> myInductLoops;
218 
220  double myMaxGap;
221 
224 
227 
230 
233 
235  std::string myFile;
236 
239 
241  std::string myVehicleTypes;
242 
245 
247  std::vector<SUMOTime> myLinkGreenTimes;
248  std::vector<SUMOTime> myLinkRedTimes;
250  std::vector<SUMOTime> myLinkMaxGreenTimes;
252  std::vector<SUMOTime> myLinkMinGreenTimes;
253 
255  std::map<std::string, std::string> myConditions;
256 
259 
260  struct SwitchingRules {
261  bool enabled = false;
262  };
263 
264  std::vector<SwitchingRules> mySwitchingRules;
265 
266  const std::string myDetectorPrefix;
267 
268  static const std::vector<std::string> OPERATOR_PRECEDENCE;
269 };
long long int SUMOTime
Definition: SUMOTime.h:32
An actuated (adaptive) traffic light logic.
double myDetectorGap
The detector distance in seconds.
MSActuatedTrafficLightLogic(MSTLLogicControl &tlcontrol, const std::string &id, const std::string &programID, const SUMOTime offset, const MSSimpleTrafficLightLogic::Phases &phases, int step, SUMOTime delay, const std::map< std::string, std::string > &parameter, const std::string &basePath, const std::map< std::string, std::string > &conditions=std::map< std::string, std::string >())
Constructor.
double myMaxGap
The maximum gap to check in seconds.
void activateProgram()
called when switching programs
std::vector< SwitchingRules > mySwitchingRules
double evalAtomicExpression(const std::string &expr) const
evaluate atomic expression
int getTarget(int step)
get the green phase following step
void setParameter(const std::string &key, const std::string &value)
Sets a parameter and updates internal constants.
SUMOTime myLastTrySwitchTime
last time trySwitch was called
int getDetectorPriority(const InductLoopInfo &loopInfo) const
std::map< std::string, double > getConditions() const
return all named conditions defined for this traffic light
SUMOTime myFreq
The frequency for aggregating detector output.
SUMOTime getMinimumMinDuration(MSLane *lane) const
get the minimum min duration for all stretchable phases that affect the given lane
bool myShowDetectors
Whether the detectors shall be shown in the GUI.
std::vector< SUMOTime > myLinkMaxGreenTimes
maximum consecutive time that the given link may remain green
std::vector< std::vector< InductLoopInfo * > > InductLoopMap
Definition of a map from phases to induct loops controlling them.
std::string myVehicleTypes
Whether detector output separates by vType.
double gapControl()
Return the minimum detection gap of all detectors if the current phase should be extended and double:...
const T * retrieveDetExpression(const std::string &arg, const std::string &expr, bool tryPrefix) const
void init(NLDetectorBuilder &nb)
Initialises the tls with information about incoming lanes.
double evalExpression(const std::string &condition) const
evaluate custom switching condition
std::vector< SUMOTime > myLinkMinGreenTimes
minimum consecutive time that the given link must remain green
SUMOTime trySwitch()
Switches to the next phase.
static const std::vector< std::string > OPERATOR_PRECEDENCE
double myPassingTime
The passing time used in seconds.
SUMOTime getLinkMinDuration(int target) const
the minimum duratin for keeping the current phase due to linkMinDur constraints
bool canExtendLinkGreen(int target)
whether the target phase is acceptable in light of linkMaxDur constraints
InductLoopMap myInductLoopsForPhase
A map from phase to induction loops to be used for gap control.
int decideNextPhaseCustom(bool mustSwitch)
select among candidate phases based on detector states and custom switching rules
double evalTernaryExpression(double a, const std::string &o, double b, const std::string &condition) const
evaluate atomic expression
bool myTraCISwitch
whether the next switch time was requested via TraCI
int getPhasePriority(int step) const
count the number of active detectors for the given step
SUMOTime duration(const double detectionGap) const
Returns the minimum duration of the current phase.
std::vector< InductLoopInfo > myInductLoops
std::map< std::string, std::string > myConditions
The custom switching conditions.
bool maxLinkDurationReached()
whether the current phase cannot be continued due to linkMaxDur constraints
bool hasMajor(const std::string &state, const LaneVector &lanes) const
return whether there is a major link from the given lane in the given phase
std::vector< SUMOTime > myLinkGreenTimes
consecutive time that the given link index has been green
std::map< std::string, double > getDetectorStates() const
retrieve all detectors used by this program
std::string myFile
The output file for generated detectors.
void initSwitchingRules()
initialize custom switching rules
int decideNextPhase()
select among candidate phases based on detector states
void changeStepAndDuration(MSTLLogicControl &tlcontrol, SUMOTime simStep, int step, SUMOTime stepDuration)
Changes the current phase and her duration.
SUMOTime myInactiveThreshold
The time threshold to avoid starved phases.
const NamedObjectCont< MSDetectorFileOutput * > & getTypedDetectors(SumoXMLTag type) const
Returns the list of detectors of the given type.
An unextended detector measuring at a fixed position on a fixed lane.
Definition: MSInductLoop.h:62
Representation of a lane in the micro simulation.
Definition: MSLane.h:82
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition: MSNet.cpp:174
MSDetectorControl & getDetectorControl()
Returns the detector control.
Definition: MSNet.h:439
A fixed traffic light logic.
A class that stores and controls tls and switching of their programs.
std::vector< MSLane * > LaneVector
Definition of the list of arrival lanes subjected to this tls.
std::vector< MSPhaseDefinition * > Phases
Definition of a list of phases, being the junction logic.
Builds detectors for microsim.
T get(const std::string &id) const
Retrieves an item.
InductLoopInfo(MSInductLoop *_loop, int numPhases, double _maxGap)