SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
NBLoadedTLDef.cpp
Go to the documentation of this file.
1 /****************************************************************************/
10 // A loaded (complete) traffic light logic
11 /****************************************************************************/
12 // SUMO, Simulation of Urban MObility; see http://sumo.sourceforge.net/
13 // Copyright (C) 2001-2013 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"
42 #include "NBLoadedTLDef.h"
43 #include "NBNode.h"
44 
45 #ifdef CHECK_MEMORY_LEAKS
46 #include <foreign/nvwa/debug_new.h>
47 #endif // CHECK_MEMORY_LEAKS
48 
49 
50 // ===========================================================================
51 // method definitions
52 // ===========================================================================
53 /* -------------------------------------------------------------------------
54  * NBLoadedTLDef::SignalGroup-methods
55  * ----------------------------------------------------------------------- */
57  : Named(id) {}
58 
60 
61 void
63  assert(c.getFromLane() < 0 || c.getFrom()->getNumLanes() > (unsigned int)c.getFromLane());
64  myConnections.push_back(c);
65 }
66 
67 
68 void
70  myPhases.push_back(PhaseDef(time, color));
71 }
72 
73 
74 void
76  myTRedYellow = tRedYellow;
77  myTYellow = tYellow;
78 }
79 
80 
81 void
83  sort(myPhases.begin(), myPhases.end(), phase_by_time_sorter());
84 }
85 
86 
87 void
89  if (myTYellow < 0) {
90  // was not set before (was not loaded)
91  myTYellow = tyellow;
92  } else if (forced && myTYellow < tyellow) {
93  WRITE_WARNING("TYellow of signal group '" + getID() + "' was less than the computed one; patched (was:" + toString<SUMOTime>(myTYellow) + ", is:" + toString<int>(tyellow) + ")");
94  myTYellow = tyellow;
95  }
96 }
97 
98 
99 std::vector<SUMOReal>
101  // within the phase container, we should have the green and red phases add their times
102  std::vector<SUMOReal> ret; // !!! time vector
103  for (std::vector<PhaseDef>::const_iterator i = myPhases.begin(); i != myPhases.end(); i++) {
104  ret.push_back((SUMOReal)(*i).myTime);
105  }
106  // further, we possibly should set the yellow phases
107  if (myTYellow > 0) {
108  for (std::vector<PhaseDef>::const_iterator i = myPhases.begin(); i != myPhases.end(); i++) {
109  if ((*i).myColor == TLCOLOR_RED) {
110  SUMOTime time = (SUMOTime)(*i).myTime + myTYellow;
111  if (time > cycleDuration) {
112  time = time - cycleDuration;
113  }
114  ret.push_back((SUMOReal) time);
115  }
116  }
117  }
118  return ret;
119 }
120 
121 
122 unsigned int
124  return (unsigned int) myConnections.size();
125 }
126 
127 
128 bool
130  assert(myPhases.size() != 0);
131  for (std::vector<PhaseDef>::const_reverse_iterator i = myPhases.rbegin(); i != myPhases.rend(); i++) {
132  SUMOTime nextTime = (*i).myTime;
133  if (time >= nextTime) {
134  return (*i).myColor == TLCOLOR_GREEN;
135  }
136  }
137  return (*(myPhases.end() - 1)).myColor == TLCOLOR_GREEN;
138 }
139 
140 
141 bool
143  bool has_red_now = !mayDrive(time);
144  bool had_green = mayDrive(time - myTYellow);
145  return has_red_now && had_green;
146 }
147 
148 
149 bool
151  for (NBConnectionVector::const_iterator i = myConnections.begin(); i != myConnections.end(); i++) {
152  if ((*i).getFrom() == from && (*i).getTo() == to) {
153  return true;
154  }
155  }
156  return false;
157 
158 }
159 
160 
161 const NBConnection&
163  assert(pos < myConnections.size());
164  return myConnections[pos];
165 }
166 
167 
168 bool
170  for (NBConnectionVector::const_iterator i = myConnections.begin(); i != myConnections.end(); i++) {
171  if ((*i).getFrom() == from) {
172  return true;
173  }
174  }
175  return false;
176 }
177 
178 
179 void
181  NBConnectionVector newConns;
182  for (NBConnectionVector::iterator i = myConnections.begin(); i != myConnections.end();) {
183  if ((*i).getFrom() == which) {
184  NBConnection conn((*i).getFrom(), (*i).getTo());
185  i = myConnections.erase(i);
186  for (EdgeVector::const_iterator j = by.begin(); j != by.end(); j++) {
187  NBConnection curr(conn);
188  if (!curr.replaceFrom(which, *j)) {
189  throw ProcessError("Could not replace edge '" + which->getID() + "' by '" + (*j)->getID() + "'.\nUndefined...");
190  }
191  newConns.push_back(curr);
192  }
193  } else {
194  i++;
195  }
196  }
197  copy(newConns.begin(), newConns.end(),
198  back_inserter(myConnections));
199 }
200 
201 
202 bool
204  for (NBConnectionVector::const_iterator i = myConnections.begin(); i != myConnections.end(); i++) {
205  if ((*i).getTo() == to) {
206  return true;
207  }
208  }
209  return false;
210 }
211 
212 
213 void
215  NBConnectionVector newConns;
216  for (NBConnectionVector::iterator i = myConnections.begin(); i != myConnections.end();) {
217  if ((*i).getTo() == which) {
218  NBConnection conn((*i).getFrom(), (*i).getTo());
219  i = myConnections.erase(i);
220  for (EdgeVector::const_iterator j = by.begin(); j != by.end(); j++) {
221  NBConnection curr(conn);
222  if (!curr.replaceTo(which, *j)) {
223  throw ProcessError("Could not replace edge '" + which->getID() + "' by '" + (*j)->getID() + "'.\nUndefined...");
224  }
225  newConns.push_back(curr);
226  }
227  } else {
228  i++;
229  }
230  }
231  copy(newConns.begin(), newConns.end(),
232  back_inserter(myConnections));
233 }
234 
235 
236 void
237 NBLoadedTLDef::SignalGroup::remap(NBEdge* removed, int removedLane,
238  NBEdge* by, int byLane) {
239  for (NBConnectionVector::iterator i = myConnections.begin(); i != myConnections.end(); i++) {
240  if ((*i).getTo() == removed
241  &&
242  ((*i).getToLane() == removedLane
243  ||
244  (*i).getToLane() == -1)) {
245  (*i).replaceTo(removed, removedLane, by, byLane);
246 
247  } else if ((*i).getTo() == removed && removedLane == -1) {
248  (*i).replaceTo(removed, by);
249  }
250 
251  if ((*i).getFrom() == removed
252  &&
253  ((*i).getFromLane() == removedLane
254  ||
255  (*i).getFromLane() == -1)) {
256  (*i).replaceFrom(removed, removedLane, by, byLane);
257 
258  } else if ((*i).getFrom() == removed && removedLane == -1) {
259  (*i).replaceFrom(removed, by);
260  }
261  }
262 }
263 
264 
265 /* -------------------------------------------------------------------------
266  * NBLoadedTLDef::Phase-methods
267  * ----------------------------------------------------------------------- */
268 NBLoadedTLDef::NBLoadedTLDef(const std::string& id,
269  const std::vector<NBNode*>& junctions, SUMOTime offset, TrafficLightType type) :
270  NBTrafficLightDefinition(id, junctions, DefaultProgramID, offset, type)
271 {}
272 
273 
274 NBLoadedTLDef::NBLoadedTLDef(const std::string& id, NBNode* junction, SUMOTime offset, TrafficLightType type) :
275  NBTrafficLightDefinition(id, junction, DefaultProgramID, offset, type)
276 {}
277 
278 
279 NBLoadedTLDef::NBLoadedTLDef(const std::string& id, SUMOTime offset, TrafficLightType type) :
280  NBTrafficLightDefinition(id, DefaultProgramID, offset, type)
281 {}
282 
283 
285  for (SignalGroupCont::iterator i = mySignalGroups.begin(); i != mySignalGroups.end(); ++i) {
286  delete(*i).second;
287  }
288 }
289 
290 
292 NBLoadedTLDef::myCompute(const NBEdgeCont& ec, unsigned int brakingTime) {
294  NBLoadedTLDef::SignalGroupCont::const_iterator i;
295  // compute the switching times
296  std::set<SUMOReal> tmpSwitchTimes;
297  for (i = mySignalGroups.begin(); i != mySignalGroups.end(); i++) {
298  NBLoadedTLDef::SignalGroup* group = (*i).second;
299  // needed later
300  group->sortPhases();
301  // patch the yellow time for this group
302  group->patchTYellow(brakingTime, OptionsCont::getOptions().getBool("tls.yellow.patch-small"));
303  // copy the now valid times into the container
304  // both the given red and green phases are added and also the
305  // yellow times
306  std::vector<SUMOReal> gtimes = group->getTimes(myCycleDuration);
307  for (std::vector<SUMOReal>::const_iterator k = gtimes.begin(); k != gtimes.end(); k++) {
308  tmpSwitchTimes.insert(*k);
309  }
310  }
311  std::vector<SUMOReal> switchTimes;
312  copy(tmpSwitchTimes.begin(), tmpSwitchTimes.end(), back_inserter(switchTimes));
313  sort(switchTimes.begin(), switchTimes.end());
314 
315  // count the signals
316  unsigned int noSignals = 0;
317  for (i = mySignalGroups.begin(); i != mySignalGroups.end(); i++) {
318  noSignals += (*i).second->getLinkNo();
319  }
320  // build the phases
322  for (std::vector<SUMOReal>::iterator l = switchTimes.begin(); l != switchTimes.end(); l++) {
323  // compute the duration of the current phase
324  unsigned int duration;
325  if (l != switchTimes.end() - 1) {
326  // get from the difference to the next switching time
327  duration = (unsigned int)((*(l + 1)) - (*l));
328  } else {
329  // get from the differenc to the first switching time
330  duration = (unsigned int)(myCycleDuration - (*l) + * (switchTimes.begin()));
331  }
332  // no information about yellow times will be generated
333  assert((*l) >= 0);
334  logic->addStep(TIME2STEPS(duration), buildPhaseState(ec, (unsigned int)(*l)));
335  }
336  // check whether any warnings were printed
337  if (MsgHandler::getWarningInstance()->wasInformed()) {
338  WRITE_WARNING("During computation of traffic light '" + getID() + "'.");
339  }
340  logic->closeBuilding();
341  return logic;
342 }
343 
344 
345 void
347  // assign the links to the connections
348  unsigned int pos = 0;
349  for (SignalGroupCont::const_iterator m = mySignalGroups.begin(); m != mySignalGroups.end(); m++) {
350  SignalGroup* group = (*m).second;
351  unsigned int linkNo = group->getLinkNo();
352  for (unsigned int j = 0; j < linkNo; j++) {
353  const NBConnection& conn = group->getConnection(j);
354  assert(conn.getFromLane() < 0 || (int) conn.getFrom()->getNumLanes() > conn.getFromLane());
355  NBConnection tst(conn);
356  tst.setTLIndex(pos);
357  if (tst.check(ec)) {
358  NBEdge* edge = conn.getFrom();
359  if (edge->setControllingTLInformation(tst, getID())) {
360  pos++;
361  }
362  } else {
363  WRITE_WARNING("Could not set signal on connection (signal: " + getID() + ", group: " + group->getID() + ")");
364  }
365  }
366  }
367 }
368 
369 
370 std::string
371 NBLoadedTLDef::buildPhaseState(const NBEdgeCont& ec, unsigned int time) const {
372  unsigned int pos = 0;
373  std::string state;
374  // set the green and yellow information first;
375  // the information whether other have to break needs those masks
376  // completely filled
377  for (SignalGroupCont::const_iterator i = mySignalGroups.begin(); i != mySignalGroups.end(); i++) {
378  SignalGroup* group = (*i).second;
379  unsigned int linkNo = group->getLinkNo();
380  bool mayDrive = group->mayDrive(time);
381  bool hasYellow = group->hasYellow(time);
382  char c = 'r';
383  if (mayDrive) {
384  c = 'g';
385  }
386  if (hasYellow) {
387  c = 'y';
388  }
389  for (unsigned int j = 0; j < linkNo; j++) {
390  const NBConnection& conn = group->getConnection(j);
391  NBConnection assConn(conn);
392  // assert that the connection really exists
393  if (assConn.check(ec)) {
394  state = state + c;
395  ++pos;
396  }
397  }
398  }
399  // set the braking mask
400  pos = 0;
401  for (SignalGroupCont::const_iterator i = mySignalGroups.begin(); i != mySignalGroups.end(); i++) {
402  SignalGroup* group = (*i).second;
403  unsigned int linkNo = group->getLinkNo();
404  for (unsigned int j = 0; j < linkNo; j++) {
405  const NBConnection& conn = group->getConnection(j);
406  NBConnection assConn(conn);
407  if (assConn.check(ec)) {
408  if (!mustBrake(ec, assConn, state, pos)) {
409  if (state[pos] == 'g') {
410  state[pos] = 'G';
411  }
412  if (state[pos] == 'y') {
413  state[pos] = 'Y';
414  }
415  }
416  pos++;
417  }
418  }
419  }
420  return state;
421 }
422 
423 
424 bool
426  const NBConnection& possProhibited,
427  const std::string& state,
428  unsigned int strmpos) const {
429  // check whether the stream has red
430  if (state[strmpos] != 'g' && state[strmpos] != 'G') {
431  return true;
432  }
433 
434  // check whether another stream which has green is a higher
435  // priorised foe to the given
436  unsigned int pos = 0;
437  for (SignalGroupCont::const_iterator i = mySignalGroups.begin(); i != mySignalGroups.end(); i++) {
438  SignalGroup* group = (*i).second;
439  // get otherlinks that have green
440  unsigned int linkNo = group->getLinkNo();
441  for (unsigned int j = 0; j < linkNo; j++) {
442  // get the current connection (possible foe)
443  const NBConnection& other = group->getConnection(j);
444  NBConnection possProhibitor(other);
445  // if the connction ist still valid ...
446  if (possProhibitor.check(ec)) {
447  // ... do nothing if it starts at the same edge
448  if (possProhibited.getFrom() == possProhibitor.getFrom()) {
449  pos++;
450  continue;
451  }
452  if (state[pos] == 'g' || state[pos] == 'G') {
453  if (NBTrafficLightDefinition::mustBrake(possProhibited, possProhibitor, true)) {
454  return true;
455  }
456  }
457  pos++;
458  }
459  }
460  }
461  return false;
462 }
463 
464 
465 void
467  myControlledNodes.clear();
468  SignalGroupCont::const_iterator m;
469  for (m = mySignalGroups.begin(); m != mySignalGroups.end(); m++) {
470  SignalGroup* group = (*m).second;
471  unsigned int linkNo = group->getLinkNo();
472  for (unsigned int j = 0; j < linkNo; j++) {
473  const NBConnection& conn = group->getConnection(j);
474  NBEdge* edge = conn.getFrom();
475  NBNode* node = edge->getToNode();
476  myControlledNodes.push_back(node);
477  }
478  }
480 }
481 
482 
483 void
485  myControlledLinks.clear();
486  // build the list of links which are controled by the traffic light
487  for (EdgeVector::iterator i = myIncomingEdges.begin(); i != myIncomingEdges.end(); i++) {
488  NBEdge* incoming = *i;
489  unsigned int noLanes = incoming->getNumLanes();
490  for (unsigned int j = 0; j < noLanes; j++) {
491  std::vector<NBEdge::Connection> elv = incoming->getConnectionsFromLane(j);
492  for (std::vector<NBEdge::Connection>::iterator k = elv.begin(); k != elv.end(); k++) {
493  NBEdge::Connection el = *k;
494  if (el.toEdge != 0) {
495  myControlledLinks.push_back(NBConnection(incoming, j, el.toEdge, el.toLane));
496  }
497  }
498  }
499  }
500 }
501 
502 
505  for (SignalGroupCont::const_iterator i = mySignalGroups.begin(); i != mySignalGroups.end(); i++) {
506  if ((*i).second->containsConnection(from, to)) {
507  return (*i).second;
508  }
509  }
510  return 0;
511 }
512 
513 
514 bool
515 NBLoadedTLDef::addToSignalGroup(const std::string& groupid,
516  const NBConnection& connection) {
517  if (mySignalGroups.find(groupid) == mySignalGroups.end()) {
518  return false;
519  }
520  mySignalGroups[groupid]->addConnection(connection);
521  NBNode* n1 = connection.getFrom()->getToNode();
522  if (n1 != 0) {
523  addNode(n1);
524  n1->addTrafficLight(this);
525  }
526  NBNode* n2 = connection.getTo()->getFromNode();
527  if (n2 != 0) {
528  addNode(n2);
529  n2->addTrafficLight(this);
530  }
531  return true;
532 }
533 
534 
535 bool
536 NBLoadedTLDef::addToSignalGroup(const std::string& groupid,
537  const NBConnectionVector& connections) {
538  bool ok = true;
539  for (NBConnectionVector::const_iterator i = connections.begin(); i != connections.end(); i++) {
540  ok &= addToSignalGroup(groupid, *i);
541  }
542  return ok;
543 }
544 
545 
546 void
547 NBLoadedTLDef::addSignalGroup(const std::string& id) {
548  assert(mySignalGroups.find(id) == mySignalGroups.end());
549  mySignalGroups[id] = new SignalGroup(id);
550 }
551 
552 
553 void
554 NBLoadedTLDef::addSignalGroupPhaseBegin(const std::string& groupid, SUMOTime time,
555  TLColor color) {
556  assert(mySignalGroups.find(groupid) != mySignalGroups.end());
557  mySignalGroups[groupid]->addPhaseBegin(time, color);
558 }
559 
560 void
561 NBLoadedTLDef::setSignalYellowTimes(const std::string& groupid,
562  SUMOTime myTRedYellow, SUMOTime myTYellow) {
563  assert(mySignalGroups.find(groupid) != mySignalGroups.end());
564  mySignalGroups[groupid]->setYellowTimes(myTRedYellow, myTYellow);
565 }
566 
567 
568 void
569 NBLoadedTLDef::setCycleDuration(unsigned int cycleDur) {
570  myCycleDuration = cycleDur;
571 }
572 
573 
574 void
576  const EdgeVector& incoming,
577  const EdgeVector& outgoing) {
578  for (SignalGroupCont::const_iterator i = mySignalGroups.begin(); i != mySignalGroups.end(); i++) {
579  SignalGroup* group = (*i).second;
580  if (group->containsIncoming(removed)) {
581  group->remapIncoming(removed, incoming);
582  }
583  if (group->containsOutgoing(removed)) {
584  group->remapOutgoing(removed, outgoing);
585  }
586  }
587 }
588 
589 
590 void
591 NBLoadedTLDef::replaceRemoved(NBEdge* removed, int removedLane,
592  NBEdge* by, int byLane) {
593  for (SignalGroupCont::const_iterator i = mySignalGroups.begin(); i != mySignalGroups.end(); i++) {
594  SignalGroup* group = (*i).second;
595  if (group->containsIncoming(removed) || group->containsOutgoing(removed)) {
596  group->remap(removed, removedLane, by, byLane);
597  }
598  }
599 }
600 
601 
602 
603 /****************************************************************************/
604