Eclipse SUMO - Simulation of Urban MObility
NBOwnTLDef.h
Go to the documentation of this file.
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 /****************************************************************************/
20 // A traffic light logics which must be computed (only nodes/edges are given)
21 /****************************************************************************/
22 #pragma once
23 #include <config.h>
24 
25 #include <vector>
26 #include <set>
29 #include "NBNode.h"
30 
31 
32 // ===========================================================================
33 // class declarations
34 // ===========================================================================
35 
36 
37 // ===========================================================================
38 // class definitions
39 // ===========================================================================
45 public:
52  NBOwnTLDef(const std::string& id,
53  const std::vector<NBNode*>& junctions,
54  SUMOTime offset,
55  TrafficLightType type);
56 
57 
64  NBOwnTLDef(const std::string& id, NBNode* junction, SUMOTime offset,
65  TrafficLightType type);
66 
67 
73  NBOwnTLDef(const std::string& id, SUMOTime offset, TrafficLightType type);
74 
75 
77  ~NBOwnTLDef();
78 
79 
82 
89  void remapRemoved(NBEdge* removed,
90  const EdgeVector& incoming, const EdgeVector& outgoing);
91 
92 
96  void setTLControllingInformation() const;
98 
99 
101  void setSinglePhase() {
102  myHaveSinglePhase = true;
103  }
104 
106  static void addGreenWithin(NBTrafficLightLogic* logic, const EdgeVector& fromEdges, EdgeVector& toProc);
107 
109  static void addPedestrianScramble(NBTrafficLightLogic* logic, int totalNumLinks, SUMOTime greenTime, SUMOTime yellowTime,
110  const std::vector<NBNode::Crossing*>& crossings, const EdgeVector& fromEdges, const EdgeVector& toEdges);
111 
113  static std::string addPedestrianPhases(NBTrafficLightLogic* logic, const SUMOTime greenTime, const SUMOTime minDur, const SUMOTime maxDur,
114  const SUMOTime earliestEnd, const SUMOTime latestEnd,
115  std::string state, const std::vector<NBNode::Crossing*>& crossings, const EdgeVector& fromEdges, const EdgeVector& toEdges);
116 
118  static std::string patchStateForCrossings(const std::string& state, const std::vector<NBNode::Crossing*>& crossings, const EdgeVector& fromEdges, const EdgeVector& toEdges);
119 
120  static std::string patchNEMAStateForCrossings(const std::string& state,
121  const std::vector<NBNode::Crossing*>& crossings,
122  const EdgeVector& fromEdges,
123  const EdgeVector& toEdges,
124  const NBEdge* greenEdge, NBEdge* otherChosen);
125 
131  NBTrafficLightLogic* computeLogicAndConts(int brakingTimeSeconds, bool onlyConts = false);
132 
133  /* initialize myNeedsContRelation and set myNeedsContRelationReady to true */
134  void initNeedsContRelation() const;
135 
136  /* build optional all-red phase */
137  void buildAllRedState(SUMOTime allRedTime, NBTrafficLightLogic* logic, const std::string& state);
138 
140  int getMaxIndex();
141 
144  myLayout = layout;
145  }
146 
148  return myLayout;
149  }
150 
152  static const double MIN_SPEED_CROSSING_TIME;
153 
154 protected:
157 
163  NBTrafficLightLogic* myCompute(int brakingTimeSeconds);
164 
165 
170  void collectLinks();
171 
172 
180  void replaceRemoved(NBEdge* removed, int removedLane,
181  NBEdge* by, int byLane, bool incoming);
183 
184 
185 protected:
186 
188  bool corridorLike() const;
189 
191  const EdgeVector& fromEdges,
192  const EdgeVector& toEdges,
193  const std::vector<NBNode::Crossing*>& crossings,
194  const std::vector<std::pair<NBEdge*, NBEdge*> >& chosenList,
195  const std::vector<std::string>& straightStates,
196  const std::vector<std::string>& leftStates);
197 
199  std::string filterState(std::string state, const EdgeVector& fromEdges, const NBEdge* e);
200 
202  void filterMissingNames(std::vector<int>& vec, const std::map<int, int>& names, bool isBarrier);
203 
205  void fixDurationSum(NBTrafficLightLogic* logic, const std::map<int, int>& names, int ring1a, int ring1b, int ring2a, int ring2b);
206 
213 
214 
219  int getToPrio(const NBEdge* const e);
220 
221 
227  double computeUnblockedWeightedStreamNumber(const NBEdge* const e1, const NBEdge* const e2);
228 
229 
234  std::pair<NBEdge*, NBEdge*> getBestCombination(const EdgeVector& edges);
235 
236 
244  std::pair<NBEdge*, NBEdge*> getBestPair(EdgeVector& incoming);
245 
246 
248  static bool hasCrossing(const NBEdge* from, const NBEdge* to, const std::vector<NBNode::Crossing*>& crossings);
249 
251  static EdgeVector getConnectedOuterEdges(const EdgeVector& incoming);
252 
253 
255  std::string allowCompatible(std::string state, const EdgeVector& fromEdges, const EdgeVector& toEdges,
256  const std::vector<int>& fromLanes, const std::vector<int>& toLanes);
257 
258  std::string allowSingleEdge(std::string state, const EdgeVector& fromEdges);
259 
260  std::string allowFollowers(std::string state, const EdgeVector& fromEdges, const EdgeVector& toEdges);
261 
262  std::string allowPredecessors(std::string state, const EdgeVector& fromEdges, const EdgeVector& toEdges,
263  const std::vector<int>& fromLanes, const std::vector<int>& toLanes);
264 
265  std::string allowUnrelated(std::string state, const EdgeVector& fromEdges, const EdgeVector& toEdges,
266  const std::vector<bool>& isTurnaround,
267  const std::vector<NBNode::Crossing*>& crossings);
268 
269  std::string allowByVClass(std::string state, const EdgeVector& fromEdges, const EdgeVector& toEdges, SVCPermissions perm);
270 
272  bool forbidden(const std::string& state, int index, const EdgeVector& fromEdges, const EdgeVector& toEdges, bool allowCont);
273 
286  std::string correctConflicting(std::string state, const EdgeVector& fromEdges, const EdgeVector& toEdges,
287  const std::vector<bool>& isTurnaround,
288  const std::vector<int>& fromLanes,
289  const std::vector<int>& toLanes,
290  const std::vector<bool>& hadGreenMajor,
291  bool& haveForbiddenLeftMover,
292  std::vector<bool>& rightTurnConflicts,
293  std::vector<bool>& mergeConflicts);
294 
296  std::string correctMixed(std::string state, const EdgeVector& fromEdges,
297  const std::vector<int>& fromLanes,
298  bool& buildMixedGreenPhase, std::vector<bool>& mixedGreen);
299 
302 
304  void fixSuperfluousYellow(NBTrafficLightLogic* logic) const;
305 
307  void deactivateAlwaysGreen(NBTrafficLightLogic* logic) const;
308 
310  void deactivateInsideEdges(NBTrafficLightLogic* logic, const EdgeVector& fromEdges) const;
311 
313  SUMOTime computeEscapeTime(const std::string& state, const EdgeVector& fromEdges, const EdgeVector& toEdges) const;
314 
316  bool hasStraightConnection(const NBEdge* fromEdge);
317 
322  public:
327  int operator()(const NBEdge* const e1, const NBEdge* const e2) const {
328  if (e1->getJunctionPriority(e1->getToNode()) != e2->getJunctionPriority(e2->getToNode())) {
329  return e1->getJunctionPriority(e1->getToNode()) > e2->getJunctionPriority(e2->getToNode());
330  }
331  return e1->getID() > e2->getID();
332  }
333  };
334 
335 
336 private:
339 
342 
343 };
long long int SUMOTime
Definition: GUI.h:35
std::vector< NBEdge * > EdgeVector
container for (sorted) edges
Definition: NBCont.h:35
long long int SVCPermissions
bitset where each bit declares whether a certain SVC may use this edge/lane
TrafficLightType
TrafficLightLayout
LinkDirection
The different directions a link between two lanes may take (or a stream between two edges)....
The representation of a single edge during network building.
Definition: NBEdge.h:92
const std::string & getID() const
Definition: NBEdge.h:1522
NBNode * getToNode() const
Returns the destination node of the edge.
Definition: NBEdge.h:542
int getJunctionPriority(const NBNode *const node) const
Returns the junction priority (normalised for the node currently build)
Definition: NBEdge.cpp:2082
Represents a single node (junction) during network building.
Definition: NBNode.h:66
Sorts edges by their priority within the node they end at.
Definition: NBOwnTLDef.h:321
int operator()(const NBEdge *const e1, const NBEdge *const e2) const
comparing operator
Definition: NBOwnTLDef.h:327
A traffic light logics which must be computed (only nodes/edges are given)
Definition: NBOwnTLDef.h:44
void setSinglePhase()
Forces the definition not to compute an additional phase for left-movers.
Definition: NBOwnTLDef.h:101
void fixSuperfluousYellow(NBTrafficLightLogic *logic) const
avoid yellow signal between successive green (major) phases
std::string correctConflicting(std::string state, const EdgeVector &fromEdges, const EdgeVector &toEdges, const std::vector< bool > &isTurnaround, const std::vector< int > &fromLanes, const std::vector< int > &toLanes, const std::vector< bool > &hadGreenMajor, bool &haveForbiddenLeftMover, std::vector< bool > &rightTurnConflicts, std::vector< bool > &mergeConflicts)
change 'G' to 'g' for conflicting connections
void setLayout(TrafficLightLayout layout)
sets the layout for the generated signal plan
Definition: NBOwnTLDef.h:143
NBTrafficLightLogic * buildNemaPhases(const EdgeVector &fromEdges, const EdgeVector &toEdges, const std::vector< NBNode::Crossing * > &crossings, const std::vector< std::pair< NBEdge *, NBEdge * > > &chosenList, const std::vector< std::string > &straightStates, const std::vector< std::string > &leftStates)
void checkCustomCrossingIndices(NBTrafficLightLogic *logic) const
fix states in regard to custom crossing indices
static std::string patchStateForCrossings(const std::string &state, const std::vector< NBNode::Crossing * > &crossings, const EdgeVector &fromEdges, const EdgeVector &toEdges)
compute phase state in regard to pedestrian crossings
Definition: NBOwnTLDef.cpp:934
std::string allowByVClass(std::string state, const EdgeVector &fromEdges, const EdgeVector &toEdges, SVCPermissions perm)
int getMaxIndex()
Returns the maximum index controlled by this traffic light.
bool myHaveSinglePhase
Whether left-mover should not have an additional phase.
Definition: NBOwnTLDef.h:338
bool corridorLike() const
test whether a joined tls with layout 'opposites' would be built without dedicated left-turn phase
SUMOTime computeEscapeTime(const std::string &state, const EdgeVector &fromEdges, const EdgeVector &toEdges) const
compute time to clear all vehicles from within an alternateOneWay layout
void replaceRemoved(NBEdge *removed, int removedLane, NBEdge *by, int byLane, bool incoming)
Replaces a removed edge/lane.
std::pair< NBEdge *, NBEdge * > getBestPair(EdgeVector &incoming)
Returns the combination of two edges from the given which has most unblocked streams.
Definition: NBOwnTLDef.cpp:224
~NBOwnTLDef()
Destructor.
Definition: NBOwnTLDef.cpp:88
void collectLinks()
Collects the links participating in this traffic light.
void deactivateAlwaysGreen(NBTrafficLightLogic *logic) const
switch of signal for links that are always green
static void addGreenWithin(NBTrafficLightLogic *logic, const EdgeVector &fromEdges, EdgeVector &toProc)
ensure inner edges all get the green light eventually
NBTrafficLightLogic * computeLogicAndConts(int brakingTimeSeconds, bool onlyConts=false)
helper function for myCompute
Definition: NBOwnTLDef.cpp:277
static bool hasCrossing(const NBEdge *from, const NBEdge *to, const std::vector< NBNode::Crossing * > &crossings)
compute whether the given connection is crossed by pedestrians
Definition: NBOwnTLDef.cpp:874
std::string allowPredecessors(std::string state, const EdgeVector &fromEdges, const EdgeVector &toEdges, const std::vector< int > &fromLanes, const std::vector< int > &toLanes)
void deactivateInsideEdges(NBTrafficLightLogic *logic, const EdgeVector &fromEdges) const
switch of signal for links that are inside a joined tls
double computeUnblockedWeightedStreamNumber(const NBEdge *const e1, const NBEdge *const e2)
Returns how many streams outgoing from the edges can pass the junction without being blocked.
Definition: NBOwnTLDef.cpp:114
void remapRemoved(NBEdge *removed, const EdgeVector &incoming, const EdgeVector &outgoing)
Replaces occurrences of the removed edge in incoming/outgoing edges of all definitions.
int getToPrio(const NBEdge *const e)
Returns this edge's priority at the node it ends at.
Definition: NBOwnTLDef.cpp:92
std::string allowCompatible(std::string state, const EdgeVector &fromEdges, const EdgeVector &toEdges, const std::vector< int > &fromLanes, const std::vector< int > &toLanes)
allow connections that are compatible with the chosen edges
std::string correctMixed(std::string state, const EdgeVector &fromEdges, const std::vector< int > &fromLanes, bool &buildMixedGreenPhase, std::vector< bool > &mixedGreen)
prevent green and red from the same lane
void fixDurationSum(NBTrafficLightLogic *logic, const std::map< int, int > &names, int ring1a, int ring1b, int ring2a, int ring2b)
ensure that phase max durations before each barrier have the same sum in both rings
NBTrafficLightLogic * myCompute(int brakingTimeSeconds)
Computes the traffic light logic finally in dependence to the type.
Definition: NBOwnTLDef.cpp:271
static std::string patchNEMAStateForCrossings(const std::string &state, const std::vector< NBNode::Crossing * > &crossings, const EdgeVector &fromEdges, const EdgeVector &toEdges, const NBEdge *greenEdge, NBEdge *otherChosen)
Definition: NBOwnTLDef.cpp:982
std::string filterState(std::string state, const EdgeVector &fromEdges, const NBEdge *e)
mask out all greens that do not originate at the given edge
TrafficLightLayout getLayout() const
Definition: NBOwnTLDef.h:147
std::string allowFollowers(std::string state, const EdgeVector &fromEdges, const EdgeVector &toEdges)
void buildAllRedState(SUMOTime allRedTime, NBTrafficLightLogic *logic, const std::string &state)
double getDirectionalWeight(LinkDirection dir)
Returns the weight of a stream given its direction.
Definition: NBOwnTLDef.cpp:98
std::string allowSingleEdge(std::string state, const EdgeVector &fromEdges)
bool hasStraightConnection(const NBEdge *fromEdge)
check whether there is a straight connection from this edge
Definition: NBOwnTLDef.cpp:260
std::pair< NBEdge *, NBEdge * > getBestCombination(const EdgeVector &edges)
Returns the combination of two edges from the given which has most unblocked streams.
Definition: NBOwnTLDef.cpp:185
void initNeedsContRelation() const
static void addPedestrianScramble(NBTrafficLightLogic *logic, int totalNumLinks, SUMOTime greenTime, SUMOTime yellowTime, const std::vector< NBNode::Crossing * > &crossings, const EdgeVector &fromEdges, const EdgeVector &toEdges)
add an additional pedestrian phase if there are crossings that did not get green yet
NBOwnTLDef(const std::string &id, const std::vector< NBNode * > &junctions, SUMOTime offset, TrafficLightType type)
Constructor.
Definition: NBOwnTLDef.cpp:63
static EdgeVector getConnectedOuterEdges(const EdgeVector &incoming)
get edges that have connections
void filterMissingNames(std::vector< int > &vec, const std::map< int, int > &names, bool isBarrier)
keep only valid NEMA phase names (for params)
TrafficLightLayout myLayout
the layout for generated signal plans
Definition: NBOwnTLDef.h:341
bool forbidden(const std::string &state, int index, const EdgeVector &fromEdges, const EdgeVector &toEdges, bool allowCont)
whether the given index is forbidden by a green link in the current state
static std::string addPedestrianPhases(NBTrafficLightLogic *logic, const SUMOTime greenTime, const SUMOTime minDur, const SUMOTime maxDur, const SUMOTime earliestEnd, const SUMOTime latestEnd, std::string state, const std::vector< NBNode::Crossing * > &crossings, const EdgeVector &fromEdges, const EdgeVector &toEdges)
add 1 or 2 phases depending on the presence of pedestrian crossings
Definition: NBOwnTLDef.cpp:893
std::string allowUnrelated(std::string state, const EdgeVector &fromEdges, const EdgeVector &toEdges, const std::vector< bool > &isTurnaround, const std::vector< NBNode::Crossing * > &crossings)
void setTLControllingInformation() const
Informs edges about being controlled by a tls.
static const double MIN_SPEED_CROSSING_TIME
minimum speed for computing time to cross intersection
Definition: NBOwnTLDef.h:152
The base class for traffic light logic definitions.
A SUMO-compliant built logic for a traffic light.