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// ===========================================================================
40
41// ===========================================================================
42// class definitions
43// ===========================================================================
49public:
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 std::map<std::string, double> getConditions() const override;
147
148 void loadState(MSTLLogicControl& tlcontrol, SUMOTime t, int step, SUMOTime spentDuration) override;
149
150protected:
153 void initSwitchingRules();
154
156 InductLoopInfo(MSInductLoop* _loop, const MSLane* _lane, int numPhases, double _maxGap, double _jamThreshold):
157 loop(_loop),
158 lane(_lane),
159 servedPhase(numPhases, false),
160 maxGap(_maxGap),
161 jamThreshold(_jamThreshold)
162 {}
163
164
165 bool isJammed() const {
167 }
168
170 const MSLane* lane;
172 std::vector<bool> servedPhase;
173 double maxGap;
175
176 };
177
179 typedef std::vector<std::vector<InductLoopInfo*> > InductLoopMap;
180
183
188 SUMOTime duration(const double detectionGap) const;
189
192
195 double gapControl();
196
197
199 bool hasMajor(const std::string& state, const LaneVector& lanes) const;
201
203 int decideNextPhase();
204
206 int decideNextPhaseCustom(bool mustSwitch);
207
209 double evalExpression(const std::string& condition) const;
210
212 double evalTernaryExpression(double a, const std::string& o, double b, const std::string& condition) const;
213
215 double evalAtomicExpression(const std::string& expr) const;
216
218 double evalCustomFunction(const std::string& fun, const std::string& arg) const;
219
221 void executeAssignments(const AssignmentMap& assignments, ConditionMap& conditions, const ConditionMap& forbidden = ConditionMap()) const;
222
223 int getDetectorPriority(const InductLoopInfo& loopInfo) const;
224
226 int getPhasePriority(int step) const;
227
229 int getTarget(int step);
230
233
235 bool canExtendLinkGreen(int target);
236
238 SUMOTime getLinkMinDuration(int target) const;
239
240 template<typename T, SumoXMLTag Tag>
241 const T* retrieveDetExpression(const std::string& arg, const std::string& expr, bool tryPrefix) const {
242 const T* det = dynamic_cast<const T*>(
244 (tryPrefix ? myDetectorPrefix : "") + arg));
245 if (det == nullptr) {
246 if (tryPrefix) {
247 // try again without prefix
248 return retrieveDetExpression<T, Tag>(arg, expr, false);
249 } else {
250 throw ProcessError("Unknown detector '" + arg + "' in expression '" + expr + "'");
251 }
252 } else {
253 return det;
254 }
255 }
256
257protected:
260
261 std::vector<InductLoopInfo> myInductLoops;
262
264 std::vector<const MSInductLoop*> myExtraLoops;
265 std::vector<const MSE2Collector*> myExtraE2;
266
268 double myMaxGap;
269
272
275
278
281
284
286 std::string myFile;
287
290
292 std::string myVehicleTypes;
293
296
298 std::vector<SUMOTime> myLinkGreenTimes;
299 std::vector<SUMOTime> myLinkRedTimes;
301 std::vector<SUMOTime> myLinkMaxGreenTimes;
303 std::vector<SUMOTime> myLinkMinGreenTimes;
304
307
310
313
315 mutable std::vector<std::map<std::string, double> > myStack;
316
318 std::set<std::string> myListedConditions;
319
322
324 bool enabled = false;
325 };
326
327 std::vector<SwitchingRules> mySwitchingRules;
328
329 const std::string myDetectorPrefix;
330
331 static const std::vector<std::string> OPERATOR_PRECEDENCE;
332};
long long int SUMOTime
Definition: GUI.h:36
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.
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:...
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.
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
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
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
const T * retrieveDetExpression(const std::string &arg, const std::string &expr, bool tryPrefix) const
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:62
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
MSDetectorControl & getDetectorControl()
Returns the detector control.
Definition: MSNet.h:442
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition: MSNet.cpp:183
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)