LCOV - code coverage report
Current view: top level - src/router - RORouteDef.cpp (source / functions) Coverage Total Hit
Test: lcov.info Lines: 97.9 % 237 232
Test Date: 2026-04-16 16:39:47 Functions: 100.0 % 13 13

            Line data    Source code
       1              : /****************************************************************************/
       2              : // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
       3              : // Copyright (C) 2002-2026 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       302001 : RORouteDef::RORouteDef(const std::string& id, const int lastUsed,
      52       302001 :                        const bool tryRepair, const bool mayBeDisconnected) :
      53       302001 :     Named(StringUtils::convertUmlaute(id)),
      54       302001 :     myPrecomputed(nullptr), myLastUsed(lastUsed), myTryRepair(tryRepair),
      55       302001 :     myMayBeDisconnected(mayBeDisconnected),
      56       302001 :     myDiscardSilent(false) {
      57       302001 : }
      58              : 
      59              : 
      60              : void
      61            4 : RORouteDef::addAlternativeDef(const RORouteDef* alt) {
      62              :     std::copy(alt->myAlternatives.begin(), alt->myAlternatives.end(),
      63            4 :               back_inserter(myAlternatives));
      64            4 : }
      65              : 
      66              : 
      67              : std::shared_ptr<RORoute>
      68       296954 : RORouteDef::buildCurrentRoute(SUMOAbstractRouter<ROEdge, ROVehicle>& router,
      69              :                               SUMOTime begin, const ROVehicle& veh) const {
      70       296954 :     if (myPrecomputed == nullptr) {
      71       296954 :         preComputeCurrentRoute(router, begin, veh);
      72              :     }
      73       296954 :     return myPrecomputed;
      74              : }
      75              : 
      76              : 
      77              : void
      78       296957 : RORouteDef::validateAlternatives(const ROVehicle* veh, MsgHandler* errorHandler) {
      79       736440 :     for (int i = 0; i < (int)myAlternatives.size();) {
      80       439483 :         if ((i != myLastUsed || mySkipNewRoutes) && !myAlternatives[i]->isPermitted(veh, errorHandler)) {
      81            5 :             myAlternatives.erase(myAlternatives.begin() + i);
      82            5 :             if (myLastUsed > i) {
      83            0 :                 myLastUsed--;
      84              :             }
      85              :         } else {
      86       439478 :             i++;
      87              :         }
      88              :     }
      89       296957 : }
      90              : 
      91              : 
      92              : void
      93       296954 : RORouteDef::preComputeCurrentRoute(SUMOAbstractRouter<ROEdge, ROVehicle>& router,
      94              :                                    SUMOTime begin, const ROVehicle& veh) const {
      95       296954 :     myNewRoute = false;
      96       296954 :     const OptionsCont& oc = OptionsCont::getOptions();
      97       296954 :     const bool ignoreErrors = oc.getBool("ignore-errors");
      98       296954 :     const bool hasRestrictions = RONet::getInstance()->hasParamRestrictions();
      99              :     assert(myAlternatives[0]->getEdgeVector().size() > 0);
     100       296954 :     MsgHandler* mh = ignoreErrors ? MsgHandler::getWarningInstance() : MsgHandler::getErrorInstance();
     101       297036 :     if (myAlternatives[0]->getFirst()->prohibits(&veh, hasRestrictions) && (!oc.getBool("repair.from")
     102              :             // do not try to reassign starting edge for trip input
     103            9 :             || myMayBeDisconnected || myAlternatives[0]->getEdgeVector().size() < 2)) {
     104           70 :         mh->inform("Vehicle '" + veh.getID() + "' is not allowed to depart on edge '" +
     105           35 :                    myAlternatives[0]->getFirst()->getID() + "'.");
     106           35 :         return;
     107       296961 :     } else if (myAlternatives[0]->getLast()->prohibits(&veh, hasRestrictions) && (!oc.getBool("repair.to")
     108              :                // do not try to reassign destination edge for trip input
     109            9 :                || myMayBeDisconnected || myAlternatives[0]->getEdgeVector().size() < 2)) {
     110              :         // this check is not strictly necessary unless myTryRepair is set.
     111              :         // However, the error message is more helpful than "no connection found"
     112           30 :         mh->inform("Vehicle '" + veh.getID() + "' is not allowed to arrive on edge '" +
     113           15 :                    myAlternatives[0]->getLast()->getID() + "'.");
     114           15 :         return;
     115              :     }
     116       976223 :     const bool skipTripRouting = (oc.exists("write-trips") && oc.getBool("write-trips")
     117       388606 :                                   && RouteCostCalculator<RORoute, ROEdge, ROVehicle>::getCalculator().skipRouteCalculation());
     118       296904 :     if ((myTryRepair && !skipTripRouting) || myUsingJTRR) {
     119              :         ConstROEdgeVector newEdges;
     120       202482 :         if (repairCurrentRoute(router, begin, veh, myAlternatives[0]->getEdgeVector(), newEdges)) {
     121       185254 :             if (myAlternatives[0]->getEdgeVector() != newEdges) {
     122       156871 :                 if (!myMayBeDisconnected) {
     123          174 :                     WRITE_WARNINGF(TL("Repaired route of vehicle '%'."), veh.getID());
     124              :                 }
     125       156871 :                 myNewRoute = true;
     126       156871 :                 RGBColor* col = myAlternatives[0]->getColor() != nullptr ? new RGBColor(*myAlternatives[0]->getColor()) : nullptr;
     127       313742 :                 myPrecomputed = std::make_shared<RORoute>(myID, 0, myAlternatives[0]->getProbability(), newEdges, col, myAlternatives[0]->getStops());
     128              :             } else {
     129              :                 myPrecomputed = myAlternatives[0];
     130              :             }
     131              :         }
     132              :         return;
     133       202482 :     }
     134        94422 :     if (RouteCostCalculator<RORoute, ROEdge, ROVehicle>::getCalculator().skipRouteCalculation()
     135       187940 :             || OptionsCont::getOptions().getBool("remove-loops")) {
     136          960 :         if (skipTripRouting || myAlternatives[myLastUsed]->isValid(veh, ignoreErrors)) {
     137          954 :             myPrecomputed = myAlternatives[myLastUsed];
     138              :         }
     139              :     } else {
     140              :         // build a new route to test whether it is better
     141        93462 :         ConstROEdgeVector oldEdges({getOrigin(), getDestination()});
     142              :         ConstROEdgeVector edges;
     143        93462 :         if (repairCurrentRoute(router, begin, veh, oldEdges, edges, true)) {
     144        93459 :             if (edges.front()->isTazConnector()) {
     145              :                 edges.erase(edges.begin());
     146              :             }
     147        93459 :             if (edges.back()->isTazConnector()) {
     148              :                 edges.pop_back();
     149              :             }
     150              :             // check whether the same route was already used
     151              :             int existing = -1;
     152       135160 :             for (int i = 0; i < (int)myAlternatives.size(); i++) {
     153       120035 :                 if (edges == myAlternatives[i]->getEdgeVector()) {
     154              :                     existing = i;
     155              :                     break;
     156              :                 }
     157              :             }
     158        93459 :             if (existing >= 0) {
     159        78334 :                 myPrecomputed = myAlternatives[existing];
     160              :             } else {
     161        15125 :                 RGBColor* col = myAlternatives[0]->getColor() != nullptr ? new RGBColor(*myAlternatives[0]->getColor()) : nullptr;
     162        15125 :                 myPrecomputed = std::make_shared<RORoute>(myID, 0, 1, edges, col, myAlternatives[0]->getStops());
     163        15125 :                 myNewRoute = true;
     164              :             }
     165              :         }
     166        93462 :     }
     167              : }
     168              : 
     169              : 
     170              : bool
     171       295944 : RORouteDef::repairCurrentRoute(SUMOAbstractRouter<ROEdge, ROVehicle>& router,
     172              :                                SUMOTime begin, const ROVehicle& veh,
     173              :                                ConstROEdgeVector oldEdges, ConstROEdgeVector& newEdges,
     174              :                                bool isTrip) const {
     175       295944 :     MsgHandler* mh = (OptionsCont::getOptions().getBool("ignore-errors") ?
     176       295944 :                       MsgHandler::getWarningInstance() : MsgHandler::getErrorInstance());
     177       295944 :     RONet* net = RONet::getInstance();
     178       295944 :     const int initialSize = (int)oldEdges.size();
     179       295944 :     const bool hasRestrictions = RONet::getInstance()->hasParamRestrictions();
     180           40 :     if (net->hasProhibitions() && router.supportsProhibitions()) {
     181           20 :         if (net->getProhibitions().size() > 0 && !router.hasProhibitions()) {
     182            2 :             router.prohibit(net->getProhibitions());
     183              :         }
     184           20 :         net->updateLaneProhibitions(begin);
     185              :     }
     186       295944 :     if (initialSize == 1) {
     187         6413 :         if (myUsingJTRR) {
     188              :             /// only ROJTRRouter is supposed to handle this type of input
     189         6134 :             bool ok = router.compute(oldEdges.front(), nullptr, &veh, begin, newEdges);
     190        10343 :             myDiscardSilent = ok && newEdges.size() == 0;
     191              :         } else {
     192          279 :             newEdges = oldEdges;
     193              :         }
     194              :     } else {
     195       289531 :         if (oldEdges.front()->prohibits(&veh, hasRestrictions)) {
     196              :             // option repair.from is in effect
     197              :             const std::string& frontID = oldEdges.front()->getID();
     198           18 :             for (ConstROEdgeVector::iterator i = oldEdges.begin(); i != oldEdges.end();) {
     199           15 :                 if ((*i)->prohibits(&veh, hasRestrictions) || (*i)->isInternal()) {
     200              :                     i = oldEdges.erase(i);
     201              :                 } else {
     202            6 :                     WRITE_MESSAGE("Changing invalid starting edge '" + frontID
     203              :                                   + "' to '" + (*i)->getID() + "' for vehicle '" + veh.getID() + "'.");
     204            3 :                     break;
     205              :                 }
     206              :             }
     207              :         }
     208       289531 :         if (oldEdges.size() == 0) {
     209            6 :             mh->inform("Could not find new starting edge for vehicle '" + veh.getID() + "'.");
     210        17231 :             return false;
     211              :         }
     212       289528 :         if (oldEdges.back()->prohibits(&veh, hasRestrictions)) {
     213              :             // option repair.to is in effect
     214              :             const std::string& backID = oldEdges.back()->getID();
     215              :             // oldEdges cannot get empty here, otherwise we would have left the stage when checking "from"
     216            6 :             while (oldEdges.back()->prohibits(&veh, hasRestrictions) || oldEdges.back()->isInternal()) {
     217              :                 oldEdges.pop_back();
     218              :             }
     219            9 :             WRITE_MESSAGE("Changing invalid destination edge '" + backID
     220              :                           + "' to edge '" + oldEdges.back()->getID() + "' for vehicle '" + veh.getID() + "'.");
     221              :         }
     222       289528 :         std::vector<ROVehicle::Mandatory> mandatory = veh.getMandatoryEdges(oldEdges.front(), oldEdges.back());
     223              :         assert(mandatory.size() >= 2);
     224              :         // removed prohibited
     225       902409 :         for (ConstROEdgeVector::iterator i = oldEdges.begin(); i != oldEdges.end();) {
     226       612881 :             if ((*i)->prohibits(&veh, hasRestrictions) || (*i)->isInternal()) {
     227              :                 // no need to check the mandatories here, this was done before
     228          132 :                 WRITE_MESSAGEF(TL("Removing invalid edge '%' from route for vehicle '%'."), (*i)->getID(), veh.getID());
     229              :                 i = oldEdges.erase(i);
     230              :             } else {
     231              :                 ++i;
     232              :             }
     233              :         }
     234              :         // reconnect remaining edges
     235       289528 :         if (mandatory.size() > oldEdges.size() && initialSize > 2) {
     236          504 :             WRITE_MESSAGEF(TL("There are stop edges which were not part of the original route for vehicle '%'."), veh.getID());
     237              :         }
     238              :         ConstROEdgeVector targets;
     239              :         bool checkPositions = false;
     240       289528 :         if (mandatory.size() >= oldEdges.size()) {
     241       902757 :             for (auto m : mandatory) {
     242       613282 :                 targets.push_back(m.edge);
     243       613282 :                 checkPositions |= m.pos >= 0;
     244              :             }
     245              :         } else {
     246           53 :             targets = oldEdges;
     247              :         }
     248       289528 :         newEdges.push_back(targets.front());
     249              :         auto nextMandatory = mandatory.begin() + 1;
     250              :         int lastMandatory = 0;
     251       289528 :         for (ConstROEdgeVector::const_iterator i = targets.begin() + 1;
     252       596285 :                 i != targets.end() && nextMandatory != mandatory.end(); ++i) {
     253       323981 :             const ROEdge* prev = *(i - 1);
     254       323981 :             const ROEdge* cur = *i;
     255       366799 :             if (prev->isConnectedTo(*cur, veh.getVClass()) && (!isRailway(veh.getVClass()) || prev->getBidiEdge() != cur)) {
     256        42814 :                 newEdges.push_back(cur);
     257              :             } else {
     258       281167 :                 if (initialSize > 2) {
     259              :                     // only inform if the input is (probably) not a trip
     260       176045 :                     WRITE_MESSAGEF(TL("Edge '%' not connected to edge '%' for vehicle '%'."), (*(i - 1))->getID(), (*i)->getID(), veh.getID());
     261              :                 }
     262       281167 :                 const ROEdge* last = newEdges.back();
     263              :                 newEdges.pop_back();
     264       281167 :                 if (last->isTazConnector() && newEdges.size() > 1) {
     265              :                     // assume this was a viaTaz
     266           28 :                     last = newEdges.back();
     267              :                     newEdges.pop_back();
     268              :                 }
     269       281167 :                 if (veh.hasJumps() && (nextMandatory - 1)->isJump) {
     270           24 :                     while (*i != nextMandatory->edge) {
     271              :                         ++i;
     272              :                     }
     273           24 :                     newEdges.push_back(last);
     274           24 :                     newEdges.push_back(*i);
     275              :                     //std::cout << " skipJump mIndex=" << (nextMandatory - 1 - mandatory.begin()) << " last=" << last->getID() << " next=" << (*i)->getID() << " newEdges=" << toString(newEdges) << "\n";
     276              :                 } else {
     277       281143 :                     if (veh.getID() == "10.T0.1-13-P-j26-1.2.H.0") {
     278            0 :                         std::cout << " beforeRepair newEdges=" << toString(newEdges) << " last=" << last->getID() << "\n";
     279              :                     }
     280              : 
     281       281143 :                     int numEdgesBefore = (int)newEdges.size();
     282              :                     //                router.setHint(targets.begin(), i, &veh, begin);
     283       281143 :                     if (myTryRepair && lastMandatory < (int)newEdges.size() && last != newEdges[lastMandatory]) {
     284           23 :                         router.setMsgHandler(MsgHandler::getWarningInstance());
     285              :                     }
     286              :                     bool ok;
     287              :                     if (checkPositions
     288       281110 :                             && mandatory[i - targets.begin() - 1].pos >= 0
     289       540508 :                             && mandatory[i - targets.begin()].pos >= 0) {
     290          602 :                         ok = router.compute(
     291              :                                  last, mandatory[i - targets.begin() - 1].pos,
     292              :                                  *i, mandatory[i - targets.begin()].pos,
     293              :                                  &veh, begin, newEdges);
     294              :                     } else {
     295       280541 :                         ok = router.compute(last, *i, &veh, begin, newEdges);
     296              :                     }
     297              :                     router.setMsgHandler(mh);
     298       281143 :                     if (!ok) {
     299              :                         // backtrack: try to route from last mandatory edge to next mandatory edge
     300              :                         // XXX add option for backtracking in smaller increments
     301              :                         // (i.e. previous edge to edge after *i)
     302              :                         // we would then need to decide whether we have found a good
     303              :                         // tradeoff between faithfulness to the input data and detour-length
     304        17230 :                         if (lastMandatory >= (int)newEdges.size() || last == newEdges[lastMandatory] || !backTrack(router, i, lastMandatory, nextMandatory->edge, newEdges, veh, begin)) {
     305        34448 :                             mh->inform("Mandatory edge '" + (*i)->getID() + "' not reachable by vehicle '" + veh.getID() + "'.");
     306        17224 :                             return false;
     307              :                         }
     308       263913 :                     } else if (!myMayBeDisconnected && !isTrip && last != (*i)) {
     309           37 :                         double airDist = last->getToJunction()->getPosition().distanceTo(
     310              :                                              (*i)->getFromJunction()->getPosition());
     311              :                         double repairDist = 0;
     312           94 :                         for (auto it2 = (newEdges.begin() + numEdgesBefore + 1); it2 != newEdges.end() && it2 != newEdges.end() - 1; it2++) {
     313           57 :                             repairDist += (*it2)->getLength();
     314              :                         }
     315           37 :                         const double detourFactor = repairDist / MAX2(airDist, 1.0);
     316           37 :                         const double detour = MAX2(0.0, repairDist - airDist);
     317           37 :                         const double maxDetourFactor = OptionsCont::getOptions().getFloat("repair.max-detour-factor");
     318           37 :                         if (detourFactor > maxDetourFactor) {
     319            3 :                             WRITE_MESSAGEF("    Backtracking to avoid detour of %m for gap of %m)", detour, airDist);
     320            3 :                             backTrack(router, i, lastMandatory, nextMandatory->edge, newEdges, veh, begin);
     321           34 :                         } else if (detourFactor > 1.1) {
     322            8 :                             WRITE_MESSAGEF("    Taking detour of %m to avoid gap of %m)", detour, airDist);
     323              :                         }
     324              :                     }
     325       263919 :                     if (veh.getID() == "10.T0.1-13-P-j26-1.2.H.0") {
     326            0 :                         std::cout << " at=" << numEdgesBefore << " before=" << numEdgesBefore << " after=" << newEdges.size() << " new=" << toString(newEdges) << "\n";
     327              :                     }
     328              :                 }
     329              :             }
     330       306757 :             if (*i == nextMandatory->edge) {
     331              :                 nextMandatory++;
     332       306590 :                 lastMandatory = (int)newEdges.size() - 1;
     333              :             }
     334              :         }
     335       272304 :         if (veh.getParameter().via.size() > 0 && veh.getParameter().stops.size() > 0) {
     336              :             // check consistency of stops and vias
     337              :             auto it = newEdges.begin();
     338           12 :             for (const auto& stop : veh.getParameter().stops) {
     339           16 :                 const ROEdge* e = net->getEdge(stop.edge);
     340            8 :                 it = std::find(it, newEdges.end(), e);
     341            8 :                 if (it == newEdges.end()) {
     342            8 :                     mh->inform("Stop edge '" + e->getID() + "' is inconsistent with via edges for vehicle '" + veh.getID() + "'.");
     343            4 :                     return false;
     344              :                 }
     345              :             }
     346              :         }
     347       289528 :     }
     348              :     return true;
     349              : }
     350              : 
     351              : 
     352              : bool
     353            9 : RORouteDef::backTrack(SUMOAbstractRouter<ROEdge, ROVehicle>& router,
     354              :                       ConstROEdgeVector::const_iterator& i, int lastMandatory, const ROEdge* nextMandatory,
     355              :                       ConstROEdgeVector& newEdges, const ROVehicle& veh, SUMOTime begin) {
     356              :     ConstROEdgeVector edges;
     357            9 :     bool ok = router.compute(newEdges[lastMandatory], nextMandatory, &veh, begin, edges, true);
     358            9 :     if (!ok) {
     359              :         return false;
     360              :     }
     361              : 
     362           18 :     while (*i != nextMandatory) {
     363              :         ++i;
     364              :     }
     365              :     newEdges.erase(newEdges.begin() + lastMandatory + 1, newEdges.end());
     366              :     std::copy(edges.begin() + 1, edges.end(), back_inserter(newEdges));
     367              :     return true;
     368            9 : }
     369              : 
     370              : 
     371              : std::shared_ptr<RORoute>
     372       277734 : RORouteDef::addAlternative(SUMOAbstractRouter<ROEdge, ROVehicle>& router,
     373              :                            const ROVehicle* const veh, std::shared_ptr<RORoute> current, SUMOTime begin,
     374              :                            MsgHandler* errorHandler) {
     375              :     std::shared_ptr<RORoute> replaced = nullptr;
     376       277734 :     if (myTryRepair || myUsingJTRR) {
     377       183329 :         if (myNewRoute) {
     378              :             replaced = myAlternatives[0];
     379              :             myAlternatives[0] = current;
     380              :         }
     381       366658 :         if (!router.isValid(current->getEdgeVector(), veh, STEPS2TIME(begin))) {
     382            0 :             throw ProcessError("Route '" + getID() + "' (vehicle '" + veh->getID() + "') is not valid.");
     383              :         }
     384       183329 :         double costs = router.recomputeCosts(current->getEdgeVector(), veh, begin);
     385       183329 :         if (veh->hasJumps()) {
     386              :             // @todo: jumpTime should be applied in recomputeCost to ensure the
     387              :             // correctness of time-dependent traveltimes
     388           20 :             costs += STEPS2TIME(veh->getJumpTime());
     389              :         }
     390       183329 :         current->setCosts(costs);
     391              :         return replaced;
     392              :     }
     393              :     // add the route when it's new
     394        94405 :     if (myAlternatives.back()->getProbability() < 0 || !myAlternatives.back()->isPermitted(veh, errorHandler)) {
     395          178 :         if (myAlternatives.back()->getProbability() >= 0 && errorHandler == MsgHandler::getErrorInstance()) {
     396           48 :             throw ProcessError("Route '" + current->getID() + "' (vehicle '" + veh->getID() + "') is not valid.");
     397              :         }
     398              :         myAlternatives.pop_back();
     399              :     }
     400        94389 :     if (myNewRoute) {
     401        15109 :         myAlternatives.push_back(current);
     402              :     }
     403              :     // recompute the costs and (when a new route was added) scale the probabilities
     404        94389 :     const double scale = double(myAlternatives.size() - 1) / double(myAlternatives.size());
     405       346246 :     for (const std::shared_ptr<RORoute>& alt : myAlternatives) {
     406       503714 :         if (!router.isValid(alt->getEdgeVector(), veh, STEPS2TIME(begin))) {
     407            0 :             throw ProcessError("Route '" + current->getID() + "' (vehicle '" + veh->getID() + "') is not valid.");
     408              :         }
     409              :         // recompute the costs for all routes
     410       251857 :         const double newCosts = router.recomputeCosts(alt->getEdgeVector(), veh, begin);
     411              :         assert(myAlternatives.size() != 0);
     412       251857 :         if (myNewRoute) {
     413        36606 :             if (alt == current) {
     414              :                 // set initial probability and costs
     415        15109 :                 alt->setProbability(1. / (double) myAlternatives.size());
     416        15109 :                 alt->setCosts(newCosts);
     417              :             } else {
     418              :                 // rescale probs for all others
     419        21497 :                 alt->setProbability(alt->getProbability() * scale);
     420              :             }
     421              :         }
     422       503730 :         RouteCostCalculator<RORoute, ROEdge, ROVehicle>::getCalculator().setCosts(alt, newCosts, alt == myAlternatives[myLastUsed]);
     423              :     }
     424              :     assert(myAlternatives.size() != 0);
     425        94389 :     RouteCostCalculator<RORoute, ROEdge, ROVehicle>::getCalculator().calculateProbabilities(myAlternatives, veh, veh->getDepartureTime());
     426        94389 :     const bool keepRoute = RouteCostCalculator<RORoute, ROEdge, ROVehicle>::getCalculator().keepRoute();
     427        94389 :     if (!RouteCostCalculator<RORoute, ROEdge, ROVehicle>::getCalculator().keepAllRoutes() && !keepRoute) {
     428              :         // remove with probability of 0 (not mentioned in Gawron)
     429       330894 :         for (std::vector<std::shared_ptr<RORoute>>::iterator i = myAlternatives.begin(); i != myAlternatives.end();) {
     430       240689 :             if ((*i)->getProbability() == 0) {
     431              :                 i = myAlternatives.erase(i);
     432              :             } else {
     433              :                 i++;
     434              :             }
     435              :         }
     436              :     }
     437        94389 :     int maxNumber = RouteCostCalculator<RORoute, ROEdge, ROVehicle>::getCalculator().getMaxRouteNumber();
     438        94389 :     if ((int)myAlternatives.size() > maxNumber) {
     439           56 :         std::shared_ptr<const RORoute> last = myAlternatives[myLastUsed];
     440              :         // only keep the routes with highest probability
     441           56 :         sort(myAlternatives.begin(), myAlternatives.end(), [](const std::shared_ptr<const RORoute> a, const std::shared_ptr<const RORoute> b) {
     442           70 :             return a->getProbability() > b->getProbability();
     443              :         });
     444           56 :         if (keepRoute) {
     445           18 :             for (int i = 0; i < (int)myAlternatives.size(); i++) {
     446           18 :                 if (myAlternatives[i] == last) {
     447           12 :                     myLastUsed = i;
     448           12 :                     break;
     449              :                 }
     450              :             }
     451           12 :             if (myLastUsed >= maxNumber) {
     452            6 :                 std::swap(myAlternatives[maxNumber - 1], myAlternatives[myLastUsed]);
     453            6 :                 myLastUsed = maxNumber - 1;
     454              :             }
     455              :         }
     456              :         myAlternatives.erase(myAlternatives.begin() + maxNumber, myAlternatives.end());
     457              :     }
     458              :     // rescale probabilities
     459              :     double newSum = 0.;
     460       346190 :     for (const std::shared_ptr<RORoute>& alt : myAlternatives) {
     461       251801 :         newSum += alt->getProbability();
     462              :     }
     463              :     assert(newSum > 0);
     464              :     // @note newSum may be larger than 1 for numerical reasons
     465       346190 :     for (const std::shared_ptr<RORoute>& alt : myAlternatives) {
     466       251801 :         alt->setProbability(alt->getProbability() / newSum);
     467              :     }
     468              : 
     469              :     // find the route to use
     470        94389 :     if (!keepRoute) {
     471        90205 :         double chosen = RandHelper::rand();
     472        90205 :         myLastUsed = 0;
     473       156009 :         for (const std::shared_ptr<RORoute>& alt : myAlternatives) {
     474       156009 :             chosen -= alt->getProbability();
     475       156009 :             if (chosen <= 0) {
     476              :                 return nullptr;
     477              :             }
     478        65804 :             myLastUsed++;
     479              :         }
     480              :     }
     481              :     return nullptr;
     482              : }
     483              : 
     484              : 
     485              : const ROEdge*
     486        93462 : RORouteDef::getOrigin() const {
     487        93462 :     return myAlternatives.back()->getFirst();
     488              : }
     489              : 
     490              : 
     491              : const ROEdge*
     492        93462 : RORouteDef::getDestination() const {
     493        93462 :     return myAlternatives.back()->getLast();
     494              : }
     495              : 
     496              : 
     497              : OutputDevice&
     498       311510 : RORouteDef::writeXMLDefinition(OutputDevice& dev, const ROVehicle* const veh,
     499              :                                bool asAlternatives, bool withExitTimes, bool withCost, bool withLength) const {
     500       311510 :     if (asAlternatives) {
     501       113007 :         dev.openTag(SUMO_TAG_ROUTE_DISTRIBUTION).writeAttr(SUMO_ATTR_LAST, myLastUsed);
     502       383429 :         for (int i = 0; i != (int)myAlternatives.size(); i++) {
     503       540844 :             myAlternatives[i]->writeXMLDefinition(dev, veh, true, true, withExitTimes, withLength);
     504              :         }
     505       113007 :         dev.closeTag();
     506       113007 :         return dev;
     507              :     } else {
     508       397006 :         return myAlternatives[myLastUsed]->writeXMLDefinition(dev, veh, withCost, false, withExitTimes, withLength);
     509              :     }
     510              : }
     511              : 
     512              : 
     513              : RORouteDef*
     514        20008 : RORouteDef::copy(const std::string& id, const SUMOTime stopOffset) const {
     515        20008 :     RORouteDef* result = new RORouteDef(id, 0, myTryRepair, myMayBeDisconnected);
     516        40896 :     for (const std::shared_ptr<const RORoute> route : myAlternatives) {
     517        20888 :         RGBColor* col = route->getColor() != nullptr ? new RGBColor(*route->getColor()) : nullptr;
     518        20888 :         std::shared_ptr<RORoute> newRoute = std::make_shared<RORoute>(id, route->getCosts(), route->getProbability(), route->getEdgeVector(), col, route->getStops());
     519              :         newRoute->addStopOffset(stopOffset);
     520        20888 :         result->addLoadedAlternative(newRoute);
     521              :     }
     522        20008 :     return result;
     523              : }
     524              : 
     525              : 
     526              : double
     527        91101 : RORouteDef::getOverallProb() const {
     528              :     double sum = 0.;
     529       323700 :     for (const std::shared_ptr<const RORoute> r : myAlternatives) {
     530       232599 :         sum += r->getProbability();
     531              :     }
     532        91101 :     return sum;
     533              : }
     534              : 
     535              : 
     536              : /****************************************************************************/
        

Generated by: LCOV version 2.0-1