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 NBLoadedTLDef.h 15 : /// @author Daniel Krajzewicz 16 : /// @author Sascha Krieg 17 : /// @date Fri, 29.04.2005 18 : /// 19 : // A loaded (complete) traffic light logic 20 : /****************************************************************************/ 21 : #pragma once 22 : #include <config.h> 23 : 24 : #include <vector> 25 : #include <string> 26 : #include <set> 27 : #include "NBTrafficLightDefinition.h" 28 : #include "NBNode.h" 29 : #include <utils/common/SUMOTime.h> 30 : 31 : 32 : // =========================================================================== 33 : // class definitions 34 : // =========================================================================== 35 : /** 36 : * @class NBLoadedTLDef 37 : * @brief A loaded (complete) traffic light logic 38 : */ 39 : class NBLoadedTLDef : public NBTrafficLightDefinition { 40 : public: 41 : /** @class SignalGroup 42 : * @brief A single signal group, may control several connections 43 : */ 44 : class SignalGroup : public Named { 45 : public: 46 : /** @brief Constructor 47 : * @param[in] id The id of the signal group 48 : */ 49 : SignalGroup(const std::string& id); 50 : 51 : /// @brief Destructor 52 : ~SignalGroup(); 53 : 54 : /** @brief Inserts a controlled connection 55 : * @param[in] c The connection to be controlled by this signal group 56 : */ 57 : void addConnection(const NBConnection& c); 58 : 59 : /** @brief Sets the begin of a phase 60 : * @param[in] time The time at which the phase starts 61 : * @param[in] color The color of this signal starting at the given time 62 : */ 63 : void addPhaseBegin(SUMOTime time, TLColor color); 64 : 65 : /** @brief Sets the times for redyellow and yellow 66 : * @param[in] tRedYellowe The duration of the redyellow phase 67 : * @param[in] tYellow The duration of the yellow phase 68 : */ 69 : void setYellowTimes(SUMOTime tRedYellowe, SUMOTime tYellow); 70 : 71 : /** @brief Returns the times at which the signal switches 72 : * @param[in] cycleDuration The duration of the complete cycle 73 : * @return The switch times of this signal 74 : */ 75 : std::vector<SUMOTime> getTimes(SUMOTime cycleDuration) const; 76 : 77 : /** @brief Sorts the phases */ 78 : void sortPhases(); 79 : 80 : /** @brief Returns the number of links (connection) controlled by this signal 81 : * @return The number of links controlled by this signal 82 : */ 83 : int getLinkNo() const; 84 : 85 : /** @brief Returns whether vehicles on controlled links may drive at the given time 86 : * @param[in] time The regarded time 87 : * @return Whether vehicles may drive at this time 88 : */ 89 : bool mayDrive(SUMOTime time) const; 90 : 91 : /** @brief Returns whether controlled links have yellow at the given time 92 : * @param[in] time The regarded time 93 : * @return Whether controlled links are yellow at this time 94 : */ 95 : bool hasYellow(SUMOTime time) const; 96 : 97 : /** @brief Returns whether this signal controls the given edge 98 : * @param[in] from The incoming edge 99 : * @return Whether this edge is controlled by this signal 100 : */ 101 : bool containsIncoming(NBEdge* from) const; 102 : 103 : /** @brief Replaces the given incoming edge by the others given 104 : * @param[in] which The edge to replace 105 : * @param[in] by The replacements 106 : */ 107 : void remapIncoming(NBEdge* which, const EdgeVector& by); 108 : 109 : /** @brief Returns whether this signal controls a connection where the given edge is the destination 110 : * @param[in] from The outgoing edge 111 : * @return Whether this edge's predecessing edge is controlled by this signal 112 : */ 113 : bool containsOutgoing(NBEdge* to) const; 114 : 115 : /** @brief Replaces the given outgoing edge by the others given 116 : * @param[in] which The edge to replace 117 : * @param[in] by The replacements 118 : */ 119 : void remapOutgoing(NBEdge* which, const EdgeVector& by); 120 : 121 : /** @brief Returns the connection at the given index 122 : * @param[in] pos The position within this signal 123 : * @return The connection at the given index 124 : */ 125 : const NBConnection& getConnection(int pos) const; 126 : 127 : /** @brief Sets the yellow time 128 : * @param[in] tyellow The yellow time to set in seconds 129 : * @param[in] forced Whether resetting tyellow was forced by the user by setting "tls.yellow.patch-small" 130 : */ 131 : void patchTYellow(SUMOTime tyellow, bool forced); 132 : 133 : /** @brief Replaces a removed edge/lane 134 : * @param[in] removed The edge to replace 135 : * @param[in] removedLane The lane of this edge to replace 136 : * @param[in] by The edge to insert instead 137 : * @param[in] byLane This edge's lane to insert instead 138 : */ 139 : void remap(NBEdge* removed, int removedLane, NBEdge* by, int byLane); 140 : 141 : /** @class PhaseDef 142 : * @brief Definition of a single, loaded phase 143 : */ 144 : class PhaseDef { 145 : public: 146 : /** @brief Constructor 147 : * @param[in] time The begin time of this phase 148 : * @param[in] color A signal's color from this time 149 : */ 150 : PhaseDef(SUMOTime time, TLColor color) 151 301 : : myTime(time), myColor(color) { } 152 : 153 : /// @brief The begin time of this phase 154 : SUMOTime myTime; 155 : /// @brief A signal's color from this time 156 : TLColor myColor; 157 : }; 158 : 159 : private: 160 : /// @brief Connections controlled by this signal 161 : NBConnectionVector myConnections; 162 : /// @brief The phases of this signal 163 : std::vector<PhaseDef> myPhases; 164 : /// @brief The times of redyellow and yellow 165 : SUMOTime myTRedYellow, myTYellow; 166 : }; 167 : 168 : 169 : 170 : /// @brief Definition of the container for signal groups 171 : typedef std::map<std::string, SignalGroup*> SignalGroupCont; 172 : 173 : 174 : /** @brief Constructor 175 : * @param[in] id The id of the tls 176 : * @param[in] junctions Junctions controlled by this tls 177 : * @param[in] offset The offset of the plan 178 : * @param[in] type The algorithm type for the computed traffic light 179 : */ 180 : NBLoadedTLDef(const NBEdgeCont& ec, const std::string& id, 181 : const std::vector<NBNode*>& junctions, SUMOTime offset, 182 : TrafficLightType type); 183 : 184 : 185 : /** @brief Constructor 186 : * @param[in] id The id of the tls 187 : * @param[in] junction The junction controlled by this tls 188 : * @param[in] offset The offset of the plan 189 : * @param[in] type The algorithm type for the computed traffic light 190 : */ 191 : NBLoadedTLDef(const NBEdgeCont& ec, const std::string& id, NBNode* junction, SUMOTime offset, 192 : TrafficLightType type); 193 : 194 : 195 : /** @brief Constructor 196 : * @param[in] id The id of the tls 197 : * @param[in] offset The offset of the plan 198 : * @param[in] type The algorithm type for the computed traffic light 199 : */ 200 : NBLoadedTLDef(const NBEdgeCont& ec, const std::string& id, SUMOTime offset, 201 : TrafficLightType type); 202 : 203 : 204 : /// @brief Destructor 205 : ~NBLoadedTLDef(); 206 : 207 : 208 : /** @brief Sets the duration of a cycle 209 : * @param[in] cycleDur The duration of the cycle 210 : */ 211 : void setCycleDuration(SUMOTime cycleDur); 212 : 213 : 214 : /** @brief Adds a signal group 215 : * @param[in] id The id of the signal group 216 : */ 217 : void addSignalGroup(const std::string& id); 218 : 219 : 220 : /** @brief Adds a connection to a signal group 221 : * @param[in] groupid The id of the signal group to add the connection to 222 : * @param[in] connection The connection to add 223 : */ 224 : bool addToSignalGroup(const std::string& groupid, 225 : const NBConnection& connection); 226 : 227 : 228 : /** @brief Adds a list of connections to a signal group 229 : * @param[in] groupid The id of the signal group to add the connections to 230 : * @param[in] connections The connections to add 231 : */ 232 : bool addToSignalGroup(const std::string& groupid, 233 : const NBConnectionVector& connections); 234 : 235 : 236 : /** @brief Sets the information about the begin of a phase 237 : * @param[in] groupid The id of the signal group to add the phase to 238 : * @param[in] time The time the phase starts at 239 : * @param[in] color The color of the signal during this phase 240 : */ 241 : void addSignalGroupPhaseBegin(const std::string& groupid, 242 : SUMOTime time, TLColor color); 243 : 244 : 245 : /** @brief Sets the times the light is yellow or red/yellow 246 : * @param[in] groupid The id of the signal group to add the phase to 247 : * @param[in] tRedYellow The duration of redyellow 248 : * @param[in] tYellow The duration of yellow 249 : */ 250 : void setSignalYellowTimes(const std::string& groupid, 251 : SUMOTime tRedYellow, SUMOTime tYellow); 252 : 253 : 254 : /// @name Public methods from NBTrafficLightDefinition-interface 255 : /// @{ 256 : 257 : /** @brief Informs edges about being controlled by a tls 258 : * @param[in] ec The container of edges 259 : * @see NBTrafficLightDefinition::setTLControllingInformation 260 : */ 261 : void setTLControllingInformation() const; 262 : 263 : 264 : /** @brief Replaces occurrences of the removed edge in incoming/outgoing edges of all definitions 265 : * @param[in] removed The removed edge 266 : * @param[in] incoming The edges to use instead if an incoming edge was removed 267 : * @param[in] outgoing The edges to use instead if an outgoing edge was removed 268 : * @see NBTrafficLightDefinition::remapRemoved 269 : */ 270 : void remapRemoved(NBEdge* removed, 271 : const EdgeVector& incoming, const EdgeVector& outgoing); 272 : 273 : /* initialize myNeedsContRelation and set myNeedsContRelationReady to true */ 274 : void initNeedsContRelation() const; 275 : 276 : ///@brief Returns the maximum index controlled by this traffic light 277 : int getMaxIndex(); 278 : 279 : /// @} 280 : 281 : 282 : protected: 283 : /// @name Protected methods from NBTrafficLightDefinition-interface 284 : /// @{ 285 : 286 : /** @brief Computes the traffic light logic finally in dependence to the type 287 : * @param[in] brakingTime Duration a vehicle needs for braking in front of the tls in seconds 288 : * @return The computed logic 289 : * @see NBTrafficLightDefinition::myCompute 290 : */ 291 : NBTrafficLightLogic* myCompute(int brakingTimeSeconds); 292 : 293 : /** @brief Builds the list of participating nodes/edges/links 294 : * @see NBTrafficLightDefinition::setParticipantsInformation 295 : */ 296 : void setParticipantsInformation(); 297 : 298 : 299 : /** @brief Collects the nodes participating in this traffic light 300 : * @see NBTrafficLightDefinition::collectNodes 301 : */ 302 : void collectNodes(); 303 : 304 : 305 : /** @brief Collects the links participating in this traffic light 306 : * @exception ProcessError If a link could not be found 307 : * @see NBTrafficLightDefinition::collectLinks 308 : */ 309 : void collectLinks(); 310 : 311 : 312 : /** @brief Returns the information whether a connection must brake, given a phase 313 : * @param[in] possProhibited The connection to investigate 314 : * @param[in] state The state 315 : * @param[in] strmpos The index of this connection within the masks 316 : * @return Whether the given connection must brake 317 : */ 318 : bool mustBrake(const NBConnection& possProhibited, 319 : const std::string& state, 320 : int strmpos) const; 321 : 322 : 323 : /** @brief Replaces a removed edge/lane 324 : * @param[in] removed The edge to replace 325 : * @param[in] removedLane The lane of this edge to replace 326 : * @param[in] by The edge to insert instead 327 : * @param[in] byLane This edge's lane to insert instead 328 : * @see NBTrafficLightDefinition::replaceRemoved 329 : */ 330 : void replaceRemoved(NBEdge* removed, int removedLane, 331 : NBEdge* by, int byLane, bool incoming); 332 : /// @} 333 : 334 : private: 335 : /** @brief Builds the phase for a given time 336 : * @param[in] ec The edge control to use 337 : * @param[in] time The time to build the phase for 338 : * @return The phase of this tls for the given time 339 : */ 340 : std::string buildPhaseState(const SUMOTime time) const; 341 : 342 : // pointer to the NBEdgeCont for checking edges 343 : const NBEdgeCont* myEdgeCont; 344 : 345 : 346 : private: 347 : /// @brief Controlled signal groups 348 : SignalGroupCont mySignalGroups; 349 : 350 : /// @brief The duration of a single cycle 351 : SUMOTime myCycleDuration; 352 : 353 : 354 : };