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.dev/sumo
3 // Copyright (C) 2002-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 // 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:
50 
52  typedef std::vector<std::tuple<std::string, std::string, std::string> > AssignmentMap;
53 
54  struct Function {
55  Function(const std::string& _id = "", int _nArgs = -1):
56  id(_id), nArgs(_nArgs) {}
57  std::string id;
58  int nArgs;
60  };
61 
62  typedef std::map<std::string, Function> FunctionMap;
63 
64 
75  const std::string& id, const std::string& programID,
76  const SUMOTime offset,
78  int step, SUMOTime delay,
79  const Parameterised::Map& parameter,
80  const std::string& basePath,
81  const ConditionMap& conditions = ConditionMap(),
82  const AssignmentMap& assignments = AssignmentMap(),
83  const FunctionMap& functions = FunctionMap());
84 
85 
90  void init(NLDetectorBuilder& nb) override;
91 
92 
95 
96 
97 
100 
105  SUMOTime trySwitch() override;
107 
108  SUMOTime getMinDur(int step = -1) const override;
109  SUMOTime getMaxDur(int step = -1) const override;
110  SUMOTime getEarliestEnd(int step = -1) const override;
111  SUMOTime getLatestEnd(int step = -1) const override;
112 
115 
123  void changeStepAndDuration(MSTLLogicControl& tlcontrol, SUMOTime simStep,
124  int step, SUMOTime stepDuration) override;
125 
127  void activateProgram() override;
128  void deactivateProgram() override;
129 
130  bool showDetectors() const {
131  return myShowDetectors;
132  }
133 
134  void setShowDetectors(bool show);
135 
137  const std::string getParameter(const std::string& key, const std::string defaultValue = "") const override;
138 
140  void setParameter(const std::string& key, const std::string& value) override;
141 
143  std::map<std::string, double> getDetectorStates() const override;
144 
146  double getDetectorState(const std::string laneID) const override;
147 
149  std::map<std::string, double> getConditions() const override;
150 
151  void loadState(MSTLLogicControl& tlcontrol, SUMOTime t, int step, SUMOTime spentDuration) override;
152 
153 protected:
155  void initAttributeOverride();
156  void initSwitchingRules();
157 
158  struct InductLoopInfo {
159  InductLoopInfo(MSInductLoop* _loop, const MSLane* _lane, int numPhases, double _maxGap, double _jamThreshold):
160  loop(_loop),
161  lane(_lane),
162  servedPhase(numPhases, false),
163  maxGap(_maxGap),
164  jamThreshold(_jamThreshold)
165  {}
166 
167 
168  bool isJammed() const {
169  return jamThreshold > 0 && loop->getOccupancyTime() >= jamThreshold;
170  }
171 
173  const MSLane* lane;
175  std::vector<bool> servedPhase;
176  double maxGap;
177  double jamThreshold;
178 
179  };
180 
182  typedef std::vector<std::vector<InductLoopInfo*> > InductLoopMap;
183 
186 
191  SUMOTime duration(const double detectionGap) const;
192 
195 
198  double gapControl();
199 
200 
202  bool hasMajor(const std::string& state, const LaneVector& lanes) const;
204 
206  int decideNextPhase();
207 
209  int decideNextPhaseCustom(bool mustSwitch);
210 
212  double evalExpression(const std::string& condition) const;
213 
215  double evalTernaryExpression(double a, const std::string& o, double b, const std::string& condition) const;
216 
218  double evalAtomicExpression(const std::string& expr) const;
219 
221  double evalCustomFunction(const std::string& fun, const std::string& arg) const;
222 
224  void executeAssignments(const AssignmentMap& assignments, ConditionMap& conditions, const ConditionMap& forbidden = ConditionMap()) const;
225 
226  int getDetectorPriority(const InductLoopInfo& loopInfo) const;
227 
229  int getPhasePriority(int step) const;
230 
232  int getTarget(int step);
233 
235  bool maxLinkDurationReached();
236 
238  bool canExtendLinkGreen(int target);
239 
241  SUMOTime getLinkMinDuration(int target) const;
242 
244  bool weakConflict(int linkIndex, const std::string& state) const;
245 
246  template<typename T, SumoXMLTag Tag>
247  const T* retrieveDetExpression(const std::string& arg, const std::string& expr, bool tryPrefix) const {
248  const T* det = dynamic_cast<const T*>(
250  (tryPrefix ? myDetectorPrefix : "") + arg));
251  if (det == nullptr) {
252  if (tryPrefix) {
253  // try again without prefix
254  return retrieveDetExpression<T, Tag>(arg, expr, false);
255  } else {
256  throw ProcessError("Unknown detector '" + arg + "' in expression '" + expr + "'");
257  }
258  } else {
259  return det;
260  }
261  }
262 
263 protected:
266 
267  std::vector<InductLoopInfo> myInductLoops;
268 
270  std::vector<const MSInductLoop*> myExtraLoops;
271  std::vector<const MSE2Collector*> myExtraE2;
272 
274  double myMaxGap;
275 
278 
281 
284 
287 
290 
293 
296 
298  std::string myFile;
299 
302 
304  std::string myVehicleTypes;
305 
308 
310  std::vector<SUMOTime> myLinkGreenTimes;
311  std::vector<SUMOTime> myLinkRedTimes;
313  std::vector<SUMOTime> myLinkMaxGreenTimes;
315  std::vector<SUMOTime> myLinkMinGreenTimes;
316 
319 
322 
325 
327  mutable std::vector<std::map<std::string, double> > myStack;
328 
330  std::set<std::string> myListedConditions;
331 
334 
335  struct SwitchingRules {
336  bool enabled = false;
337  };
338 
339  std::vector<SwitchingRules> mySwitchingRules;
340 
341  const std::string myDetectorPrefix;
342 
343  static const std::vector<std::string> OPERATOR_PRECEDENCE;
344 };
long long int SUMOTime
Definition: GUI.h:35
An actuated (adaptive) traffic light logic.
double myDetectorGap
The detector distance in seconds.
FunctionMap myFunctions
The loaded functions.
double myJamThreshold
The minimum continuous occupancy time to mark a detector as jammed.
bool myBuildAllDetectors
Whether all detectors shall be built.
double myMaxGap
The maximum gap to check in seconds.
const std::string getParameter(const std::string &key, const std::string defaultValue="") const override
try to get the value of the given parameter (including prefixed parameters)
std::vector< SwitchingRules > mySwitchingRules
std::vector< std::map< std::string, double > > myStack
The function call stack;.
double evalAtomicExpression(const std::string &expr) const
evaluate atomic expression
int getTarget(int step)
get the green phase following step
SUMOTime trySwitch() override
Switches to the next phase.
SUMOTime myLastTrySwitchTime
last time trySwitch was called
int getDetectorPriority(const InductLoopInfo &loopInfo) const
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
std::vector< const MSInductLoop * > myExtraLoops
extra loops for output/tracking
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
MSActuatedTrafficLightLogic(MSTLLogicControl &tlcontrol, const std::string &id, const std::string &programID, const SUMOTime offset, const MSSimpleTrafficLightLogic::Phases &phases, int step, SUMOTime delay, const Parameterised::Map &parameter, const std::string &basePath, const ConditionMap &conditions=ConditionMap(), const AssignmentMap &assignments=AssignmentMap(), const FunctionMap &functions=FunctionMap())
Constructor.
void loadState(MSTLLogicControl &tlcontrol, SUMOTime t, int step, SUMOTime spentDuration) override
restores the tls state
SUMOTime getMaxDur(int step=-1) const override
std::vector< std::vector< InductLoopInfo * > > InductLoopMap
Definition of a map from phases to induct loops controlling them.
AssignmentMap myAssignments
The condition assignments.
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
std::vector< std::tuple< std::string, std::string, std::string > > AssignmentMap
std::set< std::string > myListedConditions
the conditions which shall be listed in GUITLLogicPhasesTrackerWindow
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
void changeStepAndDuration(MSTLLogicControl &tlcontrol, SUMOTime simStep, int step, SUMOTime stepDuration) override
Changes the current phase and her duration.
bool weakConflict(int linkIndex, const std::string &state) const
whether a given link has only weak mode foes that are green in the given state
static const std::vector< std::string > OPERATOR_PRECEDENCE
bool myHasMultiTarget
Whether any of the phases has multiple targets.
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
SUMOTime getMinDur(int step=-1) const override
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
void executeAssignments(const AssignmentMap &assignments, ConditionMap &conditions, const ConditionMap &forbidden=ConditionMap()) const
execute assignemnts of the logic or a custom function
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.
void activateProgram() override
called when switching programs
std::vector< InductLoopInfo > myInductLoops
bool maxLinkDurationReached()
whether the current phase cannot be continued due to linkMaxDur constraints
double evalCustomFunction(const std::string &fun, const std::string &arg) const
evaluate function expression
void initAttributeOverride()
initialize custom switching rules
std::map< std::string, double > getConditions() const override
return all named conditions defined for this traffic light
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< const MSE2Collector * > myExtraE2
SUMOTime getEarliestEnd(int step=-1) const override
std::map< std::string, Function > FunctionMap
std::map< std::string, double > getDetectorStates() const override
retrieve all detectors used by this program
double getDetectorState(const std::string laneID) const override
retrieve a specific detector used by this program
void setParameter(const std::string &key, const std::string &value) override
Sets a parameter and updates internal constants.
std::vector< SUMOTime > myLinkGreenTimes
consecutive time that the given link index has been green
SUMOTime getLatestEnd(int step=-1) const override
std::string myFile
The output file for generated detectors.
ConditionMap myConditions
The custom switching conditions.
void init(NLDetectorBuilder &nb) override
Initialises the tls with information about incoming lanes.
int decideNextPhase()
select among candidate phases based on detector states
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:63
double getOccupancyTime() const
Returns the time of continous occupation by the same vehicle in seconds or 0 if there is no vehicle o...
Representation of a lane in the micro simulation.
Definition: MSLane.h:84
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition: MSNet.cpp:184
MSDetectorControl & getDetectorControl()
Returns the detector control.
Definition: MSNet.h:441
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.
std::map< std::string, std::string > Map
parameters map
Definition: Parameterised.h:45
Function(const std::string &_id="", int _nArgs=-1)
InductLoopInfo(MSInductLoop *_loop, const MSLane *_lane, int numPhases, double _maxGap, double _jamThreshold)