Line data Source code
1 : /****************************************************************************/
2 : // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3 : // Copyright (C) 2001-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 NBTrafficLightLogic.h
15 : /// @author Daniel Krajzewicz
16 : /// @author Jakob Erdmann
17 : /// @author Michael Behrisch
18 : /// @date Sept 2002
19 : ///
20 : // A SUMO-compliant built logic for a traffic light
21 : /****************************************************************************/
22 : #pragma once
23 : #include <config.h>
24 :
25 : #include <vector>
26 : #include <string>
27 : #include <bitset>
28 : #include <utility>
29 : #include <set>
30 : #include "NBConnectionDefs.h"
31 : #include <utils/common/SUMOTime.h>
32 : #include <utils/common/Named.h>
33 : #include <utils/common/Parameterised.h>
34 :
35 :
36 : // ===========================================================================
37 : // class declarations
38 : // ===========================================================================
39 : class OutputDevice;
40 :
41 :
42 : // ===========================================================================
43 : // class definitions
44 : // ===========================================================================
45 : /**
46 : * @class NBTrafficLightLogic
47 : * @brief A SUMO-compliant built logic for a traffic light
48 : */
49 : class NBTrafficLightLogic : public Named, public Parameterised {
50 : public:
51 : /**
52 : * @class PhaseDefinition
53 : * @brief The definition of a single phase of the logic
54 : */
55 : class PhaseDefinition {
56 :
57 : public:
58 : /// @brief The duration of the phase in s
59 : SUMOTime duration;
60 :
61 : /// @brief The state definition
62 : std::string state;
63 :
64 : /// @brief minimum duration (for actuated)
65 : SUMOTime minDur;
66 :
67 : /// @brief maximum duration duration (for actuated)
68 : SUMOTime maxDur;
69 :
70 : /// @brief minimum duration (for actuated)
71 : SUMOTime earliestEnd;
72 :
73 : /// @brief maximum duration duration (for actuated)
74 : SUMOTime latestEnd;
75 :
76 : /// @brief veh ext (for NEMA)
77 : SUMOTime vehExt;
78 :
79 : /// @brief yellow (for NEMA)
80 : SUMOTime yellow;
81 :
82 : /// @brief red (for NEMA)
83 : SUMOTime red;
84 :
85 : /// @brief next phase indices or empty list
86 : std::vector<int> next;
87 :
88 : /// @brief option phase name
89 : std::string name;
90 :
91 : /** @brief Constructor
92 : * @param[in] durationArg The duration of the phase
93 : * @param[in] stateArg Signals per link
94 : */
95 17145 : PhaseDefinition(const SUMOTime duration_, const std::string& state_, const SUMOTime minDur_, const SUMOTime maxDur_,
96 : const SUMOTime earliestEnd_, const SUMOTime latestEnd_, const SUMOTime vehExt_, const SUMOTime yellow_,
97 17145 : const SUMOTime red_, const std::vector<int>& next_, const std::string& name_) :
98 17145 : duration(duration_),
99 17145 : state(state_),
100 17145 : minDur(minDur_),
101 17145 : maxDur(maxDur_),
102 17145 : earliestEnd(earliestEnd_),
103 17145 : latestEnd(latestEnd_),
104 17145 : vehExt(vehExt_),
105 17145 : yellow(yellow_),
106 17145 : red(red_),
107 17145 : next(next_),
108 17145 : name(name_)
109 17145 : { }
110 :
111 : /// @brief Destructor
112 58909 : ~PhaseDefinition() { }
113 :
114 : /** @brief Comparison operator
115 : * @param[in] pd A second phase
116 : * @return Whether this and the given phases are same
117 : */
118 : bool operator!=(const PhaseDefinition& pd) const {
119 : return ((pd.duration != duration) ||
120 : (pd.minDur != minDur) ||
121 : (pd.maxDur != maxDur) ||
122 : (pd.earliestEnd != earliestEnd) ||
123 : (pd.latestEnd != latestEnd) ||
124 : (pd.state != state) ||
125 : (pd.next != next) ||
126 : (pd.name != name));
127 : }
128 :
129 : };
130 :
131 :
132 : /** @brief Constructor
133 : * @param[in] id The id of the traffic light
134 : * @param[in] subid The id of the program
135 : * @param[in] noLinks Number of links that are controlled by this tls. 0 means the value is not known beforehand
136 : * @param[in] offset The offset of the program (delay)
137 : * @param[in] type The algorithm type for the computed traffic light
138 : */
139 : NBTrafficLightLogic(const std::string& id, const std::string& subid, int noLinks,
140 : SUMOTime offset = 0, TrafficLightType type = TrafficLightType::STATIC);
141 :
142 :
143 : /** @brief Copy Constructor
144 : * @param[in] logic The logic to copy
145 : */
146 : NBTrafficLightLogic(const NBTrafficLightLogic* logic);
147 :
148 :
149 : /// @brief Destructor
150 : ~NBTrafficLightLogic();
151 :
152 :
153 : /** @brief Adds a phase to the logic (static)
154 : *
155 : * @param[in] duration The duration of the phase to add
156 : * @param[in] state The state definition of a tls phase
157 : * @param[in] name The name of the phase
158 : * @param[in] next The index of the next phase
159 : * @param[in] index The index of the new phase (-1 means append to end)
160 : * @note: the length of the state has to match the number of links
161 : * and the length given in previous calls to addStep (throws ProcessError)
162 : */
163 : void addStep(const SUMOTime duration, const std::string& state, const std::vector<int>& next = std::vector<int>(),
164 : const std::string& name = "", const int index = -1);
165 :
166 : /** @brief Adds a phase to the logic (actuated)
167 : *
168 : * @param[in] duration The duration of the phase to add
169 : * @param[in] state The state definition of a tls phase
170 : * @param[in] name The name of the phase
171 : * @param[in] minDur The minimum duration of the phase to add
172 : * @param[in] maxDur The maximum duration of the phase to add
173 : * @param[in] earliestEnd The earliest end of the phase to add
174 : * @param[in] latestEnd The latest end of the phase to add
175 : * @param[in] vehExt The vehExt of the phase to add
176 : * @param[in] yellow The yellow of the phase to add
177 : * @param[in] red The red of the phase to add
178 : * @param[in] next The index of the next phase
179 : * @param[in] index The index of the new phase (-1 means append to end)
180 : * @note: the length of the state has to match the number of links
181 : * and the length given in previous calls to addStep (throws ProcessError)
182 : */
183 : void addStep(const SUMOTime duration, const std::string& state, const SUMOTime minDur, const SUMOTime maxDur, const SUMOTime earliestEnd,
184 : const SUMOTime latestEnd,
185 : const SUMOTime vehExt = -1, // UNSPECIFIED_DURATION
186 : const SUMOTime yellow = -1, // UNSPECIFIED_DURATION
187 : const SUMOTime red = -1, // UNSPECIFIED_DURATION
188 : const std::string& name = "",
189 : const std::vector<int>& next = std::vector<int>(),
190 : int index = -1);
191 :
192 : /** @brief Modifies the state for an existing phase (used by netedit)
193 : * @param[in] phaseIndex The index of the phase to modify
194 : * @param[in] tlIndex The index at which to modify the state
195 : * @param[in] linkState The new link state for the given index
196 : */
197 : void setPhaseState(int phaseIndex, int tlIndex, LinkState linkState);
198 :
199 : /** @brief Modifies the duration for an existing phase (used by netedit)
200 : * @param[in] phaseIndex The index of the phase to modify
201 : * @param[in] duration The new duration for this phase
202 : */
203 : void setPhaseDuration(int phaseIndex, SUMOTime duration);
204 :
205 : /** @brief Modifies the min duration for an existing phase (used by netedit)
206 : * @param[in] phaseIndex The index of the phase to modify
207 : * @param[in] duration The new min duration for this phase
208 : */
209 : void setPhaseMinDuration(int phaseIndex, SUMOTime duration);
210 :
211 : /** @brief Modifies the max duration for an existing phase (used by netedit)
212 : * @param[in] phaseIndex The index of the phase to modify
213 : * @param[in] duration The new max duration for this phase
214 : */
215 : void setPhaseMaxDuration(int phaseIndex, SUMOTime duration);
216 :
217 : /** @brief Modifies the min duration for an existing phase (used by netedit)
218 : * @param[in] phaseIndex The index of the phase to modify
219 : * @param[in] duration The new earliestEnd for this phase
220 : */
221 : void setPhaseEarliestEnd(int phaseIndex, SUMOTime duration);
222 :
223 : /** @brief Modifies the max duration for an existing phase (used by netedit)
224 : * @param[in] phaseIndex The index of the phase to modify
225 : * @param[in] duration The new latestEnd for this phase
226 : */
227 : void setPhaseLatestEnd(int phaseIndex, SUMOTime duration);
228 :
229 : /** @brief Modifies the veh ex for an existing phase (used by netedit)
230 : * @param[in] phaseIndex The index of the phase to modify
231 : * @param[in] duration The new vehEx for this phase
232 : */
233 : void setPhaseVehExt(int phaseIndex, SUMOTime duration);
234 :
235 : /** @brief Modifies the veh ex for an existing phase (used by netedit)
236 : * @param[in] phaseIndex The index of the phase to modify
237 : * @param[in] duration The new vehEx for this phase
238 : */
239 : void setPhaseYellow(int phaseIndex, SUMOTime duration);
240 :
241 : /** @brief Modifies the veh ex for an existing phase (used by netedit)
242 : * @param[in] phaseIndex The index of the phase to modify
243 : * @param[in] duration The new vehEx for this phase
244 : */
245 : void setPhaseRed(int phaseIndex, SUMOTime duration);
246 :
247 : /** @brief Modifies the next phase (used by netedit)
248 : * @param[in] phaseIndex The index of the phase to modify
249 : * @param[in] duration The new duration for this phase
250 : */
251 : void setPhaseNext(int phaseIndex, const std::vector<int>& next);
252 :
253 : /** @brief Modifies the phase name (used by netedit)
254 : * @param[in] phaseIndex The index of the phase to modify
255 : * @param[in] duration The new duration for this phase
256 : */
257 : void setPhaseName(int phaseIndex, const std::string& name);
258 :
259 : /** @brief override state with the given character(used by netedit)
260 : * @param[in] phaseIndex The index of the phase to modify
261 : * @param[in] c the character to override (r, y, g, G...)
262 : */
263 : void overrideState(int phaseIndex, const char c);
264 :
265 : /* @brief deletes the phase at the given index
266 : * @note throws InvalidArgument on out-of range index
267 : */
268 : void deletePhase(int index);
269 :
270 : /* @brief swap phases
271 : * @note throws InvalidArgument on out-of range index
272 : */
273 : void swapPhase(int indexPhaseA, int indexPhaseB);
274 :
275 : /// @brief swap first phase
276 : void swapfirstPhase();
277 :
278 : /// @brief swap first phase
279 : void swaplastPhase();
280 :
281 : /* @brief changes state size either by cutting of at the end or by adding
282 : * new states at the end
283 : */
284 : void setStateLength(int numLinks, LinkState fill = LINKSTATE_TL_RED);
285 :
286 : /// @brief remove the index from all phase states
287 : void deleteStateIndex(int index);
288 :
289 : /// @brief deletes all phases and reset the expect number of links
290 : void resetPhases();
291 :
292 : /** @brief closes the building process
293 : *
294 : * Joins equal steps.
295 : */
296 : void closeBuilding(bool checkVarDurations = true);
297 :
298 : /// @brief Returns the duration of the complete cycle
299 : SUMOTime getDuration() const;
300 :
301 : /** @brief Sets the offset of this tls
302 : * @param[in] offset The offset of this cycle
303 : */
304 : void setOffset(SUMOTime offset) {
305 7 : myOffset = offset;
306 4 : }
307 :
308 : /// @brief Returns the ProgramID
309 : const std::string& getProgramID() const {
310 4197 : return mySubID;
311 : };
312 :
313 : /// @brief Returns the phases
314 : const std::vector<PhaseDefinition>& getPhases() const {
315 547 : return myPhases;
316 : }
317 :
318 : /// @brief copy phase values in other
319 : void copyPhase(const int origin, const int destination) {
320 : myPhases.at(destination) = myPhases.at(origin);
321 : }
322 :
323 : /// @brief Returns the offset of first switch
324 : SUMOTime getOffset() const {
325 2727 : return myOffset;
326 : };
327 :
328 : /// @brief Returns the number of participating links
329 : int getNumLinks() {
330 19933 : return myNumLinks;
331 : }
332 :
333 : /// @brief get the algorithm type (static etc..)
334 : TrafficLightType getType() const {
335 5475 : return myType;
336 : }
337 :
338 : /// @brief set the algorithm type (static etc..)
339 : void setType(TrafficLightType type) {
340 5 : myType = type;
341 : }
342 :
343 : /** @brief Sets the programID
344 : * @param[in] programID The new ID of the program (subID)
345 : */
346 : void setProgramID(const std::string& programID) {
347 0 : mySubID = programID;
348 : }
349 :
350 : private:
351 : /// @brief The number of participating links
352 : int myNumLinks;
353 :
354 : /// @brief The tls program's subid
355 : std::string mySubID;
356 :
357 : /// @brief The tls program's offset
358 : SUMOTime myOffset;
359 :
360 : /// @brief Definition of a vector of traffic light phases
361 : typedef std::vector<PhaseDefinition> PhaseDefinitionVector;
362 :
363 : /// @brief The junction logic's storage for traffic light phase list
364 : PhaseDefinitionVector myPhases;
365 :
366 : /// @brief The algorithm type for the traffic light
367 : TrafficLightType myType;
368 :
369 : /// @brief Invalidated assignment operator
370 : NBTrafficLightLogic& operator=(const NBTrafficLightLogic& s) = delete;
371 : };
|