SUMO - Simulation of Urban MObility
NBLoadedSUMOTLDef.cpp
Go to the documentation of this file.
1 /****************************************************************************/
9 // A complete traffic light logic loaded from a sumo-net. (opted to reimplement
10 // since NBLoadedTLDef is quite vissim specific)
11 /****************************************************************************/
12 // SUMO, Simulation of Urban MObility; see http://sumo.dlr.de/
13 // Copyright (C) 2011-2016 DLR (http://www.dlr.de/) and contributors
14 /****************************************************************************/
15 //
16 // This file is part of SUMO.
17 // SUMO is free software: you can redistribute it and/or modify
18 // it under the terms of the GNU General Public License as published by
19 // the Free Software Foundation, either version 3 of the License, or
20 // (at your option) any later version.
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 <set>
35 #include <cassert>
36 #include <iterator>
38 #include <utils/common/ToString.h>
40 #include "NBTrafficLightLogic.h"
41 #include "NBOwnTLDef.h"
43 #include "NBLoadedSUMOTLDef.h"
44 #include "NBNode.h"
45 
46 #ifdef CHECK_MEMORY_LEAKS
47 #include <foreign/nvwa/debug_new.h>
48 #endif // CHECK_MEMORY_LEAKS
49 
50 // ===========================================================================
51 // method definitions
52 // ===========================================================================
53 
54 NBLoadedSUMOTLDef::NBLoadedSUMOTLDef(const std::string& id, const std::string& programID,
55  SUMOTime offset, TrafficLightType type) :
56  NBTrafficLightDefinition(id, programID, offset, type),
57  myTLLogic(0) {
58  myTLLogic = new NBTrafficLightLogic(id, programID, 0, offset, type);
59 }
60 
61 
63  // allow for adding a new program for the same def: take the programID from the new logic
64  NBTrafficLightDefinition(def->getID(), logic->getProgramID(), def->getOffset(), def->getType()),
66  myOriginalNodes(def->getNodes().begin(), def->getNodes().end()) {
67  assert(def->getOffset() == logic->getOffset());
68  assert(def->getType() == logic->getType());
70  myControlledNodes = def->getNodes();
71 }
72 
73 
75  delete myTLLogic;
76 }
77 
78 
80 NBLoadedSUMOTLDef::myCompute(unsigned int brakingTimeSeconds) {
81  // @todo what to do with those parameters?
82  UNUSED_PARAMETER(brakingTimeSeconds);
85  return new NBTrafficLightLogic(myTLLogic);
86 }
87 
88 
89 void
90 NBLoadedSUMOTLDef::addConnection(NBEdge* from, NBEdge* to, int fromLane, int toLane, int linkIndex) {
91  assert(myTLLogic->getNumLinks() > 0); // logic should be loaded by now
92  if (linkIndex >= (int)myTLLogic->getNumLinks()) {
93  throw ProcessError("Invalid linkIndex " + toString(linkIndex) + " for traffic light '" + getID() +
94  "' with " + toString(myTLLogic->getNumLinks()) + " links.");
95  }
96  NBConnection conn(from, fromLane, to, toLane, linkIndex);
97  // avoid duplicates
98  remove_if(myControlledLinks.begin(), myControlledLinks.end(), connection_equal(conn));
99  myControlledLinks.push_back(conn);
100  addNode(from->getToNode());
101  addNode(to->getFromNode());
102  myOriginalNodes.insert(from->getToNode());
103  myOriginalNodes.insert(to->getFromNode());
104  // added connections are definitely controlled. make sure none are removed because they lie within the tl
105  // myControlledInnerEdges.insert(from->getID()); // @todo recheck: this appears to be obsolete
106  // set this information now so that it can be used while loading diffs
107  from->setControllingTLInformation(conn, getID());
108 }
109 
110 
111 void
113  // if nodes have been removed our links may have been invalidated as well
114  // since no logic will be built anyway there is no reason to inform any edges
115  if (amInvalid()) {
116  return;
117  }
118  // set the information about the link's positions within the tl into the
119  // edges the links are starting at, respectively
120  for (NBConnectionVector::const_iterator it = myControlledLinks.begin(); it != myControlledLinks.end(); it++) {
121  const NBConnection& c = *it;
122  if (c.getTLIndex() >= (int)myTLLogic->getNumLinks()) {
123  throw ProcessError("Invalid linkIndex " + toString(c.getTLIndex()) + " for traffic light '" + getID() +
124  "' with " + toString(myTLLogic->getNumLinks()) + " links.");
125  }
126  NBEdge* edge = c.getFrom();
128  }
129 }
130 
131 
132 void
134 
135 
136 void
137 NBLoadedSUMOTLDef::replaceRemoved(NBEdge* removed, int removedLane, NBEdge* by, int byLane) {
138  for (NBConnectionVector::iterator it = myControlledLinks.begin(); it != myControlledLinks.end(); ++it) {
139  (*it).replaceFrom(removed, removedLane, by, byLane);
140  (*it).replaceTo(removed, removedLane, by, byLane);
141  }
142 }
143 
144 
145 void
146 NBLoadedSUMOTLDef::addPhase(SUMOTime duration, const std::string& state) {
147  myTLLogic->addStep(duration, state);
148 }
149 
150 
151 bool
153  if (myControlledLinks.size() == 0) {
154  return true;
155  }
156  // make sure that myControlledNodes are the original nodes
157  if (myControlledNodes.size() != myOriginalNodes.size()) {
158  return true;
159  }
160  for (std::vector<NBNode*>::const_iterator i = myControlledNodes.begin(); i != myControlledNodes.end(); i++) {
161  if (myOriginalNodes.count(*i) != 1) {
162  return true;
163  }
164  }
165  return false;
166 }
167 
168 
169 void
170 NBLoadedSUMOTLDef::removeConnection(const NBConnection& conn, bool reconstruct) {
171  NBConnectionVector::iterator it = myControlledLinks.begin();
172  // find the connection but ignore its TLIndex since it might have been
173  // invalidated by an earlier removal
174  for (; it != myControlledLinks.end(); ++it) {
175  if (it->getFrom() == conn.getFrom() &&
176  it->getTo() == conn.getTo() &&
177  it->getFromLane() == conn.getFromLane() &&
178  it->getToLane() == conn.getToLane()) {
179  break;
180  }
181  }
182  if (it == myControlledLinks.end()) {
183  // a traffic light doesn't always controll all connections at a junction
184  // especially when using the option --tls.join
185  return;
186  }
187  const int removed = it->getTLIndex();
188  // remove the connection
189  myControlledLinks.erase(it);
190  if (reconstruct) {
191  // updating the edge is only needed for immediate use in NETEDIT.
192  // It may conflict with loading diffs
193  conn.getFrom()->setControllingTLInformation(conn, "");
194  // shift link numbers down so there is no gap
195  for (NBConnectionVector::iterator it = myControlledLinks.begin(); it != myControlledLinks.end(); it++) {
196  NBConnection& c = *it;
197  if (c.getTLIndex() > removed) {
198  c.setTLIndex(c.getTLIndex() - 1);
199  }
200  }
201  // update controlling information with new link numbers
203  // rebuild the logic
204  const std::vector<NBTrafficLightLogic::PhaseDefinition> phases = myTLLogic->getPhases();
206  for (std::vector<NBTrafficLightLogic::PhaseDefinition>::const_iterator it = phases.begin(); it != phases.end(); it++) {
207  std::string newState = it->state;
208  newState.erase(newState.begin() + removed);
209  newLogic->addStep(it->duration, newState);
210  }
211  delete myTLLogic;
212  myTLLogic = newLogic;
213  }
214 }
215 
216 
217 void
219  myOffset = offset;
220  myTLLogic->setOffset(offset);
221 }
222 
223 
224 void
226  myType = type;
227  myTLLogic->setType(type);
228 }
229 
230 
231 void
233  if (myControlledLinks.size() == 0) {
235  }
236  myIncomingEdges.clear();
237  EdgeVector myOutgoing;
238  // collect the edges from the participating nodes
239  for (std::vector<NBNode*>::iterator i = myControlledNodes.begin(); i != myControlledNodes.end(); i++) {
240  const EdgeVector& incoming = (*i)->getIncomingEdges();
241  copy(incoming.begin(), incoming.end(), back_inserter(myIncomingEdges));
242  const EdgeVector& outgoing = (*i)->getOutgoingEdges();
243  copy(outgoing.begin(), outgoing.end(), back_inserter(myOutgoing));
244  }
245  // check which of the edges are completely within the junction
246  // and which are uncontrolled as well (we already know myControlledLinks)
247  for (EdgeVector::iterator j = myIncomingEdges.begin(); j != myIncomingEdges.end();) {
248  NBEdge* edge = *j;
249  // an edge lies within the logic if it is outgoing as well as incoming
250  EdgeVector::iterator k = find(myOutgoing.begin(), myOutgoing.end(), edge);
251  if (k != myOutgoing.end()) {
252  if (myControlledInnerEdges.count(edge->getID()) == 0) {
253  bool controlled = false;
254  for (NBConnectionVector::iterator it = myControlledLinks.begin(); it != myControlledLinks.end(); it++) {
255  if ((*it).getFrom() == edge) {
256  controlled = true;
257  break;
258  }
259  }
260  if (controlled) {
261  myControlledInnerEdges.insert(edge->getID());
262  } else {
263  myEdgesWithin.push_back(edge);
264  (*j)->setIsInnerEdge();
265  ++j; //j = myIncomingEdges.erase(j);
266  continue;
267  }
268  }
269  }
270  ++j;
271  }
272 }
273 
274 
275 void
277  if (myControlledLinks.size() == 0) {
278  // maybe we only loaded a different program for a default traffic light.
279  // Try to build links now.
280  myOriginalNodes.insert(myControlledNodes.begin(), myControlledNodes.end());
281  collectAllLinks();
282  }
283 }
284 
285 
287 void
289  // avoid shifting twice if the edge is incoming and outgoing to a joined TLS
290  if (myShifted.count(edge) == 0) {
292  myShifted.insert(edge);
293  for (NBConnectionVector::iterator it = myControlledLinks.begin(); it != myControlledLinks.end(); it++) {
294  (*it).shiftLaneIndex(edge, offset);
295  }
296  }
297 }
298 
299 void
301  // XXX what to do if crossings are removed during network building?
302  const unsigned int size = myTLLogic->getNumLinks();
303  unsigned int noLinksAll = 0;
304  for (NBConnectionVector::const_iterator it = myControlledLinks.begin(); it != myControlledLinks.end(); it++) {
305  const NBConnection& c = *it;
307  noLinksAll = MAX2(noLinksAll, (unsigned int)c.getTLIndex() + 1);
308  }
309  }
310  int oldCrossings = 0;
311  // collect crossings
312  std::vector<NBNode::Crossing> crossings;
313  for (std::vector<NBNode*>::iterator i = myControlledNodes.begin(); i != myControlledNodes.end(); i++) {
314  const std::vector<NBNode::Crossing>& c = (*i)->getCrossings();
315  // set tl indices for crossings
316  (*i)->setCrossingTLIndices(noLinksAll);
317  copy(c.begin(), c.end(), std::back_inserter(crossings));
318  noLinksAll += (unsigned int)c.size();
319  oldCrossings += (*i)->numCrossingsFromSumoNet();
320  }
321  const int newCrossings = (int)crossings.size() - oldCrossings;
322  if (newCrossings > 0) {
323  const std::vector<NBTrafficLightLogic::PhaseDefinition> phases = myTLLogic->getPhases();
324  if (phases.size() > 0) {
325  if (phases.front().state.size() == noLinksAll - newCrossings) {
326  // patch states for the newly added crossings
327 
328  // collect edges
329  EdgeVector fromEdges(size, 0);
330  EdgeVector toEdges(size, 0);
331  std::vector<int> fromLanes(size, 0);
332  collectEdgeVectors(fromEdges, toEdges, fromLanes);
333  const std::string crossingDefaultState(newCrossings, 'r');
334 
335  // rebuild the logic (see NBOwnTLDef.cpp::myCompute)
336  const std::vector<NBTrafficLightLogic::PhaseDefinition> phases = myTLLogic->getPhases();
338  SUMOTime brakingTime = TIME2STEPS(3);
339  //std::cout << "patchIfCrossingsAdded for " << getID() << " numPhases=" << phases.size() << "\n";
340  for (std::vector<NBTrafficLightLogic::PhaseDefinition>::const_iterator it = phases.begin(); it != phases.end(); it++) {
341  if ((*it).state.find_first_of("yY") != std::string::npos) {
342  brakingTime = MAX2(brakingTime, it->duration);
343  }
344  NBOwnTLDef::addPedestrianPhases(newLogic, it->duration, it->state + crossingDefaultState, crossings, fromEdges, toEdges);
345  }
346  NBOwnTLDef::addPedestrianScramble(newLogic, noLinksAll, TIME2STEPS(10), brakingTime, crossings, fromEdges, toEdges);
347 
348  delete myTLLogic;
349  myTLLogic = newLogic;
350  } else if (phases.front().state.size() != noLinksAll) {
351  WRITE_WARNING("Could not patch tlLogic " + getID() + "for new crossings");
352  }
353  }
354  }
355 }
356 
357 
358 void
359 NBLoadedSUMOTLDef::collectEdgeVectors(EdgeVector& fromEdges, EdgeVector& toEdges, std::vector<int>& fromLanes) const {
360  assert(fromEdges.size() > 0);
361  assert(fromEdges.size() == toEdges.size());
362  const int size = (int)fromEdges.size();
363 
364  for (NBConnectionVector::const_iterator it = myControlledLinks.begin(); it != myControlledLinks.end(); it++) {
365  const NBConnection& c = *it;
367  if (c.getTLIndex() >= size) {
368  throw ProcessError("Invalid linkIndex " + toString(c.getTLIndex()) + " for traffic light '" + getID() +
369  "' with " + toString(size) + " links.");
370  }
371  fromEdges[c.getTLIndex()] = c.getFrom();
372  toEdges[c.getTLIndex()] = c.getTo();
373  fromLanes[c.getTLIndex()] = c.getFromLane();
374  }
375  }
376 }
377 
378 
379 void
381  if (!amInvalid() && !myNeedsContRelationReady) {
382  myNeedsContRelation.clear();
383  myRightOnRedConflicts.clear();
384  const bool controlledWithin = !OptionsCont::getOptions().getBool("tls.uncontrolled-within");
385  const std::vector<NBTrafficLightLogic::PhaseDefinition> phases = myTLLogic->getPhases();
386  for (std::vector<NBTrafficLightLogic::PhaseDefinition>::const_iterator it = phases.begin(); it != phases.end(); it++) {
387  const std::string state = (*it).state;
388  for (NBConnectionVector::const_iterator it1 = myControlledLinks.begin(); it1 != myControlledLinks.end(); it1++) {
389  const NBConnection& c1 = *it1;
390  const int i1 = c1.getTLIndex();
391  if (i1 == NBConnection::InvalidTlIndex || state[i1] != 'g' || c1.getFrom() == 0 || c1.getTo() == 0) {
392  continue;
393  }
394  for (NBConnectionVector::const_iterator it2 = myControlledLinks.begin(); it2 != myControlledLinks.end(); it2++) {
395  const NBConnection& c2 = *it2;
396  const int i2 = c2.getTLIndex();
398  && i2 != i1
399  && (state[i2] == 'G' || state[i2] == 'g')
400  && c2.getFrom() != 0 && c2.getTo() != 0) {
401  const bool rightTurnConflict = NBNode::rightTurnConflict(
402  c1.getFrom(), c1.getTo(), c1.getFromLane(), c2.getFrom(), c2.getTo(), c2.getFromLane());
403  const bool forbidden = forbids(c2.getFrom(), c2.getTo(), c1.getFrom(), c1.getTo(), true, controlledWithin);
404  const bool isFoes = foes(c2.getFrom(), c2.getTo(), c1.getFrom(), c1.getTo());
405  if (forbidden || rightTurnConflict) {
406  myNeedsContRelation.insert(StreamPair(c1.getFrom(), c1.getTo(), c2.getFrom(), c2.getTo()));
407  }
408  if (isFoes) {
409  myRightOnRedConflicts.insert(std::make_pair(i1, i2));
410  }
411  //std::cout << getID() << " i1=" << i1 << " i2=" << i2 << " rightTurnConflict=" << rightTurnConflict << " forbidden=" << forbidden << " isFoes=" << isFoes << "\n";
412  }
413  }
414  }
415  }
416  }
419 }
420 
421 
422 bool
423 NBLoadedSUMOTLDef::rightOnRedConflict(int index, int foeIndex) const {
424  if (amInvalid()) {
425  return false;
426  }
430  }
431  return std::find(myRightOnRedConflicts.begin(), myRightOnRedConflicts.end(), std::make_pair(index, foeIndex)) != myRightOnRedConflicts.end();
432 }
433 
434 
435 /****************************************************************************/
436 
void removeConnection(const NBConnection &conn, bool reconstruct=true)
removes the given connection from the traffic light if recontruct=true, reconstructs the logic and in...
bool setControllingTLInformation(const NBConnection &c, const std::string &tlID)
Returns if the link could be set as to be controlled.
Definition: NBEdge.cpp:1931
TrafficLightType getType() const
get the algorithm type (static etc..)
TrafficLightType myType
The algorithm type for the traffic light.
long long int SUMOTime
Definition: SUMOTime.h:43
virtual void addNode(NBNode *node)
Adds a node to the traffic light logic.
NBLoadedSUMOTLDef(const std::string &id, const std::string &programID, SUMOTime offset, TrafficLightType type)
Constructor.
int getTLIndex() const
Definition: NBConnection.h:101
bool rightOnRedConflict(int index, int foeIndex) const
whether the given index must yield to the foeIndex while turing right on a red light ...
void collectEdges()
Build the list of participating edges.
void collectAllLinks()
helper method for use in NBOwnTLDef and NBLoadedSUMOTLDef
void setOffset(SUMOTime offset)
Sets the offset of this tls.
static void addPedestrianScramble(NBTrafficLightLogic *logic, unsigned int noLinksAll, SUMOTime greenTime, SUMOTime yellowTime, const std::vector< NBNode::Crossing > &crossings, const EdgeVector &fromEdges, const EdgeVector &toEdges)
add an additional pedestrian phase if there are crossings that did not get green yet ...
Definition: NBOwnTLDef.cpp:660
void closeBuilding()
closes the building process
RightOnRedConflicts myRightOnRedConflicts
A SUMO-compliant built logic for a traffic light.
TrafficLightType getType() const
get the algorithm type (static etc..)
EdgeVector myIncomingEdges
The list of incoming edges.
bool getBool(const std::string &name) const
Returns the boolean-value of the named option (only for Option_Bool)
const std::string & getProgramID() const
Returns the ProgramID.
The representation of a single edge during network building.
Definition: NBEdge.h:70
class for identifying connections
int getFromLane() const
returns the from-lane
The base class for traffic light logic definitions.
void setOffset(SUMOTime offset)
Sets the offset of this tls.
T MAX2(T a, T b)
Definition: StdDefs.h:75
#define TIME2STEPS(x)
Definition: SUMOTime.h:66
SUMOTime myOffset
The offset in the program.
NBEdge * getFrom() const
returns the from-edge (start of the connection)
#define UNUSED_PARAMETER(x)
Definition: StdDefs.h:39
#define WRITE_WARNING(msg)
Definition: MsgHandler.h:200
void replaceRemoved(NBEdge *removed, int removedLane, NBEdge *by, int byLane)
Replaces a removed edge/lane.
static OptionsCont & getOptions()
Retrieves the options.
Definition: OptionsCont.cpp:69
static bool rightTurnConflict(const NBEdge *from, const NBEdge *to, int fromLane, const NBEdge *prohibitorFrom, const NBEdge *prohibitorTo, int prohibitorFromLane, bool lefthand=false)
return whether the given laneToLane connection is a right turn which must yield to a bicycle crossing...
Definition: NBNode.cpp:1307
void setTLIndex(int tlIndex)
Definition: NBConnection.h:106
SUMOTime getOffset()
Returns the offset.
#define new
Definition: debug_new.h:121
void setType(TrafficLightType type)
set the algorithm type (static etc..)
const std::string & getID() const
Returns the id.
Definition: Named.h:65
virtual void collectEdges()
Build the list of participating edges.
static const int InvalidTlIndex
Definition: NBConnection.h:127
const std::vector< PhaseDefinition > & getPhases() const
Returns the phases.
std::set< std::string > myControlledInnerEdges
Set of inner edges that shall be controlled, though.
const NBConnectionVector & getControlledLinks() const
returns the controlled links (depends on previous call to collectLinks)
void patchIfCrossingsAdded()
repair the plan if controlled nodes received pedestrian crossings
std::set< NBNode * > myOriginalNodes
The original nodes for which the loaded logic is valid.
void initNeedsContRelation() const
static std::string addPedestrianPhases(NBTrafficLightLogic *logic, SUMOTime greenTime, std::string state, const std::vector< NBNode::Crossing > &crossings, const EdgeVector &fromEdges, const EdgeVector &toEdges)
add 1 or 2 phases depending on the presence of pedestrian crossings
Definition: NBOwnTLDef.cpp:441
std::string toString(const T &t, std::streamsize accuracy=OUTPUT_ACCURACY)
Definition: ToString.h:54
void remapRemoved(NBEdge *removed, const EdgeVector &incoming, const EdgeVector &outgoing)
Replaces occurences of the removed edge in incoming/outgoing edges of all definitions.
const std::vector< NBNode * > & getNodes() const
Returns the list of controlled nodes.
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.
SUMOTime getOffset() const
Returns the offset of first switch.
std::set< NBEdge * > myShifted
set of edges with shifted lane indices (to avoid shifting twice)
void setTLControllingInformation() const
Informs edges about being controlled by a tls.
NBNode * getToNode() const
Returns the destination node of the edge.
Definition: NBEdge.h:369
unsigned int getNumLinks()
Returns the number of participating links.
void addPhase(SUMOTime duration, const std::string &state)
Adds a phase to the logic the new phase is inserted at the end of the list of already added phases...
std::vector< NBEdge * > EdgeVector
Definition: NBCont.h:41
NBTrafficLightLogic * myCompute(unsigned int brakingTimeSeconds)
Computes the traffic light logic finally in dependence to the type.
int getToLane() const
returns the to-lane
void collectLinks()
Collects the links participating in this traffic light (only if not previously loaded) ...
NBEdge * getTo() const
returns the to-edge (end of the connection)
~NBLoadedSUMOTLDef()
Destructor.
data structure for caching needsCont information
void setType(TrafficLightType type)
Sets the algorithm type of this tls.
void collectEdgeVectors(EdgeVector &fromEdges, EdgeVector &toEdges, std::vector< int > &fromLanes) const
Collects the edges for each tlIndex.
std::vector< NBNode * > myControlledNodes
The container with participating nodes.
void addStep(SUMOTime duration, const std::string &state, int index=-1)
Adds a phase to the logic.
NBConnectionVector myControlledLinks
The list of controlled links.
EdgeVector myEdgesWithin
The list of edges within the area controlled by the tls.
void shiftTLConnectionLaneIndex(NBEdge *edge, int offset)
patches signal plans by modifying lane indices
NBTrafficLightLogic * myTLLogic
phases are added directly to myTLLogic which is then returned in myCompute()
void addConnection(NBEdge *from, NBEdge *to, int fromLane, int toLane, int linkIndex)
Adds a connection and immediately informs the edges.
TrafficLightType
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.
NBNode * getFromNode() const
Returns the origin node of the edge.
Definition: NBEdge.h:361