Line data Source code
1 : /****************************************************************************/ 2 : // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo 3 : // Copyright (C) 2011-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 NBLoadedSUMOTLDef.h 15 : /// @author Jakob Erdmann 16 : /// @author Michael Behrisch 17 : /// @date Mar 2011 18 : /// 19 : // A complete traffic light logic loaded from a sumo-net. (opted to reimplement 20 : // since NBLoadedTLDef is quite vissim specific) 21 : /****************************************************************************/ 22 : #pragma once 23 : #include <config.h> 24 : 25 : #include <vector> 26 : #include <string> 27 : #include <set> 28 : #include "NBNode.h" 29 : #include "NBEdgeCont.h" 30 : #include "NBTrafficLightDefinition.h" 31 : #include "NBTrafficLightLogic.h" 32 : #include <utils/common/SUMOTime.h> 33 : 34 : 35 : // =========================================================================== 36 : // class definitions 37 : // =========================================================================== 38 : /** 39 : * @class NBLoadedSUMOTLDef 40 : * @brief A loaded (complete) traffic light logic 41 : */ 42 : class NBLoadedSUMOTLDef : public NBTrafficLightDefinition { 43 : public: 44 : 45 : /** @brief Constructor 46 : * @param[in] id The id of the tls 47 : * @param[in] programID The programID for the computed logic 48 : * @param[in] offset The offset for the computed logic 49 : * @param[in] type The algorithm type for the computed logic 50 : */ 51 : NBLoadedSUMOTLDef(const std::string& id, const std::string& programID, SUMOTime offset, TrafficLightType type); 52 : 53 : /** @brief Constructor that copies from an existing definition and its computed logic (used by netedit) 54 : * @param[in] def The definition to copy 55 : * @param[in] logic The computed logic of the given def 56 : */ 57 : NBLoadedSUMOTLDef(const NBTrafficLightDefinition& def, const NBTrafficLightLogic& logic); 58 : 59 : 60 : /// @brief Destructor 61 : ~NBLoadedSUMOTLDef(); 62 : 63 : void setID(const std::string& newID); 64 : 65 : /** @brief Sets the programID 66 : * @param[in] programID The new ID of the program (subID) 67 : */ 68 : void setProgramID(const std::string& programID); 69 : 70 : /** @brief Informs edges about being controlled by a tls 71 : */ 72 : void setTLControllingInformation() const; 73 : 74 : /** @brief Replaces occurrences of the removed edge in incoming/outgoing edges of all definitions 75 : * @param[in] removed The removed edge 76 : * @param[in] incoming The edges to use instead if an incoming edge was removed 77 : * @param[in] outgoing The edges to use instead if an outgoing edge was removed 78 : */ 79 : void remapRemoved(NBEdge* removed, 80 : const EdgeVector& incoming, const EdgeVector& outgoing); 81 : 82 : 83 : /** @brief Replaces a removed edge/lane 84 : * @param[in] removed The edge to replace 85 : * @param[in] removedLane The lane of this edge to replace 86 : * @param[in] by The edge to insert instead 87 : * @param[in] byLane This edge's lane to insert instead 88 : */ 89 : void replaceRemoved(NBEdge* removed, int removedLane, 90 : NBEdge* by, int byLane, bool incoming); 91 : 92 : /** @brief patches signal plans by modifying lane indices 93 : * with the given offset, only indices with a value above threshold are modified 94 : */ 95 : void shiftTLConnectionLaneIndex(NBEdge* edge, int offset, int threshold = -1); 96 : 97 : /** @brief Adds a phase to the logic 98 : * the new phase is inserted at the end of the list of already added phases 99 : * @param[in] duration The duration of the phase to add 100 : * @param[in] state The state definition of a tls phase 101 : * @param[in] minDur The minimum duration of the phase to add 102 : * @param[in] maxDur The maximum duration of the phase to add 103 : * @param[in] vehExt The vehExt of the phase to add 104 : * @param[in] yellow The yellow of the phase to add 105 : * @param[in] red The red of the phase to add 106 : * @param[in] earliestEnd The early end of the phase to add 107 : * @param[in] latestEnd The latest end of the phase to add 108 : */ 109 : void addPhase(const SUMOTime duration, const std::string& state, const SUMOTime minDur, const SUMOTime maxDur, 110 : const SUMOTime earliestEnd, const SUMOTime latestEnd, const SUMOTime vehExt, const SUMOTime yellow, 111 : const SUMOTime red, const std::vector<int>& next, const std::string& name); 112 : 113 : /// @brief mark phases as load 114 : void phasesLoaded() { 115 227 : myPhasesLoaded = true; 116 227 : } 117 : 118 : /** @brief Adds a connection and immediately informs the edges 119 : */ 120 : void addConnection(NBEdge* from, NBEdge* to, int fromLane, int toLane, int linkIndex, int linkIndex2, bool reconstruct = true); 121 : 122 : 123 : /** @brief removes the given connection from the traffic light 124 : * if recontruct=true, reconstructs the logic and informs the edges for immediate use in netedit 125 : * @note: tlIndex is not necessarily unique. we need the whole connection data here 126 : */ 127 : void removeConnection(const NBConnection& conn, bool reconstruct = true); 128 : 129 : /// @brief register changes that necessitate recomputation 130 : void registerModifications(bool addedConnections, bool removedConnections); 131 : 132 : /** @brief Returns the internal logic 133 : */ 134 : NBTrafficLightLogic* getLogic() { 135 52 : return myTLLogic; 136 : } 137 : 138 : /** @brief Sets the offset of this tls 139 : * @param[in] offset The offset of this cycle 140 : */ 141 : void setOffset(SUMOTime offset); 142 : 143 : /** @brief Sets the algorithm type of this tls 144 : * @param[in] offset The algorithm type of this tls 145 : */ 146 : void setType(TrafficLightType type); 147 : 148 : /// @brief whether the given index must yield to the foeIndex while turing right on a red light 149 : bool rightOnRedConflict(int index, int foeIndex) const; 150 : 151 : /* @brief shortens phase states to remove states that are not referenced by 152 : * any controlled link and returns whether states were shortened 153 : */ 154 : bool cleanupStates(); 155 : 156 : /// @brief whether this definition uses signal group (multiple connections with the same link index) 157 : bool usingSignalGroups() const; 158 : 159 : /// @brief join nodes and states from the given logic (append red state) 160 : void joinLogic(NBTrafficLightDefinition* def); 161 : 162 : /// @brief heuristically add minDur and maxDur when switching from tlType fixed to actuated 163 : void guessMinMaxDuration(); 164 : 165 : /// @brief let connections with the same state use the same link index 166 : void groupSignals(); 167 : 168 : /// @brief let all connections use a distinct link index 169 : void ungroupSignals(); 170 : 171 : /// @brief copy the assignment of link indices to connections from the given definition and rebuilt the states to match 172 : // Note: Issues a warning when the grouping of def is incompatible with the current states 173 : void copyIndices(NBTrafficLightDefinition* def); 174 : 175 : /// @brief perform optional final checks (on writing) 176 : void finalChecks() const; 177 : 178 : /// @brief replace the given link index in all connections 179 : void replaceIndex(int oldIndex, int newIndex); 180 : 181 : protected: 182 : /// @brief Collects the links participating in this traffic light (only if not previously loaded) 183 : void collectLinks(); 184 : 185 : /// @brief Build the list of participating edges 186 : void collectEdges(); 187 : 188 : /** @brief Computes the traffic light logic finally in dependence to the type 189 : * @param[in] brakingTime Duration a vehicle needs for braking in front of the tls in seconds 190 : * @return The computed logic 191 : */ 192 : NBTrafficLightLogic* myCompute(int brakingTimeSeconds); 193 : 194 : bool amInvalid() const; 195 : 196 : /// @brief initialize myNeedsContRelation and set myNeedsContRelationReady to true 197 : void initNeedsContRelation() const; 198 : 199 : /// @brief return the highest known tls link index used by any controlled connection or crossing 200 : int getMaxIndex(); 201 : 202 : ///@brief Returns the maximum index controlled by this traffic light 203 : int getMaxValidIndex(); 204 : 205 : /// @brief get all states for the given link index 206 : std::string getStates(int index); 207 : 208 : /// @brief return whether the given link index is used by any connectons 209 : bool isUsed(int index) const; 210 : 211 : /// brief retrieve all edges with connections that use the given traffic light index 212 : std::set<const NBEdge*> getEdgesUsingIndex(int index) const; 213 : 214 : private: 215 : 216 : /// @brief phases are added directly to myTLLogic which is then returned in myCompute() 217 : NBTrafficLightLogic* myTLLogic; 218 : 219 : /// @brief repair the plan if controlled nodes received pedestrian crossings 220 : void patchIfCrossingsAdded(); 221 : 222 : /// @brief set of edges with shifted lane indices (to avoid shifting twice) 223 : std::set<NBEdge*> myShifted; 224 : 225 : /// @brief whether the logic must be reconstructed 226 : bool myReconstructAddedConnections; 227 : bool myReconstructRemovedConnections; 228 : bool myPhasesLoaded; 229 : 230 : /** @brief Collects the edges for each tlIndex 231 : * @param[out] fromEdges The from-edge for each controlled tlIndex 232 : * @param[out] toEdges The to-edge for each controlled tlIndex 233 : * @param[out] fromLanes The from-lanes for each controlled tlIndex 234 : */ 235 : void collectEdgeVectors(EdgeVector& fromEdges, EdgeVector& toEdges, std::vector<int>& fromLanes) const; 236 : 237 : /// @brief adapt to removal or addition of connections 238 : void reconstructLogic(); 239 : 240 : /// @brief return whether all tls link indices are valid 241 : bool hasValidIndices() const; 242 : 243 : private: 244 : /// @brief class for identifying connections 245 : class connection_equal { 246 : public: 247 : /// constructor 248 24 : connection_equal(const NBConnection& c) : myC(c) {} 249 : 250 48682 : bool operator()(const NBConnection& c) const { 251 62087 : return c.getFrom() == myC.getFrom() && c.getTo() == myC.getTo() && 252 51337 : c.getFromLane() == myC.getFromLane() && c.getToLane() == myC.getToLane(); 253 : } 254 : private: 255 : const NBConnection& myC; 256 : }; 257 : 258 : };