LCOV - code coverage report
Current view: top level - src/netbuild - NBOwnTLDef.cpp (source / functions) Coverage Total Hit
Test: lcov.info Lines: 94.3 % 796 751
Test Date: 2025-11-13 15:38:19 Functions: 89.1 % 46 41

            Line data    Source code
       1              : /****************************************************************************/
       2              : // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
       3              : // Copyright (C) 2001-2025 German Aerospace Center (DLR) and others.
       4              : // This program and the accompanying materials are made available under the
       5              : // terms of the Eclipse Public License 2.0 which is available at
       6              : // https://www.eclipse.org/legal/epl-2.0/
       7              : // This Source Code may also be made available under the following Secondary
       8              : // Licenses when the conditions for such availability set forth in the Eclipse
       9              : // Public License 2.0 are satisfied: GNU General Public License, version 2
      10              : // or later which is available at
      11              : // https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
      12              : // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
      13              : /****************************************************************************/
      14              : /// @file    NBOwnTLDef.cpp
      15              : /// @author  Daniel Krajzewicz
      16              : /// @author  Jakob Erdmann
      17              : /// @author  Sascha Krieg
      18              : /// @author  Michael Behrisch
      19              : /// @date    Tue, 29.05.2005
      20              : ///
      21              : // A traffic light logics which must be computed (only nodes/edges are given)
      22              : /****************************************************************************/
      23              : #include <config.h>
      24              : 
      25              : #include <vector>
      26              : #include <cassert>
      27              : #include <iterator>
      28              : #include "NBTrafficLightDefinition.h"
      29              : #include "NBNode.h"
      30              : #include "NBOwnTLDef.h"
      31              : #include "NBTrafficLightLogic.h"
      32              : #include <utils/common/MsgHandler.h>
      33              : #include <utils/common/UtilExceptions.h>
      34              : #include <utils/common/ToString.h>
      35              : #include <utils/common/StringUtils.h>
      36              : #include <utils/options/OptionsCont.h>
      37              : #include <utils/options/Option.h>
      38              : 
      39              : #define HEIGH_WEIGHT 2
      40              : #define LOW_WEIGHT .5;
      41              : 
      42              : #define MIN_GREEN_TIME 5
      43              : 
      44              : //#define DEBUG_STREAM_ORDERING
      45              : //#define DEBUG_PHASES
      46              : //#define DEBUG_CONTRELATION
      47              : #define DEBUGID  "C"
      48              : #define DEBUGCOND (getID() == DEBUGID)
      49              : #define DEBUGCOND2(obj) (obj->getID() == DEBUGID)
      50              : //#define DEBUGEDGE(edge) (edge->getID() == "23209153#1" || edge->getID() == "319583927#0")
      51              : //#define DEBUGCOND (true)
      52              : #define DEBUGEDGE(edge) (true)
      53              : 
      54              : // ===========================================================================
      55              : // static members
      56              : // ===========================================================================
      57              : const double NBOwnTLDef::MIN_SPEED_CROSSING_TIME(25 / 3.6);
      58              : 
      59              : 
      60              : // ===========================================================================
      61              : // member method definitions
      62              : // ===========================================================================
      63          878 : NBOwnTLDef::NBOwnTLDef(const std::string& id,
      64              :                        const std::vector<NBNode*>& junctions, SUMOTime offset,
      65          878 :                        TrafficLightType type) :
      66              :     NBTrafficLightDefinition(id, junctions, DefaultProgramID, offset, type),
      67          878 :     myHaveSinglePhase(false),
      68          878 :     myLayout(TrafficLightLayout::DEFAULT) {
      69          878 : }
      70              : 
      71              : 
      72         5820 : NBOwnTLDef::NBOwnTLDef(const std::string& id, NBNode* junction, SUMOTime offset,
      73         5820 :                        TrafficLightType type) :
      74              :     NBTrafficLightDefinition(id, junction, DefaultProgramID, offset, type),
      75         5820 :     myHaveSinglePhase(false),
      76         5820 :     myLayout(TrafficLightLayout::DEFAULT) {
      77         5820 : }
      78              : 
      79              : 
      80            0 : NBOwnTLDef::NBOwnTLDef(const std::string& id, SUMOTime offset,
      81            0 :                        TrafficLightType type) :
      82              :     NBTrafficLightDefinition(id, DefaultProgramID, offset, type),
      83            0 :     myHaveSinglePhase(false),
      84            0 :     myLayout(TrafficLightLayout::DEFAULT) {
      85            0 : }
      86              : 
      87              : 
      88        10250 : NBOwnTLDef::~NBOwnTLDef() {}
      89              : 
      90              : 
      91              : int
      92        11131 : NBOwnTLDef::getToPrio(const NBEdge* const e) {
      93        11131 :     return e->getJunctionPriority(e->getToNode());
      94              : }
      95              : 
      96              : 
      97              : double
      98       124134 : NBOwnTLDef::getDirectionalWeight(LinkDirection dir) {
      99       124134 :     switch (dir) {
     100              :         case LinkDirection::STRAIGHT:
     101              :         case LinkDirection::PARTLEFT:
     102              :         case LinkDirection::PARTRIGHT:
     103              :             return HEIGH_WEIGHT;
     104        55602 :         case LinkDirection::LEFT:
     105              :         case LinkDirection::RIGHT:
     106        55602 :             return LOW_WEIGHT;
     107              :         default:
     108              :             break;
     109              :     }
     110            0 :     return 0;
     111              : }
     112              : 
     113              : double
     114         8102 : NBOwnTLDef::computeUnblockedWeightedStreamNumber(const NBEdge* const e1, const NBEdge* const e2) {
     115              :     double val = 0;
     116        23340 :     for (int e1l = 0; e1l < e1->getNumLanes(); e1l++) {
     117        15238 :         std::vector<NBEdge::Connection> approached1 = e1->getConnectionsFromLane(e1l);
     118        46403 :         for (int e2l = 0; e2l < e2->getNumLanes(); e2l++) {
     119        31165 :             std::vector<NBEdge::Connection> approached2 = e2->getConnectionsFromLane(e2l);
     120        80205 :             for (std::vector<NBEdge::Connection>::iterator e1c = approached1.begin(); e1c != approached1.end(); ++e1c) {
     121        49040 :                 if (e1->getTurnDestination() == (*e1c).toEdge) {
     122         5382 :                     continue;
     123              :                 }
     124       122298 :                 for (std::vector<NBEdge::Connection>::iterator e2c = approached2.begin(); e2c != approached2.end(); ++e2c) {
     125        78640 :                     if (e2->getTurnDestination() == (*e2c).toEdge) {
     126        10549 :                         continue;
     127              :                     }
     128        68091 :                     const double sign = (forbids(e1, (*e1c).toEdge, e2, (*e2c).toEdge, true)
     129        68091 :                                          || forbids(e2, (*e2c).toEdge, e1, (*e1c).toEdge, true)) ? -1 : 1;
     130              :                     double w1;
     131              :                     double w2;
     132        68091 :                     const int prio1 = e1->getJunctionPriority(e1->getToNode());
     133        68091 :                     const int prio2 = e2->getJunctionPriority(e2->getToNode());
     134        68091 :                     if (prio1 == prio2) {
     135        62067 :                         w1 = getDirectionalWeight(e1->getToNode()->getDirection(e1, (*e1c).toEdge));
     136        62067 :                         w2 = getDirectionalWeight(e2->getToNode()->getDirection(e2, (*e2c).toEdge));
     137              :                     } else {
     138         6024 :                         if (prio1 > prio2) {
     139              :                             w1 = HEIGH_WEIGHT;
     140              :                             w2 = LOW_WEIGHT;
     141              :                         } else {
     142              :                             w1 = LOW_WEIGHT;
     143              :                             w2 = HEIGH_WEIGHT;
     144              :                         }
     145         6024 :                         if (sign == -1) {
     146              :                             // extra penalty if edges with different junction priority are in conflict
     147         3611 :                             w1 *= 2;
     148         3611 :                             w2 *= 2;
     149              :                         }
     150              :                     }
     151        68091 :                     if (isRailway(e1->getPermissions()) != isRailway(e2->getPermissions())) {
     152         2408 :                         w1 *= 0.1;
     153         2408 :                         w2 *= 0.1;
     154              :                     }
     155        68091 :                     if ((e1->getPermissions() & SVC_PASSENGER) == 0) {
     156         7545 :                         w1 *= 0.1;
     157              :                     }
     158        68091 :                     if ((e2->getPermissions() & SVC_PASSENGER) == 0) {
     159         6550 :                         w2 *= 0.1;
     160              :                     }
     161        68091 :                     val += sign * w1;
     162        68091 :                     val += sign * w2;
     163              : #ifdef DEBUG_STREAM_ORDERING
     164              :                     if (DEBUGCOND && DEBUGEDGE(e2) && DEBUGEDGE(e1)) {
     165              :                         std::cout << "      sign=" << sign << " w1=" << w1 << " w2=" << w2 << " val=" << val
     166              :                                   << " c1=" << (*e1c).getDescription(e1)
     167              :                                   << " c2=" << (*e2c).getDescription(e2)
     168              :                                   << "\n";
     169              :                     }
     170              : #endif
     171              :                 }
     172              :             }
     173        31165 :         }
     174        15238 :     }
     175              : #ifdef DEBUG_STREAM_ORDERING
     176              :     if (DEBUGCOND && DEBUGEDGE(e2) && DEBUGEDGE(e1)) {
     177              :         std::cout << "     computeUnblockedWeightedStreamNumber e1=" << e1->getID() << " e2=" << e2->getID() << " val=" << val << "\n";
     178              :     }
     179              : #endif
     180         8102 :     return val;
     181              : }
     182              : 
     183              : 
     184              : std::pair<NBEdge*, NBEdge*>
     185         4161 : NBOwnTLDef::getBestCombination(const EdgeVector& edges) {
     186              :     std::pair<NBEdge*, NBEdge*> bestPair(static_cast<NBEdge*>(nullptr), static_cast<NBEdge*>(nullptr));
     187              :     double bestValue = -std::numeric_limits<double>::max();
     188        13630 :     for (EdgeVector::const_iterator i = edges.begin(); i != edges.end(); ++i) {
     189        17571 :         for (EdgeVector::const_iterator j = i + 1; j != edges.end(); ++j) {
     190         8102 :             const double value = computeUnblockedWeightedStreamNumber(*i, *j);
     191         8102 :             if (value > bestValue) {
     192              :                 bestValue = value;
     193              :                 bestPair = std::pair<NBEdge*, NBEdge*>(*i, *j);
     194         3134 :             } else if (value == bestValue) {
     195          911 :                 const double ca = GeomHelper::getMinAngleDiff((*i)->getAngleAtNode((*i)->getToNode()), (*j)->getAngleAtNode((*j)->getToNode()));
     196          911 :                 const double oa = GeomHelper::getMinAngleDiff(bestPair.first->getAngleAtNode(bestPair.first->getToNode()), bestPair.second->getAngleAtNode(bestPair.second->getToNode()));
     197          911 :                 if (fabs(oa - ca) < NUMERICAL_EPS) { // break ties by id
     198           14 :                     if (bestPair.first->getID() < (*i)->getID()) {
     199              :                         bestPair = std::pair<NBEdge*, NBEdge*>(*i, *j);
     200              :                     }
     201          897 :                 } else if (oa < ca) {
     202              :                     bestPair = std::pair<NBEdge*, NBEdge*>(*i, *j);
     203              :                 }
     204              :             }
     205              :         }
     206              :     }
     207         4161 :     if (bestValue <= 0) {
     208              :         // do not group edges
     209          540 :         if (bestPair.first->getPriority() < bestPair.second->getPriority()) {
     210              :             std::swap(bestPair.first, bestPair.second);
     211              :         }
     212              :         bestPair.second = nullptr;
     213              :     }
     214              : #ifdef DEBUG_STREAM_ORDERING
     215              :     if (DEBUGCOND) {
     216              :         std::cout << "   getBestCombination bestValue=" << bestValue << "  best=" << Named::getIDSecure(bestPair.first) << ", " << Named::getIDSecure(bestPair.second) << "\n";
     217              :     }
     218              : #endif
     219         4161 :     return bestPair;
     220              : }
     221              : 
     222              : 
     223              : std::pair<NBEdge*, NBEdge*>
     224         5870 : NBOwnTLDef::getBestPair(EdgeVector& incoming) {
     225         5870 :     if (incoming.size() == 1) {
     226              :         // only one there - return the one
     227              :         std::pair<NBEdge*, NBEdge*> ret(*incoming.begin(), static_cast<NBEdge*>(nullptr));
     228              :         incoming.clear();
     229         1709 :         return ret;
     230              :     }
     231              :     // determine the best combination
     232              :     //  by priority, first
     233              :     EdgeVector used;
     234         4161 :     std::sort(incoming.begin(), incoming.end(), edge_by_incoming_priority_sorter());
     235         4161 :     used.push_back(*incoming.begin()); // the first will definitely be used
     236              :     // get the ones with the same priority
     237         4161 :     int prio = getToPrio(*used.begin());
     238         8731 :     for (EdgeVector::iterator i = incoming.begin() + 1; i != incoming.end() && prio == getToPrio(*i); ++i) {
     239         4570 :         used.push_back(*i);
     240              :     }
     241              :     //  if there only lower priorised, use these, too
     242         4161 :     if (used.size() < 2) {
     243          431 :         used = incoming;
     244              :     }
     245         4161 :     std::pair<NBEdge*, NBEdge*> ret = getBestCombination(used);
     246              : #ifdef DEBUG_STREAM_ORDERING
     247              :     if (DEBUGCOND) {
     248              :         std::cout << "getBestPair tls=" << getID() << " incoming=" << toString(incoming) << " prio=" << prio << " used=" << toString(used) << " best=" << Named::getIDSecure(ret.first) << ", " << Named::getIDSecure(ret.second) << "\n";
     249              :     }
     250              : #endif
     251              : 
     252         4161 :     incoming.erase(find(incoming.begin(), incoming.end(), ret.first));
     253         4161 :     if (ret.second != nullptr) {
     254         3621 :         incoming.erase(find(incoming.begin(), incoming.end(), ret.second));
     255              :     }
     256         4161 :     return ret;
     257         4161 : }
     258              : 
     259              : bool
     260        12382 : NBOwnTLDef::hasStraightConnection(const NBEdge* fromEdge) {
     261        22055 :     for (const NBEdge::Connection& c : fromEdge->getConnections()) {
     262        19803 :         LinkDirection dir = fromEdge->getToNode()->getDirection(fromEdge, c.toEdge);
     263        19803 :         if (dir == LinkDirection::STRAIGHT) {
     264              :             return true;
     265              :         }
     266              :     }
     267              :     return false;
     268              : }
     269              : 
     270              : NBTrafficLightLogic*
     271         2191 : NBOwnTLDef::myCompute(int brakingTimeSeconds) {
     272         2191 :     if (myControlledNodes.size() > 1) {
     273              :         // call this first so that the following call to computeLogicAndConts resets linkIndices
     274          126 :         initNeedsContRelation();
     275              :         // reset insideTLS info
     276          126 :         collectEdges();
     277              :     }
     278         2191 :     return computeLogicAndConts(brakingTimeSeconds);
     279              : }
     280              : 
     281              : 
     282              : NBTrafficLightLogic*
     283         3654 : NBOwnTLDef::computeLogicAndConts(int brakingTimeSeconds, bool onlyConts) {
     284         3654 :     if (myControlledNodes.size() == 1) {
     285              :         // otherwise, use values from previous call to initNeedsContRelation
     286              :         myNeedsContRelation.clear();
     287              :     }
     288              :     myExtraConflicts.clear();
     289         3654 :     const bool isNEMA = myType == TrafficLightType::NEMA;
     290         3654 :     const SUMOTime brakingTime = TIME2STEPS(brakingTimeSeconds);
     291         3654 :     const SUMOTime leftTurnTime = TIME2STEPS(OptionsCont::getOptions().getInt("tls.left-green.time"));
     292         3849 :     const SUMOTime minMinDur = (myType == TrafficLightType::STATIC) ? UNSPECIFIED_DURATION : TIME2STEPS(OptionsCont::getOptions().getInt("tls.min-dur"));
     293         3849 :     const SUMOTime maxDur = (myType == TrafficLightType::STATIC) ? UNSPECIFIED_DURATION : TIME2STEPS(OptionsCont::getOptions().getInt("tls.max-dur"));
     294         3654 :     const SUMOTime earliestEnd = UNSPECIFIED_DURATION;
     295              :     const SUMOTime latestEnd = UNSPECIFIED_DURATION;
     296         3654 :     const SUMOTime greenTime = TIME2STEPS(OptionsCont::getOptions().getInt("tls.green.time"));
     297         3654 :     SUMOTime allRedTime = TIME2STEPS(OptionsCont::getOptions().getInt("tls.allred.time"));
     298         3654 :     const double minorLeftSpeedThreshold = OptionsCont::getOptions().getFloat("tls.minor-left.max-speed");
     299         3654 :     const bool noMixed = OptionsCont::getOptions().getBool("tls.no-mixed");
     300              :     // left-turn phases do not work well for joined tls, so we build incoming instead
     301         3654 :     if (myLayout == TrafficLightLayout::DEFAULT) {
     302              :         // @note this prevents updating after loading plain-xml into netedit computing tls and then changing the default layout
     303         6056 :         myLayout = SUMOXMLDefinitions::TrafficLightLayouts.get(OptionsCont::getOptions().getString("tls.layout"));
     304              :     }
     305              :     // corridorLike() resets crossing indices so should be called first
     306         3654 :     const bool groupOpposites = (myLayout == TrafficLightLayout::OPPOSITES && (myControlledNodes.size() <= 2 || corridorLike()));
     307              : 
     308              :     // things collect for NEMA phase building
     309              :     std::vector<std::pair<NBEdge*, NBEdge*> > chosenList;
     310              :     std::vector<std::string> straightStates;
     311              :     std::vector<std::string> leftStates;
     312              : 
     313              :     // build complete lists first
     314         3654 :     const EdgeVector& incoming = getIncomingEdges();
     315              :     EdgeVector fromEdges, toEdges;
     316              :     std::vector<bool> isTurnaround;
     317              :     std::vector<bool> hasTurnLane;
     318              :     std::vector<int> fromLanes;
     319              :     std::vector<int> toLanes;
     320              :     std::vector<SUMOTime> crossingTime;
     321              :     int totalNumLinks = 0;
     322        16036 :     for (NBEdge* const fromEdge : incoming) {
     323              :         const int numLanes = fromEdge->getNumLanes();
     324        12382 :         const bool edgeHasStraight = hasStraightConnection(fromEdge);
     325        37506 :         for (int i2 = 0; i2 < numLanes; i2++) {
     326              :             bool hasLeft = false;
     327              :             bool hasPartLeft = false;
     328              :             bool hasStraight = false;
     329              :             bool hasRight = false;
     330              :             bool hasTurnaround = false;
     331        63852 :             for (const NBEdge::Connection& approached : fromEdge->getConnectionsFromLane(i2)) {
     332        38728 :                 if (!fromEdge->mayBeTLSControlled(i2, approached.toEdge, approached.toLane)) {
     333           57 :                     continue;
     334              :                 }
     335        38671 :                 fromEdges.push_back(fromEdge);
     336        38671 :                 fromLanes.push_back(i2);
     337        38671 :                 toLanes.push_back(approached.toLane);
     338        38671 :                 toEdges.push_back(approached.toEdge);
     339        11210 :                 if (approached.vmax < NUMERICAL_EPS || (fromEdge->getPermissions() & SVC_PASSENGER) == 0
     340        49238 :                         || (approached.toEdge->getPermissions() & SVC_PASSENGER) == 0) {
     341        28454 :                     crossingTime.push_back(0);
     342              :                 } else {
     343        12868 :                     crossingTime.push_back(TIME2STEPS((approached.length + approached.viaLength) / MAX2(approached.vmax, MIN_SPEED_CROSSING_TIME)));
     344              :                 }
     345              :                 // std::cout << fromEdge->getID() << " " << approached.toEdge->getID() << " " << (fromEdge->getPermissions() & SVC_PASSENGER) << " " << approached.length << " " << approached.viaLength << " " << approached.vmax << " " << crossingTime.back() << std::endl;
     346        38671 :                 if (approached.toEdge != nullptr) {
     347        38671 :                     isTurnaround.push_back(fromEdge->isTurningDirectionAt(approached.toEdge));
     348              :                 } else {
     349            0 :                     isTurnaround.push_back(true);
     350              :                 }
     351        38671 :                 LinkDirection dir = fromEdge->getToNode()->getDirection(fromEdge, approached.toEdge);
     352              :                 if (dir == LinkDirection::STRAIGHT) {
     353              :                     hasStraight = true;
     354              :                 } else if (dir == LinkDirection::RIGHT || dir == LinkDirection::PARTRIGHT) {
     355              :                     hasRight = true;
     356              :                 } else if (dir == LinkDirection::LEFT) {
     357              :                     hasLeft = true;
     358              :                 } else if (dir == LinkDirection::PARTLEFT) {
     359              :                     hasPartLeft = true;
     360              :                 } else if (dir == LinkDirection::TURN) {
     361              :                     hasTurnaround = true;
     362              :                 }
     363        38671 :                 totalNumLinks++;
     364        25124 :             }
     365        63852 :             for (const NBEdge::Connection& approached : fromEdge->getConnectionsFromLane(i2)) {
     366        38728 :                 if (!fromEdge->mayBeTLSControlled(i2, approached.toEdge, approached.toLane)) {
     367           57 :                     continue;
     368              :                 }
     369        38671 :                 hasTurnLane.push_back(
     370              :                     (hasLeft && !hasPartLeft && !hasStraight && !hasRight)
     371        17079 :                     || (hasPartLeft && !hasLeft && !hasStraight && !hasRight)
     372        34594 :                     || (hasPartLeft && hasLeft && edgeHasStraight && !hasRight)
     373        72816 :                     || (!hasLeft && !hasPartLeft && !hasTurnaround && hasRight));
     374        25124 :             }
     375              :             //std::cout << " from=" << fromEdge->getID() << "_" << i2 << " hasTurnLane=" << hasTurnLane.back() << " s=" << hasStraight << " l=" << hasLeft << " r=" << hasRight << " t=" << hasTurnaround << "\n";
     376              :         }
     377              :     }
     378              :     // collect crossings
     379              :     std::vector<NBNode::Crossing*> crossings;
     380         7886 :     for (NBNode* const node : myControlledNodes) {
     381         4232 :         const std::vector<NBNode::Crossing*>& c = node->getCrossings();
     382         4232 :         node->setCrossingTLIndices(getID(), totalNumLinks, onlyConts);
     383         4232 :         totalNumLinks = MAX2(totalNumLinks, maxCrossingIndex(node) + 1);
     384              :         copy(c.begin(), c.end(), std::back_inserter(crossings));
     385         4232 :     }
     386              : 
     387         3654 :     NBTrafficLightLogic* logic = new NBTrafficLightLogic(getID(), getProgramID(), totalNumLinks, myOffset, myType);
     388         3654 :     EdgeVector toProc = getConnectedOuterEdges(incoming);
     389              : 
     390              :     // build all phases
     391              :     std::vector<int> greenPhases; // indices of green phases
     392         3654 :     std::vector<bool> hadGreenMajor(totalNumLinks, false);
     393        10702 :     while (toProc.size() > 0) {
     394              :         bool groupTram = false;
     395              :         bool groupOther = false;
     396         7048 :         std::pair<NBEdge*, NBEdge*> chosen;
     397              :         std::set<const NBEdge*> chosenSet;
     398         7048 :         if (groupOpposites) {
     399         6839 :             if (incoming.size() == 2) {
     400              :                 // if there are only 2 incoming edges we need to decide whether they are a crossing or a "continuation"
     401              :                 // @node: this heuristic could be extended to also check the number of outgoing edges
     402         1239 :                 double angle = fabs(NBHelpers::relAngle(incoming[0]->getAngleAtNode(incoming[0]->getToNode()), incoming[1]->getAngleAtNode(incoming[1]->getToNode())));
     403              :                 // angle would be 180 for straight opposing incoming edges
     404         1239 :                 if (angle < 135) {
     405              :                     chosen = std::pair<NBEdge*, NBEdge*>(toProc[0], static_cast<NBEdge*>(nullptr));
     406              :                     toProc.erase(toProc.begin());
     407              :                 } else {
     408          270 :                     chosen = getBestPair(toProc);
     409              :                 }
     410              :             } else {
     411         5600 :                 chosen = getBestPair(toProc);
     412         5600 :                 if (chosen.second == nullptr && chosen.first->getPermissions() == SVC_TRAM) {
     413              :                     groupTram = true;
     414          152 :                     for (auto it = toProc.begin(); it != toProc.end();) {
     415           77 :                         if ((*it)->getPermissions() == SVC_TRAM) {
     416              :                             it = toProc.erase(it);
     417              :                         } else {
     418              :                             it++;
     419              :                         }
     420              :                     }
     421              :                 }
     422              :             }
     423              :         } else {
     424          209 :             NBEdge* chosenEdge = toProc[0];
     425              :             chosen = std::pair<NBEdge*, NBEdge*>(chosenEdge, static_cast<NBEdge*>(nullptr));
     426              :             toProc.erase(toProc.begin());
     427          209 :             SVCPermissions perms = chosenEdge->getPermissions();
     428          209 :             if (perms == SVC_TRAM) {
     429              :                 groupTram = true;
     430          205 :             } else if ((perms & ~(SVC_PEDESTRIAN | SVC_BICYCLE | SVC_DELIVERY)) == 0) {
     431           36 :                 if (OptionsCont::getOptions().getBool("tls.ignore-internal-junction-jam")) {
     432              :                     // otherwise, we can get a mutual conflict for minor green
     433              :                     // streams which would create deadlock
     434              :                     groupOther = true;
     435              :                 }
     436              :             }
     437              :             // group all edges with the same permissions into a single phase (later)
     438          209 :             if (groupTram || groupOther) {
     439           44 :                 for (auto it = toProc.begin(); it != toProc.end();) {
     440           36 :                     if ((*it)->getPermissions() == perms) {
     441              :                         it = toProc.erase(it);
     442              :                     } else {
     443              :                         it++;
     444              :                     }
     445              :                 }
     446              :             }
     447              :         }
     448              :         int pos = 0;
     449              :         std::string state(totalNumLinks, 'r');
     450              : #ifdef DEBUG_PHASES
     451              :         if (DEBUGCOND) {
     452              :             std::cout << " computing " << getID() << " prog=" << getProgramID() << " cho1=" << Named::getIDSecure(chosen.first) << " cho2=" << Named::getIDSecure(chosen.second) << " toProc=" << toString(toProc) << " bentPrio=" << chosen.first->getToNode()->isBentPriority() << "\n";
     453              :         }
     454              : #endif
     455         7048 :         chosenList.push_back(chosen);
     456              :         chosenSet.insert(chosen.first);
     457         7048 :         if (chosen.second != nullptr) {
     458              :             chosenSet.insert(chosen.second);
     459              :         }
     460              :         // find parallel bike edge for the chosen (passenger) edges
     461        17784 :         for (const NBEdge* e : chosenSet) {
     462        10736 :             if ((e->getPermissions() & SVC_PASSENGER) != 0) {
     463              :                 std::vector<NBEdge*> parallelBikeEdges;
     464        19672 :                 for (NBEdge* cand : toProc) {
     465         9676 :                     if ((cand->getPermissions() & ~SVC_PEDESTRIAN) == SVC_BICYCLE) {
     466          314 :                         double angle = fabs(NBHelpers::relAngle(e->getAngleAtNode(e->getToNode()), cand->getAngleAtNode(cand->getToNode())));
     467          314 :                         if (angle < 30) {
     468              :                             // roughly parallel
     469          109 :                             parallelBikeEdges.push_back(cand);
     470              :                         }
     471              :                     }
     472              :                 }
     473        10105 :                 for (NBEdge* be : parallelBikeEdges) {
     474              : #ifdef DEBUG_PHASES
     475              :                     if (DEBUGCOND) {
     476              :                         std::cout << " chosen=" << e->getID() << " be=" << be->getID() << "\n";
     477              :                     }
     478              : #endif
     479              :                     chosenSet.insert(be);
     480          109 :                     toProc.erase(std::find(toProc.begin(), toProc.end(), be));
     481              :                 }
     482         9996 :             }
     483              :         }
     484              :         // plain straight movers
     485              :         double maxSpeed = 0;
     486              :         bool haveGreen = false;
     487        36630 :         for (const NBEdge* const fromEdge : incoming) {
     488              :             const bool inChosen = chosenSet.count(fromEdge) != 0;
     489              :             const int numLanes = fromEdge->getNumLanes();
     490        87591 :             for (int i2 = 0; i2 < numLanes; i2++) {
     491       149249 :                 for (const NBEdge::Connection& approached : fromEdge->getConnectionsFromLane(i2)) {
     492        91240 :                     if (!fromEdge->mayBeTLSControlled(i2, approached.toEdge, approached.toLane)) {
     493          137 :                         continue;
     494              :                     }
     495        91103 :                     if (inChosen) {
     496        37247 :                         state[pos] = 'G';
     497              :                         haveGreen = true;
     498        37247 :                         maxSpeed = MAX2(maxSpeed, fromEdge->getSpeed());
     499              :                     } else {
     500        53856 :                         state[pos] = 'r';
     501              :                     }
     502        91103 :                     ++pos;
     503        58009 :                 }
     504              :             }
     505              :         }
     506         7048 :         if (!haveGreen) {
     507              :             continue;
     508              :         }
     509              : 
     510              : #ifdef DEBUG_PHASES
     511              :         if (DEBUGCOND) {
     512              :             std::cout << " state after plain straight movers " << state << "\n";
     513              :         }
     514              : #endif
     515         7041 :         if (!isNEMA) {
     516              :             // correct behaviour for those that are not in chosen, but may drive, though
     517         6994 :             state = allowCompatible(state, fromEdges, toEdges, fromLanes, toLanes);
     518              : #ifdef DEBUG_PHASES
     519              :             if (DEBUGCOND) {
     520              :                 std::cout << " state after allowing compatible " << state << "\n";
     521              :             }
     522              : #endif
     523         6994 :             if (groupTram) {
     524          158 :                 state = allowByVClass(state, fromEdges, toEdges, SVC_TRAM);
     525         6915 :             } else if (groupOther) {
     526            8 :                 state = allowByVClass(state, fromEdges, toEdges, SVC_PEDESTRIAN | SVC_BICYCLE | SVC_DELIVERY);
     527              :             }
     528              : #ifdef DEBUG_PHASES
     529              :             if (DEBUGCOND) {
     530              :                 std::cout << " state after grouping by vClass " << state << " (groupTram=" << groupTram << " groupOther=" << groupOther << ")\n";
     531              :             }
     532              : #endif
     533         6994 :             if (groupOpposites || chosen.first->getToNode()->getType() == SumoXMLNodeType::TRAFFIC_LIGHT_RIGHT_ON_RED) {
     534        13582 :                 state = allowUnrelated(state, fromEdges, toEdges, isTurnaround, crossings);
     535              :             }
     536              : #ifdef DEBUG_PHASES
     537              :             if (DEBUGCOND) {
     538              :                 std::cout << " state after finding allowUnrelated " << state << "\n";
     539              :             }
     540              : #endif
     541              :         }
     542              :         // correct behaviour for those that have to wait (mainly left-mover)
     543         7041 :         bool haveForbiddenLeftMover = false;
     544         7041 :         std::vector<bool> rightTurnConflicts(pos, false);
     545         7041 :         std::vector<bool> mergeConflicts(pos, false);
     546         7041 :         state = correctConflicting(state, fromEdges, toEdges, isTurnaround, fromLanes, toLanes, hadGreenMajor, haveForbiddenLeftMover, rightTurnConflicts, mergeConflicts);
     547        98107 :         for (int i1 = 0; i1 < pos; ++i1) {
     548        91066 :             if (state[i1] == 'G') {
     549              :                 hadGreenMajor[i1] = true;
     550              :             }
     551              :         }
     552              : #ifdef DEBUG_PHASES
     553              :         if (DEBUGCOND) {
     554              :             std::cout << " state after correcting left movers=" << state << "\n";
     555              :         }
     556              : #endif
     557              : 
     558         7041 :         std::vector<bool> leftGreen(pos, false);
     559              :         // check whether at least one left-turn lane exist
     560              :         bool foundLeftTurnLane = false;
     561        98107 :         for (int i1 = 0; i1 < pos; ++i1) {
     562        91066 :             if (state[i1] == 'g' && !rightTurnConflicts[i1] && !mergeConflicts[i1] && hasTurnLane[i1]) {
     563              :                 foundLeftTurnLane = true;
     564              :             }
     565              :         }
     566         2832 :         const bool buildLeftGreenPhase = (haveForbiddenLeftMover && !myHaveSinglePhase && leftTurnTime > 0 && foundLeftTurnLane
     567         8143 :                                           && groupOpposites && !groupTram && !groupOther);
     568              : 
     569              :         // find indices for exclusive left green phase and apply option minor-left.max-speed
     570        98107 :         for (int i1 = 0; i1 < pos; ++i1) {
     571        91066 :             if (state[i1] == 'g' && !rightTurnConflicts[i1] && !mergeConflicts[i1]
     572              :                     // only activate turn-around together with a real left-turn
     573       101427 :                     && (!isTurnaround[i1] || (i1 > 0 && leftGreen[i1 - 1]))) {
     574              :                 leftGreen[i1] = true;
     575         9177 :                 if (fromEdges[i1]->getSpeed() > minorLeftSpeedThreshold) {
     576          235 :                     if (buildLeftGreenPhase) {
     577           50 :                         state[i1] = 'r';
     578              :                         //std::cout << " disabling minorLeft " << i1 << " (speed=" << fromEdges[i1]->getSpeed() << " thresh=" << minorLeftSpeedThreshold << ")\n";
     579          185 :                     } else if (!isTurnaround[i1]) {
     580          412 :                         WRITE_WARNINGF(TL("Minor green from edge '%' to edge '%' exceeds %m/s. Maybe a left-turn lane is missing."),
     581              :                                        fromEdges[i1]->getID(), toEdges[i1]->getID(), minorLeftSpeedThreshold);
     582              :                     }
     583              :                 }
     584              :             }
     585              :         }
     586              : 
     587              : #ifdef DEBUG_PHASES
     588              :         if (DEBUGCOND) {
     589              :             std::cout << getID() << " state=" << state << " buildLeft=" << buildLeftGreenPhase << " hFLM=" << haveForbiddenLeftMover << " turnLane=" << foundLeftTurnLane
     590              :                       << "   \nrtC=" << toString(rightTurnConflicts)
     591              :                       << "   \nmC=" << toString(mergeConflicts)
     592              :                       << "   \nhTL=" << toString(hasTurnLane)
     593              :                       << "   \nlGr=" << toString(leftGreen)
     594              :                       << "\n";
     595              :         }
     596              : #endif
     597         7041 :         straightStates.push_back(state);
     598              : 
     599              :         const std::string vehicleState = state; // backup state before pedestrian modifications
     600         7041 :         greenPhases.push_back((int)logic->getPhases().size());
     601              : 
     602              :         // 5s at 50km/h, 10s at 80km/h, rounded to full seconds
     603         7041 :         const double minDurBySpeed = maxSpeed * 3.6 / 6 - 3.3;
     604         7074 :         SUMOTime minDur = MAX2(minMinDur, TIME2STEPS(floor(minDurBySpeed + 0.5)));
     605         7041 :         if (chosen.first->getPermissions() == SVC_TRAM && (chosen.second == nullptr || chosen.second->getPermissions() == SVC_TRAM)) {
     606              :             // shorter minDuration for tram phase (only if the phase is
     607              :             // exclusively for tram)
     608              :             bool tramExclusive = true;
     609         1157 :             for (int i1 = 0; i1 < (int)fromEdges.size(); ++i1) {
     610         1080 :                 if (state[i1] == 'G') {
     611          326 :                     SVCPermissions linkPerm = (fromEdges[i1]->getPermissions() & toEdges[i1]->getPermissions());
     612          326 :                     if (linkPerm != SVC_TRAM) {
     613              :                         tramExclusive = false;
     614              :                         break;
     615              :                     }
     616              :                 }
     617              :             }
     618          191 :             if (tramExclusive) {
     619              :                 // one tram per actuated phase
     620              :                 minDur = TIME2STEPS(1);
     621              :             }
     622              :         }
     623              : 
     624         7041 :         state = addPedestrianPhases(logic, greenTime, minDur, maxDur, earliestEnd, latestEnd, state, crossings, fromEdges, toEdges);
     625              :         // pedestrians have 'r' from here on
     626        10133 :         for (int i1 = pos; i1 < (int)state.size(); ++i1) {
     627         3092 :             state[i1] = 'r';
     628              :         }
     629         7041 :         if (brakingTime > 0) {
     630              :             SUMOTime maxCross = 0;
     631              :             // build yellow (straight)
     632        65789 :             for (int i1 = 0; i1 < pos; ++i1) {
     633        61645 :                 if (state[i1] != 'G' && state[i1] != 'g') {
     634        33947 :                     continue;
     635              :                 }
     636         8672 :                 if ((vehicleState[i1] >= 'a' && vehicleState[i1] <= 'z')
     637         8672 :                         && buildLeftGreenPhase
     638         3357 :                         && !rightTurnConflicts[i1]
     639         3254 :                         && !mergeConflicts[i1]
     640        30926 :                         && leftGreen[i1]) {
     641         3103 :                     continue;
     642              :                 }
     643        24595 :                 state[i1] = 'y';
     644        24595 :                 maxCross = MAX2(maxCross, crossingTime[i1]);
     645              :             }
     646              :             // add step
     647         4144 :             logic->addStep(brakingTime, state);
     648              :             // add optional all-red state
     649         4144 :             if (!buildLeftGreenPhase) {
     650         3346 :                 if (myLayout == TrafficLightLayout::ALTERNATE_ONEWAY) {
     651           24 :                     allRedTime = computeEscapeTime(state, fromEdges, toEdges);
     652              :                 }
     653         3346 :                 buildAllRedState(allRedTime + MAX2(0ll, maxCross - brakingTime - allRedTime), logic, state);
     654              :             }
     655              :         }
     656              : 
     657              : 
     658         7041 :         if (buildLeftGreenPhase) {
     659              :             // build left green
     660        20676 :             for (int i1 = 0; i1 < pos; ++i1) {
     661        19585 :                 if (state[i1] == 'Y' || state[i1] == 'y') {
     662         4416 :                     state[i1] = 'r';
     663         4416 :                     continue;
     664              :                 }
     665        15169 :                 if (leftGreen[i1]) {
     666         4106 :                     state[i1] = 'G';
     667              :                 }
     668              :             }
     669         1091 :             leftStates.push_back(state);
     670         2182 :             state = allowCompatible(state, fromEdges, toEdges, fromLanes, toLanes);
     671         1091 :             state = correctConflicting(state, fromEdges, toEdges, isTurnaround, fromLanes, toLanes, hadGreenMajor, haveForbiddenLeftMover, rightTurnConflicts, mergeConflicts);
     672         1091 :             bool buildMixedGreenPhase = false;
     673         1091 :             std::vector<bool> mixedGreen(pos, false);
     674              :             const std::string oldState = state;
     675         1091 :             if (noMixed) {
     676           20 :                 state = correctMixed(state, fromEdges, fromLanes, buildMixedGreenPhase, mixedGreen);
     677              :             }
     678         1091 :             if (state != oldState) {
     679          120 :                 for (int i1 = 0; i1 < pos; ++i1) {
     680          112 :                     if (mixedGreen[i1]) {
     681              :                         // patch previous yellow and allred phase
     682            8 :                         int yellowIndex = (int)logic->getPhases().size() - 1;
     683            8 :                         if (allRedTime > 0) {
     684            0 :                             logic->setPhaseState(yellowIndex--, i1, LINKSTATE_TL_RED);
     685              :                         }
     686            8 :                         if (brakingTime > 0) {
     687            8 :                             logic->setPhaseState(yellowIndex, i1, LINKSTATE_TL_YELLOW_MINOR);
     688              :                         }
     689              :                     }
     690              :                 }
     691           16 :                 state = allowCompatible(state, fromEdges, toEdges, fromLanes, toLanes);
     692              :             }
     693              : 
     694              :             // add step
     695         2182 :             logic->addStep(leftTurnTime, state, minDur, maxDur, earliestEnd, latestEnd);
     696              : 
     697              :             // build left yellow
     698         1091 :             if (brakingTime > 0) {
     699              :                 SUMOTime maxCross = 0;
     700        16011 :                 for (int i1 = 0; i1 < pos; ++i1) {
     701        15213 :                     if (state[i1] != 'G' && state[i1] != 'g') {
     702        11727 :                         continue;
     703              :                     }
     704         3486 :                     state[i1] = 'y';
     705         3486 :                     maxCross = MAX2(maxCross, crossingTime[i1]);
     706              :                 }
     707              :                 // add step
     708          798 :                 logic->addStep(brakingTime, state);
     709              :                 // add optional all-red state
     710          798 :                 buildAllRedState(allRedTime + MAX2(0ll, maxCross - brakingTime - allRedTime), logic, state);
     711              :             }
     712              : 
     713         1091 :             if (buildMixedGreenPhase) {
     714              :                 // build mixed green
     715              :                 // @todo if there is no left green phase we might want to build two
     716              :                 // mixed-green phases but then we should consider avoid a common
     717              :                 // opposite phase for this direction
     718              : 
     719           68 :                 for (int i1 = 0; i1 < pos; ++i1) {
     720           64 :                     if (state[i1] == 'Y' || state[i1] == 'y') {
     721           16 :                         state[i1] = 'r';
     722           16 :                         continue;
     723              :                     }
     724           48 :                     if (mixedGreen[i1]) {
     725            4 :                         state[i1] = 'G';
     726              :                     }
     727              :                 }
     728            8 :                 state = allowCompatible(state, fromEdges, toEdges, fromLanes, toLanes);
     729            8 :                 state = correctConflicting(state, fromEdges, toEdges, isTurnaround, fromLanes, toLanes, hadGreenMajor, haveForbiddenLeftMover, rightTurnConflicts, mergeConflicts);
     730              : 
     731              :                 // add step
     732            8 :                 logic->addStep(leftTurnTime, state, minDur, maxDur, earliestEnd, latestEnd);
     733              : 
     734              :                 // build mixed yellow
     735            4 :                 if (brakingTime > 0) {
     736              :                     SUMOTime maxCross = 0;
     737           68 :                     for (int i1 = 0; i1 < pos; ++i1) {
     738           64 :                         if (state[i1] != 'G' && state[i1] != 'g') {
     739           48 :                             continue;
     740              :                         }
     741           16 :                         state[i1] = 'y';
     742           16 :                         maxCross = MAX2(maxCross, crossingTime[i1]);
     743              :                     }
     744              :                     // add step
     745            4 :                     logic->addStep(brakingTime, state);
     746              :                     // add optional all-red state
     747            4 :                     buildAllRedState(allRedTime + MAX2(0ll, maxCross - brakingTime - allRedTime), logic, state);
     748              :                 }
     749              :             }
     750              : 
     751         5950 :         } else if (isNEMA) {
     752              :             std::string& s = straightStates.back();
     753              :             std::string leftState = s;
     754          211 :             for (int ii = 0; ii < pos; ++ii) {
     755          192 :                 if (s[ii] != 'r') {
     756           88 :                     NBEdge* fromEdge = fromEdges[ii];
     757           88 :                     NBEdge* toEdge = toEdges[ii];
     758           88 :                     LinkDirection dir = fromEdge->getToNode()->getDirection(fromEdge, toEdge);
     759           88 :                     if (hasTurnLane[ii] && (dir == LinkDirection::LEFT || dir == LinkDirection::TURN)) {
     760           13 :                         s[ii] = 'r';
     761           13 :                         leftState[ii] = 'G';
     762              :                     } else {
     763           75 :                         leftState[ii] = 'r';
     764              :                     }
     765              :                 }
     766              :             }
     767           19 :             leftStates.push_back(leftState);
     768              :         }
     769              :         // fix edges within joined traffic lights that did not get the green light yet
     770         7041 :         if (myEdgesWithin.size() > 0 && !isNEMA && toProc.size() == 0 && !onlyConts) {
     771          114 :             addGreenWithin(logic, fromEdges, toProc);
     772              :         }
     773              :     }
     774              :     // fix pedestrian crossings that did not get the green light yet
     775         3654 :     if (crossings.size() > 0) {
     776          324 :         addPedestrianScramble(logic, totalNumLinks, TIME2STEPS(10), brakingTime, crossings, fromEdges, toEdges);
     777              :     }
     778              :     // add optional red phase if there were no foes
     779         1336 :     if (logic->getPhases().size() == 2 && brakingTime > 0
     780         5645 :             && OptionsCont::getOptions().getInt("tls.red.time") > 0) {
     781          655 :         const SUMOTime redTime = TIME2STEPS(OptionsCont::getOptions().getInt("tls.red.time"));
     782         1965 :         logic->addStep(redTime, std::string(totalNumLinks, 'r'));
     783              :     }
     784              : 
     785         3654 :     if (myLayout == TrafficLightLayout::ALTERNATE_ONEWAY) {
     786              :         // exiting the oneway section should always be possible
     787           11 :         deactivateInsideEdges(logic, fromEdges);
     788              :     }
     789         3654 :     if (isNEMA) {
     790           24 :         NBTrafficLightLogic* nemaLogic = buildNemaPhases(fromEdges, toEdges, crossings, chosenList, straightStates, leftStates);
     791           24 :         if (nemaLogic == nullptr) {
     792            6 :             WRITE_WARNINGF(TL("Generating NEMA phases is not supported for traffic light '%' with % incoming edges. Using tlType 'actuated' as fallback"), getID(), incoming.size());
     793              :             logic->setType(TrafficLightType::ACTUATED);
     794            2 :             setType(TrafficLightType::ACTUATED);
     795              :         } else {
     796           22 :             delete logic;
     797              :             logic = nemaLogic;
     798              :         }
     799              :     }
     800              : 
     801         3654 :     SUMOTime totalDuration = logic->getDuration();
     802              : 
     803         3680 :     if ((OptionsCont::getOptions().isDefault("tls.green.time") || !OptionsCont::getOptions().isDefault("tls.cycle.time")) && !isNEMA) {
     804         3628 :         const SUMOTime cycleTime = TIME2STEPS(OptionsCont::getOptions().getInt("tls.cycle.time"));
     805              :         // adapt to cycle time by changing the duration of the green phases
     806              :         SUMOTime minGreenDuration = SUMOTime_MAX;
     807        10614 :         for (std::vector<int>::const_iterator it = greenPhases.begin(); it != greenPhases.end(); ++it) {
     808         6986 :             const SUMOTime dur = logic->getPhases()[*it].duration;
     809              :             minGreenDuration = MIN2(minGreenDuration, dur);
     810              :         }
     811         3628 :         const int patchSeconds = (int)(STEPS2TIME(cycleTime - totalDuration) / (double)greenPhases.size());
     812         3628 :         const int patchSecondsRest = (int)(STEPS2TIME(cycleTime - totalDuration)) - patchSeconds * (int)greenPhases.size();
     813              :         //std::cout << "cT=" << cycleTime << " td=" << totalDuration << " pS=" << patchSeconds << " pSR=" << patchSecondsRest << "\n";
     814         3628 :         if (STEPS2TIME(minGreenDuration) + patchSeconds < MIN_GREEN_TIME
     815         3623 :                 || STEPS2TIME(minGreenDuration) + patchSeconds + patchSecondsRest < MIN_GREEN_TIME
     816         7245 :                 || greenPhases.size() == 0) {
     817           11 :             if (getID() != DummyID) {
     818           30 :                 WRITE_WARNINGF(TL("The traffic light '%' cannot be adapted to a cycle time of %."), getID(), time2string(cycleTime));
     819              :             }
     820              :             // @todo use a multiple of cycleTime ?
     821              :         } else {
     822        10511 :             for (std::vector<int>::const_iterator it = greenPhases.begin(); it != greenPhases.end(); ++it) {
     823         8651 :                 logic->setPhaseDuration(*it, logic->getPhases()[*it].duration + TIME2STEPS(patchSeconds));
     824              :             }
     825         3617 :             if (greenPhases.size() > 0) {
     826         3725 :                 logic->setPhaseDuration(greenPhases.front(), logic->getPhases()[greenPhases.front()].duration + TIME2STEPS(patchSecondsRest));
     827              :             }
     828         3617 :             totalDuration = logic->getDuration();
     829              :         }
     830              :     }
     831              : 
     832              :     // check for coherent signal sequence and remove yellow if preceded and followed by green
     833              :     const std::vector<NBTrafficLightLogic::PhaseDefinition>& allPhases = logic->getPhases();
     834         3654 :     const int phaseCount = (int)allPhases.size();
     835              :     const int stateSize = (int)logic->getNumLinks();
     836        18380 :     for (int i = 0; i < phaseCount; ++i) {
     837        14726 :         std::string currState = allPhases[i].state;
     838        14726 :         const int prevIndex = (i == 0) ? phaseCount - 1 : i - 1;
     839        14726 :         const std::string prevState = allPhases[prevIndex].state;
     840        14726 :         const std::string nextState = allPhases[(i + 1) % phaseCount].state;
     841              :         bool updatedState = false;
     842       230386 :         for (int i1 = 0; i1 < stateSize; ++i1) {
     843       215660 :             if (currState[i1] == 'y' && (nextState[i1] == prevState[i1] || nextState[i1] == 'G') && (prevState[i1] == 'g' || prevState[i1] == 'G')) {
     844         1960 :                 logic->setPhaseState(i, i1, (LinkState)prevState[i1]);
     845              :                 updatedState = true;
     846              :             }
     847              :         }
     848              :         UNUSED_PARAMETER(updatedState);  // disable warning
     849              : #ifdef DEBUG_PHASES
     850              :         if (DEBUGCOND) {
     851              :             if (updatedState) {
     852              :                 std::cout << getID() << " state of phase index " << i <<  " was patched due to yellow in between green\n";
     853              :             }
     854              : 
     855              :         }
     856              : #endif
     857              :     }
     858              : 
     859              : 
     860         3654 :     myExtraConflictsReady = true;
     861              :     // this computation only makes sense for single nodes
     862         3654 :     if (myControlledNodes.size() == 1) {
     863         3323 :         myNeedsContRelationReady = true;
     864              :     }
     865         3654 :     if (totalDuration > 0) {
     866         3654 :         if (totalDuration > 3 * (greenTime + 2 * brakingTime + leftTurnTime) && !isNEMA && getID() != DummyID) {
     867           36 :             WRITE_WARNINGF(TL("The traffic light '%' has a high cycle time of %."), getID(), time2string(totalDuration));
     868              :         }
     869         3654 :         logic->closeBuilding();
     870              :         return logic;
     871              :     } else {
     872            0 :         delete logic;
     873            0 :         return nullptr;
     874              :     }
     875         7308 : }
     876              : 
     877              : 
     878              : bool
     879         3919 : NBOwnTLDef::hasCrossing(const NBEdge* from, const NBEdge* to, const std::vector<NBNode::Crossing*>& crossings) {
     880              :     assert(to != 0);
     881         5534 :     for (auto c : crossings) {
     882              :         const NBNode::Crossing& cross = *c;
     883              :         // only check connections at this crossings node
     884         2214 :         if (to->getFromNode() == cross.node) {
     885         4182 :             for (EdgeVector::const_iterator it_e = cross.edges.begin(); it_e != cross.edges.end(); ++it_e) {
     886         2949 :                 const NBEdge* edge = *it_e;
     887         2949 :                 if (edge == from || edge == to) {
     888              :                     return true;
     889              :                 }
     890              :             }
     891              :         }
     892              :     }
     893              :     return false;
     894              : }
     895              : 
     896              : 
     897              : std::string
     898         7161 : NBOwnTLDef::addPedestrianPhases(NBTrafficLightLogic* logic, const SUMOTime greenTime, const SUMOTime minDur, const SUMOTime maxDur,
     899              :                                 const SUMOTime earliestEnd, const SUMOTime latestEnd,
     900              :                                 std::string state, const std::vector<NBNode::Crossing*>& crossings, const EdgeVector& fromEdges, const EdgeVector& toEdges) {
     901              :     // compute based on length of the crossing if not set by the user
     902         7161 :     const SUMOTime pedClearingTime = TIME2STEPS(OptionsCont::getOptions().getInt("tls.crossing-clearance.time"));
     903              :     // compute if not set by user: must be able to reach the middle of the second "Richtungsfahrbahn"
     904         7161 :     const SUMOTime minPedTime = TIME2STEPS(OptionsCont::getOptions().getInt("tls.crossing-min.time"));
     905              :     const std::string orig = state;
     906         7161 :     state = patchStateForCrossings(state, crossings, fromEdges, toEdges);
     907         7161 :     if (orig == state) {
     908              :         // add step
     909        12966 :         logic->addStep(greenTime, state, minDur, maxDur, earliestEnd, latestEnd);
     910              :     } else {
     911          678 :         const SUMOTime pedTime = greenTime - pedClearingTime;
     912          678 :         if (pedTime >= minPedTime) {
     913              :             // ensure clearing time for pedestrians
     914              :             const bool isSimpleActuatedCrossing = logic->getType() == TrafficLightType::ACTUATED
     915          655 :                                                   && minDur == UNSPECIFIED_DURATION && logic->getPhases().size() == 2;
     916              :             if (isSimpleActuatedCrossing) {
     917              :                 // permit green phase to extend when there are no pedestrians
     918           19 :                 logic->setPhaseNext(0, {0, 1});
     919              :             }
     920         1310 :             logic->addStep(pedTime, state, minDur, maxDur, earliestEnd, latestEnd);
     921              : #ifdef DEBUG_PHASES
     922              :             if (DEBUGCOND2(logic)) {
     923              :                 std::cout << " intermidate state for addPedestrianPhases " << state << "\n";
     924              :             }
     925              : #endif
     926         3659 :             for (auto cross : crossings) {
     927         3004 :                 if (cross->tlLinkIndex >= (int)fromEdges.size() || fromEdges[cross->tlLinkIndex] == nullptr) {
     928         2954 :                     state[cross->tlLinkIndex] = 'r';
     929              :                 }
     930         3004 :                 if (cross->tlLinkIndex2 >= 0 && (cross->tlLinkIndex2 >= (int)fromEdges.size() || fromEdges[cross->tlLinkIndex2] == nullptr)) {
     931           64 :                     state[cross->tlLinkIndex2] = 'r';
     932              :                 }
     933              :             }
     934          655 :             logic->addStep(pedClearingTime, state);
     935              :         } else {
     936              :             state = orig;
     937              :             // not safe for pedestrians.
     938           46 :             logic->addStep(greenTime, state, minDur, maxDur, earliestEnd, latestEnd);
     939              :         }
     940              :     }
     941              : #ifdef DEBUG_PHASES
     942              :     if (DEBUGCOND2(logic)) {
     943              :         std::cout << " state after addPedestrianPhases " << state << "\n";
     944              :     }
     945              : #endif
     946        14322 :     return state;
     947              : }
     948              : 
     949              : 
     950              : std::string
     951         7161 : NBOwnTLDef::patchStateForCrossings(const std::string& state, const std::vector<NBNode::Crossing*>& crossings, const EdgeVector& fromEdges, const EdgeVector& toEdges) {
     952              :     std::string result = state;
     953        10440 :     for (const NBNode::Crossing* cross : crossings) {
     954              :         bool isForbidden = false;
     955        47430 :         for (int i2 = 0; i2 < (int)fromEdges.size() && !isForbidden; ++i2) {
     956              :             // only check connections at this crossings node
     957        44151 :             if (fromEdges[i2] != 0 && toEdges[i2] != 0 && fromEdges[i2]->getToNode() == cross->node) {
     958        99104 :                 for (EdgeVector::const_iterator it = cross->edges.begin(); it != cross->edges.end(); ++it) {
     959        62059 :                     const NBEdge* edge = *it;
     960        62059 :                     const LinkDirection i2dir = cross->node->getDirection(fromEdges[i2], toEdges[i2]);
     961        62059 :                     if (state[i2] != 'r' && state[i2] != 's' && (edge == fromEdges[i2] ||
     962        20254 :                             (edge == toEdges[i2] && (i2dir == LinkDirection::STRAIGHT || i2dir == LinkDirection::PARTLEFT || i2dir == LinkDirection::PARTRIGHT)))) {
     963              :                         isForbidden = true;
     964              :                         break;
     965              :                     }
     966              :                 }
     967              :             }
     968              :         }
     969         3279 :         const int i1 = cross->tlLinkIndex;
     970              :         assert(i1 >= 0 && i1 < (int)result.size());
     971         3279 :         const char newState = isForbidden ? 'r' : 'G';
     972         3279 :         if (i1 < (int)toEdges.size() && toEdges[i1] != nullptr && (result[i1] != newState || !isForbidden)) {
     973           70 :             if (cross->tlID != DummyID) {
     974           54 :                 WRITE_WARNINGF(TL("Custom crossing linkIndex % conflicts with vehicular connections at tlLogic '%'"), i1, cross->tlID);
     975              :             }
     976              :         } else {
     977         3209 :             result[i1] = newState;
     978              :         }
     979         3279 :         if (cross->tlLinkIndex2 >= 0) {
     980              :             const int i2 = cross->tlLinkIndex2;
     981           82 :             if (i2 < (int)toEdges.size() && toEdges[i2] != nullptr && (result[i2] != newState || !isForbidden)) {
     982           13 :                 if (cross->tlID != DummyID) {
     983           24 :                     WRITE_WARNINGF(TL("Custom crossing linkIndex2 % conflicts with vehicular connections at tlLogic '%'"), i2, cross->tlID);
     984              :                 }
     985              :             } else {
     986           69 :                 result[i2] = newState;
     987              :             }
     988              :         }
     989              :     }
     990              : 
     991              :     // correct behaviour for roads that are in conflict with a pedestrian crossing
     992        99444 :     for (int i1 = 0; i1 < (int)fromEdges.size(); ++i1) {
     993        92283 :         if (result[i1] == 'G') {
     994        45667 :             for (const NBNode::Crossing* cross : crossings) {
     995        14648 :                 const int i2 = cross->tlLinkIndex;
     996        14648 :                 const int i3 = cross->tlLinkIndex2;
     997        14648 :                 if (fromEdges[i1] != 0 && toEdges[i1] != 0 && fromEdges[i1]->getToNode() == cross->node) {
     998        12898 :                     if ((result[i2] == 'G' || (i3 >= 0 && result[i3] == 'G'))
     999        12914 :                             && cross->node->mustBrakeForCrossing(fromEdges[i1], toEdges[i1], *cross)) {
    1000         1003 :                         result[i1] = 'g';
    1001         1003 :                         break;
    1002              :                     }
    1003              :                 }
    1004              :             }
    1005              :         }
    1006              :     }
    1007         7161 :     return result;
    1008              : }
    1009              : 
    1010              : 
    1011              : std::string
    1012           78 : NBOwnTLDef::patchNEMAStateForCrossings(const std::string& state,
    1013              :                                        const std::vector<NBNode::Crossing*>& crossings,
    1014              :                                        const EdgeVector& fromEdges,
    1015              :                                        const EdgeVector& toEdges,
    1016              :                                        const NBEdge* greenEdge, NBEdge* otherChosen) {
    1017              :     std::string result = state;
    1018           78 :     const int pos = (int)(state.size() - crossings.size()); // number of controlled vehicle links
    1019           78 :     const EdgeVector& all = greenEdge->getToNode()->getEdges();
    1020           78 :     EdgeVector::const_iterator start = std::find(all.begin(), all.end(), greenEdge);
    1021              : 
    1022              :     // permit crossings over edges between the current green edge and it's straight continuation
    1023           78 :     const NBEdge* endEdge = nullptr;
    1024          621 :     for (int i = 0; i < (int)state.size(); i++) {
    1025          609 :         if (state[i] == 'G' && fromEdges[i] == greenEdge
    1026          752 :                 && greenEdge->getToNode()->getDirection(greenEdge, toEdges[i]) == LinkDirection::STRAIGHT) {
    1027              :             // straight edge found
    1028           66 :             endEdge = toEdges[i];
    1029           66 :             break;
    1030              :         }
    1031              :     }
    1032           78 :     if (endEdge == nullptr) {
    1033           12 :         endEdge = otherChosen;
    1034              :     }
    1035           78 :     if (endEdge == nullptr) {
    1036              :         // try to find the reverse edge of the green edge
    1037            8 :         auto itCW = start;
    1038            8 :         NBContHelper::nextCW(all, itCW);
    1039            8 :         if ((*itCW)->getFromNode() == greenEdge->getToNode()) {
    1040            8 :             endEdge = *itCW;
    1041              :         }
    1042              :     }
    1043           78 :     if (endEdge == nullptr) {
    1044              :         // at least prevent an infinite loop
    1045            0 :         endEdge = greenEdge;
    1046              :     }
    1047              :     //std::cout << " patchNEMAStateForCrossings green=" << greenEdge->getID() << " other=" << Named::getIDSecure(otherChosen) << " end=" << Named::getIDSecure(end) << " all=" << toString(all) << "\n";
    1048              : 
    1049           78 :     EdgeVector::const_iterator end = std::find(all.begin(), all.end(), endEdge);
    1050           78 :     if (end == all.end()) {
    1051              :         // at least prevent an infinite loop
    1052            0 :         end = start;
    1053              :     }
    1054           78 :     auto it = start;
    1055           78 :     NBContHelper::nextCCW(all, it);
    1056          236 :     for (; it != end; NBContHelper::nextCCW(all, it)) {
    1057          258 :         for (int ic = 0; ic < (int)crossings.size(); ++ic) {
    1058          100 :             const int i1 = pos + ic;
    1059          100 :             const NBNode::Crossing& cross = *crossings[ic];
    1060          258 :             for (const NBEdge* crossed : cross.edges) {
    1061              :                 //std::cout << "   cand=" << (*it)->getID() << " crossed=" << crossed->getID() << "\n";
    1062          186 :                 if (crossed == *it) {
    1063           28 :                     result[i1] = 'G';
    1064           28 :                     break;
    1065              :                 }
    1066              :             }
    1067              :         }
    1068              :     }
    1069              :     // correct behaviour for roads that are in conflict with a pedestrian crossing
    1070         1174 :     for (int i1 = 0; i1 < pos; ++i1) {
    1071         1096 :         if (result[i1] == 'G') {
    1072          216 :             for (int ic = 0; ic < (int)crossings.size(); ++ic) {
    1073           72 :                 const NBNode::Crossing& crossing = *crossings[ic];
    1074           72 :                 const int i2 = pos + ic;
    1075           72 :                 if (result[i2] == 'G' && crossing.node->mustBrakeForCrossing(fromEdges[i1], toEdges[i1], crossing)) {
    1076           12 :                     result[i1] = 'g';
    1077           12 :                     break;
    1078              :                 }
    1079              :             }
    1080              :         }
    1081              :     }
    1082           78 :     return result;
    1083              : }
    1084              : 
    1085              : 
    1086              : void
    1087         5607 : NBOwnTLDef::collectLinks() {
    1088              :     myControlledLinks.clear();
    1089         5607 :     collectAllLinks(myControlledLinks);
    1090         5607 : }
    1091              : 
    1092              : 
    1093              : void
    1094         4144 : NBOwnTLDef::setTLControllingInformation() const {
    1095              :     // set the information about the link's positions within the tl into the
    1096              :     //  edges the links are starting at, respectively
    1097        21993 :     for (NBConnectionVector::const_iterator j = myControlledLinks.begin(); j != myControlledLinks.end(); ++j) {
    1098              :         const NBConnection& conn = *j;
    1099        17849 :         NBEdge* edge = conn.getFrom();
    1100        17849 :         edge->setControllingTLInformation(conn, getID());
    1101              :     }
    1102         4144 : }
    1103              : 
    1104              : 
    1105              : void
    1106            0 : NBOwnTLDef::remapRemoved(NBEdge* /*removed*/, const EdgeVector& /*incoming*/,
    1107            0 :                          const EdgeVector& /*outgoing*/) {}
    1108              : 
    1109              : 
    1110              : void
    1111         4186 : NBOwnTLDef::replaceRemoved(NBEdge* /*removed*/, int /*removedLane*/,
    1112         4186 :                            NBEdge* /*by*/, int /*byLane*/, bool /*incoming*/) {}
    1113              : 
    1114              : 
    1115              : void
    1116          286 : NBOwnTLDef::initNeedsContRelation() const {
    1117          286 :     if (!myNeedsContRelationReady) {
    1118          241 :         if (myControlledNodes.size() > 0) {
    1119              :             // we use a dummy node just to maintain const-correctness
    1120              :             myNeedsContRelation.clear();
    1121          907 :             for (NBNode* n : myControlledNodes) {
    1122          666 :                 NBOwnTLDef dummy(DummyID, n, 0, TrafficLightType::STATIC);
    1123          666 :                 dummy.setParticipantsInformation();
    1124          666 :                 NBTrafficLightLogic* tllDummy = dummy.computeLogicAndConts(0, true);
    1125          666 :                 delete tllDummy;
    1126          666 :                 myNeedsContRelation.insert(dummy.myNeedsContRelation.begin(), dummy.myNeedsContRelation.end());
    1127          666 :                 n->removeTrafficLight(&dummy);
    1128          666 :             }
    1129              : #ifdef DEBUG_CONTRELATION
    1130              :             if (DEBUGCOND) {
    1131              :                 std::cout << " contRelations at " << getID() << " prog=" << getProgramID() << ":\n";
    1132              :                 for (const StreamPair& s : myNeedsContRelation) {
    1133              :                     std::cout << "   " << s.from1->getID() << "->" << s.to1->getID() << " foe " << s.from2->getID() << "->" << s.to2->getID() << "\n";
    1134              :                 }
    1135              :             }
    1136              : #endif
    1137              : 
    1138              :         }
    1139          241 :         myNeedsContRelationReady = true;
    1140              :     }
    1141          286 : }
    1142              : 
    1143              : 
    1144              : EdgeVector
    1145         3654 : NBOwnTLDef::getConnectedOuterEdges(const EdgeVector& incoming) {
    1146         3654 :     EdgeVector result = incoming;
    1147        16036 :     for (EdgeVector::iterator it = result.begin(); it != result.end();) {
    1148        12382 :         if ((*it)->getConnections().size() == 0 || (*it)->isInsideTLS()) {
    1149              :             it = result.erase(it);
    1150              :         } else {
    1151              :             ++it;
    1152              :         }
    1153              :     }
    1154         3654 :     return result;
    1155              : }
    1156              : 
    1157              : 
    1158              : std::string
    1159         8097 : NBOwnTLDef::allowCompatible(std::string state, const EdgeVector& fromEdges, const EdgeVector& toEdges,
    1160              :                             const std::vector<int>& fromLanes, const std::vector<int>& toLanes) {
    1161        16194 :     state = allowSingleEdge(state, fromEdges);
    1162              : #ifdef DEBUG_PHASES
    1163              :     if (DEBUGCOND) {
    1164              :         std::cout << " state after allowSingle " << state << "\n";
    1165              :     }
    1166              : #endif
    1167         8097 :     if (myControlledNodes.size() > 1) {
    1168         2046 :         state = allowFollowers(state, fromEdges, toEdges);
    1169              : #ifdef DEBUG_PHASES
    1170              :         if (DEBUGCOND) {
    1171              :             std::cout << " state after allowFollowers " << state << "\n";
    1172              :         }
    1173              : #endif
    1174         2046 :         state = allowPredecessors(state, fromEdges, toEdges, fromLanes, toLanes);
    1175              : #ifdef DEBUG_PHASES
    1176              :         if (DEBUGCOND) {
    1177              :             std::cout << " state after allowPredecessors " << state << "\n";
    1178              :         }
    1179              : #endif
    1180              :     }
    1181         8097 :     return state;
    1182              : }
    1183              : 
    1184              : 
    1185              : std::string
    1186         8097 : NBOwnTLDef::allowSingleEdge(std::string state, const EdgeVector& fromEdges) {
    1187              :     // if only one edge has green, ensure sure that all connections from that edge are green
    1188         8097 :     const int size = (int)fromEdges.size();
    1189              :     NBEdge* greenEdge = nullptr;
    1190        90096 :     for (int i1 = 0; i1 < size; ++i1) {
    1191        86553 :         if (state[i1] == 'G') {
    1192        30181 :             if (greenEdge == nullptr) {
    1193         8097 :                 greenEdge = fromEdges[i1];
    1194        22084 :             } else if (greenEdge != fromEdges[i1]) {
    1195         4554 :                 return state;
    1196              :             }
    1197              :         }
    1198              :     }
    1199         3543 :     if (greenEdge != nullptr) {
    1200        37860 :         for (int i1 = 0; i1 < size; ++i1) {
    1201        34317 :             if (fromEdges[i1] == greenEdge) {
    1202        10136 :                 state[i1] = 'G';
    1203              :             }
    1204              :         }
    1205              :     }
    1206         3543 :     return state;
    1207              : }
    1208              : 
    1209              : 
    1210              : std::string
    1211         1023 : NBOwnTLDef::allowFollowers(std::string state, const EdgeVector& fromEdges, const EdgeVector& toEdges) {
    1212              :     // check continuation within joined traffic lights
    1213              :     bool check = true;
    1214         2627 :     while (check) {
    1215              :         check = false;
    1216        37040 :         for (int i1 = 0; i1 < (int)fromEdges.size(); ++i1) {
    1217        35436 :             if (state[i1] == 'G') {
    1218         9785 :                 continue;
    1219              :             }
    1220        25651 :             if (forbidden(state, i1, fromEdges, toEdges, true)) {
    1221        10210 :                 continue;
    1222              :             }
    1223              :             bool followsChosen = false;
    1224       602671 :             for (int i2 = 0; i2 < (int)fromEdges.size(); ++i2) {
    1225       589311 :                 if (state[i2] == 'G' && fromEdges[i1] == toEdges[i2]) {
    1226              :                     followsChosen = true;
    1227              :                     break;
    1228              :                 }
    1229              :             }
    1230        15441 :             if (followsChosen) {
    1231         2081 :                 state[i1] = 'G';
    1232              :                 check = true;
    1233              :             }
    1234              :         }
    1235              :     }
    1236         1023 :     return state;
    1237              : }
    1238              : 
    1239              : 
    1240              : std::string
    1241         1023 : NBOwnTLDef::allowPredecessors(std::string state, const EdgeVector& fromEdges, const EdgeVector& toEdges,
    1242              :                               const std::vector<int>& fromLanes, const std::vector<int>& toLanes) {
    1243              :     // also allow predecessors of chosen edges if the lanes match and there is no conflict
    1244              :     // (must be done after the followers are done because followers are less specific)
    1245              :     bool check = true;
    1246         2059 :     while (check) {
    1247              :         check = false;
    1248        23027 :         for (int i1 = 0; i1 < (int)fromEdges.size(); ++i1) {
    1249        21991 :             if (state[i1] == 'G') {
    1250         6842 :                 continue;
    1251              :             }
    1252        15149 :             if (forbidden(state, i1, fromEdges, toEdges, false)) {
    1253         8300 :                 continue;
    1254              :             }
    1255              :             bool preceedsChosen = false;
    1256       284629 :             for (int i2 = 0; i2 < (int)fromEdges.size(); ++i2) {
    1257       277798 :                 if (state[i2] == 'G' && fromEdges[i2] == toEdges[i1]
    1258       277877 :                         && fromLanes[i2] == toLanes[i1]) {
    1259              :                     preceedsChosen = true;
    1260              :                     break;
    1261              :                 }
    1262              :             }
    1263         6849 :             if (preceedsChosen) {
    1264           18 :                 state[i1] = 'G';
    1265              :                 check = true;
    1266              :             }
    1267              :         }
    1268              :     }
    1269         1023 :     return state;
    1270              : }
    1271              : 
    1272              : 
    1273              : std::string
    1274         6791 : NBOwnTLDef::allowUnrelated(std::string state, const EdgeVector& fromEdges, const EdgeVector& toEdges,
    1275              :                            const std::vector<bool>& isTurnaround,
    1276              :                            const std::vector<NBNode::Crossing*>& crossings) {
    1277        90490 :     for (int i1 = 0; i1 < (int)fromEdges.size(); ++i1) {
    1278        83699 :         if (state[i1] == 'G') {
    1279        37916 :             continue;
    1280              :         }
    1281              :         bool isForbidden = false;
    1282       369400 :         for (int i2 = 0; i2 < (int)fromEdges.size(); ++i2) {
    1283       500602 :             if (state[i2] == 'G' && !isTurnaround[i2] &&
    1284       246996 :                     (forbids(fromEdges[i2], toEdges[i2], fromEdges[i1], toEdges[i1], true) || forbids(fromEdges[i1], toEdges[i1], fromEdges[i2], toEdges[i2], true))) {
    1285              :                 isForbidden = true;
    1286              :                 break;
    1287              :             }
    1288              :         }
    1289        45783 :         if (!isForbidden && !hasCrossing(fromEdges[i1], toEdges[i1], crossings)) {
    1290         3320 :             state[i1] = 'G';
    1291              :         }
    1292              :     }
    1293         6791 :     return state;
    1294              : }
    1295              : 
    1296              : 
    1297              : std::string
    1298           83 : NBOwnTLDef::allowByVClass(std::string state, const EdgeVector& fromEdges, const EdgeVector& toEdges, SVCPermissions perm) {
    1299         1182 :     for (int i1 = 0; i1 < (int)fromEdges.size(); ++i1) {
    1300         1099 :         SVCPermissions linkPerm = (fromEdges[i1]->getPermissions() & toEdges[i1]->getPermissions());
    1301         1099 :         if ((linkPerm & ~perm) == 0) {
    1302          317 :             state[i1] = 'G';
    1303              :         }
    1304              :     }
    1305           83 :     return state;
    1306              : }
    1307              : 
    1308              : 
    1309              : bool
    1310        40800 : NBOwnTLDef::forbidden(const std::string& state, int index, const EdgeVector& fromEdges, const EdgeVector& toEdges, bool allowCont) {
    1311      1187767 :     for (int i2 = 0; i2 < (int)fromEdges.size(); ++i2) {
    1312      1165477 :         if (state[i2] == 'G' && foes(fromEdges[i2], toEdges[i2], fromEdges[index], toEdges[index])) {
    1313        40452 :             if (!allowCont || (
    1314        28949 :                         !needsCont(fromEdges[i2], toEdges[i2], fromEdges[index], toEdges[index]) &&
    1315        12873 :                         !needsCont(fromEdges[index], toEdges[index], fromEdges[i2], toEdges[i2]))) {
    1316        18510 :                 return true;
    1317              :             }
    1318              :         }
    1319              :     }
    1320              :     return false;
    1321              : }
    1322              : 
    1323              : 
    1324              : std::string
    1325         8136 : NBOwnTLDef::correctConflicting(std::string state, const EdgeVector& fromEdges, const EdgeVector& toEdges,
    1326              :                                const std::vector<bool>& isTurnaround,
    1327              :                                const std::vector<int>& fromLanes,
    1328              :                                const std::vector<int>& toLanes,
    1329              :                                const std::vector<bool>& hadGreenMajor,
    1330              :                                bool& haveForbiddenLeftMover,
    1331              :                                std::vector<bool>& rightTurnConflicts,
    1332              :                                std::vector<bool>& mergeConflicts) {
    1333         8136 :     const bool controlledWithin = !OptionsCont::getOptions().getBool("tls.uncontrolled-within");
    1334       118851 :     for (int i1 = 0; i1 < (int)fromEdges.size(); ++i1) {
    1335       110715 :         if (state[i1] == 'G') {
    1336       929779 :             for (int i2 = 0; i2 < (int)fromEdges.size(); ++i2) {
    1337       881052 :                 if ((state[i2] == 'G' || state[i2] == 'g')) {
    1338       422337 :                     if (NBNode::rightTurnConflict(
    1339              :                                 fromEdges[i1], toEdges[i1], fromLanes[i1], fromEdges[i2], toEdges[i2], fromLanes[i2])) {
    1340              :                         rightTurnConflicts[i1] = true;
    1341              :                     }
    1342       422337 :                     if (forbids(fromEdges[i2], toEdges[i2], fromEdges[i1], toEdges[i1], true, controlledWithin) || rightTurnConflicts[i1]) {
    1343        31244 :                         state[i1] = 'g';
    1344        31244 :                         if (myControlledNodes.size() == 1) {
    1345        28315 :                             myNeedsContRelation.insert(StreamPair(fromEdges[i1], toEdges[i1], fromEdges[i2], toEdges[i2]));
    1346              : #ifdef DEBUG_CONTRELATION
    1347              :                             if (DEBUGCOND) {
    1348              :                                 std::cout << getID() << " p=" << getProgramID() << " contRel: " << fromEdges[i1]->getID() << "->" << toEdges[i1]->getID()
    1349              :                                           << " foe " << fromEdges[i2]->getID() << "->" << toEdges[i2]->getID() << "\n";
    1350              :                             }
    1351              : #endif
    1352              :                         }
    1353        31244 :                         if (!isTurnaround[i1] && !hadGreenMajor[i1] && !rightTurnConflicts[i1]) {
    1354        18869 :                             haveForbiddenLeftMover = true;
    1355              :                         }
    1356       391093 :                     } else if (fromEdges[i1] == fromEdges[i2]
    1357       184814 :                                && fromLanes[i1] != fromLanes[i2]
    1358        79634 :                                && toEdges[i1] == toEdges[i2]
    1359        22493 :                                && toLanes[i1] == toLanes[i2]
    1360       391399 :                                && fromEdges[i1]->getToNode()->mergeConflictYields(fromEdges[i1], fromLanes[i1], fromLanes[i2], toEdges[i1], toLanes[i1])) {
    1361              :                         mergeConflicts[i1] = true;
    1362          148 :                         state[i1] = 'g';
    1363              :                     }
    1364              :                 }
    1365              :             }
    1366              :         }
    1367       110715 :         if (state[i1] == 'r') {
    1368        62170 :             if (fromEdges[i1]->getToNode()->getType() == SumoXMLNodeType::TRAFFIC_LIGHT_RIGHT_ON_RED &&
    1369          261 :                     fromEdges[i1]->getToNode()->getDirection(fromEdges[i1], toEdges[i1]) == LinkDirection::RIGHT) {
    1370           56 :                 state[i1] = 's';
    1371              :                 // do not allow right-on-red when in conflict with exclusive left-turn phase
    1372          824 :                 for (int i2 = 0; i2 < (int)fromEdges.size(); ++i2) {
    1373         1046 :                     if (state[i2] == 'G' && !isTurnaround[i2] &&
    1374          499 :                             (forbids(fromEdges[i2], toEdges[i2], fromEdges[i1], toEdges[i1], true) ||
    1375          233 :                              forbids(fromEdges[i1], toEdges[i1], fromEdges[i2], toEdges[i2], true))) {
    1376           74 :                         const LinkDirection foeDir = fromEdges[i2]->getToNode()->getDirection(fromEdges[i2], toEdges[i2]);
    1377           74 :                         if (foeDir == LinkDirection::LEFT || foeDir == LinkDirection::PARTLEFT) {
    1378           12 :                             state[i1] = 'r';
    1379           12 :                             break;
    1380              :                         }
    1381              :                     }
    1382              :                 }
    1383           56 :                 if (state[i1] == 's') {
    1384              :                     // handle right-on-red conflicts
    1385          716 :                     for (int i2 = 0; i2 < (int)fromEdges.size(); ++i2) {
    1386          913 :                         if (state[i2] == 'G' && !isTurnaround[i2] &&
    1387          449 :                                 (forbids(fromEdges[i2], toEdges[i2], fromEdges[i1], toEdges[i1], true) ||
    1388          208 :                                  forbids(fromEdges[i1], toEdges[i1], fromEdges[i2], toEdges[i2], true))) {
    1389           62 :                             myExtraConflicts.insert(std::make_pair(i1, i2));
    1390              :                         }
    1391              :                     }
    1392              :                 }
    1393              :             }
    1394              :         }
    1395              :     }
    1396         8136 :     return state;
    1397              : }
    1398              : 
    1399              : 
    1400              : std::string
    1401           10 : NBOwnTLDef::correctMixed(std::string state, const EdgeVector& fromEdges,
    1402              :                          const std::vector<int>& fromLanes,
    1403              :                          bool& buildMixedGreenPhase, std::vector<bool>& mixedGreen) {
    1404          152 :     for (int i1 = 0; i1 < (int)fromEdges.size(); ++i1) {
    1405          142 :         if ((state[i1] == 'G' || state[i1] == 'g')) {
    1406          368 :             for (int i2 = 0; i2 < (int)fromEdges.size(); ++i2) {
    1407          320 :                 if (i1 != i2 && fromEdges[i1] == fromEdges[i2] && fromLanes[i1] == fromLanes[i2]
    1408          360 :                         && state[i2] != 'G' && state[i2] != 'g') {
    1409           12 :                     state[i1] = state[i2];
    1410              :                     //std::cout << " mixedGreen i1=" << i1 << " i2=" << i2 << "\n";
    1411              :                     mixedGreen[i1] = true;
    1412           12 :                     if (fromEdges[i1]->getNumLanesThatAllow(SVC_PASSENGER) > 1) {
    1413            4 :                         buildMixedGreenPhase = true;
    1414              :                     }
    1415              :                 }
    1416              :             }
    1417              :         }
    1418              :     }
    1419           10 :     return state;
    1420              : }
    1421              : 
    1422              : 
    1423              : void
    1424          114 : NBOwnTLDef::addGreenWithin(NBTrafficLightLogic* logic, const EdgeVector& fromEdges, EdgeVector& toProc) {
    1425          114 :     std::vector<bool> foundGreen(fromEdges.size(), false);
    1426         1109 :     for (const auto& phase : logic->getPhases()) {
    1427              :         const std::string state = phase.state;
    1428        27092 :         for (int j = 0; j < (int)fromEdges.size(); j++) {
    1429        26097 :             LinkState ls = (LinkState)state[j];
    1430        26097 :             if (ls == LINKSTATE_TL_GREEN_MAJOR || ls == LINKSTATE_TL_GREEN_MINOR) {
    1431              :                 foundGreen[j] = true;
    1432              :             }
    1433              :         }
    1434              :     }
    1435         4942 :     for (int j = 0; j < (int)foundGreen.size(); j++) {
    1436         2414 :         if (!foundGreen[j]) {
    1437            4 :             NBEdge* e = fromEdges[j];
    1438            4 :             if (std::find(toProc.begin(), toProc.end(), e) == toProc.end()) {
    1439            3 :                 toProc.push_back(e);
    1440              :             }
    1441              :         }
    1442              :     }
    1443          114 : }
    1444              : 
    1445              : 
    1446              : void
    1447          330 : NBOwnTLDef::addPedestrianScramble(NBTrafficLightLogic* logic, int totalNumLinks, SUMOTime /* greenTime */, SUMOTime brakingTime,
    1448              :                                   const std::vector<NBNode::Crossing*>& crossings, const EdgeVector& fromEdges, const EdgeVector& toEdges) {
    1449              :     // check both indices for each crossing (they may have green in different phases)
    1450          330 :     std::vector<bool> foundGreen(crossings.size() * 2, false);
    1451              :     const std::vector<NBTrafficLightLogic::PhaseDefinition>& phases = logic->getPhases();
    1452         2374 :     for (int i = 0; i < (int)phases.size(); i++) {
    1453         2044 :         const std::string state = phases[i].state;
    1454              :         int j = 0;
    1455        11618 :         for (auto cross : crossings) {
    1456         9574 :             LinkState ls = (LinkState)state[cross->tlLinkIndex];
    1457         9574 :             LinkState ls2 = cross->tlLinkIndex2 >= 0 ? (LinkState)state[cross->tlLinkIndex2] : ls;
    1458         9574 :             if (ls == LINKSTATE_TL_GREEN_MAJOR || ls == LINKSTATE_TL_GREEN_MINOR) {
    1459              :                 foundGreen[j] = true;
    1460              :             }
    1461         9574 :             if (ls2 == LINKSTATE_TL_GREEN_MAJOR || ls2 == LINKSTATE_TL_GREEN_MINOR) {
    1462         1657 :                 foundGreen[j + crossings.size()] = true;
    1463              :             }
    1464         9574 :             j++;
    1465              :         }
    1466              :     }
    1467              : #ifdef DEBUG_PHASES
    1468              :     if (DEBUGCOND2(logic)) {
    1469              :         std::cout << " foundCrossingGreen=" << toString(foundGreen) << "\n";
    1470              :     }
    1471              : #endif
    1472         4606 :     for (int j = 0; j < (int)foundGreen.size(); j++) {
    1473         2214 :         if (!foundGreen[j]) {
    1474              :             // add a phase where all pedestrians may walk, (preceded by a yellow phase and followed by a clearing phase)
    1475           76 :             if (phases.size() > 0) {
    1476              :                 bool needYellowPhase = false;
    1477              :                 std::string state = phases.back().state;
    1478          671 :                 for (int i1 = 0; i1 < (int)fromEdges.size(); ++i1) {
    1479          595 :                     if (state[i1] == 'G' || state[i1] == 'g') {
    1480           99 :                         state[i1] = 'y';
    1481              :                         needYellowPhase = true;
    1482              :                     }
    1483              :                 }
    1484              :                 // add yellow step
    1485           76 :                 if (needYellowPhase && brakingTime > 0) {
    1486            0 :                     logic->addStep(brakingTime, state);
    1487              :                 }
    1488              :             }
    1489           76 :             const SUMOTime pedClearingTime = TIME2STEPS(OptionsCont::getOptions().getInt("tls.crossing-clearance.time"));
    1490           76 :             const SUMOTime scrambleTime = TIME2STEPS(OptionsCont::getOptions().getInt("tls.scramble.time"));
    1491           76 :             addPedestrianPhases(logic, scrambleTime + pedClearingTime, UNSPECIFIED_DURATION,
    1492           76 :                                 UNSPECIFIED_DURATION, UNSPECIFIED_DURATION, UNSPECIFIED_DURATION, std::string(totalNumLinks, 'r'), crossings, fromEdges, toEdges);
    1493           76 :             break;
    1494              :         }
    1495              :     }
    1496          330 : }
    1497              : 
    1498              : 
    1499              : void
    1500         4148 : NBOwnTLDef::buildAllRedState(SUMOTime allRedTime, NBTrafficLightLogic* logic, const std::string& state) {
    1501         4148 :     if (allRedTime > 0) {
    1502              :         // build all-red phase
    1503              :         std::string allRedState = state;
    1504         6116 :         for (int i = 0; i < (int)state.size(); i++) {
    1505         5833 :             if (allRedState[i] == 'Y' || allRedState[i] == 'y') {
    1506         1692 :                 allRedState[i] = 'r';
    1507              :             }
    1508              :         }
    1509          283 :         logic->addStep(TIME2STEPS(ceil(STEPS2TIME(allRedTime))), allRedState);
    1510              :     }
    1511         4148 : }
    1512              : 
    1513              : 
    1514              : int
    1515         4232 : NBOwnTLDef::maxCrossingIndex(const NBNode* node) const {
    1516              :     int result = 0;
    1517         5459 :     for (auto crossing : node->getCrossings()) {
    1518         1227 :         result = MAX2(result, crossing->tlLinkIndex);
    1519         1227 :         result = MAX2(result, crossing->tlLinkIndex2);
    1520         4232 :     }
    1521         4232 :     return result;
    1522              : }
    1523              : 
    1524              : void
    1525            0 : NBOwnTLDef::fixSuperfluousYellow(NBTrafficLightLogic* logic) const {
    1526              :     // assume that yellow states last at most one phase
    1527              :     const int n = logic->getNumLinks();
    1528            0 :     const int p = (int)logic->getPhases().size();
    1529            0 :     for (int i1 = 0; i1 < n; ++i1) {
    1530            0 :         LinkState prev = (LinkState)logic->getPhases().back().state[i1];
    1531            0 :         for (int i2 = 0; i2 < p; ++i2) {
    1532            0 :             LinkState cur = (LinkState)logic->getPhases()[i2].state[i1];
    1533            0 :             LinkState next = (LinkState)logic->getPhases()[(i2 + 1) % p].state[i1];
    1534            0 :             if (cur == LINKSTATE_TL_YELLOW_MINOR
    1535            0 :                     && (prev == LINKSTATE_TL_GREEN_MAJOR || prev == LINKSTATE_TL_YELLOW_MINOR)
    1536            0 :                     && next == LINKSTATE_TL_GREEN_MAJOR) {
    1537            0 :                 logic->setPhaseState(i2, i1, prev);
    1538              :             }
    1539              :             prev = cur;
    1540              :         }
    1541              :     }
    1542            0 : }
    1543              : 
    1544              : 
    1545              : void
    1546            0 : NBOwnTLDef::deactivateAlwaysGreen(NBTrafficLightLogic* logic) const {
    1547              :     const int n = logic->getNumLinks();
    1548            0 :     std::vector<bool> alwaysGreen(n, true);
    1549            0 :     for (int i1 = 0; i1 < n; ++i1) {
    1550            0 :         for (const auto& phase : logic->getPhases()) {
    1551            0 :             if (phase.state[i1] != 'G') {
    1552              :                 alwaysGreen[i1] = false;
    1553            0 :                 break;
    1554              :             }
    1555              :         }
    1556              :     }
    1557            0 :     const int p = (int)logic->getPhases().size();
    1558            0 :     for (int i1 = 0; i1 < n; ++i1) {
    1559            0 :         if (alwaysGreen[i1]) {
    1560            0 :             for (int i2 = 0; i2 < p; ++i2) {
    1561            0 :                 logic->setPhaseState(i2, i1, LINKSTATE_TL_OFF_NOSIGNAL);
    1562              :             }
    1563              :         }
    1564              :     }
    1565            0 : }
    1566              : 
    1567              : 
    1568              : void
    1569           11 : NBOwnTLDef::deactivateInsideEdges(NBTrafficLightLogic* logic, const EdgeVector& fromEdges) const {
    1570           11 :     const int n = (int)fromEdges.size();
    1571           11 :     const int p = (int)logic->getPhases().size();
    1572           89 :     for (int i1 = 0; i1 < n; ++i1) {
    1573           78 :         if (fromEdges[i1]->isInsideTLS()) {
    1574          470 :             for (int i2 = 0; i2 < p; ++i2) {
    1575          428 :                 logic->setPhaseState(i2, i1, LINKSTATE_TL_OFF_NOSIGNAL);
    1576              :             }
    1577              :         }
    1578              :     }
    1579           11 : }
    1580              : 
    1581              : 
    1582              : SUMOTime
    1583           24 : NBOwnTLDef::computeEscapeTime(const std::string& state, const EdgeVector& fromEdges, const EdgeVector& toEdges) const {
    1584           24 :     const int n = (int)fromEdges.size();
    1585              :     double maxTime = 0;
    1586          232 :     for (int i1 = 0; i1 < n; ++i1) {
    1587          208 :         if (state[i1] == 'y' && !fromEdges[i1]->isInsideTLS()) {
    1588          344 :             for (int i2 = 0; i2 < n; ++i2) {
    1589          312 :                 if (fromEdges[i2]->isInsideTLS()) {
    1590          180 :                     double gapSpeed = (toEdges[i1]->getSpeed() + fromEdges[i2]->getSpeed()) / 2;
    1591          180 :                     double time = fromEdges[i1]->getGeometry().back().distanceTo2D(fromEdges[i2]->getGeometry().back()) / gapSpeed;
    1592              :                     maxTime = MAX2(maxTime, time);
    1593              :                 }
    1594              :             }
    1595              :         }
    1596              :     }
    1597              :     // give some slack
    1598           24 :     return TIME2STEPS(floor(maxTime * 1.2 + 5));
    1599              : }
    1600              : 
    1601              : 
    1602              : int
    1603            0 : NBOwnTLDef::getMaxIndex() {
    1604            0 :     setParticipantsInformation();
    1605            0 :     NBTrafficLightLogic* logic = compute(OptionsCont::getOptions());
    1606            0 :     if (logic != nullptr) {
    1607            0 :         return logic->getNumLinks() - 1;
    1608              :     } else {
    1609              :         return -1;
    1610              :     }
    1611              : }
    1612              : 
    1613              : 
    1614              : bool
    1615          133 : NBOwnTLDef::corridorLike() const {
    1616          133 :     if (getID() == DummyID) {
    1617              :         // avoid infinite recursion
    1618              :         return true;
    1619              :     }
    1620              :     // setParticipantsInformation resets myAmInTLS so we need to make a copy
    1621              :     std::vector<bool> edgeInsideTLS;
    1622          441 :     for (const NBEdge* e : myIncomingEdges) {
    1623          399 :         edgeInsideTLS.push_back(e->isInsideTLS());
    1624              :     }
    1625              :     assert(myControlledNodes.size() >= 2);
    1626           42 :     NBOwnTLDef dummy(DummyID, myControlledNodes, 0, TrafficLightType::STATIC);
    1627           42 :     dummy.setParticipantsInformation();
    1628           42 :     NBTrafficLightLogic* tllDummy = dummy.computeLogicAndConts(0, true);
    1629              :     int greenPhases = 0;
    1630          162 :     for (const auto& phase : tllDummy->getPhases()) {
    1631          120 :         if (phase.state.find_first_of("gG") != std::string::npos) {
    1632          120 :             greenPhases++;
    1633              :         }
    1634              :     }
    1635           42 :     delete tllDummy;
    1636          212 :     for (const auto& controlledNode : myControlledNodes) {
    1637          170 :         controlledNode->removeTrafficLight(&dummy);
    1638              :     }
    1639              :     int i = 0;
    1640          441 :     for (NBEdge* e : myIncomingEdges) {
    1641              :         e->setInsideTLS(edgeInsideTLS[i]);
    1642          399 :         i++;
    1643              :     }
    1644           42 :     return greenPhases <= 2;
    1645           42 : }
    1646              : 
    1647              : 
    1648              : NBTrafficLightLogic*
    1649           24 : NBOwnTLDef::buildNemaPhases(
    1650              :     const EdgeVector& fromEdges,
    1651              :     const EdgeVector& toEdges,
    1652              :     const std::vector<NBNode::Crossing*>& crossings,
    1653              :     const std::vector<std::pair<NBEdge*, NBEdge*> >& chosenList,
    1654              :     const std::vector<std::string>& straightStates,
    1655              :     const std::vector<std::string>& leftStates) {
    1656           24 :     if (chosenList.size() != 2) {
    1657              :         return nullptr;
    1658              :     }
    1659           23 :     const SUMOTime dur = TIME2STEPS(OptionsCont::getOptions().getInt("tls.cycle.time"));
    1660           23 :     const SUMOTime vehExt = TIME2STEPS(OptionsCont::getOptions().getInt("tls.nema.vehExt"));
    1661           23 :     const SUMOTime yellow = TIME2STEPS(OptionsCont::getOptions().getInt("tls.nema.yellow"));
    1662           23 :     const SUMOTime red = TIME2STEPS(OptionsCont::getOptions().getInt("tls.nema.red"));
    1663           23 :     const SUMOTime minMinDur = TIME2STEPS(OptionsCont::getOptions().getInt("tls.min-dur"));
    1664           23 :     const SUMOTime maxDur = TIME2STEPS(OptionsCont::getOptions().getInt("tls.max-dur"));
    1665           23 :     const SUMOTime earliestEnd = UNSPECIFIED_DURATION;
    1666              :     const SUMOTime latestEnd = UNSPECIFIED_DURATION;
    1667              : 
    1668           23 :     const int totalNumLinks = (int)straightStates[0].size();
    1669           23 :     NBTrafficLightLogic* logic = new NBTrafficLightLogic(getID(), getProgramID(), totalNumLinks, myOffset, myType);
    1670           23 :     std::vector<int> ring1({1, 2, 3, 4});
    1671           23 :     std::vector<int> ring2({5, 6, 7, 8});
    1672           23 :     std::vector<int> barrier1({4, 8});
    1673           23 :     std::vector<int> barrier2({2, 6});
    1674           23 :     int phaseNameLeft = 1;
    1675           67 :     for (int i = 0; i < (int)chosenList.size(); i++) {
    1676           45 :         NBEdge* e1 = chosenList[i].first;
    1677              :         assert(e1 != nullptr);
    1678           45 :         NBEdge* e2 = chosenList[i].second;
    1679           45 :         if (i < (int)leftStates.size()) {
    1680           90 :             std::string left1 = filterState(leftStates[i], fromEdges, e1);
    1681           45 :             if (left1 != "") {
    1682           70 :                 logic->addStep(dur, left1, minMinDur, maxDur, earliestEnd, latestEnd, vehExt, yellow, red, toString(phaseNameLeft));
    1683              :             }
    1684              :         }
    1685           45 :         if (e2 != nullptr) {
    1686           34 :             std::string straight2 = filterState(straightStates[i], fromEdges, e2);
    1687           68 :             straight2 = patchNEMAStateForCrossings(straight2, crossings, fromEdges, toEdges, e2, e1);
    1688              : 
    1689           68 :             logic->addStep(dur, straight2, minMinDur, maxDur, earliestEnd, latestEnd, vehExt, yellow, red, toString(phaseNameLeft + 1));
    1690           34 :             if (i < (int)leftStates.size()) {
    1691           68 :                 std::string left2 = filterState(leftStates[i], fromEdges, e2);
    1692           34 :                 if (left2 != "") {
    1693           44 :                     logic->addStep(dur, left2, minMinDur, maxDur, earliestEnd, latestEnd, vehExt, yellow, red, toString(phaseNameLeft + 4));
    1694              :                 }
    1695              :             }
    1696              : 
    1697              :         }
    1698           45 :         std::string straight1 = filterState(straightStates[i], fromEdges, e1);
    1699           45 :         if (straight1 == "") {
    1700            1 :             delete logic;
    1701              :             return nullptr;
    1702              :         }
    1703           88 :         straight1 = patchNEMAStateForCrossings(straight1, crossings, fromEdges, toEdges, e1, e2);
    1704           88 :         logic->addStep(dur, straight1, minMinDur, maxDur, earliestEnd, latestEnd, vehExt, yellow, red, toString(phaseNameLeft + 5));
    1705           44 :         phaseNameLeft += 2;
    1706              :     }
    1707              :     std::map<int, int> names; // nema phase name -> sumo phase index
    1708          156 :     for (int i = 0; i < (int)logic->getPhases().size(); i++) {
    1709          134 :         names[StringUtils::toInt(logic->getPhases()[i].name)] = i;
    1710              :     }
    1711              : 
    1712           22 :     filterMissingNames(ring1, names, false);
    1713           22 :     filterMissingNames(ring2, names, false);
    1714           22 :     filterMissingNames(barrier1, names, true, 8);
    1715           22 :     filterMissingNames(barrier2, names, true, 6);
    1716           22 :     if (ring1[0] == 0 && ring1[1] == 0) {
    1717            1 :         ring1[1] = 6;
    1718              :     }
    1719           22 :     if (ring1[2] == 0 && ring1[3] == 0) {
    1720            3 :         ring1[3] = 8;
    1721              :     }
    1722           22 :     fixDurationSum(logic, names, ring1[0], ring1[1], ring2[0], ring2[1]);
    1723           22 :     fixDurationSum(logic, names, ring1[2], ring1[3], ring2[2], ring2[3]);
    1724              : 
    1725           44 :     logic->setParameter("ring1", joinToString(ring1, ","));
    1726           44 :     logic->setParameter("ring2", joinToString(ring2, ","));
    1727           44 :     logic->setParameter("barrierPhases", joinToString(barrier1, ","));
    1728           44 :     logic->setParameter("barrier2Phases", joinToString(barrier2, ","));
    1729              :     return logic;
    1730           23 : }
    1731              : 
    1732              : 
    1733              : std::string
    1734          158 : NBOwnTLDef::filterState(std::string state, const EdgeVector& fromEdges, const NBEdge* e) {
    1735              :     bool haveGreen = false;
    1736         2354 :     for (int j = 0; j < (int)fromEdges.size(); j++) {
    1737         2196 :         if (fromEdges[j] != e) {
    1738         1602 :             state[j] = 'r';
    1739          594 :         } else if (state[j] != 'r') {
    1740              :             haveGreen = true;
    1741              :         }
    1742              :     }
    1743          158 :     if (haveGreen) {
    1744          135 :         return state;
    1745              :     } else {
    1746           23 :         return "";
    1747              :     }
    1748              : }
    1749              : 
    1750              : void
    1751           88 : NBOwnTLDef::filterMissingNames(std::vector<int>& vec, const std::map<int, int>& names, bool isBarrier, int barrierDefault) {
    1752          352 :     for (int i = 0; i < (int)vec.size(); i++) {
    1753          264 :         if (names.count(vec[i]) == 0) {
    1754           52 :             if (isBarrier) {
    1755           10 :                 if (names.count(vec[i] - 1) > 0) {
    1756            6 :                     vec[i] = vec[i] - 1;
    1757              :                 } else {
    1758            4 :                     vec[i] = barrierDefault;
    1759              :                 }
    1760              :             } else {
    1761           42 :                 vec[i] = 0;
    1762              :             }
    1763              :         }
    1764              :     }
    1765           88 : }
    1766              : 
    1767              : void
    1768           44 : NBOwnTLDef::fixDurationSum(NBTrafficLightLogic* logic, const std::map<int, int>& names, int ring1a, int ring1b, int ring2a, int ring2b) {
    1769              :     std::set<int> ring1existing;
    1770              :     std::set<int> ring2existing;
    1771              :     if (names.count(ring1a) != 0) {
    1772              :         ring1existing.insert(ring1a);
    1773              :     }
    1774              :     if (names.count(ring1b) != 0) {
    1775              :         ring1existing.insert(ring1b);
    1776              :     }
    1777              :     if (names.count(ring2a) != 0) {
    1778              :         ring2existing.insert(ring2a);
    1779              :     }
    1780              :     if (names.count(ring2b) != 0) {
    1781              :         ring2existing.insert(ring2b);
    1782              :     }
    1783           44 :     if (ring1existing.size() > 0 && ring2existing.size() > 0 &&
    1784              :             ring1existing.size() != ring2existing.size()) {
    1785              :         int pI; // sumo phase index
    1786            6 :         if (ring1existing.size() < ring2existing.size()) {
    1787            0 :             pI = names.find(*ring1existing.begin())->second;
    1788              :         } else {
    1789            6 :             pI = names.find(*ring2existing.begin())->second;
    1790              :         }
    1791            6 :         const auto& p = logic->getPhases()[pI];
    1792            6 :         SUMOTime newMaxDur = 2 * p.maxDur + p.yellow + p.red;
    1793            6 :         logic->setPhaseMaxDuration(pI, newMaxDur);
    1794              :     }
    1795           44 : }
    1796              : 
    1797              : /****************************************************************************/
        

Generated by: LCOV version 2.0-1