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 MSPhaseDefinition.h
15 : /// @author Daniel Krajzewicz
16 : /// @author Julia Ringel
17 : /// @author Jakob Erdmann
18 : /// @author Sascha Krieg
19 : /// @author Michael Behrisch
20 : /// @date Jan 2004
21 : ///
22 : // The definition of a single phase of a tls logic
23 : /****************************************************************************/
24 : #pragma once
25 : #include <config.h>
26 :
27 : #define TARGET_BIT 0
28 : #define TRANSIENT_NOTDECISIONAL_BIT 1
29 : #define COMMIT_BIT 2
30 : #define UNDEFINED_BIT 3
31 : #include <config.h>
32 :
33 : #include <bitset>
34 : #include <string>
35 : #include <vector>
36 : #include <utils/common/MsgHandler.h>
37 : #include <utils/common/SUMOTime.h>
38 : #include <utils/options/OptionsCont.h>
39 : #include <utils/xml/SUMOXMLDefinitions.h>
40 :
41 :
42 : // ===========================================================================
43 : // class definitions
44 : // ===========================================================================
45 : /**
46 : * @class MSPhaseDefinition
47 : * @brief The definition of a single phase of a tls logic
48 : */
49 :
50 :
51 : class MSPhaseDefinition {
52 : public:
53 :
54 : static const SUMOTime UNSPECIFIED_DURATION = -1;
55 : static const SUMOTime OVERRIDE_DURATION = TIME2STEPS(-1);
56 :
57 : /// @brief The duration of the phase
58 : SUMOTime duration;
59 :
60 : /// @brief The previous duration of the phase
61 : SUMOTime lastDuration;
62 :
63 : /// @brief The minimum duration of the phase
64 : SUMOTime minDuration;
65 :
66 : /// @brief The maximum duration of the phase
67 : SUMOTime maxDuration;
68 :
69 : /// @brief The minimum time within the cycle for switching (for coordinated actuation)
70 : SUMOTime earliestEnd;
71 :
72 : /// @brief The maximum time within the cycle for switching (for coordinated actuation)
73 : SUMOTime latestEnd;
74 :
75 : /// @brief The condition expression for an early switch into this phase
76 : std::string earlyTarget;
77 :
78 : /// @brief The condition expression for switching into this phase when the active phase must end
79 : std::string finalTarget;
80 :
81 : /// @brief Stores the timestep of the last on-switched of the phase
82 : SUMOTime myLastSwitch;
83 :
84 : /// @brief Stores the timestep when the previous instance of this phase was switched off
85 : SUMOTime myLastEnd;
86 :
87 : /// @brief The index of the phase that suceeds this one (or -1)
88 : std::vector<int> nextPhases;
89 :
90 : /// @brief Optional name or description for the current phase
91 : std::string name;
92 :
93 : /// @brief for NEMA phase
94 : SUMOTime yellow;
95 :
96 : /// @brief for NEMA phase
97 : SUMOTime red;
98 :
99 : /// @brief for NEMA phase
100 : SUMOTime vehext;
101 :
102 : /// @brief the phase is a transient one or a decisional one, compulsory directive for SOTL policies
103 : bool myTransientNotDecisional;
104 :
105 : /// @brief the phase is a commit, compulsory directive for SOTL policies
106 : bool myCommit;
107 :
108 : /// @brief Leaving the phase type as "undefined" lets SOTL policies malfunction
109 : bool myUndefined;
110 :
111 : /*
112 : * @brief The lanes-set
113 : * This array can be empty if this phase is not a target step,
114 : * otherwise, a bit is true if the corresponding lane belongs to a
115 : * set of input lanes.
116 : * SOTL traffic light logics choose the target step according to sensors
117 : * belonging to the lane-set.
118 : */
119 : std::vector<std::string> myTargetLaneSet;
120 :
121 : private:
122 : /// @brief The phase definition
123 : std::string myState;
124 :
125 :
126 : public:
127 : /** @brief Constructor **/
128 520994 : MSPhaseDefinition(SUMOTime _duration, const std::string& state, const std::string& _name = ""):
129 520994 : duration(_duration),
130 520994 : lastDuration(UNSPECIFIED_DURATION),
131 520994 : minDuration(duration),
132 520994 : maxDuration(duration),
133 520994 : earliestEnd(UNSPECIFIED_DURATION),
134 520994 : latestEnd(UNSPECIFIED_DURATION),
135 520994 : myLastSwitch(UNSPECIFIED_DURATION),
136 520994 : myLastEnd(UNSPECIFIED_DURATION),
137 520994 : name(_name),
138 520994 : yellow(UNSPECIFIED_DURATION),
139 520994 : red(UNSPECIFIED_DURATION),
140 520994 : vehext(UNSPECIFIED_DURATION),
141 520994 : myTransientNotDecisional(false),
142 520994 : myCommit(false),
143 520994 : myUndefined(false),
144 1041988 : myState(state)
145 520994 : {}
146 :
147 :
148 : /// @brief Destructor
149 1447675 : virtual ~MSPhaseDefinition() { }
150 :
151 :
152 : /** @brief Returns the state within this phase
153 : * @return The state in this phase
154 : */
155 : const std::string& getState() const {
156 20244520 : return myState;
157 : }
158 :
159 : void setState(const std::string& _state) {
160 67 : myState = _state;
161 36759 : }
162 :
163 : const std::vector<std::string>& getTargetLaneSet() const {
164 14752 : return myTargetLaneSet;
165 : }
166 :
167 : const std::vector<int>& getNextPhases() const {
168 1047 : return nextPhases;
169 : }
170 :
171 : const std::string& getName() const {
172 31958 : return name;
173 : }
174 :
175 : void setName(const std::string& _name) {
176 9645 : name = _name;
177 9631 : }
178 :
179 : /** @brief Returns whether this phase is a pure "green" phase
180 : *
181 : * "pure green" means in this case that at least one stream has green
182 : * and no stream has yellow. Such phases are meant to be candidates
183 : * for being stretched by actuated or agentbased traffic light logics.
184 : * @return Whether this phase is a "pure green" phase
185 : */
186 540117 : bool isGreenPhase() const {
187 540117 : if (myState.find_first_of("gG") == std::string::npos) {
188 : return false;
189 : }
190 474816 : if (myState.find_first_of("yY") != std::string::npos) {
191 : return false;
192 : }
193 : return true;
194 : }
195 :
196 :
197 : /** @brief Returns whether this phase is an "all red" phase
198 : *
199 : * "all red " means in this case that at all streams have red
200 : * and no stream has yellow.
201 : * @return Whether this phase is an "all red" phase
202 : */
203 : bool isAllRedPhase() const {
204 0 : return (int)myState.size() == (int)std::count(myState.begin(), myState.end(), 'r');
205 : }
206 :
207 :
208 : /** @brief Returns the state of the tls signal at the given position
209 : * @param[in] pos The position of the signal to return the state for
210 : * @return The state of the signal at the given position
211 : */
212 : LinkState getSignalState(int pos) const {
213 288 : return (LinkState) myState[pos];
214 : }
215 :
216 : inline bool isActuated() const {
217 26658 : return minDuration != maxDuration || minDuration == OVERRIDE_DURATION;
218 : }
219 :
220 : /** @brief Comparison operator
221 : *
222 : * Note that only the state must differ, not the duration!
223 : * @param[in] pd The phase definition to compare against
224 : * @return Whether the given phase definition differs
225 : */
226 0 : bool operator!=(const MSPhaseDefinition& pd) {
227 0 : return myState != pd.myState || name != pd.name;
228 : }
229 :
230 :
231 : /*
232 : * @return true if the phase type is undefined
233 : */
234 : bool isUndefined() const {
235 576 : return myUndefined;
236 : }
237 :
238 : /*
239 : * @return true if this is a target phase
240 : */
241 : bool isTarget() const {
242 : return !myTargetLaneSet.empty();
243 : }
244 :
245 : /*
246 : * @return true if this is a transient phase
247 : */
248 : bool isTransient() const {
249 193865 : return myTransientNotDecisional;
250 : }
251 :
252 : /*
253 : * @return true if this is a decisional phase
254 : */
255 : bool isDecisional() const {
256 : return !myTransientNotDecisional;
257 : }
258 :
259 : /*
260 : * @return true if this is a commit phase
261 : */
262 : bool isCommit() const {
263 113288 : return myCommit;
264 : }
265 :
266 : };
|