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 /****************************************************************************/
16 // The base class for traffic light logic definitions
17 /****************************************************************************/
18 #ifndef NBTrafficLightDefinition_h
19 #define NBTrafficLightDefinition_h
20 
21 
22 // ===========================================================================
23 // included modules
24 // ===========================================================================
25 #include <config.h>
26 
27 #include <vector>
28 #include <string>
29 #include <bitset>
30 #include <utility>
31 #include <set>
32 #include <utils/common/StdDefs.h>
33 #include <utils/common/Named.h>
35 #include <utils/common/SUMOTime.h>
38 #include "NBCont.h"
39 #include "NBConnection.h"
40 #include "NBConnectionDefs.h"
42 
43 
44 // ===========================================================================
45 // class declarations
46 // ===========================================================================
47 class NBNode;
48 class OptionsCont;
50 
51 
52 // ===========================================================================
53 // class definitions
54 // ===========================================================================
68 public:
69 
71  static const std::string DefaultProgramID;
72 
77  enum TLColor {
88  };
89 
90 
98  NBTrafficLightDefinition(const std::string& id,
99  const std::vector<NBNode*>& junctions,
100  const std::string& programID,
101  SUMOTime offset,
102  TrafficLightType type);
103 
104 
112  NBTrafficLightDefinition(const std::string& id,
113  NBNode* junction,
114  const std::string& programID,
115  SUMOTime offset,
116  TrafficLightType type);
117 
118 
125  NBTrafficLightDefinition(const std::string& id, const std::string& programID,
126  SUMOTime offset,
127  TrafficLightType type);
128 
129 
131  virtual ~NBTrafficLightDefinition();
132 
133 
143 
144 
145 
148 
152  virtual void addNode(NBNode* node);
153 
154 
158  virtual void removeNode(NBNode* node);
159 
164  virtual void removeConnection(const NBConnection& conn, bool reconstruct = true) {
165  UNUSED_PARAMETER(conn);
166  UNUSED_PARAMETER(reconstruct);
167  }
168 
172  const std::vector<NBNode*>& getNodes() const {
173  return myControlledNodes;
174  }
176 
177 
188  bool mustBrake(const NBEdge* const from, const NBEdge* const to) const;
189 
190 
198  bool mustBrake(const NBConnection& possProhibited,
199  const NBConnection& possProhibitor,
200  bool regardNonSignalisedLowerPriority) const;
201 
211  bool mustBrake(const NBEdge* const possProhibitedFrom, const NBEdge* const possProhibitedTo,
212  const NBEdge* const possProhibitorFrom, const NBEdge* const possProhibitorTo,
213  bool regardNonSignalisedLowerPriority) const;
214 
215 
226  bool forbids(const NBEdge* const possProhibitorFrom, const NBEdge* const possProhibitorTo,
227  const NBEdge* const possProhibitedFrom, const NBEdge* const possProhibitedTo,
228  bool regardNonSignalisedLowerPriority,
229  bool sameNodeOnly = false) const;
230 
231 
239  bool foes(const NBEdge* const from1, const NBEdge* const to1,
240  const NBEdge* const from2, const NBEdge* const to2) const;
241 
242 
245  virtual void setTLControllingInformation() const = 0;
246 
247 
250  virtual void setParticipantsInformation();
251 
252 
256  void addControlledInnerEdges(const std::vector<std::string>& edges);
257 
260  std::vector<std::string> getControlledInnerEdges() const;
261 
267  virtual void remapRemoved(NBEdge* removed,
268  const EdgeVector& incoming, const EdgeVector& outgoing) = 0;
269 
270 
277  virtual void replaceRemoved(NBEdge* removed, int removedLane,
278  NBEdge* by, int byLane) = 0;
279 
281  virtual void shiftTLConnectionLaneIndex(NBEdge* edge, int offset, int threshold = -1) {
282  UNUSED_PARAMETER(edge);
283  UNUSED_PARAMETER(offset);
284  UNUSED_PARAMETER(threshold);
285  }
286 
290  const EdgeVector& getIncomingEdges() const;
291 
292 
295  return myControlledLinks;
296  }
297 
298 
301  return myControlledLinks;
302  }
303 
304 
308  const std::string& getProgramID() const {
309  return mySubID;
310  };
311 
312 
316  void setProgramID(const std::string& programID) {
317  mySubID = programID;
318  }
319 
320 
325  return myOffset;
326  }
327 
328 
331  return myType;
332  }
333 
335  virtual void setType(TrafficLightType type) {
336  myType = type;
337  }
338 
339  /* @brief computes whether the given stream may have green minor while the
340  * other stream has green major in the same phase
341  */
342  bool needsCont(const NBEdge* fromE, const NBEdge* toE, const NBEdge* otherFromE, const NBEdge* otherToE) const;
343 
345  virtual bool rightOnRedConflict(int index, int foeIndex) const;
346 
347  /* initialize myNeedsContRelation and set myNeedsContRelationReady to true
348  * This information is a byproduct of NBOwnTLDef::myCompute. All other
349  * subclasses instantiate a private instance of NBOwnTLDef to answer this query */
350  virtual void initNeedsContRelation() const;
351 
353  virtual int getMaxIndex() = 0;
354 
356  virtual int getMaxValidIndex() {
357  return getMaxIndex();
358  }
359 
365  int computeBrakingTime(double minDecel) const;
366 
368  virtual bool usingSignalGroups() const {
369  return false;
370  };
371 
373  std::string getDescription() const;
374 
375 
376 protected:
378  static const std::string DummyID;
379 
384  virtual NBTrafficLightLogic* myCompute(int brakingTime) = 0;
385 
386 
390  virtual void collectLinks() = 0;
391 
392 
395  virtual void collectEdges();
396 
397 
398  // @return whether this traffic light is invalid and should be computed
399  virtual bool amInvalid() const;
400 
402  void collectAllLinks();
403 
404 protected:
406  std::vector<NBNode*> myControlledNodes;
407 
410 
413 
416 
418  std::set<std::string> myControlledInnerEdges;
419 
421  std::string mySubID;
422 
425 
428 
430  struct StreamPair {
431  StreamPair(const NBEdge* _from1, const NBEdge* _to1, const NBEdge* _from2, const NBEdge* _to2):
432  from1(_from1),
433  to1(_to1),
434  from2(_from2),
435  to2(_to2) {}
436 
437  bool operator==(const StreamPair& o) const {
438  return (from1 == o.from1 && to1 == o.to1
439  && from2 == o.from2 && to2 == o.to2);
440  }
441 
442  bool operator<(const StreamPair& o) const {
443  if (from1 != o.from1) {
444  return from1 < o.from1;
445  }
446  if (to1 != o.to1) {
447  return to1 < o.to1;
448  }
449  if (from2 != o.from2) {
450  return from2 < o.from2;
451  }
452  return to2 < o.to2;
453  }
454 
455  const NBEdge* from1;
456  const NBEdge* to1;
457  const NBEdge* from2;
458  const NBEdge* to2;
459  };
460  typedef std::set<StreamPair> NeedsContRelation;
463 
464  typedef std::set<std::pair<int, int> > RightOnRedConflicts;
467 
468 private:
469  static std::set<NBEdge*> collectReachable(EdgeVector outer, const EdgeVector& within, bool checkControlled);
470 
471 
472 };
473 
474 
475 #endif
476 
477 /****************************************************************************/
478 
NBTrafficLightDefinition::StreamPair
data structure for caching needsCont information
Definition: NBTrafficLightDefinition.h:430
UNUSED_PARAMETER
#define UNUSED_PARAMETER(x)
Definition: StdDefs.h:31
NBTrafficLightDefinition::~NBTrafficLightDefinition
virtual ~NBTrafficLightDefinition()
Destructor.
Definition: NBTrafficLightDefinition.cpp:102
NBTrafficLightDefinition::StreamPair::from1
const NBEdge * from1
Definition: NBTrafficLightDefinition.h:455
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:281
NBTrafficLightDefinition::compute
NBTrafficLightLogic * compute(OptionsCont &oc)
Computes the traffic light logic.
Definition: NBTrafficLightDefinition.cpp:106
NBTrafficLightDefinition::getControlledInnerEdges
std::vector< std::string > getControlledInnerEdges() const
Retrieve the ids of edges explicitly controlled by the tls.
Definition: NBTrafficLightDefinition.cpp:440
NBTrafficLightDefinition::initNeedsContRelation
virtual void initNeedsContRelation() const
Definition: NBTrafficLightDefinition.cpp:500
Parameterised
An upper class for objects with additional parameters.
Definition: Parameterised.h:42
SUMOTime.h
NBTrafficLightDefinition::getMaxValidIndex
virtual int getMaxValidIndex()
Returns the maximum index controlled by this traffic light.
Definition: NBTrafficLightDefinition.h:356
Named
Base class for objects which have an id.
Definition: Named.h:56
NBTrafficLightDefinition::RightOnRedConflicts
std::set< std::pair< int, int > > RightOnRedConflicts
Definition: NBTrafficLightDefinition.h:464
NBTrafficLightDefinition::TLCOLOR_RED
@ TLCOLOR_RED
Signal shows red.
Definition: NBTrafficLightDefinition.h:79
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:421
NBTrafficLightDefinition::myControlledNodes
std::vector< NBNode * > myControlledNodes
The container with participating nodes.
Definition: NBTrafficLightDefinition.h:406
NBTrafficLightDefinition::StreamPair::operator<
bool operator<(const StreamPair &o) const
Definition: NBTrafficLightDefinition.h:442
EdgeVector
std::vector< NBEdge * > EdgeVector
container for (sorted) edges
Definition: NBCont.h:34
NBTrafficLightDefinition::myControlledLinks
NBConnectionVector myControlledLinks
The list of controlled links.
Definition: NBTrafficLightDefinition.h:415
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:514
NBTrafficLightDefinition::NeedsContRelation
std::set< StreamPair > NeedsContRelation
Definition: NBTrafficLightDefinition.h:460
NBTrafficLightDefinition::usingSignalGroups
virtual bool usingSignalGroups() const
whether this definition uses signal group (multiple connections with the same link index)
Definition: NBTrafficLightDefinition.h:368
TrafficLightType
TrafficLightType
Definition: SUMOXMLDefinitions.h:1197
SUMOTime
long long int SUMOTime
Definition: SUMOTime.h:34
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:235
NBTrafficLightDefinition::myOffset
SUMOTime myOffset
The offset in the program.
Definition: NBTrafficLightDefinition.h:424
NBTrafficLightDefinition::collectLinks
virtual void collectLinks()=0
Collects the links participating in this traffic light.
NBTrafficLightDefinition::StreamPair::to1
const NBEdge * to1
Definition: NBTrafficLightDefinition.h:456
NBTrafficLightDefinition::DummyID
static const std::string DummyID
id for temporary definitions
Definition: NBTrafficLightDefinition.h:378
NBTrafficLightDefinition::getOffset
SUMOTime getOffset()
Returns the offset.
Definition: NBTrafficLightDefinition.h:324
NBTrafficLightDefinition::myType
TrafficLightType myType
The algorithm type for the traffic light.
Definition: NBTrafficLightDefinition.h:427
NBTrafficLightDefinition::collectEdges
virtual void collectEdges()
Build the list of participating edges.
Definition: NBTrafficLightDefinition.cpp:185
NBTrafficLightDefinition::setProgramID
void setProgramID(const std::string &programID)
Sets the programID.
Definition: NBTrafficLightDefinition.h:316
NBEdge
The representation of a single edge during network building.
Definition: NBEdge.h:91
NBTrafficLightDefinition::getMaxIndex
virtual int getMaxIndex()=0
Returns the maximum index controlled by this traffic light and assigned to a connection.
NBTrafficLightDefinition::TLCOLOR_REDYELLOW
@ TLCOLOR_REDYELLOW
Signal shows red/yellow (unused)
Definition: NBTrafficLightDefinition.h:83
VectorHelper.h
NBTrafficLightDefinition::myRightOnRedConflicts
RightOnRedConflicts myRightOnRedConflicts
Definition: NBTrafficLightDefinition.h:465
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:434
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:330
NBTrafficLightDefinition::TLCOLOR_BLINK
@ TLCOLOR_BLINK
Signal is blinking yellow.
Definition: NBTrafficLightDefinition.h:87
NBTrafficLightDefinition::UNSPECIFIED_DURATION
static const SUMOTime UNSPECIFIED_DURATION
Definition: NBTrafficLightDefinition.h:70
NBTrafficLightDefinition::TLColor
TLColor
An enumeration of possible tl-signal states.
Definition: NBTrafficLightDefinition.h:77
Parameterised.h
Named.h
NBTrafficLightDefinition::getControlledLinks
const NBConnectionVector & getControlledLinks() const
returns the controlled links (depends on previous call to collectLinks)
Definition: NBTrafficLightDefinition.h:294
NBTrafficLightDefinition::myRightOnRedConflictsReady
bool myRightOnRedConflictsReady
Definition: NBTrafficLightDefinition.h:466
NBTrafficLightDefinition::StreamPair::StreamPair
StreamPair(const NBEdge *_from1, const NBEdge *_to1, const NBEdge *_from2, const NBEdge *_to2)
Definition: NBTrafficLightDefinition.h:431
NBTrafficLightDefinition::StreamPair::to2
const NBEdge * to2
Definition: NBTrafficLightDefinition.h:458
NBTrafficLightDefinition::computeBrakingTime
int computeBrakingTime(double minDecel) const
Computes the time vehicles may need to brake.
Definition: NBTrafficLightDefinition.cpp:137
NBTrafficLightDefinition::getIncomingEdges
const EdgeVector & getIncomingEdges() const
Returns the list of incoming edges (must be build first)
Definition: NBTrafficLightDefinition.cpp:446
NBTrafficLightDefinition::getNodes
const std::vector< NBNode * > & getNodes() const
Returns the list of controlled nodes.
Definition: NBTrafficLightDefinition.h:172
NBTrafficLightDefinition::myNeedsContRelation
NeedsContRelation myNeedsContRelation
Definition: NBTrafficLightDefinition.h:461
UtilExceptions.h
NBTrafficLightDefinition::needsCont
bool needsCont(const NBEdge *fromE, const NBEdge *toE, const NBEdge *otherFromE, const NBEdge *otherToE) const
Definition: NBTrafficLightDefinition.cpp:489
OptionsCont
A storage for options typed value containers)
Definition: OptionsCont.h:89
NBConnection
Definition: NBConnection.h:43
NBTrafficLightDefinition::TLCOLOR_YELLOW
@ TLCOLOR_YELLOW
Signal shows yellow.
Definition: NBTrafficLightDefinition.h:81
NBTrafficLightDefinition::myNeedsContRelationReady
bool myNeedsContRelationReady
Definition: NBTrafficLightDefinition.h:462
NBTrafficLightDefinition::getControlledLinks
NBConnectionVector & getControlledLinks()
returns the controlled links (non const version)
Definition: NBTrafficLightDefinition.h:300
NBTrafficLightDefinition::myEdgesWithin
EdgeVector myEdgesWithin
The list of edges within the area controlled by the tls.
Definition: NBTrafficLightDefinition.h:412
NBTrafficLightDefinition::NBTrafficLightDefinition
NBTrafficLightDefinition(const std::string &id, const std::vector< NBNode * > &junctions, const std::string &programID, SUMOTime offset, TrafficLightType type)
Constructor.
Definition: NBTrafficLightDefinition.cpp:52
NBTrafficLightDefinition::StreamPair::operator==
bool operator==(const StreamPair &o) const
Definition: NBTrafficLightDefinition.h:437
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:164
NBTrafficLightDefinition::getDescription
std::string getDescription() const
get ID and programID together (for convenient debugging)
Definition: NBTrafficLightDefinition.cpp:534
NBTrafficLightDefinition::myControlledInnerEdges
std::set< std::string > myControlledInnerEdges
Set of inner edges that shall be controlled, though.
Definition: NBTrafficLightDefinition.h:418
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:391
NBTrafficLightDefinition::DefaultProgramID
static const std::string DefaultProgramID
Definition: NBTrafficLightDefinition.h:71
NBTrafficLightDefinition::setParticipantsInformation
virtual void setParticipantsInformation()
Builds the list of participating nodes/edges/links.
Definition: NBTrafficLightDefinition.cpp:157
NBConnectionVector
std::vector< NBConnection > NBConnectionVector
Definition of a connection vector.
Definition: NBConnectionDefs.h:34
NBTrafficLightDefinition::getProgramID
const std::string & getProgramID() const
Returns the ProgramID.
Definition: NBTrafficLightDefinition.h:308
NBTrafficLightDefinition::addNode
virtual void addNode(NBNode *node)
Adds a node to the traffic light logic.
Definition: NBTrafficLightDefinition.cpp:414
NBTrafficLightDefinition::removeNode
virtual void removeNode(NBNode *node)
Removes the given node from the list of controlled nodes.
Definition: NBTrafficLightDefinition.cpp:424
config.h
NBTrafficLightDefinition::StreamPair::from2
const NBEdge * from2
Definition: NBTrafficLightDefinition.h:457
NBTrafficLightDefinition::setType
virtual void setType(TrafficLightType type)
set the algorithm type (static etc..)
Definition: NBTrafficLightDefinition.h:335
StdDefs.h
NBTrafficLightLogic
A SUMO-compliant built logic for a traffic light.
Definition: NBTrafficLightLogic.h:51
NBNode
Represents a single node (junction) during network building.
Definition: NBNode.h:67
NBTrafficLightDefinition::myIncomingEdges
EdgeVector myIncomingEdges
The list of incoming edges.
Definition: NBTrafficLightDefinition.h:409
NBTrafficLightDefinition::collectAllLinks
void collectAllLinks()
helper method for use in NBOwnTLDef and NBLoadedSUMOTLDef
Definition: NBTrafficLightDefinition.cpp:452
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:164
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:272
NBTrafficLightDefinition
The base class for traffic light logic definitions.
Definition: NBTrafficLightDefinition.h:67
NBCont.h
NBTrafficLightDefinition::amInvalid
virtual bool amInvalid() const
Definition: NBTrafficLightDefinition.cpp:131
NBTrafficLightDefinition::TLCOLOR_GREEN
@ TLCOLOR_GREEN
Signal shows green.
Definition: NBTrafficLightDefinition.h:85