Line data Source code
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 : /****************************************************************************/
14 : /// @file MSActuatedTrafficLightLogic.h
15 : /// @author Daniel Krajzewicz
16 : /// @author Michael Behrisch
17 : /// @author Jakob Erdmann
18 : /// @date Sept 2002
19 : ///
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>
29 : #include <microsim/MSEventControl.h>
30 : #include <microsim/traffic_lights/MSTrafficLightLogic.h>
31 : #include <microsim/output/MSDetectorControl.h>
32 : #include <microsim/output/MSInductLoop.h>
33 : #include "MSSimpleTrafficLightLogic.h"
34 :
35 :
36 : // ===========================================================================
37 : // class declarations
38 : // ===========================================================================
39 : class NLDetectorBuilder;
40 :
41 : // ===========================================================================
42 : // class definitions
43 : // ===========================================================================
44 : /**
45 : * @class MSActuatedTrafficLightLogic
46 : * @brief An actuated (adaptive) traffic light logic
47 : */
48 : class MSActuatedTrafficLightLogic : public MSSimpleTrafficLightLogic {
49 : public:
50 :
51 : typedef Parameterised::Map ConditionMap;
52 : typedef std::vector<std::tuple<std::string, std::string, std::string> > AssignmentMap;
53 :
54 42991 : struct Function {
55 42991 : Function(const std::string& _id = "", int _nArgs = -1):
56 42991 : id(_id), nArgs(_nArgs) {}
57 : std::string id;
58 : int nArgs;
59 : AssignmentMap assignments;
60 : };
61 :
62 : typedef std::map<std::string, Function> FunctionMap;
63 :
64 :
65 : /** @brief Constructor
66 : * @param[in] tlcontrol The tls control responsible for this tls
67 : * @param[in] id This tls' id
68 : * @param[in] programID This tls' sub-id (program id)
69 : * @param[in] phases Definitions of the phases
70 : * @param[in] step The initial phase index
71 : * @param[in] delay The time to wait before the first switch
72 : * @param[in] parameter The parameter to use for tls set-up
73 : */
74 : MSActuatedTrafficLightLogic(MSTLLogicControl& tlcontrol,
75 : const std::string& id, const std::string& programID,
76 : const SUMOTime offset,
77 : const MSSimpleTrafficLightLogic::Phases& phases,
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 :
86 : /** @brief Initialises the tls with information about incoming lanes
87 : * @param[in] nb The detector builder
88 : * @exception ProcessError If something fails on initialisation
89 : */
90 : void init(NLDetectorBuilder& nb) override;
91 :
92 :
93 : /// @brief Destructor
94 : ~MSActuatedTrafficLightLogic();
95 :
96 :
97 :
98 : /// @name Switching and setting current rows
99 : /// @{
100 :
101 : /** @brief Switches to the next phase
102 : * @return The time of the next switch
103 : * @see MSTrafficLightLogic::trySwitch
104 : */
105 : SUMOTime trySwitch() override;
106 : /// @}
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 :
113 : /// @name Changing phases and phase durations
114 : /// @{
115 :
116 : /** @brief Changes the current phase and her duration
117 : * @param[in] tlcontrol The responsible traffic lights control
118 : * @param[in] simStep The current simulation step
119 : * @param[in] step Index of the phase to use
120 : * @param[in] stepDuration The left duration of the phase
121 : * @see MSTrafficLightLogic::changeStepAndDuration
122 : */
123 : void changeStepAndDuration(MSTLLogicControl& tlcontrol, SUMOTime simStep,
124 : int step, SUMOTime stepDuration) override;
125 :
126 : /// @brief called when switching programs
127 : void activateProgram() override;
128 : void deactivateProgram() override;
129 :
130 : bool showDetectors() const {
131 0 : return myShowDetectors;
132 : }
133 :
134 : void setShowDetectors(bool show);
135 :
136 : /// @brief try to get the value of the given parameter (including prefixed parameters)
137 : const std::string getParameter(const std::string& key, const std::string defaultValue = "") const override;
138 :
139 : /**@brief Sets a parameter and updates internal constants */
140 : void setParameter(const std::string& key, const std::string& value) override;
141 :
142 : /// @brief retrieve all detectors used by this program
143 : std::map<std::string, double> getDetectorStates() const override;
144 :
145 : /// @brief retrieve a specific detector used by this program
146 : double getDetectorState(const std::string laneID) const override;
147 :
148 : /// @brief return all named conditions defined for this traffic light
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:
154 : /// @brief initialize custom switching rules
155 : void initAttributeOverride();
156 : void initSwitchingRules();
157 :
158 70663 : struct InductLoopInfo {
159 3137 : InductLoopInfo(MSInductLoop* _loop, const MSLane* _lane, int numPhases, double _maxGap, double _jamThreshold):
160 3137 : loop(_loop),
161 3137 : lane(_lane),
162 6274 : servedPhase(numPhases, false),
163 3137 : maxGap(_maxGap),
164 3137 : jamThreshold(_jamThreshold)
165 : {}
166 :
167 :
168 : bool isJammed() const {
169 651519 : return jamThreshold > 0 && loop->getOccupancyTime() >= jamThreshold;
170 : }
171 :
172 : MSInductLoop* loop;
173 : const MSLane* lane;
174 : SUMOTime lastGreenTime = 0;
175 : std::vector<bool> servedPhase;
176 : double maxGap;
177 : double jamThreshold;
178 :
179 : };
180 :
181 : /// @brief Definition of a map from phases to induct loops controlling them
182 : typedef std::vector<std::vector<InductLoopInfo*> > InductLoopMap;
183 :
184 : /// @name "actuated" algorithm methods
185 : /// @{
186 :
187 : /** @brief Returns the minimum duration of the current phase
188 : * @param[in] detectionGap The minimum detection gap for the current phase
189 : * @return The minimum duration of the current phase
190 : */
191 : SUMOTime duration(const double detectionGap) const;
192 :
193 : /// @brief get the minimum min duration for all stretchable phases that affect the given lane
194 : SUMOTime getMinimumMinDuration(MSLane* lane, const std::set<int>& multiNextTargets) const;
195 :
196 : /** @brief Return the minimum detection gap of all detectors if the current phase should be extended and double::max otherwise
197 : */
198 : double gapControl();
199 :
200 :
201 : /// @brief return whether there is a major link from the given lane in the given phase
202 : bool hasMajor(const std::string& state, const LaneVector& lanes) const;
203 : /// @}
204 :
205 : /// @brief select among candidate phases based on detector states
206 : int decideNextPhase();
207 :
208 : /// @brief select among candidate phases based on detector states and custom switching rules
209 : int decideNextPhaseCustom(bool mustSwitch);
210 :
211 : /// @brief evaluate custom switching condition
212 : double evalExpression(const std::string& condition) const;
213 :
214 : /// @brief evaluate atomic expression
215 : double evalTernaryExpression(double a, const std::string& o, double b, const std::string& condition) const;
216 :
217 : /// @brief evaluate atomic expression
218 : double evalAtomicExpression(const std::string& expr) const;
219 :
220 : /// @brief evaluate function expression
221 : double evalCustomFunction(const std::string& fun, const std::string& arg) const;
222 :
223 : /// @brief execute assignemnts of the logic or a custom function
224 : void executeAssignments(const AssignmentMap& assignments, ConditionMap& conditions, const ConditionMap& forbidden = ConditionMap()) const;
225 :
226 : int getDetectorPriority(const InductLoopInfo& loopInfo) const;
227 :
228 : /// @brief count the number of active detectors for the given step
229 : int getPhasePriority(int step) const;
230 :
231 : /// @brief get the green phase following step and the transition time
232 : std::pair<int, SUMOTime> getTarget(int step) const;
233 :
234 : /// @brief whether the current phase cannot be continued due to linkMaxDur constraints
235 : bool maxLinkDurationReached();
236 :
237 : /// @brief whether the target phase is acceptable in light of linkMaxDur constraints
238 : bool canExtendLinkGreen(int target);
239 :
240 : /// @brief the minimum duratin for keeping the current phase due to linkMinDur constraints
241 : SUMOTime getLinkMinDuration(int target) const;
242 :
243 : /// @brief whether a given link has only weak mode foes that are green in the given state
244 : bool weakConflict(int linkIndex, const std::string& state) const;
245 :
246 : template<typename T, SumoXMLTag Tag>
247 47527 : const T* retrieveDetExpression(const std::string& arg, const std::string& expr, bool tryPrefix) const {
248 30102 : const T* det = dynamic_cast<const T*>(
249 47527 : MSNet::getInstance()->getDetectorControl().getTypedDetectors(Tag).get(
250 47527 : (tryPrefix ? myDetectorPrefix : "") + arg));
251 47527 : if (det == nullptr) {
252 17425 : if (tryPrefix) {
253 : // try again without prefix
254 13156 : return retrieveDetExpression<T, Tag>(arg, expr, false);
255 : } else {
256 8538 : throw ProcessError("Unknown detector '" + arg + "' in expression '" + expr + "'");
257 : }
258 : } else {
259 : return det;
260 : }
261 : }
262 :
263 : /// find green phases target by a next attribute
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 :
269 : protected:
270 : /// @brief A map from phase to induction loops to be used for gap control
271 : InductLoopMap myInductLoopsForPhase;
272 : std::vector<std::vector<const MSLink*> > myCrossingsForPhase;
273 :
274 : std::vector<InductLoopInfo> myInductLoops;
275 :
276 : /// @brief extra loops for output/tracking
277 : std::vector<const MSInductLoop*> myExtraLoops;
278 : std::vector<const MSE2Collector*> myExtraE2;
279 :
280 : /// The maximum gap to check in seconds
281 : double myMaxGap;
282 :
283 : /// The minimum continuous occupancy time to mark a detector as jammed
284 : double myJamThreshold;
285 :
286 : /// The passing time used in seconds
287 : double myPassingTime;
288 :
289 : /// The detector distance in seconds
290 : double myDetectorGap;
291 :
292 : /// The time threshold to avoid starved phases
293 : SUMOTime myInactiveThreshold;
294 :
295 : /// Whether the detectors shall be shown in the GUI
296 : bool myShowDetectors;
297 :
298 : /// Whether all detectors shall be built
299 : bool myBuildAllDetectors;
300 :
301 : /// Whether any of the phases has multiple targets
302 : bool myHasMultiTarget;
303 :
304 : /// The output file for generated detectors
305 : std::string myFile;
306 :
307 : /// The frequency for aggregating detector output
308 : SUMOTime myFreq;
309 :
310 : /// Whether detector output separates by vType
311 : std::string myVehicleTypes;
312 :
313 : /// @brief last time trySwitch was called
314 : SUMOTime myLastTrySwitchTime;
315 :
316 : /// @brief consecutive time that the given link index has been green
317 : std::vector<SUMOTime> myLinkGreenTimes;
318 : std::vector<SUMOTime> myLinkRedTimes;
319 : /// @brief maximum consecutive time that the given link may remain green
320 : std::vector<SUMOTime> myLinkMaxGreenTimes;
321 : /// @brief minimum consecutive time that the given link must remain green
322 : std::vector<SUMOTime> myLinkMinGreenTimes;
323 :
324 : /// @brief The custom switching conditions
325 : ConditionMap myConditions;
326 :
327 : /// @brief The condition assignments
328 : AssignmentMap myAssignments;
329 :
330 : /// @brief The loaded functions
331 : FunctionMap myFunctions;
332 :
333 : /// @brief The function call stack;
334 : mutable std::vector<std::map<std::string, double> > myStack;
335 :
336 : /// @brief the conditions which shall be listed in GUITLLogicPhasesTrackerWindow
337 : std::set<std::string> myListedConditions;
338 :
339 : /// @brief whether the next switch time was requested via TraCI
340 : bool myTraCISwitch;
341 :
342 : struct SwitchingRules {
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 : };
|