Eclipse SUMO - Simulation of Urban MObility
NBTrafficLightDefinition.h
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2002-2022 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 // The base class for traffic light logic definitions
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 <utils/common/StdDefs.h>
31 #include <utils/common/Named.h>
33 #include <utils/common/SUMOTime.h>
36 #include "NBCont.h"
37 #include "NBConnection.h"
38 #include "NBConnectionDefs.h"
40 
41 
42 // ===========================================================================
43 // class declarations
44 // ===========================================================================
45 class NBNode;
46 class OptionsCont;
48 
49 
50 // ===========================================================================
51 // class definitions
52 // ===========================================================================
66 public:
67 
69  static const std::string DefaultProgramID;
70 
75  enum TLColor {
86  };
87 
88 
96  NBTrafficLightDefinition(const std::string& id,
97  const std::vector<NBNode*>& junctions,
98  const std::string& programID,
99  SUMOTime offset,
100  TrafficLightType type);
101 
102 
110  NBTrafficLightDefinition(const std::string& id,
111  NBNode* junction,
112  const std::string& programID,
113  SUMOTime offset,
114  TrafficLightType type);
115 
116 
123  NBTrafficLightDefinition(const std::string& id, const std::string& programID,
124  SUMOTime offset,
125  TrafficLightType type);
126 
127 
129  virtual ~NBTrafficLightDefinition();
130 
131 
141 
142 
143 
146 
150  virtual void addNode(NBNode* node);
151 
152 
156  virtual void removeNode(NBNode* node);
157 
162  virtual void removeConnection(const NBConnection& conn, bool reconstruct = true) {
163  UNUSED_PARAMETER(conn);
164  UNUSED_PARAMETER(reconstruct);
165  }
166 
170  const std::vector<NBNode*>& getNodes() const {
171  return myControlledNodes;
172  }
174 
175 
186  bool mustBrake(const NBEdge* const from, const NBEdge* const to) const;
187 
188 
196  bool mustBrake(const NBConnection& possProhibited,
197  const NBConnection& possProhibitor,
198  bool regardNonSignalisedLowerPriority) const;
199 
209  bool mustBrake(const NBEdge* const possProhibitedFrom, const NBEdge* const possProhibitedTo,
210  const NBEdge* const possProhibitorFrom, const NBEdge* const possProhibitorTo,
211  bool regardNonSignalisedLowerPriority) const;
212 
213 
224  bool forbids(const NBEdge* const possProhibitorFrom, const NBEdge* const possProhibitorTo,
225  const NBEdge* const possProhibitedFrom, const NBEdge* const possProhibitedTo,
226  bool regardNonSignalisedLowerPriority,
227  bool sameNodeOnly = false) const;
228 
229 
237  bool foes(const NBEdge* const from1, const NBEdge* const to1,
238  const NBEdge* const from2, const NBEdge* const to2) const;
239 
240 
243  virtual void setTLControllingInformation() const = 0;
244 
245 
248  virtual void setParticipantsInformation();
249 
250 
254  void addControlledInnerEdges(const std::vector<std::string>& edges);
255 
258  std::vector<std::string> getControlledInnerEdges() const;
259 
265  virtual void remapRemoved(NBEdge* removed,
266  const EdgeVector& incoming, const EdgeVector& outgoing) = 0;
267 
268 
276  virtual void replaceRemoved(NBEdge* removed, int removedLane,
277  NBEdge* by, int byLane, bool incoming) = 0;
278 
280  virtual void shiftTLConnectionLaneIndex(NBEdge* edge, int offset, int threshold = -1) {
281  UNUSED_PARAMETER(edge);
282  UNUSED_PARAMETER(offset);
283  UNUSED_PARAMETER(threshold);
284  }
285 
289  const EdgeVector& getIncomingEdges() const;
290 
291 
294  return myControlledLinks;
295  }
296 
297 
300  return myControlledLinks;
301  }
302 
303 
307  const std::string& getProgramID() const {
308  return mySubID;
309  };
310 
311 
315  virtual void setProgramID(const std::string& programID) {
316  mySubID = programID;
317  }
318 
319  virtual TrafficLightLayout getLayout() const {
321  }
322 
327  return myOffset;
328  }
329 
330 
333  return myType;
334  }
335 
337  virtual void setType(TrafficLightType type) {
338  myType = type;
339  }
340 
341  /* @brief computes whether the given stream may have green minor while the
342  * other stream has green major in the same phase
343  */
344  bool needsCont(const NBEdge* fromE, const NBEdge* toE, const NBEdge* otherFromE, const NBEdge* otherToE) const;
345 
347  virtual bool rightOnRedConflict(int index, int foeIndex) const;
348 
349  /* initialize myNeedsContRelation and set myNeedsContRelationReady to true
350  * This information is a byproduct of NBOwnTLDef::myCompute. All other
351  * subclasses instantiate a private instance of NBOwnTLDef to answer this query */
352  virtual void initNeedsContRelation() const;
353 
355  virtual int getMaxIndex() = 0;
356 
358  virtual int getMaxValidIndex() {
359  return getMaxIndex();
360  }
361 
367  int computeBrakingTime(double minDecel) const;
368 
370  virtual bool usingSignalGroups() const {
371  return false;
372  };
373 
375  std::string getDescription() const;
376 
377 
378 protected:
380  static const std::string DummyID;
381 
386  virtual NBTrafficLightLogic* myCompute(int brakingTime) = 0;
387 
388 
392  virtual void collectLinks() = 0;
393 
394 
397  virtual void collectEdges();
398 
399 
400  // @return whether this traffic light is invalid and should be computed
401  virtual bool amInvalid() const;
402 
405 
406 protected:
408  std::vector<NBNode*> myControlledNodes;
409 
412 
415 
418 
420  std::set<std::string> myControlledInnerEdges;
421 
423  std::string mySubID;
424 
427 
430 
432  struct StreamPair {
433  StreamPair(const NBEdge* _from1, const NBEdge* _to1, const NBEdge* _from2, const NBEdge* _to2):
434  from1(_from1),
435  to1(_to1),
436  from2(_from2),
437  to2(_to2) {}
438 
439  bool operator==(const StreamPair& o) const {
440  return (from1 == o.from1 && to1 == o.to1
441  && from2 == o.from2 && to2 == o.to2);
442  }
443 
444  bool operator<(const StreamPair& o) const {
445  if (from1 != o.from1) {
446  return from1 < o.from1;
447  }
448  if (to1 != o.to1) {
449  return to1 < o.to1;
450  }
451  if (from2 != o.from2) {
452  return from2 < o.from2;
453  }
454  return to2 < o.to2;
455  }
456 
457  const NBEdge* from1;
458  const NBEdge* to1;
459  const NBEdge* from2;
460  const NBEdge* to2;
461  };
462  typedef std::set<StreamPair> NeedsContRelation;
465 
466  typedef std::set<std::pair<int, int> > RightOnRedConflicts;
469 
470 private:
471  static std::set<NBEdge*> collectReachable(EdgeVector outer, const EdgeVector& within, bool checkControlled);
472 
473 
474 };
std::vector< NBConnection > NBConnectionVector
Definition of a connection vector.
std::vector< NBEdge * > EdgeVector
container for (sorted) edges
Definition: NBCont.h:35
long long int SUMOTime
Definition: SUMOTime.h:32
TrafficLightType
TrafficLightLayout
#define UNUSED_PARAMETER(x)
Definition: StdDefs.h:30
The representation of a single edge during network building.
Definition: NBEdge.h:91
Represents a single node (junction) during network building.
Definition: NBNode.h:66
The base class for traffic light logic definitions.
virtual bool rightOnRedConflict(int index, int foeIndex) const
whether the given index must yield to the foeIndex while turning right on a red light
const NBConnectionVector & getControlledLinks() const
returns the controlled links (depends on previous call to collectLinks)
const std::string & getProgramID() const
Returns the ProgramID.
virtual void remapRemoved(NBEdge *removed, const EdgeVector &incoming, const EdgeVector &outgoing)=0
Replaces occurences of the removed edge in incoming/outgoing edges of all definitions.
virtual ~NBTrafficLightDefinition()
Destructor.
std::set< std::pair< int, int > > RightOnRedConflicts
void addControlledInnerEdges(const std::vector< std::string > &edges)
Adds the given ids into the list of inner edges controlled by the tls.
const EdgeVector & getIncomingEdges() const
Returns the list of incoming edges (must be build first)
bool needsCont(const NBEdge *fromE, const NBEdge *toE, const NBEdge *otherFromE, const NBEdge *otherToE) const
TLColor
An enumeration of possible tl-signal states.
@ TLCOLOR_BLINK
Signal is blinking yellow.
@ TLCOLOR_GREEN
Signal shows green.
@ TLCOLOR_YELLOW
Signal shows yellow.
@ TLCOLOR_REDYELLOW
Signal shows red/yellow (unused)
std::vector< NBNode * > myControlledNodes
The container with participating nodes.
TrafficLightType getType() const
get the algorithm type (static etc..)
virtual NBTrafficLightLogic * myCompute(int brakingTime)=0
Computes the traffic light logic finally in dependence to the type.
virtual void setProgramID(const std::string &programID)
Sets the programID.
virtual void removeNode(NBNode *node)
Removes the given node from the list of controlled nodes.
EdgeVector myIncomingEdges
The list of incoming edges.
virtual bool usingSignalGroups() const
whether this definition uses signal group (multiple connections with the same link index)
virtual void addNode(NBNode *node)
Adds a node to the traffic light logic.
virtual void shiftTLConnectionLaneIndex(NBEdge *edge, int offset, int threshold=-1)
patches (loaded) signal plans by modifying lane indices
const std::vector< NBNode * > & getNodes() const
Returns the list of controlled nodes.
std::vector< std::string > getControlledInnerEdges() const
Retrieve the ids of edges explicitly controlled by the tls.
SUMOTime getOffset()
Returns the offset.
static std::set< NBEdge * > collectReachable(EdgeVector outer, const EdgeVector &within, bool checkControlled)
RightOnRedConflicts myRightOnRedConflicts
TrafficLightType myType
The algorithm type for the traffic light.
EdgeVector myEdgesWithin
The list of edges within the area controlled by the tls.
static const std::string DummyID
id for temporary definitions
virtual void collectLinks()=0
Collects the links participating in this traffic light.
int computeBrakingTime(double minDecel) const
Computes the time vehicles may need to brake.
bool forbids(const NBEdge *const possProhibitorFrom, const NBEdge *const possProhibitorTo, const NBEdge *const possProhibitedFrom, const NBEdge *const possProhibitedTo, bool regardNonSignalisedLowerPriority, bool sameNodeOnly=false) const
Returns the information whether "prohibited" flow must let "prohibitor" flow pass.
NBTrafficLightLogic * compute(OptionsCont &oc)
Computes the traffic light logic.
std::set< StreamPair > NeedsContRelation
virtual void setTLControllingInformation() const =0
Informs edges about being controlled by a tls.
NBConnectionVector myControlledLinks
The list of controlled links.
static const std::string DefaultProgramID
bool mustBrake(const NBEdge *const from, const NBEdge *const to) const
Returns the information whether the described flow must let any other flow pass.
virtual void setType(TrafficLightType type)
set the algorithm type (static etc..)
NBConnectionVector & getControlledLinks()
returns the controlled links (non const version)
virtual void initNeedsContRelation() const
virtual void setParticipantsInformation()
Builds the list of participating nodes/edges/links.
virtual int getMaxIndex()=0
Returns the maximum index controlled by this traffic light and assigned to a connection.
void collectAllLinks(NBConnectionVector &into)
helper method for use in NBOwnTLDef and NBLoadedSUMOTLDef
NBTrafficLightDefinition(const std::string &id, const std::vector< NBNode * > &junctions, const std::string &programID, SUMOTime offset, TrafficLightType type)
Constructor.
SUMOTime myOffset
The offset in the program.
static const SUMOTime UNSPECIFIED_DURATION
bool foes(const NBEdge *const from1, const NBEdge *const to1, const NBEdge *const from2, const NBEdge *const to2) const
Returns the information whether the given flows cross.
virtual int getMaxValidIndex()
Returns the maximum index controlled by this traffic light.
virtual void replaceRemoved(NBEdge *removed, int removedLane, NBEdge *by, int byLane, bool incoming)=0
Replaces a removed edge/lane.
std::string getDescription() const
get ID and programID together (for convenient debugging)
virtual TrafficLightLayout getLayout() const
virtual void removeConnection(const NBConnection &conn, bool reconstruct=true)
removes the given connection from the traffic light if recontruct=true, reconstructs the logic and in...
std::string mySubID
The tls program's subid.
virtual void collectEdges()
Build the list of participating edges.
std::set< std::string > myControlledInnerEdges
Set of inner edges that shall be controlled, though.
A SUMO-compliant built logic for a traffic light.
Base class for objects which have an id.
Definition: Named.h:54
A storage for options typed value containers)
Definition: OptionsCont.h:89
An upper class for objects with additional parameters.
Definition: Parameterised.h:41
data structure for caching needsCont information
bool operator==(const StreamPair &o) const
bool operator<(const StreamPair &o) const
StreamPair(const NBEdge *_from1, const NBEdge *_to1, const NBEdge *_from2, const NBEdge *_to2)