SUMO - Simulation of Urban MObility
NBOwnTLDef.cpp
Go to the documentation of this file.
1 /****************************************************************************/
10 // A traffic light logics which must be computed (only nodes/edges are given)
11 /****************************************************************************/
12 // SUMO, Simulation of Urban MObility; see http://sumo.dlr.de/
13 // Copyright (C) 2001-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 // ===========================================================================
26 // included modules
27 // ===========================================================================
28 #ifdef _MSC_VER
29 #include <windows_config.h>
30 #else
31 #include <config.h>
32 #endif
33 
34 #include <vector>
35 #include <cassert>
36 #include <iterator>
38 #include "NBNode.h"
39 #include "NBOwnTLDef.h"
40 #include "NBTrafficLightLogic.h"
43 #include <utils/common/ToString.h>
45 #include <utils/options/Option.h>
46 
47 #ifdef CHECK_MEMORY_LEAKS
48 #include <foreign/nvwa/debug_new.h>
49 #endif // CHECK_MEMORY_LEAKS
50 
51 #define MIN_GREEN_TIME 5
52 
53 // ===========================================================================
54 // member method definitions
55 // ===========================================================================
56 NBOwnTLDef::NBOwnTLDef(const std::string& id,
57  const std::vector<NBNode*>& junctions, SUMOTime offset,
58  TrafficLightType type) :
59  NBTrafficLightDefinition(id, junctions, DefaultProgramID, offset, type),
60  myHaveSinglePhase(false) {
61 }
62 
63 
64 NBOwnTLDef::NBOwnTLDef(const std::string& id, NBNode* junction, SUMOTime offset,
65  TrafficLightType type) :
66  NBTrafficLightDefinition(id, junction, DefaultProgramID, offset, type),
67  myHaveSinglePhase(false) {
68 }
69 
70 
71 NBOwnTLDef::NBOwnTLDef(const std::string& id, SUMOTime offset,
72  TrafficLightType type) :
73  NBTrafficLightDefinition(id, DefaultProgramID, offset, type),
74  myHaveSinglePhase(false) {
75 }
76 
77 
79 
80 
81 int
82 NBOwnTLDef::getToPrio(const NBEdge* const e) {
83  return e->getJunctionPriority(e->getToNode());
84 }
85 
86 
89  switch (dir) {
90  case LINKDIR_STRAIGHT:
91  case LINKDIR_PARTLEFT:
92  case LINKDIR_PARTRIGHT:
93  return 2.;
94  case LINKDIR_LEFT:
95  case LINKDIR_RIGHT:
96  return .5;
97  default:
98  break;
99  }
100  return 0;
101 }
102 
103 SUMOReal
105  SUMOReal val = 0;
106  for (int e1l = 0; e1l < e1->getNumLanes(); e1l++) {
107  std::vector<NBEdge::Connection> approached1 = e1->getConnectionsFromLane(e1l);
108  for (int e2l = 0; e2l < e2->getNumLanes(); e2l++) {
109  std::vector<NBEdge::Connection> approached2 = e2->getConnectionsFromLane(e2l);
110  for (std::vector<NBEdge::Connection>::iterator e1c = approached1.begin(); e1c != approached1.end(); ++e1c) {
111  if (e1->getTurnDestination() == (*e1c).toEdge) {
112  continue;
113  }
114  for (std::vector<NBEdge::Connection>::iterator e2c = approached2.begin(); e2c != approached2.end(); ++e2c) {
115  if (e2->getTurnDestination() == (*e2c).toEdge) {
116  continue;
117  }
118  if (!forbids(e1, (*e1c).toEdge, e2, (*e2c).toEdge, true)) {
119  val += getDirectionalWeight(e1->getToNode()->getDirection(e1, (*e1c).toEdge));
120  val += getDirectionalWeight(e2->getToNode()->getDirection(e2, (*e2c).toEdge));
121  }
122  }
123  }
124  }
125  }
126  return val;
127 }
128 
129 
130 std::pair<NBEdge*, NBEdge*>
132  std::pair<NBEdge*, NBEdge*> bestPair(static_cast<NBEdge*>(0), static_cast<NBEdge*>(0));
133  SUMOReal bestValue = -1;
134  for (EdgeVector::const_iterator i = edges.begin(); i != edges.end(); ++i) {
135  for (EdgeVector::const_iterator j = i + 1; j != edges.end(); ++j) {
136  const SUMOReal value = computeUnblockedWeightedStreamNumber(*i, *j);
137  if (value > bestValue) {
138  bestValue = value;
139  bestPair = std::pair<NBEdge*, NBEdge*>(*i, *j);
140  } else if (value == bestValue) {
141  const SUMOReal ca = GeomHelper::getMinAngleDiff((*i)->getAngleAtNode((*i)->getToNode()), (*j)->getAngleAtNode((*j)->getToNode()));
142  const SUMOReal oa = GeomHelper::getMinAngleDiff(bestPair.first->getAngleAtNode(bestPair.first->getToNode()), bestPair.second->getAngleAtNode(bestPair.second->getToNode()));
143  if (fabs(oa - ca) < NUMERICAL_EPS) { // break ties by id
144  if (bestPair.first->getID() < (*i)->getID()) {
145  bestPair = std::pair<NBEdge*, NBEdge*>(*i, *j);
146  }
147  } else if (oa < ca) {
148  bestPair = std::pair<NBEdge*, NBEdge*>(*i, *j);
149  }
150  }
151  }
152  }
153  return bestPair;
154 }
155 
156 
157 std::pair<NBEdge*, NBEdge*>
159  if (incoming.size() == 1) {
160  // only one there - return the one
161  std::pair<NBEdge*, NBEdge*> ret(*incoming.begin(), static_cast<NBEdge*>(0));
162  incoming.clear();
163  return ret;
164  }
165  // determine the best combination
166  // by priority, first
167  EdgeVector used;
168  std::sort(incoming.begin(), incoming.end(), edge_by_incoming_priority_sorter());
169  used.push_back(*incoming.begin()); // the first will definitely be used
170  // get the ones with the same priority
171  int prio = getToPrio(*used.begin());
172  for (EdgeVector::iterator i = incoming.begin() + 1; i != incoming.end() && prio == getToPrio(*i); ++i) {
173  used.push_back(*i);
174  }
175  // if there only lower priorised, use these, too
176  if (used.size() < 2) {
177  used = incoming;
178  }
179  std::pair<NBEdge*, NBEdge*> ret = getBestCombination(used);
180  incoming.erase(find(incoming.begin(), incoming.end(), ret.first));
181  incoming.erase(find(incoming.begin(), incoming.end(), ret.second));
182  return ret;
183 }
184 
186 NBOwnTLDef::myCompute(int brakingTimeSeconds) {
187  return computeLogicAndConts(brakingTimeSeconds);
188 }
189 
191 NBOwnTLDef::computeLogicAndConts(int brakingTimeSeconds, bool onlyConts) {
192  myNeedsContRelation.clear();
193  myRightOnRedConflicts.clear();
194  const SUMOTime brakingTime = TIME2STEPS(brakingTimeSeconds);
195  const SUMOTime leftTurnTime = TIME2STEPS(OptionsCont::getOptions().getInt("tls.left-green.time"));
196  // build complete lists first
197  const EdgeVector& incoming = getIncomingEdges();
198  EdgeVector fromEdges, toEdges;
199  std::vector<bool> isTurnaround;
200  std::vector<int> fromLanes;
201  int noLanesAll = 0;
202  int noLinksAll = 0;
203  for (int i1 = 0; i1 < (int)incoming.size(); i1++) {
204  int noLanes = incoming[i1]->getNumLanes();
205  noLanesAll += noLanes;
206  for (int i2 = 0; i2 < noLanes; i2++) {
207  NBEdge* fromEdge = incoming[i1];
208  std::vector<NBEdge::Connection> approached = fromEdge->getConnectionsFromLane(i2);
209  noLinksAll += (int) approached.size();
210  for (int i3 = 0; i3 < (int)approached.size(); i3++) {
211  if (!fromEdge->mayBeTLSControlled(i2, approached[i3].toEdge, approached[i3].toLane)) {
212  --noLinksAll;
213  continue;
214  }
215  assert(i3 < (int)approached.size());
216  NBEdge* toEdge = approached[i3].toEdge;
217  fromEdges.push_back(fromEdge);
218  fromLanes.push_back((int)i2);
219  toEdges.push_back(toEdge);
220  if (toEdge != 0) {
221  isTurnaround.push_back(fromEdge->isTurningDirectionAt(toEdge));
222  } else {
223  isTurnaround.push_back(true);
224  }
225  }
226  }
227  }
228  // collect crossings
229  std::vector<NBNode::Crossing> crossings;
230  for (std::vector<NBNode*>::iterator i = myControlledNodes.begin(); i != myControlledNodes.end(); i++) {
231  const std::vector<NBNode::Crossing>& c = (*i)->getCrossings();
232  if (!onlyConts) {
233  // set tl indices for crossings
234  (*i)->setCrossingTLIndices(noLinksAll);
235  }
236  copy(c.begin(), c.end(), std::back_inserter(crossings));
237  noLinksAll += (int)c.size();
238  }
239 
240  NBTrafficLightLogic* logic = new NBTrafficLightLogic(getID(), getProgramID(), noLinksAll, myOffset, myType);
241  EdgeVector toProc = getConnectedOuterEdges(incoming);
242  const int greenSeconds = OptionsCont::getOptions().getInt("tls.green.time");
243  const SUMOTime greenTime = TIME2STEPS(greenSeconds);
244  // build all phases
245  std::vector<int> greenPhases; // indices of green phases
246  std::vector<bool> hadGreenMajor(noLinksAll, false);
247  while (toProc.size() > 0) {
248  std::pair<NBEdge*, NBEdge*> chosen;
249  if (incoming.size() == 2) {
250  // if there are only 2 incoming edges we need to decide whether they are a crossing or a "continuation"
251  // @node: this heuristic could be extended to also check the number of outgoing edges
252  SUMOReal angle = fabs(NBHelpers::relAngle(incoming[0]->getAngleAtNode(incoming[0]->getToNode()), incoming[1]->getAngleAtNode(incoming[1]->getToNode())));
253  // angle would be 180 for straight opposing incoming edges
254  if (angle < 135) {
255  chosen = std::pair<NBEdge*, NBEdge*>(toProc[0], static_cast<NBEdge*>(0));
256  toProc.erase(toProc.begin());
257  } else {
258  chosen = getBestPair(toProc);
259  }
260  } else {
261  chosen = getBestPair(toProc);
262  }
263  int pos = 0;
264  std::string state((int) noLinksAll, 'r');
265  //std::cout << " computing " << getID() << " prog=" << getProgramID() << " cho1=" << Named::getIDSecure(chosen.first) << " cho2=" << Named::getIDSecure(chosen.second) << " toProc=" << toString(toProc) << "\n";
266  // plain straight movers
267  for (int i1 = 0; i1 < (int) incoming.size(); ++i1) {
268  NBEdge* fromEdge = incoming[i1];
269  const bool inChosen = fromEdge == chosen.first || fromEdge == chosen.second; //chosen.find(fromEdge)!=chosen.end();
270  const int numLanes = fromEdge->getNumLanes();
271  for (int i2 = 0; i2 < numLanes; i2++) {
272  std::vector<NBEdge::Connection> approached = fromEdge->getConnectionsFromLane(i2);
273  for (int i3 = 0; i3 < (int)approached.size(); ++i3) {
274  if (!fromEdge->mayBeTLSControlled(i2, approached[i3].toEdge, approached[i3].toLane)) {
275  continue;
276  }
277  if (inChosen) {
278  state[pos] = 'G';
279  } else {
280  state[pos] = 'r';
281  }
282  ++pos;
283  }
284  }
285  }
286  //std::cout << " state after plain straight movers=" << state << "\n";
287  // correct behaviour for those that are not in chosen, but may drive, though
288  state = allowFollowersOfChosen(state, fromEdges, toEdges);
289  for (int i1 = 0; i1 < pos; ++i1) {
290  if (state[i1] == 'G') {
291  continue;
292  }
293  bool isForbidden = false;
294  for (int i2 = 0; i2 < pos && !isForbidden; ++i2) {
295  if (state[i2] == 'G' && !isTurnaround[i2] &&
296  (forbids(fromEdges[i2], toEdges[i2], fromEdges[i1], toEdges[i1], true) || forbids(fromEdges[i1], toEdges[i1], fromEdges[i2], toEdges[i2], true))) {
297  isForbidden = true;
298  }
299  }
300  if (!isForbidden && !hasCrossing(fromEdges[i1], toEdges[i1], crossings)) {
301  state[i1] = 'G';
302  }
303  }
304  //std::cout << " state after finding additional 'G's=" << state << "\n";
305  // correct behaviour for those that have to wait (mainly left-mover)
306  bool haveForbiddenLeftMover = false;
307  std::vector<bool> rightTurnConflicts(pos, false);
308  state = correctConflicting(state, fromEdges, toEdges, isTurnaround, fromLanes, hadGreenMajor, haveForbiddenLeftMover, rightTurnConflicts);
309  for (int i1 = 0; i1 < pos; ++i1) {
310  if (state[i1] == 'G') {
311  hadGreenMajor[i1] = true;
312  }
313  }
314  //std::cout << " state after correcting left movers=" << state << "\n";
315  const std::string vehicleState = state; // backup state before pedestrian modifications
316  greenPhases.push_back((int)logic->getPhases().size());
317  state = addPedestrianPhases(logic, greenTime, state, crossings, fromEdges, toEdges);
318  // pedestrians have 'r' from here on
319  for (int i1 = pos; i1 < pos + (int)crossings.size(); ++i1) {
320  state[i1] = 'r';
321  }
322  const bool buildLeftGreenPhase = haveForbiddenLeftMover && !myHaveSinglePhase && leftTurnTime > 0;
323  if (brakingTime > 0) {
324  // build yellow (straight)
325  for (int i1 = 0; i1 < pos; ++i1) {
326  if (state[i1] != 'G' && state[i1] != 'g') {
327  continue;
328  }
329  if ((vehicleState[i1] >= 'a' && vehicleState[i1] <= 'z') && buildLeftGreenPhase && !rightTurnConflicts[i1]) {
330  continue;
331  }
332  state[i1] = 'y';
333  }
334  // add step
335  logic->addStep(brakingTime, state);
336  }
337 
338  if (buildLeftGreenPhase) {
339  // build left green
340  for (int i1 = 0; i1 < pos; ++i1) {
341  if (state[i1] == 'Y' || state[i1] == 'y') {
342  state[i1] = 'r';
343  continue;
344  }
345  if (state[i1] == 'g') {
346  state[i1] = 'G';
347  }
348  }
349  state = allowFollowersOfChosen(state, fromEdges, toEdges);
350  state = correctConflicting(state, fromEdges, toEdges, isTurnaround, fromLanes, hadGreenMajor, haveForbiddenLeftMover, rightTurnConflicts);
351 
352  // add step
353  logic->addStep(leftTurnTime, state);
354 
355  // build left yellow
356  if (brakingTime > 0) {
357  for (int i1 = 0; i1 < pos; ++i1) {
358  if (state[i1] != 'G' && state[i1] != 'g') {
359  continue;
360  }
361  state[i1] = 'y';
362  }
363  // add step
364  logic->addStep(brakingTime, state);
365  }
366  }
367  }
368  // fix pedestrian crossings that did not get the green light yet
369  if (crossings.size() > 0) {
370  addPedestrianScramble(logic, noLinksAll, TIME2STEPS(10), brakingTime, crossings, fromEdges, toEdges);
371  }
372 
373  SUMOTime totalDuration = logic->getDuration();
374  if (OptionsCont::getOptions().isDefault("tls.green.time") || !OptionsCont::getOptions().isDefault("tls.cycle.time")) {
375  const SUMOTime cycleTime = TIME2STEPS(OptionsCont::getOptions().getInt("tls.cycle.time"));
376  // adapt to cycle time by changing the duration of the green phases
377  SUMOTime greenPhaseTime = 0;
378  SUMOTime minGreenDuration = SUMOTime_MAX;
379  for (std::vector<int>::const_iterator it = greenPhases.begin(); it != greenPhases.end(); ++it) {
380  const SUMOTime dur = logic->getPhases()[*it].duration;
381  greenPhaseTime += dur;
382  minGreenDuration = MIN2(minGreenDuration, dur);
383  }
384  const int patchSeconds = (int)(STEPS2TIME(cycleTime - totalDuration) / greenPhases.size());
385  const int patchSecondsRest = (int)(STEPS2TIME(cycleTime - totalDuration)) - patchSeconds * (int)greenPhases.size();
386  //std::cout << "cT=" << cycleTime << " td=" << totalDuration << " pS=" << patchSeconds << " pSR=" << patchSecondsRest << "\n";
387  if (STEPS2TIME(minGreenDuration) + patchSeconds < MIN_GREEN_TIME
388  || STEPS2TIME(minGreenDuration) + patchSeconds + patchSecondsRest < MIN_GREEN_TIME
389  || greenPhases.size() == 0) {
390  if (getID() != DummyID) {
391  WRITE_WARNING("The traffic light '" + getID() + "' cannot be adapted to a cycle time of " + time2string(cycleTime) + ".");
392  }
393  // @todo use a multiple of cycleTime ?
394  } else {
395  for (std::vector<int>::const_iterator it = greenPhases.begin(); it != greenPhases.end(); ++it) {
396  logic->setPhaseDuration(*it, logic->getPhases()[*it].duration + TIME2STEPS(patchSeconds));
397  }
398  if (greenPhases.size() > 0) {
399  logic->setPhaseDuration(greenPhases.front(), logic->getPhases()[greenPhases.front()].duration + TIME2STEPS(patchSecondsRest));
400  }
401  totalDuration = logic->getDuration();
402  }
403  }
404 
406  // this computation only makes sense for single nodes
408  if (totalDuration > 0) {
409  if (totalDuration > 3 * (greenTime + 2 * brakingTime + leftTurnTime)) {
410  WRITE_WARNING("The traffic light '" + getID() + "' has a high cycle time of " + time2string(totalDuration) + ".");
411  }
412  logic->closeBuilding();
413  return logic;
414  } else {
415  delete logic;
416  return 0;
417  }
418 }
419 
420 
421 bool
422 NBOwnTLDef::hasCrossing(const NBEdge* from, const NBEdge* to, const std::vector<NBNode::Crossing>& crossings) {
423  assert(to != 0);
424  for (std::vector<NBNode::Crossing>::const_iterator it = crossings.begin(); it != crossings.end(); it++) {
425  const NBNode::Crossing& cross = *it;
426  // only check connections at this crossings node
427  if (to->getFromNode() == cross.node) {
428  for (EdgeVector::const_iterator it_e = cross.edges.begin(); it_e != cross.edges.end(); ++it_e) {
429  const NBEdge* edge = *it_e;
430  if (edge == from || edge == to) {
431  return true;
432  }
433  }
434  }
435  }
436  return false;
437 }
438 
439 
440 std::string
442  std::string state, const std::vector<NBNode::Crossing>& crossings, const EdgeVector& fromEdges, const EdgeVector& toEdges) {
443  const SUMOTime pedClearingTime = TIME2STEPS(5); // compute based on length of the crossing
444  const SUMOTime minPedTime = TIME2STEPS(4); // compute: must be able to reach the middle of the second "Richtungsfahrbahn"
445  const std::string orig = state;
446  state = patchStateForCrossings(state, crossings, fromEdges, toEdges);
447  if (orig == state) {
448  // add step
449  logic->addStep(greenTime, state);
450  } else {
451  const SUMOTime pedTime = greenTime - pedClearingTime;
452  if (pedTime >= minPedTime) {
453  // ensure clearing time for pedestrians
454  const int pedStates = (int)crossings.size();
455  logic->addStep(pedTime, state);
456  state = state.substr(0, state.size() - pedStates) + std::string(pedStates, 'r');
457  logic->addStep(pedClearingTime, state);
458  } else {
459  state = orig;
460  // not safe for pedestrians.
461  logic->addStep(greenTime, state);
462  }
463  }
464  return state;
465 }
466 
467 
468 std::string
469 NBOwnTLDef::patchStateForCrossings(const std::string& state, const std::vector<NBNode::Crossing>& crossings, const EdgeVector& fromEdges, const EdgeVector& toEdges) {
470  std::string result = state;
471  const int pos = (int)(state.size() - crossings.size()); // number of controlled vehicle links
472  for (int ic = 0; ic < (int)crossings.size(); ++ic) {
473  const int i1 = pos + ic;
474  const NBNode::Crossing& cross = crossings[ic];
475  bool isForbidden = false;
476  for (int i2 = 0; i2 < pos && !isForbidden; ++i2) {
477  // only check connections at this crossings node
478  if (fromEdges[i2] != 0 && toEdges[i2] != 0 && fromEdges[i2]->getToNode() == cross.node) {
479  for (EdgeVector::const_iterator it = cross.edges.begin(); it != cross.edges.end(); ++it) {
480  const NBEdge* edge = *it;
481  const LinkDirection i2dir = cross.node->getDirection(fromEdges[i2], toEdges[i2]);
482  if (state[i2] != 'r' && (edge == fromEdges[i2] ||
483  (edge == toEdges[i2] && (i2dir == LINKDIR_STRAIGHT || i2dir == LINKDIR_PARTLEFT || i2dir == LINKDIR_PARTRIGHT)))) {
484  isForbidden = true;
485  break;
486  }
487  }
488  }
489  }
490  if (!isForbidden) {
491  result[i1] = 'G';
492  } else {
493  result[i1] = 'r';
494  }
495  }
496 
497  // correct behaviour for roads that are in conflict with a pedestrian crossing
498  for (int i1 = 0; i1 < pos; ++i1) {
499  if (result[i1] == 'G') {
500  for (int ic = 0; ic < (int)crossings.size(); ++ic) {
501  const NBNode::Crossing& crossing = crossings[ic];
502  if (fromEdges[i1] != 0 && toEdges[i1] != 0 && fromEdges[i1]->getToNode() == crossing.node) {
503  const int i2 = pos + ic;
504  if (result[i2] == 'G' && crossing.node->mustBrakeForCrossing(fromEdges[i1], toEdges[i1], crossing)) {
505  result[i1] = 'g';
506  break;
507  }
508  }
509  }
510  }
511  }
512  return result;
513 }
514 
515 
516 void
518 
519 
520 void
522  collectAllLinks();
523 }
524 
525 
526 void
528  // assign participating nodes to the request
529  collectNodes();
530  // collect the information about participating edges and links
531  collectEdges();
532  collectLinks();
533 }
534 
535 
536 void
538  // set the information about the link's positions within the tl into the
539  // edges the links are starting at, respectively
540  for (NBConnectionVector::const_iterator j = myControlledLinks.begin(); j != myControlledLinks.end(); ++j) {
541  const NBConnection& conn = *j;
542  NBEdge* edge = conn.getFrom();
543  edge->setControllingTLInformation(conn, getID());
544  }
545 }
546 
547 
548 void
549 NBOwnTLDef::remapRemoved(NBEdge* /*removed*/, const EdgeVector& /*incoming*/,
550  const EdgeVector& /*outgoing*/) {}
551 
552 
553 void
554 NBOwnTLDef::replaceRemoved(NBEdge* /*removed*/, int /*removedLane*/,
555  NBEdge* /*by*/, int /*byLane*/) {}
556 
557 
558 void
561  if (myControlledNodes.size() > 0) {
562  // we use a dummy node just to maintain const-correctness
563  myNeedsContRelation.clear();
566  NBTrafficLightLogic* tllDummy = dummy.computeLogicAndConts(0, true);
567  delete tllDummy;
569  for (std::vector<NBNode*>::const_iterator i = myControlledNodes.begin(); i != myControlledNodes.end(); i++) {
570  (*i)->removeTrafficLight(&dummy);
571  }
572  }
574  }
575 }
576 
577 
580  EdgeVector result = incoming;
581  for (EdgeVector::iterator it = result.begin(); it != result.end();) {
582  if ((*it)->getConnections().size() == 0 || (*it)->isInnerEdge()) {
583  it = result.erase(it);
584  } else {
585  ++it;
586  }
587  }
588  return result;
589 }
590 
591 
592 std::string
593 NBOwnTLDef::allowFollowersOfChosen(std::string state, const EdgeVector& fromEdges, const EdgeVector& toEdges) {
594  bool check = true;
595  while (check) {
596  check = false;
597  for (int i1 = 0; i1 < (int)fromEdges.size(); ++i1) {
598  if (state[i1] == 'G') {
599  continue;
600  }
601  bool followsChosen = false;
602  for (int i2 = 0; i2 < (int)fromEdges.size() && !followsChosen; ++i2) {
603  if (state[i2] == 'G' && fromEdges[i1] == toEdges[i2]) {
604  followsChosen = true;
605  }
606  }
607  if (followsChosen) {
608  state[i1] = 'G';
609  check = true;
610  }
611  }
612  }
613  return state;
614 }
615 
616 
617 std::string
618 NBOwnTLDef::correctConflicting(std::string state, const EdgeVector& fromEdges, const EdgeVector& toEdges,
619  const std::vector<bool>& isTurnaround,
620  const std::vector<int>& fromLanes,
621  const std::vector<bool>& hadGreenMajor,
622  bool& haveForbiddenLeftMover,
623  std::vector<bool>& rightTurnConflicts) {
624  const bool controlledWithin = !OptionsCont::getOptions().getBool("tls.uncontrolled-within");
625  for (int i1 = 0; i1 < (int)fromEdges.size(); ++i1) {
626  if (state[i1] == 'G') {
627  for (int i2 = 0; i2 < (int)fromEdges.size(); ++i2) {
628  if ((state[i2] == 'G' || state[i2] == 'g')) {
630  fromEdges[i1], toEdges[i1], fromLanes[i1], fromEdges[i2], toEdges[i2], fromLanes[i2])) {
631  rightTurnConflicts[i1] = true;
632  }
633  if (forbids(fromEdges[i2], toEdges[i2], fromEdges[i1], toEdges[i1], true, controlledWithin) || rightTurnConflicts[i1]) {
634  state[i1] = 'g';
635  myNeedsContRelation.insert(StreamPair(fromEdges[i1], toEdges[i1], fromEdges[i2], toEdges[i2]));
636  if (!isTurnaround[i1] && !hadGreenMajor[i1]) {
637  haveForbiddenLeftMover = true;
638  }
639  }
640  }
641  }
642  }
643  if (state[i1] == 'r') {
644  if (fromEdges[i1]->getToNode()->getType() == NODETYPE_TRAFFIC_LIGHT_RIGHT_ON_RED &&
645  fromEdges[i1]->getToNode()->getDirection(fromEdges[i1], toEdges[i1]) == LINKDIR_RIGHT) {
646  // handle right-on-red conflicts
647  state[i1] = 's';
648  for (int i2 = 0; i2 < (int)fromEdges.size(); ++i2) {
649  if (state[i2] == 'G' && !isTurnaround[i2] &&
650  (forbids(fromEdges[i2], toEdges[i2], fromEdges[i1], toEdges[i1], true) || forbids(fromEdges[i1], toEdges[i1], fromEdges[i2], toEdges[i2], true))) {
651  myRightOnRedConflicts.insert(std::make_pair(i1, i2));
652  }
653  }
654  }
655  }
656  }
657  return state;
658 }
659 
660 
661 void
662 NBOwnTLDef::addPedestrianScramble(NBTrafficLightLogic* logic, int noLinksAll, SUMOTime /* greenTime */, SUMOTime brakingTime,
663  const std::vector<NBNode::Crossing>& crossings, const EdgeVector& fromEdges, const EdgeVector& toEdges) {
664  const int vehLinks = noLinksAll - (int)crossings.size();
665  std::vector<bool> foundGreen(crossings.size(), false);
666  const std::vector<NBTrafficLightLogic::PhaseDefinition>& phases = logic->getPhases();
667  for (int i = 0; i < (int)phases.size(); ++i) {
668  const std::string state = phases[i].state;
669  for (int j = 0; j < (int)crossings.size(); ++j) {
670  LinkState ls = (LinkState)state[vehLinks + j];
672  foundGreen[j] = true;
673  }
674  }
675  }
676  for (int j = 0; j < (int)foundGreen.size(); ++j) {
677  if (!foundGreen[j]) {
678 
679  // add a phase where all pedestrians may walk, (preceded by a yellow phase and followed by a clearing phase)
680  if (phases.size() > 0) {
681  bool needYellowPhase = false;
682  std::string state = phases.back().state;
683  for (int i1 = 0; i1 < vehLinks; ++i1) {
684  if (state[i1] == 'G' || state[i1] == 'g') {
685  state[i1] = 'y';
686  needYellowPhase = true;
687  }
688  }
689  // add yellow step
690  if (needYellowPhase && brakingTime > 0) {
691  logic->addStep(brakingTime, state);
692  }
693  }
694  addPedestrianPhases(logic, TIME2STEPS(10), std::string(noLinksAll, 'r'), crossings, fromEdges, toEdges);
695  break;
696  }
697  }
698 }
699 
700 /****************************************************************************/
bool mayBeTLSControlled(int fromLane, NBEdge *toEdge, int toLane) const
return true if certain connection must be controlled by TLS
Definition: NBEdge.cpp:2242
#define MIN_GREEN_TIME
Definition: NBOwnTLDef.cpp:51
static std::string patchStateForCrossings(const std::string &state, const std::vector< NBNode::Crossing > &crossings, const EdgeVector &fromEdges, const EdgeVector &toEdges)
compute phase state in regard to pedestrian crossings
Definition: NBOwnTLDef.cpp:469
The link is a partial left direction.
The link has green light, may pass.
bool setControllingTLInformation(const NBConnection &c, const std::string &tlID)
Returns if the link could be set as to be controlled.
Definition: NBEdge.cpp:2253
void setTLControllingInformation() const
Informs edges about being controlled by a tls.
Definition: NBOwnTLDef.cpp:537
TrafficLightType myType
The algorithm type for the traffic light.
long long int SUMOTime
Definition: SUMOTime.h:43
int getInt(const std::string &name) const
Returns the int-value of the named option (only for Option_Integer)
void collectAllLinks()
helper method for use in NBOwnTLDef and NBLoadedSUMOTLDef
static const std::string DummyID
id for temporary definitions
void closeBuilding()
closes the building process
RightOnRedConflicts myRightOnRedConflicts
A SUMO-compliant built logic for a traffic light.
int getJunctionPriority(const NBNode *const node) const
Returns the junction priority (normalised for the node currently build)
Definition: NBEdge.cpp:1429
The link has green light, has to brake.
std::string correctConflicting(std::string state, const EdgeVector &fromEdges, const EdgeVector &toEdges, const std::vector< bool > &isTurnaround, const std::vector< int > &fromLanes, const std::vector< bool > &hadGreenMajor, bool &haveForbiddenLeftMover, std::vector< bool > &rightTurnConflicts)
change &#39;G&#39; to &#39;g&#39; for conflicting connections
Definition: NBOwnTLDef.cpp:618
The representation of a single edge during network building.
Definition: NBEdge.h:71
std::string allowFollowersOfChosen(std::string state, const EdgeVector &fromEdges, const EdgeVector &toEdges)
allow connections that follow on of the chosen edges
Definition: NBOwnTLDef.cpp:593
TrafficLightType getType() const
get the algorithm type (static etc..)
const std::vector< PhaseDefinition > & getPhases() const
Returns the phases.
void collectNodes()
Collects the nodes participating in this traffic light.
Definition: NBOwnTLDef.cpp:517
std::string time2string(SUMOTime t)
Definition: SUMOTime.cpp:59
The base class for traffic light logic definitions.
NBEdge * getTurnDestination(bool possibleDestination=false) const
Definition: NBEdge.cpp:2490
NBEdge * getFrom() const
returns the from-edge (start of the connection)
static bool hasCrossing(const NBEdge *from, const NBEdge *to, const std::vector< NBNode::Crossing > &crossings)
compute whether the given connection is crossed by pedestrians
Definition: NBOwnTLDef.cpp:422
bool isForbidden(SVCPermissions permissions)
Returns whether an edge with the given permission is a forbidden edge.
bool getBool(const std::string &name) const
Returns the boolean-value of the named option (only for Option_Bool)
const std::string & getID() const
Returns the id.
Definition: Named.h:66
#define TIME2STEPS(x)
Definition: SUMOTime.h:66
static EdgeVector getConnectedOuterEdges(const EdgeVector &incoming)
get edges that have connections
Definition: NBOwnTLDef.cpp:579
SUMOTime myOffset
The offset in the program.
The link is a (hard) left direction.
#define WRITE_WARNING(msg)
Definition: MsgHandler.h:200
std::pair< NBEdge *, NBEdge * > getBestPair(EdgeVector &incoming)
Returns the combination of two edges from the given which has most unblocked streams.
Definition: NBOwnTLDef.cpp:158
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:1391
LinkDirection
The different directions a link between two lanes may take (or a stream between two edges)...
The link is a straight direction.
std::vector< Connection > getConnectionsFromLane(int lane) const
Returns connections from a given lane.
Definition: NBEdge.cpp:912
virtual void collectEdges()
Build the list of participating edges.
int getNumLanes() const
Returns the number of lanes.
Definition: NBEdge.h:395
void replaceRemoved(NBEdge *removed, int removedLane, NBEdge *by, int byLane)
Replaces a removed edge/lane.
Definition: NBOwnTLDef.cpp:554
std::pair< NBEdge *, NBEdge * > getBestCombination(const EdgeVector &edges)
Returns the combination of two edges from the given which has most unblocked streams.
Definition: NBOwnTLDef.cpp:131
bool myHaveSinglePhase
Whether left-mover should not have an additional phase.
Definition: NBOwnTLDef.h:270
#define STEPS2TIME(x)
Definition: SUMOTime.h:65
~NBOwnTLDef()
Destructor.
Definition: NBOwnTLDef.cpp:78
LinkState
The right-of-way state of a link between two lanes used when constructing a NBTrafficLightLogic, in MSLink and GNEInternalLane.
NBOwnTLDef(const std::string &id, const std::vector< NBNode *> &junctions, SUMOTime offset, TrafficLightType type)
Constructor.
Definition: NBOwnTLDef.cpp:56
T MIN2(T a, T b)
Definition: StdDefs.h:69
The link is a (hard) right direction.
static const std::string DefaultProgramID
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
const std::string & getProgramID() const
Returns the ProgramID.
The link is a partial right direction.
SUMOReal computeUnblockedWeightedStreamNumber(const NBEdge *const e1, const NBEdge *const e2)
Returns how many streams outgoing from the edges can pass the junction without being blocked...
Definition: NBOwnTLDef.cpp:104
SUMOTime getDuration() const
Returns the duration of the complete cycle.
LinkDirection getDirection(const NBEdge *const incoming, const NBEdge *const outgoing, bool leftHand=false) const
Returns the representation of the described stream&#39;s direction.
Definition: NBNode.cpp:1555
const EdgeVector & getIncomingEdges() const
Returns the list of incoming edges (must be build first)
static void addPedestrianScramble(NBTrafficLightLogic *logic, 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:662
NBTrafficLightLogic * myCompute(int brakingTimeSeconds)
Computes the traffic light logic finally in dependence to the type.
Definition: NBOwnTLDef.cpp:186
#define SUMOTime_MAX
Definition: SUMOTime.h:44
NBTrafficLightLogic * computeLogicAndConts(int brakingTimeSeconds, bool onlyConts=false)
helper function for myCompute
Definition: NBOwnTLDef.cpp:191
void collectLinks()
Collects the links participating in this traffic light.
Definition: NBOwnTLDef.cpp:521
SUMOReal getDirectionalWeight(LinkDirection dir)
Returns the weight of a stream given its direction.
Definition: NBOwnTLDef.cpp:88
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.
std::vector< NBEdge * > EdgeVector
Definition: NBCont.h:41
static SUMOReal getMinAngleDiff(SUMOReal angle1, SUMOReal angle2)
Returns the minimum distance (clockwise/counter-clockwise) between both angles.
Definition: GeomHelper.cpp:172
const NBNode * node
The parent node of this crossing.
Definition: NBNode.h:137
void setPhaseDuration(int phaseIndex, SUMOTime duration)
Modifies the duration for an existing phase (used by NETEDIT)
bool isTurningDirectionAt(const NBEdge *const edge) const
Returns whether the given edge is the opposite direction to this edge.
Definition: NBEdge.cpp:2189
EdgeVector edges
The edges being crossed.
Definition: NBNode.h:139
Represents a single node (junction) during network building.
Definition: NBNode.h:74
A definition of a pedestrian crossing.
Definition: NBNode.h:132
#define SUMOReal
Definition: config.h:213
bool mustBrakeForCrossing(const NBEdge *const from, const NBEdge *const to, const Crossing &crossing) const
Returns the information whether the described flow must brake for the given crossing.
Definition: NBNode.cpp:1385
static SUMOReal relAngle(SUMOReal angle1, SUMOReal angle2)
Definition: NBHelpers.cpp:56
#define NUMERICAL_EPS
Definition: config.h:160
data structure for caching needsCont information
std::vector< NBNode * > myControlledNodes
The container with participating nodes.
NBNode * getFromNode() const
Returns the origin node of the edge.
Definition: NBEdge.h:409
A traffic light logics which must be computed (only nodes/edges are given)
Definition: NBOwnTLDef.h:54
void addStep(SUMOTime duration, const std::string &state, int index=-1)
Adds a phase to the logic.
void initNeedsContRelation() const
Definition: NBOwnTLDef.cpp:559
Sorts edges by their priority within the node they end at.
Definition: NBOwnTLDef.h:253
void setParticipantsInformation()
Builds the list of participating nodes/edges/links.
Definition: NBOwnTLDef.cpp:527
NBConnectionVector myControlledLinks
The list of controlled links.
NBNode * getToNode() const
Returns the destination node of the edge.
Definition: NBEdge.h:416
int getToPrio(const NBEdge *const e)
Returns this edge&#39;s priority at the node it ends at.
Definition: NBOwnTLDef.cpp:82
TrafficLightType
void remapRemoved(NBEdge *removed, const EdgeVector &incoming, const EdgeVector &outgoing)
Replaces occurences of the removed edge in incoming/outgoing edges of all definitions.
Definition: NBOwnTLDef.cpp:549