SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules 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 brakingTime) {
80  // @todo what to do with those parameters?
81  UNUSED_PARAMETER(ec);
82  UNUSED_PARAMETER(brakingTime);
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
142 
143 
144 void
145 NBLoadedSUMOTLDef::addPhase(SUMOTime duration, const std::string& state) {
146  myTLLogic->addStep(duration, state);
147 }
148 
149 
150 bool
152  if (myControlledLinks.size() == 0) {
153  return true;
154  }
155  // make sure that myControlledNodes are the original nodes
156  if (myControlledNodes.size() != myOriginalNodes.size()) {
157  return true;
158  }
159  for (std::vector<NBNode*>::const_iterator i = myControlledNodes.begin(); i != myControlledNodes.end(); i++) {
160  if (myOriginalNodes.count(*i) != 1) {
161  return true;
162  }
163  }
164  return false;
165 }
166 
167 
168 void
169 NBLoadedSUMOTLDef::removeConnection(const NBConnection& conn, bool reconstruct) {
170  NBConnectionVector::iterator it = myControlledLinks.begin();
171  // find the connection but ignore its TLIndex since it might have been
172  // invalidated by an earlier removal
173  for (; it != myControlledLinks.end(); ++it) {
174  if (it->getFrom() == conn.getFrom() &&
175  it->getTo() == conn.getTo() &&
176  it->getFromLane() == conn.getFromLane() &&
177  it->getToLane() == conn.getToLane()) {
178  break;
179  }
180  }
181  if (it == myControlledLinks.end()) {
182  // a traffic light doesn't always controll all connections at a junction
183  // especially when using the option --tls.join
184  return;
185  }
186  const int removed = it->getTLIndex();
187  // remove the connection
188  myControlledLinks.erase(it);
189  if (reconstruct) {
190  // updating the edge is only needed for immediate use in NETEDIT.
191  // It may conflict with loading diffs
192  conn.getFrom()->setControllingTLInformation(conn, "");
193  // shift link numbers down so there is no gap
194  for (NBConnectionVector::iterator it = myControlledLinks.begin(); it != myControlledLinks.end(); it++) {
195  NBConnection& c = *it;
196  if (c.getTLIndex() > removed) {
197  c.setTLIndex(c.getTLIndex() - 1);
198  }
199  }
200  // update controlling information with new link numbers
202  // rebuild the logic
203  const std::vector<NBTrafficLightLogic::PhaseDefinition> phases = myTLLogic->getPhases();
205  for (std::vector<NBTrafficLightLogic::PhaseDefinition>::const_iterator it = phases.begin(); it != phases.end(); it++) {
206  std::string newState = it->state;
207  newState.erase(newState.begin() + removed);
208  newLogic->addStep(it->duration, newState);
209  }
210  delete myTLLogic;
211  myTLLogic = newLogic;
212  }
213 }
214 
215 
216 void
218  myOffset = offset;
219  myTLLogic->setOffset(offset);
220 }
221 
222 
223 void
225  myType = type;
226  myTLLogic->setType(type);
227 }
228 
229 
230 void
232  if (myControlledLinks.size() == 0) {
234  }
235  myIncomingEdges.clear();
236  EdgeVector myOutgoing;
237  // collect the edges from the participating nodes
238  for (std::vector<NBNode*>::iterator i = myControlledNodes.begin(); i != myControlledNodes.end(); i++) {
239  const EdgeVector& incoming = (*i)->getIncomingEdges();
240  copy(incoming.begin(), incoming.end(), back_inserter(myIncomingEdges));
241  const EdgeVector& outgoing = (*i)->getOutgoingEdges();
242  copy(outgoing.begin(), outgoing.end(), back_inserter(myOutgoing));
243  }
244  // check which of the edges are completely within the junction
245  // and which are uncontrolled as well (we already know myControlledLinks)
246  for (EdgeVector::iterator j = myIncomingEdges.begin(); j != myIncomingEdges.end();) {
247  NBEdge* edge = *j;
248  // an edge lies within the logic if it is outgoing as well as incoming
249  EdgeVector::iterator k = find(myOutgoing.begin(), myOutgoing.end(), edge);
250  if (k != myOutgoing.end()) {
251  if (myControlledInnerEdges.count(edge->getID()) == 0) {
252  bool controlled = false;
253  for (NBConnectionVector::iterator it = myControlledLinks.begin(); it != myControlledLinks.end(); it++) {
254  if ((*it).getFrom() == edge) {
255  controlled = true;
256  break;
257  }
258  }
259  if (controlled) {
260  myControlledInnerEdges.insert(edge->getID());
261  } else {
262  myEdgesWithin.push_back(edge);
263  (*j)->setIsInnerEdge();
264  j = myIncomingEdges.erase(j);
265  continue;
266  }
267  }
268  }
269  ++j;
270  }
271 }
272 
273 
274 void
276  if (myControlledLinks.size() == 0) {
277  // maybe we only loaded a different program for a default traffic light.
278  // Try to build links now.
279  myOriginalNodes.insert(myControlledNodes.begin(), myControlledNodes.end());
280  collectAllLinks();
281  }
282 }
283 
284 
286 void
288  // avoid shifting twice if the edge is incoming and outgoing to a joined TLS
289  if (myShifted.count(edge) == 0) {
291  myShifted.insert(edge);
292  for (NBConnectionVector::iterator it = myControlledLinks.begin(); it != myControlledLinks.end(); it++) {
293  (*it).shiftLaneIndex(edge, offset);
294  }
295  }
296 }
297 
298 void
300  // XXX what to do if crossings are removed during network building?
301  const unsigned int size = myTLLogic->getNumLinks();
302  unsigned int noLinksAll = size;
303  // collect crossings
304  std::vector<NBNode::Crossing> crossings;
305  for (std::vector<NBNode*>::iterator i = myControlledNodes.begin(); i != myControlledNodes.end(); i++) {
306  const std::vector<NBNode::Crossing>& c = (*i)->getCrossings();
307  // set tl indices for crossings
308  (*i)->setCrossingTLIndices(noLinksAll);
309  copy(c.begin(), c.end(), std::back_inserter(crossings));
310  noLinksAll += (unsigned int)c.size();
311  }
312  if (crossings.size() > 0) {
313  // collect edges
314  assert(size > 0);
315  EdgeVector fromEdges(size, 0);
316  EdgeVector toEdges(size, 0);
317  for (NBConnectionVector::const_iterator it = myControlledLinks.begin(); it != myControlledLinks.end(); it++) {
318  const NBConnection& c = *it;
320  assert(c.getTLIndex() < (int)size);
321  fromEdges[c.getTLIndex()] = c.getFrom();
322  toEdges[c.getTLIndex()] = c.getTo();
323  }
324  }
326  const std::string crossingDefaultState(crossings.size(), 'r');
327 
328  // rebuild the logic (see NBOwnTLDef.cpp::myCompute)
329  const std::vector<NBTrafficLightLogic::PhaseDefinition> phases = myTLLogic->getPhases();
331  //std::cout << "patchIfCrossingsAdded for " << getID() << " numPhases=" << phases.size() << "\n";
332  for (std::vector<NBTrafficLightLogic::PhaseDefinition>::const_iterator it = phases.begin(); it != phases.end(); it++) {
333  NBOwnTLDef::addPedestrianPhases(newLogic, it->duration, it->state + crossingDefaultState, crossings, fromEdges, toEdges);
334  }
335  delete myTLLogic;
336  myTLLogic = newLogic;
337  }
338 }
339 
340 
341 /****************************************************************************/
342 
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:1822
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.
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:123
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:406
NBTrafficLightLogic * myCompute(const NBEdgeCont &ec, unsigned int brakingTime)
Computes the traffic light logic finally in dependence to the type.
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:371
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) ...
int SUMOTime
Definition: SUMOTime.h:43
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.
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:363