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-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// ===========================================================================
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 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
153protected:
156 void initSwitchingRules();
157
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 {
170 }
171
173 const MSLane* lane;
175 std::vector<bool> servedPhase;
176 double maxGap;
178
179 };
180
182 typedef std::vector<std::vector<InductLoopInfo*> > InductLoopMap;
183
186
191 SUMOTime duration(const double detectionGap) const;
192
194 SUMOTime getMinimumMinDuration(MSLane* lane, const std::set<int>& multiNextTargets) const;
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 std::pair<int, SUMOTime> getTarget(int step) const;
233
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
264 std::set<int> getMultiNextTargets() const;
265
266 void initTargets(int step);
267 void findTargets(int origStep, int n, SUMOTime priorTransition, std::map<int, SUMOTime>& found);
268
269protected:
272 std::vector<std::vector<const MSLink*> > myCrossingsForPhase;
273
274 std::vector<InductLoopInfo> myInductLoops;
275
277 std::vector<const MSInductLoop*> myExtraLoops;
278 std::vector<const MSE2Collector*> myExtraE2;
279
281 double myMaxGap;
282
285
288
291
294
297
300
303
305 std::string myFile;
306
309
311 std::string myVehicleTypes;
312
315
317 std::vector<SUMOTime> myLinkGreenTimes;
318 std::vector<SUMOTime> myLinkRedTimes;
320 std::vector<SUMOTime> myLinkMaxGreenTimes;
322 std::vector<SUMOTime> myLinkMinGreenTimes;
323
326
329
332
334 mutable std::vector<std::map<std::string, double> > myStack;
335
337 std::set<std::string> myListedConditions;
338
341
343 bool enabled = false;
344 };
345
346 std::vector<SwitchingRules> mySwitchingRules;
347
348 const std::string myDetectorPrefix;
349
350 /* @brief for every actuated phase,
351 * then for every target phase,
352 * provide the list of green phases that are reached quickest from the target phase
353 */
354 std::map<int, std::map<int, std::vector<int> > > myTargets;
355
356 static const std::vector<std::string> OPERATOR_PRECEDENCE;
357};
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.
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
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.
std::vector< std::vector< const MSLink * > > myCrossingsForPhase
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.
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
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:441
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition MSNet.cpp:186
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
Function(const std::string &_id="", int _nArgs=-1)
InductLoopInfo(MSInductLoop *_loop, const MSLane *_lane, int numPhases, double _maxGap, double _jamThreshold)