LCOV - code coverage report
Current view: top level - src/router - RORouteDef.cpp (source / functions) Coverage Total Hit
Test: lcov.info Lines: 97.8 % 229 224
Test Date: 2025-12-06 15:35:27 Functions: 100.0 % 16 16

            Line data    Source code
       1              : /****************************************************************************/
       2              : // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
       3              : // Copyright (C) 2002-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    RORouteDef.cpp
      15              : /// @author  Daniel Krajzewicz
      16              : /// @author  Michael Behrisch
      17              : /// @author  Jakob Erdmann
      18              : /// @date    Sept 2002
      19              : ///
      20              : // Base class for a vehicle's route definition
      21              : /****************************************************************************/
      22              : #include <config.h>
      23              : 
      24              : #include <string>
      25              : #include <iterator>
      26              : #include <algorithm>
      27              : #include <utils/common/StringUtils.h>
      28              : #include <utils/common/ToString.h>
      29              : #include <utils/common/Named.h>
      30              : #include <utils/common/StringUtils.h>
      31              : #include <utils/common/MsgHandler.h>
      32              : #include <utils/common/RandHelper.h>
      33              : #include <utils/iodevices/OutputDevice.h>
      34              : #include <utils/options/OptionsCont.h>
      35              : #include "ROEdge.h"
      36              : #include "RORoute.h"
      37              : #include <utils/router/SUMOAbstractRouter.h>
      38              : #include <utils/router/RouteCostCalculator.h>
      39              : #include "RORouteDef.h"
      40              : #include "ROVehicle.h"
      41              : 
      42              : // ===========================================================================
      43              : // static members
      44              : // ===========================================================================
      45              : bool RORouteDef::myUsingJTRR(false);
      46              : bool RORouteDef::mySkipNewRoutes(false);
      47              : 
      48              : // ===========================================================================
      49              : // method definitions
      50              : // ===========================================================================
      51       301685 : RORouteDef::RORouteDef(const std::string& id, const int lastUsed,
      52       301685 :                        const bool tryRepair, const bool mayBeDisconnected) :
      53       301685 :     Named(StringUtils::convertUmlaute(id)),
      54       301685 :     myPrecomputed(nullptr), myLastUsed(lastUsed), myTryRepair(tryRepair),
      55       301685 :     myMayBeDisconnected(mayBeDisconnected),
      56       301685 :     myDiscardSilent(false) {
      57       301685 : }
      58              : 
      59              : 
      60       603370 : RORouteDef::~RORouteDef() {
      61       760147 :     for (std::vector<RORoute*>::iterator i = myAlternatives.begin(); i != myAlternatives.end(); i++) {
      62              :         if (myRouteRefs.count(*i) == 0) {
      63       458458 :             delete *i;
      64              :         }
      65              :     }
      66       603370 : }
      67              : 
      68              : 
      69              : void
      70       443568 : RORouteDef::addLoadedAlternative(RORoute* alt) {
      71       443568 :     myAlternatives.push_back(alt);
      72       443568 : }
      73              : 
      74              : 
      75              : void
      76            4 : RORouteDef::addAlternativeDef(const RORouteDef* alt) {
      77              :     std::copy(alt->myAlternatives.begin(), alt->myAlternatives.end(),
      78            4 :               back_inserter(myAlternatives));
      79              :     std::copy(alt->myAlternatives.begin(), alt->myAlternatives.end(),
      80            4 :               std::inserter(myRouteRefs, myRouteRefs.end()));
      81            4 : }
      82              : 
      83              : 
      84              : RORoute*
      85       296646 : RORouteDef::buildCurrentRoute(SUMOAbstractRouter<ROEdge, ROVehicle>& router,
      86              :                               SUMOTime begin, const ROVehicle& veh) const {
      87       296646 :     if (myPrecomputed == nullptr) {
      88       296646 :         preComputeCurrentRoute(router, begin, veh);
      89              :     }
      90       296646 :     return myPrecomputed;
      91              : }
      92              : 
      93              : 
      94              : void
      95       296649 : RORouteDef::validateAlternatives(const ROVehicle* veh, MsgHandler* errorHandler) {
      96       735824 :     for (int i = 0; i < (int)myAlternatives.size();) {
      97       439175 :         if (i != myLastUsed || mySkipNewRoutes) {
      98       143437 :             if (myAlternatives[i]->isPermitted(veh, errorHandler)) {
      99       143432 :                 i++;
     100              :             } else {
     101            5 :                 myAlternatives.erase(myAlternatives.begin() + i); 
     102            5 :                 if (myLastUsed > i) {
     103            0 :                     myLastUsed--;
     104              :                 }
     105              :             }
     106              :         } else {
     107       295738 :             i++;
     108              :         }
     109              :     }
     110       296649 : }
     111              : 
     112              : 
     113              : void
     114       296646 : RORouteDef::preComputeCurrentRoute(SUMOAbstractRouter<ROEdge, ROVehicle>& router,
     115              :                                    SUMOTime begin, const ROVehicle& veh) const {
     116       296646 :     myNewRoute = false;
     117       296646 :     const OptionsCont& oc = OptionsCont::getOptions();
     118       296646 :     const bool ignoreErrors = oc.getBool("ignore-errors");
     119       296646 :     const bool hasRestrictions = RONet::getInstance()->hasParamRestrictions();
     120              :     assert(myAlternatives[0]->getEdgeVector().size() > 0);
     121       296646 :     MsgHandler* mh = ignoreErrors ? MsgHandler::getWarningInstance() : MsgHandler::getErrorInstance();
     122       296728 :     if (myAlternatives[0]->getFirst()->prohibits(&veh, hasRestrictions) && (!oc.getBool("repair.from")
     123              :             // do not try to reassign starting edge for trip input
     124            9 :             || myMayBeDisconnected || myAlternatives[0]->getEdgeVector().size() < 2)) {
     125           70 :         mh->inform("Vehicle '" + veh.getID() + "' is not allowed to depart on edge '" +
     126           35 :                    myAlternatives[0]->getFirst()->getID() + "'.");
     127           35 :         return;
     128       296653 :     } else if (myAlternatives[0]->getLast()->prohibits(&veh, hasRestrictions) && (!oc.getBool("repair.to")
     129              :                // do not try to reassign destination edge for trip input
     130            9 :                || myMayBeDisconnected || myAlternatives[0]->getEdgeVector().size() < 2)) {
     131              :         // this check is not strictly necessary unless myTryRepair is set.
     132              :         // However, the error message is more helpful than "no connection found"
     133           30 :         mh->inform("Vehicle '" + veh.getID() + "' is not allowed to arrive on edge '" +
     134           15 :                    myAlternatives[0]->getLast()->getID() + "'.");
     135           15 :         return;
     136              :     }
     137       975294 :     const bool skipTripRouting = (oc.exists("write-trips") && oc.getBool("write-trips")
     138       388294 :                                   && RouteCostCalculator<RORoute, ROEdge, ROVehicle>::getCalculator().skipRouteCalculation());
     139       296596 :     if ((myTryRepair && !skipTripRouting) || myUsingJTRR) {
     140              :         ConstROEdgeVector newEdges;
     141       202182 :         if (repairCurrentRoute(router, begin, veh, myAlternatives[0]->getEdgeVector(), newEdges)) {
     142       185043 :             if (myAlternatives[0]->getEdgeVector() != newEdges) {
     143       156673 :                 if (!myMayBeDisconnected) {
     144          120 :                     WRITE_WARNINGF(TL("Repaired route of vehicle '%'."), veh.getID());
     145              :                 }
     146       156673 :                 myNewRoute = true;
     147       156673 :                 RGBColor* col = myAlternatives[0]->getColor() != nullptr ? new RGBColor(*myAlternatives[0]->getColor()) : nullptr;
     148       156673 :                 myPrecomputed = new RORoute(myID, 0, myAlternatives[0]->getProbability(), newEdges, col, myAlternatives[0]->getStops());
     149              :             } else {
     150        28370 :                 myPrecomputed = myAlternatives[0];
     151              :             }
     152              :         }
     153              :         return;
     154       202182 :     }
     155        94414 :     if ((RouteCostCalculator<RORoute, ROEdge, ROVehicle>::getCalculator().skipRouteCalculation()
     156       187980 :             || OptionsCont::getOptions().getBool("remove-loops"))
     157        94470 :             && (skipTripRouting || myAlternatives[myLastUsed]->isValid(veh, ignoreErrors))) {
     158          956 :         myPrecomputed = myAlternatives[myLastUsed];
     159              :     } else {
     160              :         // build a new route to test whether it is better
     161        93458 :         ConstROEdgeVector oldEdges({getOrigin(), getDestination()});
     162              :         ConstROEdgeVector edges;
     163        93458 :         if (repairCurrentRoute(router, begin, veh, oldEdges, edges, true)) {
     164        93455 :             if (edges.front()->isTazConnector()) {
     165              :                 edges.erase(edges.begin());
     166              :             }
     167        93455 :             if (edges.back()->isTazConnector()) {
     168              :                 edges.pop_back();
     169              :             }
     170              :             // check whether the same route was already used
     171              :             int existing = -1;
     172       135130 :             for (int i = 0; i < (int)myAlternatives.size(); i++) {
     173       120001 :                 if (edges == myAlternatives[i]->getEdgeVector()) {
     174              :                     existing = i;
     175              :                     break;
     176              :                 }
     177              :             }
     178        93455 :             if (existing >= 0) {
     179        78326 :                 myPrecomputed = myAlternatives[existing];
     180              :             } else {
     181        15129 :                 RGBColor* col = myAlternatives[0]->getColor() != nullptr ? new RGBColor(*myAlternatives[0]->getColor()) : nullptr;
     182        15129 :                 myPrecomputed = new RORoute(myID, 0, 1, edges, col, myAlternatives[0]->getStops());
     183        15129 :                 myNewRoute = true;
     184              :             }
     185              :         }
     186        93458 :     }
     187              : }
     188              : 
     189              : 
     190              : bool
     191       295640 : RORouteDef::repairCurrentRoute(SUMOAbstractRouter<ROEdge, ROVehicle>& router,
     192              :                                SUMOTime begin, const ROVehicle& veh,
     193              :                                ConstROEdgeVector oldEdges, ConstROEdgeVector& newEdges,
     194              :                                bool isTrip) const {
     195       591280 :     MsgHandler* mh = (OptionsCont::getOptions().getBool("ignore-errors") ?
     196       295640 :                       MsgHandler::getWarningInstance() : MsgHandler::getErrorInstance());
     197       295640 :     const int initialSize = (int)oldEdges.size();
     198       295640 :     const bool hasRestrictions = RONet::getInstance()->hasParamRestrictions();
     199       295640 :     if (initialSize == 1) {
     200         6414 :         if (myUsingJTRR) {
     201              :             /// only ROJTRRouter is supposed to handle this type of input
     202         6135 :             bool ok = router.compute(oldEdges.front(), nullptr, &veh, begin, newEdges);
     203        10345 :             myDiscardSilent = ok && newEdges.size() == 0;
     204              :         } else {
     205          279 :             newEdges = oldEdges;
     206              :         }
     207              :     } else {
     208       289226 :         if (oldEdges.front()->prohibits(&veh, hasRestrictions)) {
     209              :             // option repair.from is in effect
     210              :             const std::string& frontID = oldEdges.front()->getID();
     211           18 :             for (ConstROEdgeVector::iterator i = oldEdges.begin(); i != oldEdges.end();) {
     212           15 :                 if ((*i)->prohibits(&veh, hasRestrictions) || (*i)->isInternal()) {
     213              :                     i = oldEdges.erase(i);
     214              :                 } else {
     215            6 :                     WRITE_MESSAGE("Changing invalid starting edge '" + frontID
     216              :                                   + "' to '" + (*i)->getID() + "' for vehicle '" + veh.getID() + "'.");
     217            3 :                     break;
     218              :                 }
     219              :             }
     220              :         }
     221       289226 :         if (oldEdges.size() == 0) {
     222            6 :             mh->inform("Could not find new starting edge for vehicle '" + veh.getID() + "'.");
     223        17142 :             return false;
     224              :         }
     225       289223 :         if (oldEdges.back()->prohibits(&veh, hasRestrictions)) {
     226              :             // option repair.to is in effect
     227              :             const std::string& backID = oldEdges.back()->getID();
     228              :             // oldEdges cannot get empty here, otherwise we would have left the stage when checking "from"
     229            6 :             while (oldEdges.back()->prohibits(&veh, hasRestrictions) || oldEdges.back()->isInternal()) {
     230              :                 oldEdges.pop_back();
     231              :             }
     232            9 :             WRITE_MESSAGE("Changing invalid destination edge '" + backID
     233              :                           + "' to edge '" + oldEdges.back()->getID() + "' for vehicle '" + veh.getID() + "'.");
     234              :         }
     235       289223 :         ConstROEdgeVector mandatory = veh.getMandatoryEdges(oldEdges.front(), oldEdges.back());
     236              :         std::set<ConstROEdgeVector::const_iterator> jumpStarts;
     237       289223 :         veh.collectJumps(mandatory, jumpStarts);
     238              :         assert(mandatory.size() >= 2);
     239              :         // removed prohibited
     240       901291 :         for (ConstROEdgeVector::iterator i = oldEdges.begin(); i != oldEdges.end();) {
     241       612068 :             if ((*i)->prohibits(&veh, hasRestrictions) || (*i)->isInternal()) {
     242              :                 // no need to check the mandatories here, this was done before
     243           96 :                 WRITE_MESSAGEF(TL("Removing invalid edge '%' from route for vehicle '%'."), (*i)->getID(), veh.getID());
     244              :                 i = oldEdges.erase(i);
     245              :             } else {
     246              :                 ++i;
     247              :             }
     248              :         }
     249              :         // reconnect remaining edges
     250       289223 :         if (mandatory.size() > oldEdges.size() && initialSize > 2) {
     251            0 :             WRITE_MESSAGEF(TL("There are stop edges which were not part of the original route for vehicle '%'."), veh.getID());
     252              :         }
     253       289223 :         const ConstROEdgeVector& targets = mandatory.size() > oldEdges.size() ? mandatory : oldEdges;
     254       289223 :         newEdges.push_back(targets.front());
     255              :         ConstROEdgeVector::iterator nextMandatory = mandatory.begin() + 1;
     256              :         int lastMandatory = 0;
     257       289223 :         for (ConstROEdgeVector::const_iterator i = targets.begin() + 1;
     258       593215 :                 i != targets.end() && nextMandatory != mandatory.end(); ++i) {
     259       321131 :             const ROEdge* prev = *(i - 1);
     260       321131 :             const ROEdge* cur = *i;
     261       363778 :             if (prev->isConnectedTo(*cur, veh.getVClass()) && (!isRailway(veh.getVClass()) || prev->getBidiEdge() != cur)) {
     262        42643 :                 newEdges.push_back(cur);
     263              :             } else {
     264       278488 :                 if (initialSize > 2) {
     265              :                     // only inform if the input is (probably) not a trip
     266       164130 :                     WRITE_MESSAGEF(TL("Edge '%' not connected to edge '%' for vehicle '%'."), (*(i - 1))->getID(), (*i)->getID(), veh.getID());
     267              :                 }
     268       278488 :                 const ROEdge* last = newEdges.back();
     269              :                 newEdges.pop_back();
     270       278488 :                 if (last->isTazConnector() && newEdges.size() > 1) {
     271              :                     // assume this was a viaTaz
     272           28 :                     last = newEdges.back();
     273              :                     newEdges.pop_back();
     274              :                 }
     275       278488 :                 if (veh.hasJumps() && jumpStarts.count(nextMandatory - 1) != 0) {
     276           24 :                     while (*i != *nextMandatory) {
     277              :                         ++i;
     278              :                     }
     279           24 :                     newEdges.push_back(last);
     280           24 :                     newEdges.push_back(*i);
     281              :                     //std::cout << " skipJump mIndex=" << (nextMandatory - 1 - mandatory.begin()) << " last=" << last->getID() << " next=" << (*i)->getID() << " newEdges=" << toString(newEdges) << "\n";
     282              :                 } else {
     283              : 
     284       278464 :                     int numEdgesBefore = (int)newEdges.size();
     285              :                     //                router.setHint(targets.begin(), i, &veh, begin);
     286       278464 :                     if (!router.compute(last, *i, &veh, begin, newEdges)) {
     287              :                         // backtrack: try to route from last mandatory edge to next mandatory edge
     288              :                         // XXX add option for backtracking in smaller increments
     289              :                         // (i.e. previous edge to edge after *i)
     290              :                         // we would then need to decide whether we have found a good
     291              :                         // tradeoff between faithfulness to the input data and detour-length
     292        17142 :                         if (lastMandatory >= (int)newEdges.size() || last == newEdges[lastMandatory] || !backTrack(router, i, lastMandatory, nextMandatory, newEdges, veh, begin)) {
     293        34278 :                             mh->inform("Mandatory edge '" + (*i)->getID() + "' not reachable by vehicle '" + veh.getID() + "'.");
     294        17139 :                             return false;
     295              :                         }
     296       261322 :                     } else if (!myMayBeDisconnected && !isTrip && last != (*i)) {
     297           25 :                         double airDist = last->getToJunction()->getPosition().distanceTo(
     298              :                                              (*i)->getFromJunction()->getPosition());
     299              :                         double repairDist = 0;
     300           70 :                         for (auto it2 = (newEdges.begin() + numEdgesBefore + 1); it2 != newEdges.end() && it2 != newEdges.end() - 1; it2++) {
     301           45 :                             repairDist += (*it2)->getLength();
     302              :                         }
     303           25 :                         const double detourFactor = repairDist / MAX2(airDist, 1.0);
     304           25 :                         const double detour = MAX2(0.0, repairDist - airDist);
     305           25 :                         const double maxDetourFactor = OptionsCont::getOptions().getFloat("repair.max-detour-factor");
     306           25 :                         if (detourFactor > maxDetourFactor) {
     307            3 :                             WRITE_MESSAGEF("    Backtracking to avoid detour of %m for gap of %m)", detour, airDist);
     308            3 :                             backTrack(router, i, lastMandatory, nextMandatory, newEdges, veh, begin);
     309           22 :                         } else if (detourFactor > 1.1) {
     310            8 :                             WRITE_MESSAGEF("    Taking detour of %m to avoid gap of %m)", detour, airDist);
     311              :                         }
     312              :                     }
     313              :                 }
     314              :             }
     315       303992 :             if (*i == *nextMandatory) {
     316              :                 nextMandatory++;
     317       298809 :                 lastMandatory = (int)newEdges.size() - 1;
     318              :             }
     319              :         }
     320       289223 :     }
     321              :     return true;
     322              : }
     323              : 
     324              : 
     325              : bool
     326            6 : RORouteDef::backTrack(SUMOAbstractRouter<ROEdge, ROVehicle>& router,
     327              :                       ConstROEdgeVector::const_iterator& i, int lastMandatory, ConstROEdgeVector::iterator nextMandatory,
     328              :                       ConstROEdgeVector& newEdges, const ROVehicle& veh, SUMOTime begin) {
     329              :     ConstROEdgeVector edges;
     330            6 :     bool ok = router.compute(newEdges[lastMandatory], *nextMandatory, &veh, begin, edges);
     331            6 :     if (!ok) {
     332              :         return false;
     333              :     }
     334              : 
     335           12 :     while (*i != *nextMandatory) {
     336              :         ++i;
     337              :     }
     338              :     newEdges.erase(newEdges.begin() + lastMandatory + 1, newEdges.end());
     339              :     std::copy(edges.begin() + 1, edges.end(), back_inserter(newEdges));
     340              :     return true;
     341            6 : }
     342              : 
     343              : 
     344              : void
     345       277529 : RORouteDef::addAlternative(SUMOAbstractRouter<ROEdge, ROVehicle>& router,
     346              :                            const ROVehicle* const veh, RORoute* current, SUMOTime begin,
     347              :                            MsgHandler* errorHandler) {
     348       277529 :     if (myTryRepair || myUsingJTRR) {
     349       183126 :         if (myNewRoute) {
     350       154748 :             delete myAlternatives[0];
     351       154748 :             myAlternatives[0] = current;
     352              :         }
     353       366252 :         if (!router.isValid(current->getEdgeVector(), veh)) {
     354            0 :             throw ProcessError("Route '" + getID() + "' (vehicle '" + veh->getID() + "') is not valid.");
     355              :         }
     356       183126 :         double costs = router.recomputeCosts(current->getEdgeVector(), veh, begin);
     357       183126 :         if (veh->hasJumps()) {
     358              :             // @todo: jumpTime should be applied in recomputeCost to ensure the
     359              :             // correctness of time-dependent traveltimes
     360           20 :             costs += STEPS2TIME(veh->getJumpTime());
     361              :         }
     362       183126 :         current->setCosts(costs);
     363       183126 :         return;
     364              :     }
     365              :     // add the route when it's new
     366        94403 :     if (myAlternatives.back()->getProbability() < 0 || !myAlternatives.back()->isPermitted(veh, errorHandler)) {
     367          178 :         if (myAlternatives.back()->getProbability() >= 0 && errorHandler == MsgHandler::getErrorInstance()) {
     368           48 :             throw ProcessError("Route '" + current->getID() + "' (vehicle '" + veh->getID() + "') is not valid.");
     369              :         }
     370          162 :         delete myAlternatives.back();
     371              :         myAlternatives.pop_back();
     372              :     }
     373        94387 :     if (myNewRoute) {
     374        15113 :         myAlternatives.push_back(current);
     375              :     }
     376              :     // recompute the costs and (when a new route was added) scale the probabilities
     377        94387 :     const double scale = double(myAlternatives.size() - 1) / double(myAlternatives.size());
     378       346246 :     for (RORoute* const alt : myAlternatives) {
     379       251859 :         if (!router.isValid(alt->getEdgeVector(), veh)) {
     380            0 :             throw ProcessError("Route '" + current->getID() + "' (vehicle '" + veh->getID() + "') is not valid.");
     381              :         }
     382              :         // recompute the costs for all routes
     383       251859 :         const double newCosts = router.recomputeCosts(alt->getEdgeVector(), veh, begin);
     384              :         assert(myAlternatives.size() != 0);
     385       251859 :         if (myNewRoute) {
     386        36614 :             if (alt == current) {
     387              :                 // set initial probability and costs
     388        15113 :                 alt->setProbability(1. / (double) myAlternatives.size());
     389        15113 :                 alt->setCosts(newCosts);
     390              :             } else {
     391              :                 // rescale probs for all others
     392        21501 :                 alt->setProbability(alt->getProbability() * scale);
     393              :             }
     394              :         }
     395       251859 :         RouteCostCalculator<RORoute, ROEdge, ROVehicle>::getCalculator().setCosts(alt, newCosts, alt == myAlternatives[myLastUsed]);
     396              :     }
     397              :     assert(myAlternatives.size() != 0);
     398        94387 :     RouteCostCalculator<RORoute, ROEdge, ROVehicle>::getCalculator().calculateProbabilities(myAlternatives, veh, veh->getDepartureTime());
     399        94387 :     const bool keepRoute = RouteCostCalculator<RORoute, ROEdge, ROVehicle>::getCalculator().keepRoute();
     400        94387 :     if (!RouteCostCalculator<RORoute, ROEdge, ROVehicle>::getCalculator().keepAllRoutes() && !keepRoute) {
     401              :         // remove with probability of 0 (not mentioned in Gawron)
     402       330894 :         for (std::vector<RORoute*>::iterator i = myAlternatives.begin(); i != myAlternatives.end();) {
     403       240691 :             if ((*i)->getProbability() == 0) {
     404            0 :                 delete *i;
     405              :                 i = myAlternatives.erase(i);
     406              :             } else {
     407              :                 i++;
     408              :             }
     409              :         }
     410              :     }
     411        94387 :     int maxNumber = RouteCostCalculator<RORoute, ROEdge, ROVehicle>::getCalculator().getMaxRouteNumber();
     412        94387 :     if ((int)myAlternatives.size() > maxNumber) {
     413           56 :         const RORoute* last = myAlternatives[myLastUsed];
     414              :         // only keep the routes with highest probability
     415           56 :         sort(myAlternatives.begin(), myAlternatives.end(), [](const RORoute * const a, const RORoute * const b) {
     416              :             return a->getProbability() > b->getProbability();
     417              :         });
     418           56 :         if (keepRoute) {
     419           18 :             for (int i = 0; i < (int)myAlternatives.size(); i++) {
     420           18 :                 if (myAlternatives[i] == last) {
     421           12 :                     myLastUsed = i;
     422           12 :                     break;
     423              :                 }
     424              :             }
     425           12 :             if (myLastUsed >= maxNumber) {
     426            6 :                 std::swap(myAlternatives[maxNumber - 1], myAlternatives[myLastUsed]);
     427            6 :                 myLastUsed = maxNumber - 1;
     428              :             }
     429              :         }
     430          112 :         for (std::vector<RORoute*>::iterator i = myAlternatives.begin() + maxNumber; i != myAlternatives.end(); i++) {
     431           56 :             delete *i;
     432              :         }
     433              :         myAlternatives.erase(myAlternatives.begin() + maxNumber, myAlternatives.end());
     434              :     }
     435              :     // rescale probabilities
     436              :     double newSum = 0.;
     437       346190 :     for (const RORoute* const alt : myAlternatives) {
     438       251803 :         newSum += alt->getProbability();
     439              :     }
     440              :     assert(newSum > 0);
     441              :     // @note newSum may be larger than 1 for numerical reasons
     442       346190 :     for (RORoute* const alt : myAlternatives) {
     443       251803 :         alt->setProbability(alt->getProbability() / newSum);
     444              :     }
     445              : 
     446              :     // find the route to use
     447        94387 :     if (!keepRoute) {
     448        90203 :         double chosen = RandHelper::rand();
     449        90203 :         myLastUsed = 0;
     450       156013 :         for (const RORoute* const alt : myAlternatives) {
     451       156013 :             chosen -= alt->getProbability();
     452       156013 :             if (chosen <= 0) {
     453              :                 return;
     454              :             }
     455        65810 :             myLastUsed++;
     456              :         }
     457              :     }
     458              : }
     459              : 
     460              : const ROEdge*
     461        93458 : RORouteDef::getOrigin() const {
     462        93458 :     return myAlternatives.back()->getFirst();
     463              : }
     464              : 
     465              : 
     466              : const ROEdge*
     467        93458 : RORouteDef::getDestination() const {
     468        93458 :     return myAlternatives.back()->getLast();
     469              : }
     470              : 
     471              : 
     472              : OutputDevice&
     473       311187 : RORouteDef::writeXMLDefinition(OutputDevice& dev, const ROVehicle* const veh,
     474              :                                bool asAlternatives, bool withExitTimes, bool withCost, bool withLength) const {
     475       311187 :     if (asAlternatives) {
     476       112881 :         dev.openTag(SUMO_TAG_ROUTE_DISTRIBUTION).writeAttr(SUMO_ATTR_LAST, myLastUsed);
     477       383181 :         for (int i = 0; i != (int)myAlternatives.size(); i++) {
     478       540600 :             myAlternatives[i]->writeXMLDefinition(dev, veh, true, true, withExitTimes, withLength);
     479              :         }
     480       112881 :         dev.closeTag();
     481       112881 :         return dev;
     482              :     } else {
     483       396612 :         return myAlternatives[myLastUsed]->writeXMLDefinition(dev, veh, withCost, false, withExitTimes, withLength);
     484              :     }
     485              : }
     486              : 
     487              : 
     488              : RORouteDef*
     489        19965 : RORouteDef::copy(const std::string& id, const SUMOTime stopOffset) const {
     490        19965 :     RORouteDef* result = new RORouteDef(id, 0, myTryRepair, myMayBeDisconnected);
     491        40810 :     for (const RORoute* const route : myAlternatives) {
     492        20845 :         RGBColor* col = route->getColor() != nullptr ? new RGBColor(*route->getColor()) : nullptr;
     493        20845 :         RORoute* newRoute = new RORoute(id, route->getCosts(), route->getProbability(), route->getEdgeVector(), col, route->getStops());
     494              :         newRoute->addStopOffset(stopOffset);
     495        20845 :         result->addLoadedAlternative(newRoute);
     496              :     }
     497        19965 :     return result;
     498              : }
     499              : 
     500              : 
     501              : double
     502        91101 : RORouteDef::getOverallProb() const {
     503              :     double sum = 0.;
     504       323700 :     for (std::vector<RORoute*>::const_iterator i = myAlternatives.begin(); i != myAlternatives.end(); i++) {
     505       232599 :         sum += (*i)->getProbability();
     506              :     }
     507        91101 :     return sum;
     508              : }
     509              : 
     510              : 
     511              : /****************************************************************************/
        

Generated by: LCOV version 2.0-1