SUMO - Simulation of Urban MObility
NBTrafficLightDefinition.h
Go to the documentation of this file.
1 /****************************************************************************/
9 // The base class for traffic light logic definitions
10 /****************************************************************************/
11 // SUMO, Simulation of Urban MObility; see http://sumo.dlr.de/
12 // Copyright (C) 2002-2016 DLR (http://www.dlr.de/) and contributors
13 /****************************************************************************/
14 //
15 // This file is part of SUMO.
16 // SUMO is free software: you can redistribute it and/or modify
17 // it under the terms of the GNU General Public License as published by
18 // the Free Software Foundation, either version 3 of the License, or
19 // (at your option) any later version.
20 //
21 /****************************************************************************/
22 #ifndef NBTrafficLightDefinition_h
23 #define NBTrafficLightDefinition_h
24 
25 
26 // ===========================================================================
27 // included modules
28 // ===========================================================================
29 #ifdef _MSC_VER
30 #include <windows_config.h>
31 #else
32 #include <config.h>
33 #endif
34 
35 #include <vector>
36 #include <string>
37 #include <bitset>
38 #include <utility>
39 #include <set>
40 #include <utils/common/StdDefs.h>
41 #include <utils/common/Named.h>
43 #include <utils/common/SUMOTime.h>
46 #include "NBCont.h"
47 #include "NBConnection.h"
48 #include "NBConnectionDefs.h"
50 
51 
52 // ===========================================================================
53 // class declarations
54 // ===========================================================================
55 class NBNode;
56 class OptionsCont;
58 
59 
60 // ===========================================================================
61 // class definitions
62 // ===========================================================================
76 public:
77 
78  static const std::string DefaultProgramID;
79 
84  enum TLColor {
95  };
96 
97 
105  NBTrafficLightDefinition(const std::string& id,
106  const std::vector<NBNode*>& junctions,
107  const std::string& programID,
108  SUMOTime offset,
109  TrafficLightType type);
110 
111 
119  NBTrafficLightDefinition(const std::string& id,
120  NBNode* junction,
121  const std::string& programID,
122  SUMOTime offset,
123  TrafficLightType type);
124 
125 
132  NBTrafficLightDefinition(const std::string& id, const std::string& programID,
133  SUMOTime offset,
134  TrafficLightType type);
135 
136 
138  virtual ~NBTrafficLightDefinition();
139 
140 
150 
151 
152 
155 
159  virtual void addNode(NBNode* node);
160 
161 
165  virtual void removeNode(NBNode* node);
166 
171  virtual void removeConnection(const NBConnection& conn, bool reconstruct = true) {
172  UNUSED_PARAMETER(conn);
173  UNUSED_PARAMETER(reconstruct);
174  }
175 
179  const std::vector<NBNode*>& getNodes() const {
180  return myControlledNodes;
181  }
183 
184 
195  bool mustBrake(const NBEdge* const from, const NBEdge* const to) const;
196 
197 
205  bool mustBrake(const NBConnection& possProhibited,
206  const NBConnection& possProhibitor,
207  bool regardNonSignalisedLowerPriority) const;
208 
218  bool mustBrake(const NBEdge* const possProhibitedFrom, const NBEdge* const possProhibitedTo,
219  const NBEdge* const possProhibitorFrom, const NBEdge* const possProhibitorTo,
220  bool regardNonSignalisedLowerPriority) const;
221 
222 
233  bool forbids(const NBEdge* const possProhibitorFrom, const NBEdge* const possProhibitorTo,
234  const NBEdge* const possProhibitedFrom, const NBEdge* const possProhibitedTo,
235  bool regardNonSignalisedLowerPriority,
236  bool sameNodeOnly = false) const;
237 
238 
246  bool foes(const NBEdge* const from1, const NBEdge* const to1,
247  const NBEdge* const from2, const NBEdge* const to2) const;
248 
249 
252  virtual void setTLControllingInformation() const = 0;
253 
254 
257  virtual void setParticipantsInformation();
258 
259 
263  void addControlledInnerEdges(const std::vector<std::string>& edges);
264 
267  std::vector<std::string> getControlledInnerEdges() const;
268 
274  virtual void remapRemoved(NBEdge* removed,
275  const EdgeVector& incoming, const EdgeVector& outgoing) = 0;
276 
277 
284  virtual void replaceRemoved(NBEdge* removed, int removedLane,
285  NBEdge* by, int byLane) = 0;
286 
288  virtual void shiftTLConnectionLaneIndex(NBEdge* edge, int offset) {
289  UNUSED_PARAMETER(edge);
290  UNUSED_PARAMETER(offset);
291  }
292 
296  const EdgeVector& getIncomingEdges() const;
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 
352 protected:
354  static const std::string DummyID;
355 
360  virtual NBTrafficLightLogic* myCompute(int brakingTime) = 0;
361 
362 
366  virtual void collectLinks() = 0;
367 
368 
371  virtual void collectEdges();
372 
373 
379  int computeBrakingTime(SUMOReal minDecel) const;
380 
381 
382  // @return whether this traffic light is invalid and should be computed
383  virtual bool amInvalid() const;
384 
386  void collectAllLinks();
387 
388 protected:
390  std::vector<NBNode*> myControlledNodes;
391 
394 
397 
400 
402  std::set<std::string> myControlledInnerEdges;
403 
405  std::string mySubID;
406 
409 
412 
414  struct StreamPair {
415  StreamPair(const NBEdge* _from1, const NBEdge* _to1, const NBEdge* _from2, const NBEdge* _to2):
416  from1(_from1),
417  to1(_to1),
418  from2(_from2),
419  to2(_to2) {}
420 
421  bool operator==(const StreamPair& o) const {
422  return (from1 == o.from1 && to1 == o.to1
423  && from2 == o.from2 && to2 == o.to2);
424  }
425 
426  bool operator<(const StreamPair& o) const {
427  if (from1 != o.from1) {
428  return from1 < o.from1;
429  }
430  if (to1 != o.to1) {
431  return to1 < o.to1;
432  }
433  if (from2 != o.from2) {
434  return from2 < o.from2;
435  }
436  return to2 < o.to2;
437  }
438 
439  const NBEdge* from1;
440  const NBEdge* to1;
441  const NBEdge* from2;
442  const NBEdge* to2;
443  };
444  typedef std::set<StreamPair> NeedsContRelation;
445  mutable NeedsContRelation myNeedsContRelation;
447 
448  typedef std::set<std::pair<int, int> > RightOnRedConflicts;
449  mutable RightOnRedConflicts myRightOnRedConflicts;
451 
452 private:
453  static std::set<NBEdge*> collectReachable(EdgeVector outer, const EdgeVector& within, bool checkControlled);
454 
455 
456 };
457 
458 
459 #endif
460 
461 /****************************************************************************/
462 
bool operator==(const StreamPair &o) const
virtual void setParticipantsInformation()
Builds the list of participating nodes/edges/links.
StreamPair(const NBEdge *_from1, const NBEdge *_to1, const NBEdge *_from2, const NBEdge *_to2)
TrafficLightType myType
The algorithm type for the traffic light.
long long int SUMOTime
Definition: SUMOTime.h:43
virtual bool rightOnRedConflict(int index, int foeIndex) const
whether the given index must yield to the foeIndex while turing right on a red light ...
virtual void addNode(NBNode *node)
Adds a node to the traffic light logic.
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...
void collectAllLinks()
helper method for use in NBOwnTLDef and NBLoadedSUMOTLDef
static const std::string DummyID
id for temporary definitions
RightOnRedConflicts myRightOnRedConflicts
A SUMO-compliant built logic for a traffic light.
EdgeVector myIncomingEdges
The list of incoming edges.
virtual ~NBTrafficLightDefinition()
Destructor.
The representation of a single edge during network building.
Definition: NBEdge.h:71
TrafficLightType getType() const
get the algorithm type (static etc..)
const NBConnectionVector & getControlledLinks() const
returns the controlled links (depends on previous call to collectLinks)
NBTrafficLightLogic * compute(OptionsCont &oc)
Computes the traffic light logic.
The base class for traffic light logic definitions.
virtual void replaceRemoved(NBEdge *removed, int removedLane, NBEdge *by, int byLane)=0
Replaces a removed edge/lane.
std::set< std::pair< int, int > > RightOnRedConflicts
bool needsCont(const NBEdge *fromE, const NBEdge *toE, const NBEdge *otherFromE, const NBEdge *otherToE) const
TLColor
An enumeration of possible tl-signal states.
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.
#define UNUSED_PARAMETER(x)
Definition: StdDefs.h:39
virtual void collectLinks()=0
Collects the links participating in this traffic light.
virtual void initNeedsContRelation() const
std::set< StreamPair > NeedsContRelation
SUMOTime getOffset()
Returns the offset.
virtual void collectEdges()
Build the list of participating edges.
std::set< std::string > myControlledInnerEdges
Set of inner edges that shall be controlled, though.
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.
int computeBrakingTime(SUMOReal minDecel) const
Computes the time vehicles may need to brake.
virtual void shiftTLConnectionLaneIndex(NBEdge *edge, int offset)
patches (loaded) signal plans by modifying lane indices
bool operator<(const StreamPair &o) const
virtual NBTrafficLightLogic * myCompute(int brakingTime)=0
Computes the traffic light logic finally in dependence to the type.
static const std::string DefaultProgramID
void setProgramID(const std::string &programID)
Sets the programID.
void addControlledInnerEdges(const std::vector< std::string > &edges)
Adds the given ids into the list of inner edges controlled by the tls.
const std::string & getProgramID() const
Returns the ProgramID.
An upper class for objects with additional parameters.
Definition: Parameterised.h:47
virtual void removeNode(NBNode *node)
Removes the given node from the list of controlled nodes.
Base class for objects which have an id.
Definition: Named.h:46
std::vector< NBConnection > NBConnectionVector
Definition of a connection vector.
const EdgeVector & getIncomingEdges() const
Returns the list of incoming edges (must be build first)
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.
std::vector< NBEdge * > EdgeVector
Definition: NBCont.h:41
const std::vector< NBNode * > & getNodes() const
Returns the list of controlled nodes.
A storage for options typed value containers)
Definition: OptionsCont.h:99
Represents a single node (junction) during network building.
Definition: NBNode.h:74
#define SUMOReal
Definition: config.h:213
data structure for caching needsCont information
std::vector< NBNode * > myControlledNodes
The container with participating nodes.
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.
static std::set< NBEdge * > collectReachable(EdgeVector outer, const EdgeVector &within, bool checkControlled)
NBConnectionVector myControlledLinks
The list of controlled links.
bool mustBrake(const NBEdge *const from, const NBEdge *const to) const
Returns the information whether the described flow must let any other flow pass.
std::vector< std::string > getControlledInnerEdges() const
Retrieve the ids of edges explicitly controlled by the tls.
EdgeVector myEdgesWithin
The list of edges within the area controlled by the tls.
virtual void setTLControllingInformation() const =0
Informs edges about being controlled by a tls.
virtual void setType(TrafficLightType type)
set the algorithm type (static etc..)
std::string mySubID
The tls program&#39;s subid.
TrafficLightType