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-2012 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)
270  : NBTrafficLightDefinition(id, junctions, DefaultProgramID, offset)
271 {}
272 
273 
274 NBLoadedTLDef::NBLoadedTLDef(const std::string& id, NBNode* junction, SUMOTime offset)
275  : NBTrafficLightDefinition(id, junction, DefaultProgramID, offset)
276 {}
277 
278 
279 NBLoadedTLDef::NBLoadedTLDef(const std::string& id, SUMOTime offset)
280  : NBTrafficLightDefinition(id, DefaultProgramID, offset)
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
321  NBTrafficLightLogic* logic = new NBTrafficLightLogic(getID(), getProgramID(), noSignals);
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->setOffset(myOffset);
341  logic->closeBuilding();
342  return logic;
343 }
344 
345 
346 void
348  // assign the links to the connections
349  unsigned int pos = 0;
350  for (SignalGroupCont::const_iterator m = mySignalGroups.begin(); m != mySignalGroups.end(); m++) {
351  SignalGroup* group = (*m).second;
352  unsigned int linkNo = group->getLinkNo();
353  for (unsigned int j = 0; j < linkNo; j++) {
354  const NBConnection& conn = group->getConnection(j);
355  assert(conn.getFromLane() < 0 || (int) conn.getFrom()->getNumLanes() > conn.getFromLane());
356  NBConnection tst(conn);
357  tst.setTLIndex(pos);
358  if (tst.check(ec)) {
359  NBEdge* edge = conn.getFrom();
360  if (edge->setControllingTLInformation(tst, getID())) {
361  pos++;
362  }
363  } else {
364  WRITE_WARNING("Could not set signal on connection (signal: " + getID() + ", group: " + group->getID() + ")");
365  }
366  }
367  }
368 }
369 
370 
371 std::string
372 NBLoadedTLDef::buildPhaseState(const NBEdgeCont& ec, unsigned int time) const {
373  unsigned int pos = 0;
374  std::string state;
375  // set the green and yellow information first;
376  // the information whether other have to break needs those masks
377  // completely filled
378  for (SignalGroupCont::const_iterator i = mySignalGroups.begin(); i != mySignalGroups.end(); i++) {
379  SignalGroup* group = (*i).second;
380  unsigned int linkNo = group->getLinkNo();
381  bool mayDrive = group->mayDrive(time);
382  bool hasYellow = group->hasYellow(time);
383  char c = 'r';
384  if (mayDrive) {
385  c = 'g';
386  }
387  if (hasYellow) {
388  c = 'y';
389  }
390  for (unsigned int j = 0; j < linkNo; j++) {
391  const NBConnection& conn = group->getConnection(j);
392  NBConnection assConn(conn);
393  // assert that the connection really exists
394  if (assConn.check(ec)) {
395  state = state + c;
396  ++pos;
397  }
398  }
399  }
400  // set the braking mask
401  pos = 0;
402  for (SignalGroupCont::const_iterator i = mySignalGroups.begin(); i != mySignalGroups.end(); i++) {
403  SignalGroup* group = (*i).second;
404  unsigned int linkNo = group->getLinkNo();
405  for (unsigned int j = 0; j < linkNo; j++) {
406  const NBConnection& conn = group->getConnection(j);
407  NBConnection assConn(conn);
408  if (assConn.check(ec)) {
409  if (!mustBrake(ec, assConn, state, pos)) {
410  if (state[pos] == 'g') {
411  state[pos] = 'G';
412  }
413  if (state[pos] == 'y') {
414  state[pos] = 'Y';
415  }
416  }
417  pos++;
418  }
419  }
420  }
421  return state;
422 }
423 
424 
425 bool
427  const NBConnection& possProhibited,
428  const std::string& state,
429  unsigned int strmpos) const {
430  // check whether the stream has red
431  if (state[strmpos] != 'g' && state[strmpos] != 'G') {
432  return true;
433  }
434 
435  // check whether another stream which has green is a higher
436  // priorised foe to the given
437  unsigned int pos = 0;
438  for (SignalGroupCont::const_iterator i = mySignalGroups.begin(); i != mySignalGroups.end(); i++) {
439  SignalGroup* group = (*i).second;
440  // get otherlinks that have green
441  unsigned int linkNo = group->getLinkNo();
442  for (unsigned int j = 0; j < linkNo; j++) {
443  // get the current connection (possible foe)
444  const NBConnection& other = group->getConnection(j);
445  NBConnection possProhibitor(other);
446  // if the connction ist still valid ...
447  if (possProhibitor.check(ec)) {
448  // ... do nothing if it starts at the same edge
449  if (possProhibited.getFrom() == possProhibitor.getFrom()) {
450  pos++;
451  continue;
452  }
453  if (state[pos] == 'g' || state[pos] == 'G') {
454  if (NBTrafficLightDefinition::mustBrake(possProhibited, possProhibitor, true)) {
455  return true;
456  }
457  }
458  pos++;
459  }
460  }
461  }
462  return false;
463 }
464 
465 
466 void
468  myControlledNodes.clear();
469  SignalGroupCont::const_iterator m;
470  for (m = mySignalGroups.begin(); m != mySignalGroups.end(); m++) {
471  SignalGroup* group = (*m).second;
472  unsigned int linkNo = group->getLinkNo();
473  for (unsigned int j = 0; j < linkNo; j++) {
474  const NBConnection& conn = group->getConnection(j);
475  NBEdge* edge = conn.getFrom();
476  NBNode* node = edge->getToNode();
477  myControlledNodes.push_back(node);
478  }
479  }
481 }
482 
483 
484 void
486  myControlledLinks.clear();
487  // build the list of links which are controled by the traffic light
488  for (EdgeVector::iterator i = myIncomingEdges.begin(); i != myIncomingEdges.end(); i++) {
489  NBEdge* incoming = *i;
490  unsigned int noLanes = incoming->getNumLanes();
491  for (unsigned int j = 0; j < noLanes; j++) {
492  std::vector<NBEdge::Connection> elv = incoming->getConnectionsFromLane(j);
493  for (std::vector<NBEdge::Connection>::iterator k = elv.begin(); k != elv.end(); k++) {
494  NBEdge::Connection el = *k;
495  if (el.toEdge != 0) {
496  myControlledLinks.push_back(NBConnection(incoming, j, el.toEdge, el.toLane));
497  }
498  }
499  }
500  }
501 }
502 
503 
506  for (SignalGroupCont::const_iterator i = mySignalGroups.begin(); i != mySignalGroups.end(); i++) {
507  if ((*i).second->containsConnection(from, to)) {
508  return (*i).second;
509  }
510  }
511  return 0;
512 }
513 
514 
515 bool
516 NBLoadedTLDef::addToSignalGroup(const std::string& groupid,
517  const NBConnection& connection) {
518  if (mySignalGroups.find(groupid) == mySignalGroups.end()) {
519  return false;
520  }
521  mySignalGroups[groupid]->addConnection(connection);
522  NBNode* n1 = connection.getFrom()->getToNode();
523  if (n1 != 0) {
524  addNode(n1);
525  n1->addTrafficLight(this);
526  }
527  NBNode* n2 = connection.getTo()->getFromNode();
528  if (n2 != 0) {
529  addNode(n2);
530  n2->addTrafficLight(this);
531  }
532  return true;
533 }
534 
535 
536 bool
537 NBLoadedTLDef::addToSignalGroup(const std::string& groupid,
538  const NBConnectionVector& connections) {
539  bool ok = true;
540  for (NBConnectionVector::const_iterator i = connections.begin(); i != connections.end(); i++) {
541  ok &= addToSignalGroup(groupid, *i);
542  }
543  return ok;
544 }
545 
546 
547 void
548 NBLoadedTLDef::addSignalGroup(const std::string& id) {
549  assert(mySignalGroups.find(id) == mySignalGroups.end());
550  mySignalGroups[id] = new SignalGroup(id);
551 }
552 
553 
554 void
555 NBLoadedTLDef::addSignalGroupPhaseBegin(const std::string& groupid, SUMOTime time,
556  TLColor color) {
557  assert(mySignalGroups.find(groupid) != mySignalGroups.end());
558  mySignalGroups[groupid]->addPhaseBegin(time, color);
559 }
560 
561 void
562 NBLoadedTLDef::setSignalYellowTimes(const std::string& groupid,
563  SUMOTime myTRedYellow, SUMOTime myTYellow) {
564  assert(mySignalGroups.find(groupid) != mySignalGroups.end());
565  mySignalGroups[groupid]->setYellowTimes(myTRedYellow, myTYellow);
566 }
567 
568 
569 void
570 NBLoadedTLDef::setCycleDuration(unsigned int cycleDur) {
571  myCycleDuration = cycleDur;
572 }
573 
574 
575 void
577  const EdgeVector& incoming,
578  const EdgeVector& outgoing) {
579  for (SignalGroupCont::const_iterator i = mySignalGroups.begin(); i != mySignalGroups.end(); i++) {
580  SignalGroup* group = (*i).second;
581  if (group->containsIncoming(removed)) {
582  group->remapIncoming(removed, incoming);
583  }
584  if (group->containsOutgoing(removed)) {
585  group->remapOutgoing(removed, outgoing);
586  }
587  }
588 }
589 
590 
591 void
592 NBLoadedTLDef::replaceRemoved(NBEdge* removed, int removedLane,
593  NBEdge* by, int byLane) {
594  for (SignalGroupCont::const_iterator i = mySignalGroups.begin(); i != mySignalGroups.end(); i++) {
595  SignalGroup* group = (*i).second;
596  if (group->containsIncoming(removed) || group->containsOutgoing(removed)) {
597  group->remap(removed, removedLane, by, byLane);
598  }
599  }
600 }
601 
602 
603 
604 /****************************************************************************/
605