Eclipse SUMO - Simulation of Urban MObility
Loading...
Searching...
No Matches
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-2026 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
136 void saveState(OutputDevice& out) const override;
137
138 void loadExtraState(const std::string& state) override;
139
141 const std::string getParameter(const std::string& key, const std::string defaultValue = "") const override;
142
144 void setParameter(const std::string& key, const std::string& value) override;
145
147 std::map<std::string, double> getDetectorStates() const override;
148
150 double getDetectorState(const std::string laneID) const override;
151
153 std::map<std::string, double> getConditions() const override;
154
155 void loadState(MSTLLogicControl& tlcontrol, SUMOTime t, int step, SUMOTime spentDuration, bool active) override;
156
157protected:
160 void initSwitchingRules();
161
163 InductLoopInfo(MSInductLoop* _loop, const MSLane* _lane, int numPhases, double _maxGap, double _jamThreshold):
164 loop(_loop),
165 lane(_lane),
166 servedPhase(numPhases, false),
167 maxGap(_maxGap),
168 jamThreshold(_jamThreshold)
169 {}
170
171
172 bool isJammed() const {
174 }
175
177 const MSLane* lane;
179 std::vector<bool> servedPhase;
180 double maxGap;
182
183 };
184
186 typedef std::vector<std::vector<InductLoopInfo*> > InductLoopMap;
187
190
195 SUMOTime duration(const double detectionGap) const;
196
198 SUMOTime getMinimumMinDuration(MSLane* lane, const std::set<int>& multiNextTargets) const;
199
202 double gapControl();
203
204
206 bool hasMajor(const std::string& state, const LaneVector& lanes) const;
208
210 int decideNextPhase();
211
213 int decideNextPhaseCustom(bool mustSwitch);
214
216 double evalExpression(const std::string& condition) const;
217
219 double evalTernaryExpression(double a, const std::string& o, double b, const std::string& condition) const;
220
222 double evalAtomicExpression(const std::string& expr) const;
223
225 double evalCustomFunction(const std::string& fun, const std::string& arg) const;
226
228 void executeAssignments(const AssignmentMap& assignments, ConditionMap& conditions, const ConditionMap& forbidden = ConditionMap()) const;
229
230 int getDetectorPriority(const InductLoopInfo& loopInfo) const;
231
233 int getPhasePriority(int step) const;
234
236 std::pair<int, SUMOTime> getTarget(int step) const;
237
240
242 bool canExtendLinkGreen(int target);
243
245 SUMOTime getLinkMinDuration(int target) const;
246
248 bool weakConflict(int linkIndex, const std::string& state) const;
249
250 template<typename T, SumoXMLTag Tag>
251 const T* retrieveDetExpression(const std::string& arg, const std::string& expr, bool tryPrefix) const {
252 const T* det = dynamic_cast<const T*>(
254 (tryPrefix ? myDetectorPrefix : "") + arg));
255 if (det == nullptr) {
256 if (tryPrefix) {
257 // try again without prefix
258 return retrieveDetExpression<T, Tag>(arg, expr, false);
259 } else {
260 throw ProcessError("Unknown detector '" + arg + "' in expression '" + expr + "'");
261 }
262 } else {
263 return det;
264 }
265 }
266
268 std::set<int> getMultiNextTargets() const;
269
270 void initTargets(int step);
271 void findTargets(int origStep, int n, SUMOTime priorTransition, std::map<int, SUMOTime>& found);
272
273protected:
276 std::vector<std::vector<const MSLink*> > myCrossingsForPhase;
277
278 std::vector<InductLoopInfo> myInductLoops;
279
281 std::vector<const MSInductLoop*> myExtraLoops;
282 std::vector<const MSE2Collector*> myExtraE2;
283
285 double myMaxGap;
286
289
292
295
298
301
304
307
309 std::string myFile;
310
313
315 std::string myVehicleTypes;
316
319
321 std::vector<SUMOTime> myLinkGreenTimes;
322 std::vector<SUMOTime> myLinkRedTimes;
324 std::vector<SUMOTime> myLinkMaxGreenTimes;
326 std::vector<SUMOTime> myLinkMinGreenTimes;
327
330
333
336
338 mutable std::vector<std::map<std::string, double> > myStack;
339
341 std::set<std::string> myListedConditions;
342
345
347 bool enabled = false;
348 };
349
350 std::vector<SwitchingRules> mySwitchingRules;
351
352 const std::string myDetectorPrefix;
353
354 /* @brief for every actuated phase,
355 * then for every target phase,
356 * provide the list of green phases that are reached quickest from the target phase
357 */
358 std::map<int, std::map<int, std::vector<int> > > myTargets;
359
360 static const std::vector<std::string> OPERATOR_PRECEDENCE;
361};
long long int SUMOTime
Definition GUI.h:36
An actuated (adaptive) traffic light logic.
double myDetectorGap
The detector distance in seconds.
void findTargets(int origStep, int n, SUMOTime priorTransition, std::map< int, SUMOTime > &found)
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::map< int, std::map< int, std::vector< int > > > myTargets
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
SUMOTime trySwitch() override
Switches to the next phase.
std::set< int > getMultiNextTargets() const
find green phases target by a next attribute
SUMOTime myLastTrySwitchTime
last time trySwitch was called
int getDetectorPriority(const InductLoopInfo &loopInfo) const
SUMOTime myFreq
The frequency for aggregating detector output.
void saveState(OutputDevice &out) const override
Saves the current tls states into the given stream.
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
SUMOTime getMaxDur(int step=-1) const override
std::vector< std::vector< InductLoopInfo * > > InductLoopMap
Definition of a map from phases to induct loops controlling them.
std::vector< std::vector< const MSLink * > > myCrossingsForPhase
AssignmentMap myAssignments
The condition assignments.
std::string myVehicleTypes
Whether detector output separates by vType.
void loadState(MSTLLogicControl &tlcontrol, SUMOTime t, int step, SUMOTime spentDuration, bool active) override
restores the tls state
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.
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.
SUMOTime getMinimumMinDuration(MSLane *lane, const std::set< int > &multiNextTargets) const
get the minimum min duration for all stretchable phases that affect the given lane
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
void loadExtraState(const std::string &state) override
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.
std::pair< int, SUMOTime > getTarget(int step) const
get the green phase following step and the transition time
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.
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:455
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition MSNet.cpp:199
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.
Static storage of an output device and its base (abstract) implementation.
std::map< std::string, std::string > Map
parameters map
Function(const std::string &_id="", int _nArgs=-1)
InductLoopInfo(MSInductLoop *_loop, const MSLane *_lane, int numPhases, double _maxGap, double _jamThreshold)