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

Generated by: LCOV version 2.0-1