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-2019 German Aerospace Center (DLR) and others.
4 // This program and the accompanying materials
5 // are made available under the terms of the Eclipse Public License v2.0
6 // which accompanies this distribution, and is available at
7 // http://www.eclipse.org/legal/epl-v20.html
8 // SPDX-License-Identifier: EPL-2.0
9 /****************************************************************************/
17 // The base class for traffic light logic definitions
18 /****************************************************************************/
19 #ifndef NBTrafficLightDefinition_h
20 #define NBTrafficLightDefinition_h
21 
22 
23 // ===========================================================================
24 // included modules
25 // ===========================================================================
26 #include <config.h>
27 
28 #include <vector>
29 #include <string>
30 #include <bitset>
31 #include <utility>
32 #include <set>
33 #include <utils/common/StdDefs.h>
34 #include <utils/common/Named.h>
36 #include <utils/common/SUMOTime.h>
39 #include "NBCont.h"
40 #include "NBConnection.h"
41 #include "NBConnectionDefs.h"
43 
44 
45 // ===========================================================================
46 // class declarations
47 // ===========================================================================
48 class NBNode;
49 class OptionsCont;
51 
52 
53 // ===========================================================================
54 // class definitions
55 // ===========================================================================
69 public:
70 
72  static const std::string DefaultProgramID;
73 
78  enum TLColor {
89  };
90 
91 
99  NBTrafficLightDefinition(const std::string& id,
100  const std::vector<NBNode*>& junctions,
101  const std::string& programID,
102  SUMOTime offset,
103  TrafficLightType type);
104 
105 
113  NBTrafficLightDefinition(const std::string& id,
114  NBNode* junction,
115  const std::string& programID,
116  SUMOTime offset,
117  TrafficLightType type);
118 
119 
126  NBTrafficLightDefinition(const std::string& id, const std::string& programID,
127  SUMOTime offset,
128  TrafficLightType type);
129 
130 
132  virtual ~NBTrafficLightDefinition();
133 
134 
144 
145 
146 
149 
153  virtual void addNode(NBNode* node);
154 
155 
159  virtual void removeNode(NBNode* node);
160 
165  virtual void removeConnection(const NBConnection& conn, bool reconstruct = true) {
166  UNUSED_PARAMETER(conn);
167  UNUSED_PARAMETER(reconstruct);
168  }
169 
173  const std::vector<NBNode*>& getNodes() const {
174  return myControlledNodes;
175  }
177 
178 
189  bool mustBrake(const NBEdge* const from, const NBEdge* const to) const;
190 
191 
199  bool mustBrake(const NBConnection& possProhibited,
200  const NBConnection& possProhibitor,
201  bool regardNonSignalisedLowerPriority) const;
202 
212  bool mustBrake(const NBEdge* const possProhibitedFrom, const NBEdge* const possProhibitedTo,
213  const NBEdge* const possProhibitorFrom, const NBEdge* const possProhibitorTo,
214  bool regardNonSignalisedLowerPriority) const;
215 
216 
227  bool forbids(const NBEdge* const possProhibitorFrom, const NBEdge* const possProhibitorTo,
228  const NBEdge* const possProhibitedFrom, const NBEdge* const possProhibitedTo,
229  bool regardNonSignalisedLowerPriority,
230  bool sameNodeOnly = false) const;
231 
232 
240  bool foes(const NBEdge* const from1, const NBEdge* const to1,
241  const NBEdge* const from2, const NBEdge* const to2) const;
242 
243 
246  virtual void setTLControllingInformation() const = 0;
247 
248 
251  virtual void setParticipantsInformation();
252 
253 
257  void addControlledInnerEdges(const std::vector<std::string>& edges);
258 
261  std::vector<std::string> getControlledInnerEdges() const;
262 
268  virtual void remapRemoved(NBEdge* removed,
269  const EdgeVector& incoming, const EdgeVector& outgoing) = 0;
270 
271 
278  virtual void replaceRemoved(NBEdge* removed, int removedLane,
279  NBEdge* by, int byLane) = 0;
280 
282  virtual void shiftTLConnectionLaneIndex(NBEdge* edge, int offset, int threshold = -1) {
283  UNUSED_PARAMETER(edge);
284  UNUSED_PARAMETER(offset);
285  UNUSED_PARAMETER(threshold);
286  }
287 
291  const EdgeVector& getIncomingEdges() const;
292 
293 
296  return myControlledLinks;
297  }
298 
299 
302  return myControlledLinks;
303  }
304 
305 
309  const std::string& getProgramID() const {
310  return mySubID;
311  };
312 
313 
317  void setProgramID(const std::string& programID) {
318  mySubID = programID;
319  }
320 
321 
326  return myOffset;
327  }
328 
329 
332  return myType;
333  }
334 
336  virtual void setType(TrafficLightType type) {
337  myType = type;
338  }
339 
340  /* @brief computes whether the given stream may have green minor while the
341  * other stream has green major in the same phase
342  */
343  bool needsCont(const NBEdge* fromE, const NBEdge* toE, const NBEdge* otherFromE, const NBEdge* otherToE) const;
344 
346  virtual bool rightOnRedConflict(int index, int foeIndex) const;
347 
348  /* initialize myNeedsContRelation and set myNeedsContRelationReady to true
349  * This information is a byproduct of NBOwnTLDef::myCompute. All other
350  * subclasses instantiate a private instance of NBOwnTLDef to answer this query */
351  virtual void initNeedsContRelation() const;
352 
354  virtual int getMaxIndex() = 0;
355 
357  virtual int getMaxValidIndex() {
358  return getMaxIndex();
359  }
360 
366  int computeBrakingTime(double minDecel) const;
367 
369  virtual bool usingSignalGroups() const {
370  return false;
371  };
372 
374  std::string getDescription() const;
375 
376 
377 protected:
379  static const std::string DummyID;
380 
385  virtual NBTrafficLightLogic* myCompute(int brakingTime) = 0;
386 
387 
391  virtual void collectLinks() = 0;
392 
393 
396  virtual void collectEdges();
397 
398 
399  // @return whether this traffic light is invalid and should be computed
400  virtual bool amInvalid() const;
401 
403  void collectAllLinks();
404 
405 protected:
407  std::vector<NBNode*> myControlledNodes;
408 
411 
414 
417 
419  std::set<std::string> myControlledInnerEdges;
420 
422  std::string mySubID;
423 
426 
429 
431  struct StreamPair {
432  StreamPair(const NBEdge* _from1, const NBEdge* _to1, const NBEdge* _from2, const NBEdge* _to2):
433  from1(_from1),
434  to1(_to1),
435  from2(_from2),
436  to2(_to2) {}
437 
438  bool operator==(const StreamPair& o) const {
439  return (from1 == o.from1 && to1 == o.to1
440  && from2 == o.from2 && to2 == o.to2);
441  }
442 
443  bool operator<(const StreamPair& o) const {
444  if (from1 != o.from1) {
445  return from1 < o.from1;
446  }
447  if (to1 != o.to1) {
448  return to1 < o.to1;
449  }
450  if (from2 != o.from2) {
451  return from2 < o.from2;
452  }
453  return to2 < o.to2;
454  }
455 
456  const NBEdge* from1;
457  const NBEdge* to1;
458  const NBEdge* from2;
459  const NBEdge* to2;
460  };
461  typedef std::set<StreamPair> NeedsContRelation;
464 
465  typedef std::set<std::pair<int, int> > RightOnRedConflicts;
468 
469 private:
470  static std::set<NBEdge*> collectReachable(EdgeVector outer, const EdgeVector& within, bool checkControlled);
471 
472 
473 };
474 
475 
476 #endif
477 
478 /****************************************************************************/
479 
NBTrafficLightDefinition::StreamPair
data structure for caching needsCont information
Definition: NBTrafficLightDefinition.h:431
UNUSED_PARAMETER
#define UNUSED_PARAMETER(x)
Definition: StdDefs.h:32
NBTrafficLightDefinition::~NBTrafficLightDefinition
virtual ~NBTrafficLightDefinition()
Destructor.
Definition: NBTrafficLightDefinition.cpp:103
NBTrafficLightDefinition::StreamPair::from1
const NBEdge * from1
Definition: NBTrafficLightDefinition.h:456
NBConnectionDefs.h
NBTrafficLightDefinition::shiftTLConnectionLaneIndex
virtual void shiftTLConnectionLaneIndex(NBEdge *edge, int offset, int threshold=-1)
patches (loaded) signal plans by modifying lane indices
Definition: NBTrafficLightDefinition.h:282
NBTrafficLightDefinition::compute
NBTrafficLightLogic * compute(OptionsCont &oc)
Computes the traffic light logic.
Definition: NBTrafficLightDefinition.cpp:107
NBTrafficLightDefinition::getControlledInnerEdges
std::vector< std::string > getControlledInnerEdges() const
Retrieve the ids of edges explicitly controlled by the tls.
Definition: NBTrafficLightDefinition.cpp:441
NBTrafficLightDefinition::initNeedsContRelation
virtual void initNeedsContRelation() const
Definition: NBTrafficLightDefinition.cpp:501
Parameterised
An upper class for objects with additional parameters.
Definition: Parameterised.h:43
SUMOTime.h
NBTrafficLightDefinition::getMaxValidIndex
virtual int getMaxValidIndex()
Returns the maximum index controlled by this traffic light.
Definition: NBTrafficLightDefinition.h:357
Named
Base class for objects which have an id.
Definition: Named.h:57
NBTrafficLightDefinition::RightOnRedConflicts
std::set< std::pair< int, int > > RightOnRedConflicts
Definition: NBTrafficLightDefinition.h:465
NBTrafficLightDefinition::TLCOLOR_RED
Signal shows red.
Definition: NBTrafficLightDefinition.h:80
NBTrafficLightDefinition::setTLControllingInformation
virtual void setTLControllingInformation() const =0
Informs edges about being controlled by a tls.
NBTrafficLightDefinition::replaceRemoved
virtual void replaceRemoved(NBEdge *removed, int removedLane, NBEdge *by, int byLane)=0
Replaces a removed edge/lane.
NBTrafficLightDefinition::mySubID
std::string mySubID
The tls program's subid.
Definition: NBTrafficLightDefinition.h:422
NBTrafficLightDefinition::myControlledNodes
std::vector< NBNode * > myControlledNodes
The container with participating nodes.
Definition: NBTrafficLightDefinition.h:407
NBTrafficLightDefinition::StreamPair::operator<
bool operator<(const StreamPair &o) const
Definition: NBTrafficLightDefinition.h:443
EdgeVector
std::vector< NBEdge * > EdgeVector
container for (sorted) edges
Definition: NBCont.h:35
NBTrafficLightDefinition::myControlledLinks
NBConnectionVector myControlledLinks
The list of controlled links.
Definition: NBTrafficLightDefinition.h:416
NBConnection.h
NBTrafficLightDefinition::rightOnRedConflict
virtual bool rightOnRedConflict(int index, int foeIndex) const
whether the given index must yield to the foeIndex while turning right on a red light
Definition: NBTrafficLightDefinition.cpp:515
NBTrafficLightDefinition::NeedsContRelation
std::set< StreamPair > NeedsContRelation
Definition: NBTrafficLightDefinition.h:461
NBTrafficLightDefinition::usingSignalGroups
virtual bool usingSignalGroups() const
whether this definition uses signal group (multiple connections with the same link index)
Definition: NBTrafficLightDefinition.h:369
TrafficLightType
TrafficLightType
Definition: SUMOXMLDefinitions.h:1192
SUMOTime
long long int SUMOTime
Definition: SUMOTime.h:35
NBTrafficLightDefinition::mustBrake
bool mustBrake(const NBEdge *const from, const NBEdge *const to) const
Returns the information whether the described flow must let any other flow pass.
Definition: NBTrafficLightDefinition.cpp:236
NBTrafficLightDefinition::myOffset
SUMOTime myOffset
The offset in the program.
Definition: NBTrafficLightDefinition.h:425
NBTrafficLightDefinition::collectLinks
virtual void collectLinks()=0
Collects the links participating in this traffic light.
NBTrafficLightDefinition::StreamPair::to1
const NBEdge * to1
Definition: NBTrafficLightDefinition.h:457
NBTrafficLightDefinition::DummyID
static const std::string DummyID
id for temporary definitions
Definition: NBTrafficLightDefinition.h:379
NBTrafficLightDefinition::getOffset
SUMOTime getOffset()
Returns the offset.
Definition: NBTrafficLightDefinition.h:325
NBTrafficLightDefinition::myType
TrafficLightType myType
The algorithm type for the traffic light.
Definition: NBTrafficLightDefinition.h:428
NBTrafficLightDefinition::collectEdges
virtual void collectEdges()
Build the list of participating edges.
Definition: NBTrafficLightDefinition.cpp:186
NBTrafficLightDefinition::setProgramID
void setProgramID(const std::string &programID)
Sets the programID.
Definition: NBTrafficLightDefinition.h:317
NBEdge
The representation of a single edge during network building.
Definition: NBEdge.h:86
NBTrafficLightDefinition::getMaxIndex
virtual int getMaxIndex()=0
Returns the maximum index controlled by this traffic light and assigned to a connection.
NBTrafficLightDefinition::TLCOLOR_REDYELLOW
Signal shows red/yellow (unused)
Definition: NBTrafficLightDefinition.h:84
VectorHelper.h
NBTrafficLightDefinition::myRightOnRedConflicts
RightOnRedConflicts myRightOnRedConflicts
Definition: NBTrafficLightDefinition.h:466
NBTrafficLightDefinition::addControlledInnerEdges
void addControlledInnerEdges(const std::vector< std::string > &edges)
Adds the given ids into the list of inner edges controlled by the tls.
Definition: NBTrafficLightDefinition.cpp:435
NBTrafficLightDefinition::myCompute
virtual NBTrafficLightLogic * myCompute(int brakingTime)=0
Computes the traffic light logic finally in dependence to the type.
NBTrafficLightDefinition::getType
TrafficLightType getType() const
get the algorithm type (static etc..)
Definition: NBTrafficLightDefinition.h:331
NBTrafficLightDefinition::TLCOLOR_BLINK
Signal is blinking yellow.
Definition: NBTrafficLightDefinition.h:88
NBTrafficLightDefinition::UNSPECIFIED_DURATION
static const SUMOTime UNSPECIFIED_DURATION
Definition: NBTrafficLightDefinition.h:71
NBTrafficLightDefinition::TLColor
TLColor
An enumeration of possible tl-signal states.
Definition: NBTrafficLightDefinition.h:78
Parameterised.h
Named.h
NBTrafficLightDefinition::getControlledLinks
const NBConnectionVector & getControlledLinks() const
returns the controlled links (depends on previous call to collectLinks)
Definition: NBTrafficLightDefinition.h:295
NBTrafficLightDefinition::myRightOnRedConflictsReady
bool myRightOnRedConflictsReady
Definition: NBTrafficLightDefinition.h:467
NBTrafficLightDefinition::StreamPair::StreamPair
StreamPair(const NBEdge *_from1, const NBEdge *_to1, const NBEdge *_from2, const NBEdge *_to2)
Definition: NBTrafficLightDefinition.h:432
NBTrafficLightDefinition::StreamPair::to2
const NBEdge * to2
Definition: NBTrafficLightDefinition.h:459
NBTrafficLightDefinition::computeBrakingTime
int computeBrakingTime(double minDecel) const
Computes the time vehicles may need to brake.
Definition: NBTrafficLightDefinition.cpp:138
NBTrafficLightDefinition::getIncomingEdges
const EdgeVector & getIncomingEdges() const
Returns the list of incoming edges (must be build first)
Definition: NBTrafficLightDefinition.cpp:447
NBTrafficLightDefinition::getNodes
const std::vector< NBNode * > & getNodes() const
Returns the list of controlled nodes.
Definition: NBTrafficLightDefinition.h:173
NBTrafficLightDefinition::myNeedsContRelation
NeedsContRelation myNeedsContRelation
Definition: NBTrafficLightDefinition.h:462
UtilExceptions.h
NBTrafficLightDefinition::needsCont
bool needsCont(const NBEdge *fromE, const NBEdge *toE, const NBEdge *otherFromE, const NBEdge *otherToE) const
Definition: NBTrafficLightDefinition.cpp:490
OptionsCont
A storage for options typed value containers)
Definition: OptionsCont.h:90
NBConnection
Definition: NBConnection.h:44
NBTrafficLightDefinition::TLCOLOR_YELLOW
Signal shows yellow.
Definition: NBTrafficLightDefinition.h:82
NBTrafficLightDefinition::myNeedsContRelationReady
bool myNeedsContRelationReady
Definition: NBTrafficLightDefinition.h:463
NBTrafficLightDefinition::getControlledLinks
NBConnectionVector & getControlledLinks()
returns the controlled links (non const version)
Definition: NBTrafficLightDefinition.h:301
NBTrafficLightDefinition::myEdgesWithin
EdgeVector myEdgesWithin
The list of edges within the area controlled by the tls.
Definition: NBTrafficLightDefinition.h:413
NBTrafficLightDefinition::NBTrafficLightDefinition
NBTrafficLightDefinition(const std::string &id, const std::vector< NBNode * > &junctions, const std::string &programID, SUMOTime offset, TrafficLightType type)
Constructor.
Definition: NBTrafficLightDefinition.cpp:53
NBTrafficLightDefinition::StreamPair::operator==
bool operator==(const StreamPair &o) const
Definition: NBTrafficLightDefinition.h:438
NBTrafficLightDefinition::remapRemoved
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.
NBTrafficLightDefinition::collectReachable
static std::set< NBEdge * > collectReachable(EdgeVector outer, const EdgeVector &within, bool checkControlled)
Definition: NBTrafficLightDefinition.cpp:165
NBTrafficLightDefinition::getDescription
std::string getDescription() const
get ID and programID together (for convenient debugging)
Definition: NBTrafficLightDefinition.cpp:535
NBTrafficLightDefinition::myControlledInnerEdges
std::set< std::string > myControlledInnerEdges
Set of inner edges that shall be controlled, though.
Definition: NBTrafficLightDefinition.h:419
NBTrafficLightDefinition::foes
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.
Definition: NBTrafficLightDefinition.cpp:392
NBTrafficLightDefinition::DefaultProgramID
static const std::string DefaultProgramID
Definition: NBTrafficLightDefinition.h:72
NBTrafficLightDefinition::setParticipantsInformation
virtual void setParticipantsInformation()
Builds the list of participating nodes/edges/links.
Definition: NBTrafficLightDefinition.cpp:158
NBConnectionVector
std::vector< NBConnection > NBConnectionVector
Definition of a connection vector.
Definition: NBConnectionDefs.h:35
NBTrafficLightDefinition::getProgramID
const std::string & getProgramID() const
Returns the ProgramID.
Definition: NBTrafficLightDefinition.h:309
NBTrafficLightDefinition::addNode
virtual void addNode(NBNode *node)
Adds a node to the traffic light logic.
Definition: NBTrafficLightDefinition.cpp:415
NBTrafficLightDefinition::removeNode
virtual void removeNode(NBNode *node)
Removes the given node from the list of controlled nodes.
Definition: NBTrafficLightDefinition.cpp:425
config.h
NBTrafficLightDefinition::StreamPair::from2
const NBEdge * from2
Definition: NBTrafficLightDefinition.h:458
NBTrafficLightDefinition::setType
virtual void setType(TrafficLightType type)
set the algorithm type (static etc..)
Definition: NBTrafficLightDefinition.h:336
StdDefs.h
NBTrafficLightLogic
A SUMO-compliant built logic for a traffic light.
Definition: NBTrafficLightLogic.h:52
NBNode
Represents a single node (junction) during network building.
Definition: NBNode.h:68
NBTrafficLightDefinition::myIncomingEdges
EdgeVector myIncomingEdges
The list of incoming edges.
Definition: NBTrafficLightDefinition.h:410
NBTrafficLightDefinition::collectAllLinks
void collectAllLinks()
helper method for use in NBOwnTLDef and NBLoadedSUMOTLDef
Definition: NBTrafficLightDefinition.cpp:453
NBTrafficLightDefinition::removeConnection
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...
Definition: NBTrafficLightDefinition.h:165
NBTrafficLightDefinition::forbids
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.
Definition: NBTrafficLightDefinition.cpp:273
NBTrafficLightDefinition
The base class for traffic light logic definitions.
Definition: NBTrafficLightDefinition.h:68
NBCont.h
NBTrafficLightDefinition::amInvalid
virtual bool amInvalid() const
Definition: NBTrafficLightDefinition.cpp:132
NBTrafficLightDefinition::TLCOLOR_GREEN
Signal shows green.
Definition: NBTrafficLightDefinition.h:86