SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
NBTrafficLightDefinition.cpp
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) 2001-2015 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 
23 
24 // ===========================================================================
25 // included modules
26 // ===========================================================================
27 #ifdef _MSC_VER
28 #include <windows_config.h>
29 #else
30 #include <config.h>
31 #endif
32 
33 #include <vector>
34 #include <string>
35 #include <algorithm>
36 #include <cassert>
37 #include <iterator>
39 #include <utils/common/ToString.h>
43 #include "NBTrafficLightLogic.h"
44 #include "NBOwnTLDef.h"
45 #include "NBContHelper.h"
46 
47 #ifdef CHECK_MEMORY_LEAKS
48 #include <foreign/nvwa/debug_new.h>
49 #endif // CHECK_MEMORY_LEAKS
50 
51 // ===========================================================================
52 // static members
53 // ===========================================================================
54 const std::string NBTrafficLightDefinition::DefaultProgramID = "0";
55 
56 // ===========================================================================
57 // method definitions
58 // ===========================================================================
60  const std::vector<NBNode*>& junctions, const std::string& programID,
61  SUMOTime offset, TrafficLightType type) :
62  Named(id),
63  myControlledNodes(junctions),
64  mySubID(programID), myOffset(offset),
65  myType(type),
66  myNeedsContRelationReady(false) {
67  std::vector<NBNode*>::iterator i = myControlledNodes.begin();
68  while (i != myControlledNodes.end()) {
69  for (std::vector<NBNode*>::iterator j = i + 1; j != myControlledNodes.end();) {
70  if (*i == *j) {
71  j = myControlledNodes.erase(j);
72  } else {
73  j++;
74  }
75  }
76  i++;
77  }
79  for (std::vector<NBNode*>::const_iterator i = junctions.begin(); i != junctions.end(); i++) {
80  (*i)->addTrafficLight(this);
81  }
82 }
83 
84 
86  NBNode* junction, const std::string& programID, SUMOTime offset, TrafficLightType type) :
87  Named(id),
88  mySubID(programID),
89  myOffset(offset),
90  myType(type),
91  myNeedsContRelationReady(false) {
92  addNode(junction);
93 }
94 
95 
96 NBTrafficLightDefinition::NBTrafficLightDefinition(const std::string& id, const std::string& programID,
97  SUMOTime offset, TrafficLightType type) :
98  Named(id),
99  mySubID(programID),
100  myOffset(offset),
101  myType(type),
102  myNeedsContRelationReady(false)
103 {}
104 
105 
107 
108 
111  // it is not really a traffic light if no incoming edge exists
112  if (amInvalid()) {
113  // make a copy of myControlledNodes because it will be modified;
114  std::vector<NBNode*> nodes = myControlledNodes;
115  for (std::vector<NBNode*>::iterator it = nodes.begin(); it != nodes.end(); it++) {
116  (*it)->removeTrafficLight(this);
117  }
118  WRITE_WARNING("The traffic light '" + getID() + "' does not control any links; it will not be build.");
119  return 0;
120  }
121  // compute the time needed to brake
122  unsigned int brakingTime = computeBrakingTime(oc.getFloat("tls.yellow.min-decel"));
123  // perform the computation depending on whether the traffic light
124  // definition was loaded or shall be computed new completely
125  if (oc.isSet("tls.yellow.time")) {
126  brakingTime = oc.getInt("tls.yellow.time");
127  }
128  NBTrafficLightLogic* ret = myCompute(ec, brakingTime);
129  ret->addParameter(getMap());
130  return ret;
131 }
132 
133 
134 bool
136  return myControlledLinks.size() == 0;
137 }
138 
139 
140 unsigned int
143  return (unsigned int)(vmax / minDecel);
144 }
145 
146 
147 void
149  // collect the information about participating edges and links
150  collectEdges();
151  collectLinks();
152 }
153 
154 
155 void
157  myIncomingEdges.clear();
158  EdgeVector myOutgoing;
159  // collect the edges from the participating nodes
160  for (std::vector<NBNode*>::iterator i = myControlledNodes.begin(); i != myControlledNodes.end(); i++) {
161  const EdgeVector& incoming = (*i)->getIncomingEdges();
162  copy(incoming.begin(), incoming.end(), back_inserter(myIncomingEdges));
163  const EdgeVector& outgoing = (*i)->getOutgoingEdges();
164  copy(outgoing.begin(), outgoing.end(), back_inserter(myOutgoing));
165  }
166  // check which of the edges are completely within the junction
167  // remove these edges from the list of incoming edges
168  // add them to the list of edges lying within the node
169  for (EdgeVector::iterator j = myIncomingEdges.begin(); j != myIncomingEdges.end();) {
170  NBEdge* edge = *j;
171  // an edge lies within the logic if it is outgoing as well as incoming
172  EdgeVector::iterator k = find(myOutgoing.begin(), myOutgoing.end(), edge);
173  if (k != myOutgoing.end()) {
174  if (myControlledInnerEdges.count(edge->getID()) == 0) {
175  myEdgesWithin.push_back(edge);
176  (*j)->setIsInnerEdge();
177  j = myIncomingEdges.erase(j);
178  continue;
179  }
180  }
181  ++j;
182  }
183 }
184 
185 
186 bool
187 NBTrafficLightDefinition::mustBrake(const NBEdge* const from, const NBEdge* const to) const {
188  std::vector<NBNode*>::const_iterator i =
189  find_if(myControlledNodes.begin(), myControlledNodes.end(),
191  assert(i != myControlledNodes.end());
192  NBNode* node = *i;
193  if (!node->hasOutgoing(to)) {
194  return true; // !!!
195  }
196  return node->mustBrake(from, to, -1, true);
197 }
198 
199 
200 bool
201 NBTrafficLightDefinition::mustBrake(const NBEdge* const possProhibitedFrom,
202  const NBEdge* const possProhibitedTo,
203  const NBEdge* const possProhibitorFrom,
204  const NBEdge* const possProhibitorTo,
205  bool regardNonSignalisedLowerPriority) const {
206  return forbids(possProhibitorFrom, possProhibitorTo,
207  possProhibitedFrom, possProhibitedTo,
208  regardNonSignalisedLowerPriority);
209 }
210 
211 
212 bool
214  const NBConnection& possProhibitor,
215  bool regardNonSignalisedLowerPriority) const {
216  return forbids(possProhibitor.getFrom(), possProhibitor.getTo(),
217  possProhibited.getFrom(), possProhibited.getTo(),
218  regardNonSignalisedLowerPriority);
219 }
220 
221 
222 bool
223 NBTrafficLightDefinition::forbids(const NBEdge* const possProhibitorFrom,
224  const NBEdge* const possProhibitorTo,
225  const NBEdge* const possProhibitedFrom,
226  const NBEdge* const possProhibitedTo,
227  bool regardNonSignalisedLowerPriority) const {
228  if (possProhibitorFrom == 0 || possProhibitorTo == 0 || possProhibitedFrom == 0 || possProhibitedTo == 0) {
229  return false;
230  }
231  // retrieve both nodes
232  std::vector<NBNode*>::const_iterator incoming =
233  find_if(myControlledNodes.begin(), myControlledNodes.end(), NBContHelper::node_with_incoming_finder(possProhibitorFrom));
234  std::vector<NBNode*>::const_iterator outgoing =
235  find_if(myControlledNodes.begin(), myControlledNodes.end(), NBContHelper::node_with_outgoing_finder(possProhibitedTo));
236  assert(incoming != myControlledNodes.end());
237  NBNode* incnode = *incoming;
238  NBNode* outnode = *outgoing;
239  EdgeVector::const_iterator i;
240  if (incnode != outnode) {
241  // the links are located at different nodes
242  const EdgeVector& ev1 = possProhibitedTo->getConnectedEdges();
243  // go through the following edge,
244  // check whether one of these connections is prohibited
245  for (i = ev1.begin(); i != ev1.end(); ++i) {
246  std::vector<NBNode*>::const_iterator outgoing2 =
248  if (outgoing2 == myControlledNodes.end()) {
249  continue;
250  }
251  NBNode* outnode2 = *outgoing2;
252  if (incnode != outnode2) {
253  continue;
254  }
255  bool ret1 = incnode->foes(possProhibitorTo, *i,
256  possProhibitedFrom, possProhibitedTo);
257  bool ret2 = incnode->forbids(possProhibitorFrom, possProhibitorTo,
258  possProhibitedTo, *i,
259  regardNonSignalisedLowerPriority);
260  bool ret = ret1 || ret2;
261  if (ret) {
262  return true;
263  }
264  }
265 
266  const EdgeVector& ev2 = possProhibitorTo->getConnectedEdges();
267  // go through the following edge,
268  // check whether one of these connections is prohibited
269  for (i = ev2.begin(); i != ev2.end(); ++i) {
270  std::vector<NBNode*>::const_iterator incoming2 =
271  find_if(myControlledNodes.begin(), myControlledNodes.end(), NBContHelper::node_with_incoming_finder(possProhibitorTo));
272  if (incoming2 == myControlledNodes.end()) {
273  continue;
274  }
275  NBNode* incnode2 = *incoming2;
276  if (incnode2 != outnode) {
277  continue;
278  }
279  bool ret1 = incnode2->foes(possProhibitorTo, *i,
280  possProhibitedFrom, possProhibitedTo);
281  bool ret2 = incnode2->forbids(possProhibitorTo, *i,
282  possProhibitedFrom, possProhibitedTo,
283  regardNonSignalisedLowerPriority);
284  bool ret = ret1 || ret2;
285  if (ret) {
286  return true;
287  }
288  }
289  return false;
290  }
291  // both links are located at the same node
292  // check using this node's information
293  return incnode->forbids(possProhibitorFrom, possProhibitorTo,
294  possProhibitedFrom, possProhibitedTo,
295  regardNonSignalisedLowerPriority);
296 }
297 
298 
299 bool
300 NBTrafficLightDefinition::foes(const NBEdge* const from1, const NBEdge* const to1,
301  const NBEdge* const from2, const NBEdge* const to2) const {
302  if (to1 == 0 || to2 == 0) {
303  return false;
304  }
305  // retrieve both nodes (it is possible that a connection
306  std::vector<NBNode*>::const_iterator incoming =
307  find_if(myControlledNodes.begin(), myControlledNodes.end(),
309  std::vector<NBNode*>::const_iterator outgoing =
310  find_if(myControlledNodes.begin(), myControlledNodes.end(),
312  assert(incoming != myControlledNodes.end());
313  NBNode* incnode = *incoming;
314  NBNode* outnode = *outgoing;
315  if (incnode != outnode) {
316  return false;
317  }
318  return incnode->foes(from1, to1, from2, to2);
319 }
320 
321 
322 void
324  if (std::find(myControlledNodes.begin(), myControlledNodes.end(), node) == myControlledNodes.end()) {
325  myControlledNodes.push_back(node);
327  node->addTrafficLight(this);
328  }
329 }
330 
331 
332 void
334  std::vector<NBNode*>::iterator i = std::find(myControlledNodes.begin(), myControlledNodes.end(), node);
335  if (i != myControlledNodes.end()) {
336  myControlledNodes.erase(i);
337  }
338  // !!! remove in node?
339 }
340 
341 
342 void
343 NBTrafficLightDefinition::addControlledInnerEdges(const std::vector<std::string>& edges) {
344  myControlledInnerEdges.insert(edges.begin(), edges.end());
345 }
346 
347 
348 std::vector<std::string>
350  return std::vector<std::string>(myControlledInnerEdges.begin(), myControlledInnerEdges.end());
351 }
352 
353 
354 const EdgeVector&
356  return myIncomingEdges;
357 }
358 
359 
360 void
362  myControlledLinks.clear();
363  // build the list of links which are controled by the traffic light
364  for (EdgeVector::iterator i = myIncomingEdges.begin(); i != myIncomingEdges.end(); i++) {
365  NBEdge* incoming = *i;
366  unsigned int noLanes = incoming->getNumLanes();
367  for (unsigned int j = 0; j < noLanes; j++) {
368  std::vector<NBEdge::Connection> connected = incoming->getConnectionsFromLane(j);
369  for (std::vector<NBEdge::Connection>::iterator k = connected.begin(); k != connected.end(); k++) {
370  const NBEdge::Connection& el = *k;
371  if (incoming->mayBeTLSControlled(el.fromLane, el.toEdge, el.toLane)) {
372  if (el.toEdge != 0 && el.toLane >= (int) el.toEdge->getNumLanes()) {
373  throw ProcessError("Connection '" + incoming->getID() + "_" + toString(j) + "->" + el.toEdge->getID() + "_" + toString(el.toLane) + "' yields in a not existing lane.");
374  }
375  int tlIndex = (int)myControlledLinks.size();
376  myControlledLinks.push_back(NBConnection(incoming, el.fromLane, el.toEdge, el.toLane, tlIndex));
377  }
378  }
379  }
380  }
381 }
382 
383 
384 bool
385 NBTrafficLightDefinition::needsCont(const NBEdge* fromE, const NBEdge* toE, const NBEdge* otherFromE, const NBEdge* otherToE) const {
388  assert(myNeedsContRelationReady);
389  }
390  return std::find(myNeedsContRelation.begin(), myNeedsContRelation.end(),
391  StreamPair(fromE, toE, otherFromE, otherToE)) != myNeedsContRelation.end();
392 }
393 
394 
395 void
397  if (!amInvalid()) {
398  NBOwnTLDef dummy("dummy", myControlledNodes, 0, TLTYPE_STATIC);
399  dummy.initNeedsContRelation();
401  for (std::vector<NBNode*>::const_iterator i = myControlledNodes.begin(); i != myControlledNodes.end(); i++) {
402  (*i)->removeTrafficLight(&dummy);
403  }
404  }
406 }
407 
408 /****************************************************************************/
409 
virtual void setParticipantsInformation()
Builds the list of participating nodes/edges/links.
A structure which describes a connection between edges or lanes.
Definition: NBEdge.h:148
int toLane
The lane the connections yields in.
Definition: NBEdge.h:166
virtual void addNode(NBNode *node)
Adds a node to the traffic light logic.
void collectAllLinks()
helper method for use in NBOwnTLDef and NBLoadedSUMOTLDef
NBEdge * toEdge
The edge the connections yields in.
Definition: NBEdge.h:164
std::vector< std::string > getControlledInnerEdges() const
Retrieve the ids of edges explicitly controlled by the tls.
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
bool mayBeTLSControlled(int fromLane, NBEdge *toEdge, int toLane) const
Definition: NBEdge.cpp:1811
Used for sorting the cells by the begin time they describe.
Definition: NBNode.h:651
bool mustBrake(const NBEdge *const from, const NBEdge *const to, int fromLane, bool includePedCrossings) const
Returns the information whether the described flow must let any other flow pass.
Definition: NBNode.cpp:1167
SUMOReal getFloat(const std::string &name) const
Returns the SUMOReal-value of the named option (only for Option_Float)
const std::map< std::string, std::string > & getMap() const
Returns the inner key/value map.
const EdgeVector & getIncomingEdges() const
Returns the list of incoming edges (must be build first)
std::vector< Connection > getConnectionsFromLane(unsigned int lane) const
Returns connections from a given lane.
Definition: NBEdge.cpp:747
NBEdge * getFrom() const
returns the from-edge (start of the connection)
#define WRITE_WARNING(msg)
Definition: MsgHandler.h:200
virtual void collectLinks()=0
Collects the links participating in this traffic light If a link could not be found.
EdgeVector getConnectedEdges() const
Returns the list of outgoing edges unsorted.
Definition: NBEdge.cpp:815
const std::string & getID() const
Returns the id.
Definition: Named.h:60
virtual void collectEdges()
Build the list of participating edges.
std::set< std::string > myControlledInnerEdges
Set of inner edges that shall be controlled, though.
int fromLane
The lane the connections starts at.
Definition: NBEdge.h:162
NBTrafficLightLogic * compute(const NBEdgeCont &ec, OptionsCont &oc)
Computes the traffic light logic.
static SUMOReal maxSpeed(const EdgeVector &ev)
unsigned int getNumLanes() const
Returns the number of lanes.
Definition: NBEdge.h:347
Storage for edges, including some functionality operating on multiple edges.
Definition: NBEdgeCont.h:66
static const std::string DefaultProgramID
unsigned int computeBrakingTime(SUMOReal minDecel) const
Computes the time vehicles may need to brake.
void addControlledInnerEdges(const std::vector< std::string > &edges)
Adds the given ids into the list of inner edges controlled by the tls.
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::string toString(const T &t, std::streamsize accuracy=OUTPUT_ACCURACY)
Definition: ToString.h:53
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:45
void addParameter(const std::string &key, const std::string &value)
Adds a parameter.
bool needsCont(const NBEdge *fromE, const NBEdge *toE, const NBEdge *otherFromE, const NBEdge *otherToE) const
void addTrafficLight(NBTrafficLightDefinition *tlDef)
Adds a traffic light to the list of traffic lights that control this node.
Definition: NBNode.cpp:295
std::vector< NBEdge * > EdgeVector
Definition: NBCont.h:41
A storage for options typed value containers)
Definition: OptionsCont.h:108
int SUMOTime
Definition: SUMOTime.h:43
NBEdge * getTo() const
returns the to-edge (end of the connection)
Represents a single node (junction) during network building.
Definition: NBNode.h:75
bool forbids(const NBEdge *const possProhibitorFrom, const NBEdge *const possProhibitorTo, const NBEdge *const possProhibitedFrom, const NBEdge *const possProhibitedTo, bool regardNonSignalisedLowerPriority) const
Returns the information whether "prohibited" flow must let "prohibitor" flow pass.
virtual NBTrafficLightLogic * myCompute(const NBEdgeCont &ec, unsigned int brakingTime)=0
Computes the traffic light logic finally in dependence to the type.
#define SUMOReal
Definition: config.h:218
void initNeedsContRelation() const
Definition: NBOwnTLDef.cpp:524
data structure for caching needsCont information
std::vector< NBNode * > myControlledNodes
The container with participating nodes.
NBTrafficLightDefinition(const std::string &id, const std::vector< NBNode * > &junctions, const std::string &programID, SUMOTime offset, TrafficLightType type)
Constructor.
A traffic light logics which must be computed (only nodes/edges are given)
Definition: NBOwnTLDef.h:54
int getInt(const std::string &name) const
Returns the int-value of the named option (only for Option_Integer)
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: NBNode.cpp:1223
NBConnectionVector myControlledLinks
The list of controlled links.
EdgeVector myEdgesWithin
The list of edges within the area controlled by the tls.
TrafficLightType
bool isSet(const std::string &name, bool failOnNonExistant=true) const
Returns the information whether the named option is set.
bool forbids(const NBEdge *const possProhibitorFrom, const NBEdge *const possProhibitorTo, const NBEdge *const possProhibitedFrom, const NBEdge *const possProhibitedTo, bool regardNonSignalisedLowerPriority) const
Returns the information whether "prohibited" flow must let "prohibitor" flow pass.
Definition: NBNode.cpp:1213
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 void initNeedsContRelation() const