SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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-2015 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()),
65  myTLLogic(new NBTrafficLightLogic(logic)),
66  myOriginalNodes(def->getNodes().begin(), def->getNodes().end()) {
67  assert(def->getOffset() == logic->getOffset());
68  assert(def->getType() == logic->getType());
70 }
71 
72 
74  delete myTLLogic;
75 }
76 
77 
79 NBLoadedSUMOTLDef::myCompute(const NBEdgeCont& ec, unsigned int brakingTimeSeconds) {
80  // @todo what to do with those parameters?
81  UNUSED_PARAMETER(ec);
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  WRITE_ERROR("Invalid linkIndex " + toString(linkIndex) + " for traffic light '" + getID() +
94  "' with " + toString(myTLLogic->getNumLinks()) + " links.");
95  return;
96  }
97  NBConnection conn(from, fromLane, to, toLane, linkIndex);
98  // avoid duplicates
99  remove_if(myControlledLinks.begin(), myControlledLinks.end(), connection_equal(conn));
100  myControlledLinks.push_back(conn);
101  addNode(from->getToNode());
102  addNode(to->getFromNode());
103  myOriginalNodes.insert(from->getToNode());
104  myOriginalNodes.insert(to->getFromNode());
105  // added connections are definitely controlled. make sure none are removed because they lie within the tl
106  // myControlledInnerEdges.insert(from->getID()); // @todo recheck: this appears to be obsolete
107  // set this information now so that it can be used while loading diffs
108  from->setControllingTLInformation(conn, getID());
109 }
110 
111 
112 void
115 }
116 
117 
118 void
120  // if nodes have been removed our links may have been invalidated as well
121  // since no logic will be built anyway there is no reason to inform any edges
122  if (amInvalid()) {
123  return;
124  }
125  // set the information about the link's positions within the tl into the
126  // edges the links are starting at, respectively
127  for (NBConnectionVector::const_iterator it = myControlledLinks.begin(); it != myControlledLinks.end(); it++) {
128  const NBConnection& c = *it;
129  assert(c.getTLIndex() < (int)myTLLogic->getNumLinks());
130  NBEdge* edge = c.getFrom();
132  }
133 }
134 
135 
136 void
138 
139 
140 void
141 NBLoadedSUMOTLDef::replaceRemoved(NBEdge* removed, int removedLane, NBEdge* by, int byLane) {
142  for (NBConnectionVector::iterator it = myControlledLinks.begin(); it != myControlledLinks.end(); ++it) {
143  (*it).replaceFrom(removed, removedLane, by, byLane);
144  (*it).replaceTo(removed, removedLane, by, byLane);
145  }
146 }
147 
148 
149 void
150 NBLoadedSUMOTLDef::addPhase(SUMOTime duration, const std::string& state) {
151  myTLLogic->addStep(duration, state);
152 }
153 
154 
155 bool
157  if (myControlledLinks.size() == 0) {
158  return true;
159  }
160  // make sure that myControlledNodes are the original nodes
161  if (myControlledNodes.size() != myOriginalNodes.size()) {
162  return true;
163  }
164  for (std::vector<NBNode*>::const_iterator i = myControlledNodes.begin(); i != myControlledNodes.end(); i++) {
165  if (myOriginalNodes.count(*i) != 1) {
166  return true;
167  }
168  }
169  return false;
170 }
171 
172 
173 void
174 NBLoadedSUMOTLDef::removeConnection(const NBConnection& conn, bool reconstruct) {
175  NBConnectionVector::iterator it = myControlledLinks.begin();
176  // find the connection but ignore its TLIndex since it might have been
177  // invalidated by an earlier removal
178  for (; it != myControlledLinks.end(); ++it) {
179  if (it->getFrom() == conn.getFrom() &&
180  it->getTo() == conn.getTo() &&
181  it->getFromLane() == conn.getFromLane() &&
182  it->getToLane() == conn.getToLane()) {
183  break;
184  }
185  }
186  if (it == myControlledLinks.end()) {
187  // a traffic light doesn't always controll all connections at a junction
188  // especially when using the option --tls.join
189  return;
190  }
191  const int removed = it->getTLIndex();
192  // remove the connection
193  myControlledLinks.erase(it);
194  if (reconstruct) {
195  // updating the edge is only needed for immediate use in NETEDIT.
196  // It may conflict with loading diffs
197  conn.getFrom()->setControllingTLInformation(conn, "");
198  // shift link numbers down so there is no gap
199  for (NBConnectionVector::iterator it = myControlledLinks.begin(); it != myControlledLinks.end(); it++) {
200  NBConnection& c = *it;
201  if (c.getTLIndex() > removed) {
202  c.setTLIndex(c.getTLIndex() - 1);
203  }
204  }
205  // update controlling information with new link numbers
207  // rebuild the logic
208  const std::vector<NBTrafficLightLogic::PhaseDefinition> phases = myTLLogic->getPhases();
210  for (std::vector<NBTrafficLightLogic::PhaseDefinition>::const_iterator it = phases.begin(); it != phases.end(); it++) {
211  std::string newState = it->state;
212  newState.erase(newState.begin() + removed);
213  newLogic->addStep(it->duration, newState);
214  }
215  delete myTLLogic;
216  myTLLogic = newLogic;
217  }
218 }
219 
220 
221 void
223  myOffset = offset;
224  myTLLogic->setOffset(offset);
225 }
226 
227 
228 void
230  myType = type;
231  myTLLogic->setType(type);
232 }
233 
234 
235 void
237  if (myControlledLinks.size() == 0) {
239  }
240  myIncomingEdges.clear();
241  EdgeVector myOutgoing;
242  // collect the edges from the participating nodes
243  for (std::vector<NBNode*>::iterator i = myControlledNodes.begin(); i != myControlledNodes.end(); i++) {
244  const EdgeVector& incoming = (*i)->getIncomingEdges();
245  copy(incoming.begin(), incoming.end(), back_inserter(myIncomingEdges));
246  const EdgeVector& outgoing = (*i)->getOutgoingEdges();
247  copy(outgoing.begin(), outgoing.end(), back_inserter(myOutgoing));
248  }
249  // check which of the edges are completely within the junction
250  // and which are uncontrolled as well (we already know myControlledLinks)
251  for (EdgeVector::iterator j = myIncomingEdges.begin(); j != myIncomingEdges.end();) {
252  NBEdge* edge = *j;
253  // an edge lies within the logic if it is outgoing as well as incoming
254  EdgeVector::iterator k = find(myOutgoing.begin(), myOutgoing.end(), edge);
255  if (k != myOutgoing.end()) {
256  if (myControlledInnerEdges.count(edge->getID()) == 0) {
257  bool controlled = false;
258  for (NBConnectionVector::iterator it = myControlledLinks.begin(); it != myControlledLinks.end(); it++) {
259  if ((*it).getFrom() == edge) {
260  controlled = true;
261  break;
262  }
263  }
264  if (controlled) {
265  myControlledInnerEdges.insert(edge->getID());
266  } else {
267  myEdgesWithin.push_back(edge);
268  (*j)->setIsInnerEdge();
269  j = myIncomingEdges.erase(j);
270  continue;
271  }
272  }
273  }
274  ++j;
275  }
276 }
277 
278 
279 void
281  if (myControlledLinks.size() == 0) {
282  // maybe we only loaded a different program for a default traffic light.
283  // Try to build links now.
284  myOriginalNodes.insert(myControlledNodes.begin(), myControlledNodes.end());
285  collectAllLinks();
286  }
287 }
288 
289 
291 void
293  // avoid shifting twice if the edge is incoming and outgoing to a joined TLS
294  if (myShifted.count(edge) == 0) {
296  myShifted.insert(edge);
297  for (NBConnectionVector::iterator it = myControlledLinks.begin(); it != myControlledLinks.end(); it++) {
298  (*it).shiftLaneIndex(edge, offset);
299  }
300  }
301 }
302 
303 void
305  // XXX what to do if crossings are removed during network building?
306  const unsigned int size = myTLLogic->getNumLinks();
307  unsigned int noLinksAll = 0;
308  for (NBConnectionVector::const_iterator it = myControlledLinks.begin(); it != myControlledLinks.end(); it++) {
309  const NBConnection& c = *it;
311  noLinksAll = MAX2(noLinksAll, (unsigned int)c.getTLIndex() + 1);
312  }
313  }
314  int oldCrossings = 0;
315  // collect crossings
316  std::vector<NBNode::Crossing> crossings;
317  for (std::vector<NBNode*>::iterator i = myControlledNodes.begin(); i != myControlledNodes.end(); i++) {
318  const std::vector<NBNode::Crossing>& c = (*i)->getCrossings();
319  // set tl indices for crossings
320  (*i)->setCrossingTLIndices(noLinksAll);
321  copy(c.begin(), c.end(), std::back_inserter(crossings));
322  noLinksAll += (unsigned int)c.size();
323  oldCrossings += (*i)->numCrossingsFromSumoNet();
324  }
325  const int newCrossings = (int)crossings.size() - oldCrossings;
326  if (newCrossings > 0) {
327  // collect edges
328  assert(size > 0);
329  EdgeVector fromEdges(size, 0);
330  EdgeVector toEdges(size, 0);
331  for (NBConnectionVector::const_iterator it = myControlledLinks.begin(); it != myControlledLinks.end(); it++) {
332  const NBConnection& c = *it;
334  assert(c.getTLIndex() < (int)size);
335  fromEdges[c.getTLIndex()] = c.getFrom();
336  toEdges[c.getTLIndex()] = c.getTo();
337  }
338  }
339  const std::string crossingDefaultState(newCrossings, 'r');
340 
341  // rebuild the logic (see NBOwnTLDef.cpp::myCompute)
342  const std::vector<NBTrafficLightLogic::PhaseDefinition> phases = myTLLogic->getPhases();
344  //std::cout << "patchIfCrossingsAdded for " << getID() << " numPhases=" << phases.size() << "\n";
345  for (std::vector<NBTrafficLightLogic::PhaseDefinition>::const_iterator it = phases.begin(); it != phases.end(); it++) {
346  NBOwnTLDef::addPedestrianPhases(newLogic, it->duration, it->state + crossingDefaultState, crossings, fromEdges, toEdges);
347  }
348  delete myTLLogic;
349  myTLLogic = newLogic;
350  }
351 }
352 
353 
354 /****************************************************************************/
355 
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:1924
TrafficLightType getType() const
get the algorithm type (static etc..)
TrafficLightType myType
The algorithm type for the traffic light.
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
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.
void closeBuilding()
closes the building process
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.
const std::string & getProgramID() const
Returns the ProgramID.
The representation of a single edge during network building.
Definition: NBEdge.h:71
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:74
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
void replaceRemoved(NBEdge *removed, int removedLane, NBEdge *by, int byLane)
Replaces a removed edge/lane.
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:60
virtual void collectEdges()
Build the list of participating edges.
static const int InvalidTlIndex
Definition: NBConnection.h:124
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.
Storage for edges, including some functionality operating on multiple edges.
Definition: NBEdgeCont.h:66
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:405
std::string toString(const T &t, std::streamsize accuracy=OUTPUT_ACCURACY)
Definition: ToString.h:53
void remapRemoved(NBEdge *removed, const EdgeVector &incoming, const EdgeVector &outgoing)
Replaces occurences of the removed edge in incoming/outgoing edges of all definitions.
SUMOTime getOffset() const
Returns the offset of first switch.
#define WRITE_ERROR(msg)
Definition: MsgHandler.h:205
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:367
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
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.
void setType(TrafficLightType type)
Sets the algorithm type of this tls.
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.
NBTrafficLightLogic * myCompute(const NBEdgeCont &ec, unsigned int brakingTimeSeconds)
Computes the traffic light logic finally in dependence to the type.
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
NBNode * getFromNode() const
Returns the origin node of the edge.
Definition: NBEdge.h:359