LCOV - code coverage report
Current view: top level - src/utils/router - SUMOAbstractRouter.h (source / functions) Coverage Total Hit
Test: lcov.info Lines: 98.2 % 164 161
Test Date: 2026-03-26 16:31:35 Functions: 53.8 % 171 92

            Line data    Source code
       1              : /****************************************************************************/
       2              : // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
       3              : // Copyright (C) 2006-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    SUMOAbstractRouter.h
      15              : /// @author  Daniel Krajzewicz
      16              : /// @author  Michael Behrisch
      17              : /// @author  Jakob Erdmann
      18              : /// @date    25.Jan 2006
      19              : ///
      20              : // An abstract router base class
      21              : /****************************************************************************/
      22              : #pragma once
      23              : #include <config.h>
      24              : 
      25              : #include <string>
      26              : #include <vector>
      27              : #include <algorithm>
      28              : #include <iterator>
      29              : #include <assert.h>
      30              : #include <utils/common/SysUtils.h>
      31              : #include <utils/common/MsgHandler.h>
      32              : #include <utils/common/SUMOTime.h>
      33              : #include <utils/common/ToString.h>
      34              : 
      35              : //#define ROUTER_DEBUG_HINT
      36              : //#define ROUTER_DEBUG_COND (true)
      37              : 
      38              : 
      39              : // ===========================================================================
      40              : // class definitions
      41              : // ===========================================================================
      42              : 
      43              : /// Prohibitions and their estimated end time
      44      2549097 : struct RouterProhibition {
      45              :     double begin = 0;
      46              :     double end = std::numeric_limits<double>::max();
      47              :     SVCPermissions permissions = SVC_IGNORING;
      48              : };
      49              : 
      50              : /**
      51              :  * @class SUMOAbstractRouter
      52              :  * The interface for routing the vehicles over the network.
      53              :  */
      54              : template<class E, class V>
      55              : class SUMOAbstractRouter {
      56              : public:
      57              :     /**
      58              :     * @class EdgeInfo
      59              :     * A definition about a route's edge with the effort needed to reach it and
      60              :     *  the information about the previous edge.
      61              :     */
      62              :     class EdgeInfo {
      63              :     public:
      64              :         /// Constructor
      65      6962824 :         EdgeInfo(const E* const e)
      66      6962824 :             : edge(e), effort(std::numeric_limits<double>::max()),
      67      6962824 :               heuristicEffort(std::numeric_limits<double>::max()),
      68      6962824 :               leaveTime(0.), prev(nullptr), visited(false), prohibitedPermissions(SVCAll), prohibitionBegin(-1), prohibitionEnd(-1) {}
      69              : 
      70              :         /// The current edge
      71              :         const E* const edge;
      72              : 
      73              :         /// Effort to reach the edge
      74              :         double effort;
      75              : 
      76              :         /// Estimated effort to reach the edge (effort + lower bound on remaining effort)
      77              :         // only used by A*
      78              :         double heuristicEffort;
      79              : 
      80              :         /// The time the vehicle leaves the edge
      81              :         double leaveTime;
      82              : 
      83              :         /// The previous edge
      84              :         const EdgeInfo* prev;
      85              : 
      86              :         /// whether the edge was already evaluated
      87              :         bool visited;
      88              : 
      89              :         /// temporary permission change
      90              :         SVCPermissions prohibitedPermissions;
      91              : 
      92              :         /// the time at which a temporary prohibition begins
      93              :         double prohibitionBegin;
      94              : 
      95              :         /// the time at which a temporary prohibition ends
      96              :         double prohibitionEnd;
      97              : 
      98              :         inline void reset() {
      99     91810410 :             effort = std::numeric_limits<double>::max();
     100     91810410 :             heuristicEffort = std::numeric_limits<double>::max();
     101      2903746 :             visited = false;
     102              :         }
     103              : 
     104              :     };
     105              : 
     106              :     /// Type of the function that is used to retrieve the edge effort.
     107              :     typedef double(* Operation)(const E* const, const V* const, double);
     108              : 
     109              :     typedef std::map<const E*, RouterProhibition> Prohibitions;
     110              : 
     111              :     /// Constructor
     112        53177 :     SUMOAbstractRouter(const std::string& type, bool unbuildIsWarning, Operation operation, Operation ttOperation,
     113              :                        const bool havePermissions, const bool haveRestrictions) :
     114        53177 :         myErrorMsgHandler(unbuildIsWarning ? MsgHandler::getWarningInstance() : MsgHandler::getErrorInstance()),
     115        53177 :         myOperation(operation), myTTOperation(ttOperation),
     116        53177 :         myBulkMode(false),
     117        53177 :         myAutoBulkMode(false),
     118        53177 :         myAmClean(true),
     119        53177 :         myHavePermissions(havePermissions),
     120        53177 :         myHaveRestrictions(haveRestrictions),
     121        53177 :         myType(type),
     122        53177 :         myQueryVisits(0),
     123        53177 :         myNumQueries(0),
     124        53177 :         myQueryStartTime(0),
     125        53177 :         myQueryTimeSum(0) {
     126        53177 :     }
     127              : 
     128              :     /// Copy Constructor
     129          379 :     SUMOAbstractRouter(SUMOAbstractRouter* other) :
     130          379 :         myErrorMsgHandler(other->myErrorMsgHandler),
     131          379 :         myOperation(other->myOperation), myTTOperation(other->myTTOperation),
     132          379 :         myBulkMode(false),
     133          379 :         myAutoBulkMode(false),
     134          379 :         myAmClean(true),
     135          379 :         myHavePermissions(other->myHavePermissions),
     136          379 :         myHaveRestrictions(other->myHaveRestrictions),
     137          379 :         myType(other->myType),
     138          379 :         myQueryVisits(0),
     139          379 :         myNumQueries(0),
     140          379 :         myQueryStartTime(0),
     141          758 :         myQueryTimeSum(0) { }
     142              : 
     143              : 
     144              : 
     145              :     /// Destructor
     146        53551 :     virtual ~SUMOAbstractRouter() {
     147        53551 :         if (myNumQueries > 0) {
     148        73688 :             WRITE_MESSAGE(myType + " answered " + toString(myNumQueries) + " queries and explored " + toString((double)myQueryVisits / (double)myNumQueries) +  " edges on average.");
     149        73688 :             WRITE_MESSAGE(myType + " spent " + elapsedMs2string(myQueryTimeSum) + " answering queries (" + toString((double)myQueryTimeSum / (double)myNumQueries) +  "ms on average).");
     150              :         }
     151        71973 :     }
     152              : 
     153              :     virtual SUMOAbstractRouter* clone() = 0;
     154              : 
     155      4591483 :     inline void init(const int edgeID, const SUMOTime msTime) {
     156              : //        if (!myAmClean) {
     157              :         // all EdgeInfos touched in the previous query are either in myFrontierList or myFound: clean those up
     158     19189238 :         for (auto& edgeInfo : myFrontierList) {
     159     14597755 :             edgeInfo->reset();
     160              :         }
     161              :         myFrontierList.clear();
     162     78910482 :         for (auto& edgeInfo : myFound) {
     163     74318999 :             edgeInfo->reset();
     164              :         }
     165              :         myFound.clear();
     166      4591483 :         if (edgeID > -1) {
     167              :             // add begin node
     168      4591483 :             auto& fromInfo = myEdgeInfos[edgeID];
     169      4591483 :             fromInfo.effort = 0.;
     170      4591483 :             fromInfo.heuristicEffort = 0.;
     171      4591483 :             fromInfo.prev = nullptr;
     172      4591483 :             fromInfo.leaveTime = STEPS2TIME(msTime);
     173      4591483 :             myFrontierList.push_back(&fromInfo);
     174              :         }
     175      4591483 :         myAmClean = true;
     176              : //        }
     177      4591483 :     }
     178              : 
     179              :     /// reset internal caches, used by CHRouter
     180         4260 :     virtual void reset(const V* const vehicle) {
     181              :         UNUSED_PARAMETER(vehicle);
     182         4260 :     }
     183              : 
     184              :     void setMsgHandler(MsgHandler* const errorMsgHandler) {
     185       280997 :         myErrorMsgHandler = errorMsgHandler;
     186           23 :     }
     187              : 
     188              :     const std::string& getType() const {
     189              :         return myType;
     190              :     }
     191              : 
     192              :     inline const typename SUMOAbstractRouter<E, V>::EdgeInfo& getEdgeInfo(int index) const {
     193           56 :         return myEdgeInfos[index];
     194              :     }
     195              : 
     196              :     /** @brief Builds the route between the given edges using the minimum effort at the given time
     197              :         The definition of the effort depends on the wished routing scheme */
     198              :     virtual bool compute(const E* from, const E* to, const V* const vehicle,
     199              :                          SUMOTime msTime, std::vector<const E*>& into, bool silent = false) = 0;
     200              : 
     201              : 
     202              :     /** @brief Builds the route between the given edges using the minimum effort at the given time,
     203              :      * also taking into account position along the edges to ensure currect
     204              :      * handling of looped routes
     205              :      * The definition of the effort depends on the wished routing scheme */
     206      3236691 :     inline bool compute(
     207              :         const E* from, double fromPos,
     208              :         const E* to, double toPos,
     209              :         const V* const vehicle,
     210              :         SUMOTime msTime, std::vector<const E*>& into, bool silent = false) {
     211      3236691 :         if (from != to || fromPos <= toPos) {
     212      3229748 :             return compute(from, to, vehicle, msTime, into, silent);
     213              :         } else {
     214         6943 :             return computeLooped(from, to, vehicle, msTime, into, silent);
     215              :         }
     216              :     }
     217              : 
     218              : 
     219              :     /** @brief Builds the route between the given edges using the minimum effort at the given time
     220              :      * if from == to, return the shortest looped route */
     221         7433 :     inline bool computeLooped(const E* from, const E* to, const V* const vehicle,
     222              :                               SUMOTime msTime, std::vector<const E*>& into, bool silent = false) {
     223         7433 :         if (from != to) {
     224            0 :             return compute(from, to, vehicle, msTime, into, silent);
     225              :         }
     226              :         double minEffort = std::numeric_limits<double>::max();
     227            0 :         std::vector<const E*> best;
     228         7433 :         const SUMOVehicleClass vClass = vehicle == 0 ? SVC_IGNORING : vehicle->getVClass();
     229        25669 :         for (const std::pair<const E*, const E*>& follower : from->getViaSuccessors(vClass)) {
     230            0 :             std::vector<const E*> tmp;
     231         9118 :             compute(follower.first, to, vehicle, msTime, tmp, true);
     232         9118 :             if (tmp.size() > 0) {
     233         8531 :                 double effort = recomputeCosts(tmp, vehicle, msTime);
     234         8531 :                 if (effort < minEffort) {
     235              :                     minEffort = effort;
     236         8120 :                     best = tmp;
     237              :                 }
     238              :             }
     239              :         }
     240         7433 :         if (minEffort != std::numeric_limits<double>::max()) {
     241         6891 :             into.push_back(from);
     242              :             std::copy(best.begin(), best.end(), std::back_inserter(into));
     243              :             return true;
     244          542 :         } else if (!silent && myErrorMsgHandler != nullptr) {
     245          140 :             myErrorMsgHandler->informf(TL("No connection between edge '%' and edge '%' found."), from->getID(), to->getID());
     246              :         }
     247              :         return false;
     248         7433 :     }
     249              : 
     250    185775602 :     inline bool isProhibited(const E* const edge, const V* const vehicle, double t) const {
     251              :         return (myProhibited.size() > 0
     252     21055069 :                 && (myEdgeInfos[edge->getNumericalID()].prohibitedPermissions & vehicle->getVClass()) != vehicle->getVClass()
     253       873822 :                 && myEdgeInfos[edge->getNumericalID()].prohibitionBegin <= t
     254       873818 :                 && myEdgeInfos[edge->getNumericalID()].prohibitionEnd == std::numeric_limits<double>::max())
     255    205969072 :                || (myHavePermissions && edge->prohibits(vehicle)) || (myHaveRestrictions && edge->restricts(vehicle));
     256              :     }
     257              : 
     258              :     inline double getTravelTime(const E* const e, const V* const v, const double t, const double effort) const {
     259     74828309 :         return myTTOperation == nullptr ? effort : (*myTTOperation)(e, v, t);
     260              :     }
     261              : 
     262    204065133 :     inline void updateViaEdgeCost(const E* viaEdge, const V* const v, double& time, double& effort, double& length) const {
     263    298287903 :         while (viaEdge != nullptr && viaEdge->isInternal()) {
     264     94222770 :             const double viaEffortDelta = this->getEffort(viaEdge, v, time);
     265     94222770 :             time += getTravelTime(viaEdge, v, time, viaEffortDelta);
     266     94222770 :             effort += viaEffortDelta;
     267     94222770 :             length += viaEdge->getLength();
     268     94222770 :             viaEdge = viaEdge->getViaSuccessors().front().second;
     269              :         }
     270    204065133 :     }
     271              : 
     272     44508915 :     inline void updateViaCost(const E* const prev, const E* const e, const V* const v, double& time, double& effort, double& length) const {
     273     44508915 :         if (prev != nullptr) {
     274     56093190 :             for (const std::pair<const E*, const E*>& follower : prev->getViaSuccessors()) {
     275     56089040 :                 if (follower.first == e) {
     276     34067295 :                     updateViaEdgeCost(follower.second, v, time, effort, length);
     277              :                     break;
     278              :                 }
     279              :             }
     280              :         }
     281     44508915 :         const double effortDelta = this->getEffort(e, v, time);
     282     44508915 :         effort += effortDelta;
     283     44508915 :         time += getTravelTime(e, v, time, effortDelta);
     284     44508915 :         length += e->getLength();
     285     44508915 :     }
     286              : 
     287              :     bool isValid(const std::vector<const E*>& edges, const V* const v, double t) const {
     288      2847663 :         for (const E* const e : edges) {
     289      2412545 :             if (isProhibited(e, v, t)) {
     290              :                 return false;
     291              :             }
     292              :         }
     293              :         return true;
     294              :     }
     295              : 
     296     10225841 :     virtual double recomputeCosts(const std::vector<const E*>& edges, const V* const v, SUMOTime msTime, double* lengthp = nullptr) const {
     297     10225841 :         double time = STEPS2TIME(msTime);
     298     10225841 :         double effort = 0.;
     299     10225841 :         double length = 0.;
     300     10225841 :         if (lengthp == nullptr) {
     301              :             lengthp = &length;
     302              :         } else {
     303         5427 :             *lengthp = 0.;
     304              :         }
     305              :         const E* prev = nullptr;
     306     52753025 :         for (const E* const e : edges) {
     307     42527184 :             updateViaCost(prev, e, v, time, effort, *lengthp);
     308              :             prev = e;
     309              :         }
     310     10225841 :         return effort;
     311              :     }
     312              : 
     313              : 
     314      6281224 :     inline double recomputeCostsPos(const std::vector<const E*>& edges, const V* const v, double fromPos, double toPos, SUMOTime msTime, double* lengthp = nullptr) const {
     315      6281224 :         double effort = recomputeCosts(edges, v, msTime, lengthp);
     316      6281224 :         if (!edges.empty()) {
     317      6281224 :             const E* first = edges.front();
     318      6281224 :             if (first->getLength() == 0) {
     319      1155240 :                 if (edges.size() > 1 && edges[1]->getLength() > 0) {
     320              :                     first = edges[1];
     321              :                 } else {
     322              :                     return effort;
     323              :                 }
     324              :             }
     325      6281224 :             const E* last = edges.back();
     326      6281224 :             if (last->getLength() == 0) {
     327      1155352 :                 if (edges.size() > 1 && edges[edges.size() - 2]->getLength() > 0) {
     328              :                     last = edges[edges.size() - 2];
     329              :                 } else {
     330              :                     return effort;
     331              :                 }
     332              :             }
     333              :             assert(first->getLength() > 0);
     334              :             assert(last->getLength() > 0);
     335      6281224 :             double firstEffort = this->getEffort(first, v, STEPS2TIME(msTime));
     336      6281224 :             double lastEffort = this->getEffort(last, v, STEPS2TIME(msTime));
     337      6281224 :             effort -= firstEffort * fromPos / first->getLength();
     338      6281224 :             effort -= lastEffort * (last->getLength() - toPos) / last->getLength();
     339      6281224 :             if (lengthp != nullptr) {
     340         5427 :                 (*lengthp) -= fromPos + last->getLength() - toPos;
     341              :             }
     342              :         }
     343              :         return effort;
     344              :     }
     345              : 
     346              : 
     347              :     inline double setHint(const typename std::vector<const E*>::const_iterator routeBegin, const typename std::vector<const E*>::const_iterator routeEnd, const V* const v, SUMOTime msTime) {
     348              :         double time = STEPS2TIME(msTime);
     349              :         double effort = 0.;
     350              :         double length = 0.;
     351              :         const EdgeInfo* prev = &myEdgeInfos[(*routeBegin)->getNumericalID()];
     352              :         init((*routeBegin)->getNumericalID(), msTime);
     353              :         for (auto e = routeBegin + 1; e != routeEnd; ++e) {
     354              :             if (isProhibited(*e, v)) {
     355              :                 return -1;
     356              :             }
     357              :             auto& edgeInfo = myEdgeInfos[(*e)->getNumericalID()];
     358              :             edgeInfo.heuristicEffort = effort;
     359              :             edgeInfo.prev = prev;
     360              :             updateViaCost(prev->edge, *e, v, time, effort, length);
     361              :             edgeInfo.effort = effort;
     362              :             edgeInfo.leaveTime = time;
     363              :             myFound.push_back(&edgeInfo);
     364              :             prev = &edgeInfo;
     365              : #ifdef ROUTER_DEBUG_HINT
     366              :             if (ROUTER_DEBUG_COND) {
     367              :                 std::cout << "DEBUG: hit=" << (*e)->getID()
     368              :                           << " TT=" << edgeInfo.effort
     369              :                           << " EF=" << this->getEffort(*e, v, edgeInfo.leaveTime)
     370              :                           << " HT=" << edgeInfo.heuristicEffort << "\n";
     371              :             }
     372              : #endif
     373              :         }
     374              :         return effort;
     375              :     }
     376              : 
     377              : 
     378    229871432 :     inline double getEffort(const E* const e, const V* const v, double t) const {
     379     14055978 :         if (this->myProhibited.size() > 0 && myEdgeInfos.size() > 0
     380     14055546 :                 && myEdgeInfos[e->getNumericalID()].prohibitionEnd > t
     381         1775 :                 && myEdgeInfos[e->getNumericalID()].prohibitionBegin <= t
     382    229873207 :                 && (myEdgeInfos[e->getNumericalID()].prohibitedPermissions & v->getVClass()) != v->getVClass()) {
     383              :             // pass edge after prohibition ends
     384         1708 :             return (myEdgeInfos[e->getNumericalID()].prohibitionEnd - t) + (*myOperation)(e, v, myEdgeInfos[e->getNumericalID()].prohibitionEnd);
     385              :         } else {
     386    229869724 :             return (*myOperation)(e, v, t);
     387              :         }
     388              :     }
     389              : 
     390              :     inline void startQuery() {
     391      4653699 :         myNumQueries++;
     392      4653699 :         myQueryStartTime = SysUtils::getCurrentMillis();
     393              :     }
     394              : 
     395              :     inline void endQuery(int visits) {
     396      4653699 :         myQueryVisits += visits;
     397      4653699 :         myQueryTimeSum += (SysUtils::getCurrentMillis() - myQueryStartTime);
     398              :     }
     399              : 
     400        81485 :     virtual void setBulkMode(const bool mode) {
     401        81989 :         myBulkMode = mode;
     402        81485 :     }
     403              : 
     404              :     inline void setAutoBulkMode(const bool mode) {
     405         2707 :         myAutoBulkMode = mode;
     406            4 :     }
     407              : 
     408      1972576 :     virtual void prohibit(const Prohibitions& toProhibit) {
     409      3666859 :         for (auto item : this->myProhibited) {
     410      1694283 :             myEdgeInfos[item.first->getNumericalID()].prohibitedPermissions = SVCAll;
     411      1694283 :             myEdgeInfos[item.first->getNumericalID()].prohibitionBegin = -1;
     412      1694283 :             myEdgeInfos[item.first->getNumericalID()].prohibitionEnd = -1;
     413              :         }
     414      3672358 :         for (auto item : toProhibit) {
     415      1699782 :             myEdgeInfos[item.first->getNumericalID()].prohibitionBegin = item.second.begin;
     416      1699782 :             myEdgeInfos[item.first->getNumericalID()].prohibitionEnd = item.second.end;
     417      1699782 :             myEdgeInfos[item.first->getNumericalID()].prohibitedPermissions = item.second.permissions;
     418              :             //std::cout << item.first->getID() << " " << item.second.begin << " " << item.second.end << " (" << getVehicleClassNames(item.second.permissions) << "\n";
     419              :         }
     420              :         this->myProhibited = toProhibit;
     421      1972576 :     }
     422              : 
     423              :     bool hasProhibitions() const {
     424              :         return this->myProhibited.size() > 0;
     425              :     }
     426              : 
     427           20 :     virtual bool supportsProhibitions() const {
     428           20 :         return true;
     429              :     }
     430              : 
     431              :     /// Builds the path from marked edges
     432      4604938 :     void buildPathFrom(const typename SUMOAbstractRouter<E, V>::EdgeInfo* rbegin, std::vector<const E*>& edges) {
     433       307021 :         std::vector<const E*> tmp;
     434     24972463 :         while (rbegin != nullptr) {
     435     20367525 :             tmp.push_back(rbegin->edge);
     436     20367525 :             rbegin = rbegin->prev;
     437              :         }
     438              :         std::copy(tmp.rbegin(), tmp.rend(), std::back_inserter(edges));
     439      4604938 :     }
     440              : 
     441              : protected:
     442              :     /// @brief the handler for routing errors
     443              :     MsgHandler* myErrorMsgHandler;
     444              : 
     445              :     /// @brief The object's operation to perform.
     446              :     Operation myOperation;
     447              : 
     448              :     /// @brief The object's operation to perform for travel times
     449              :     Operation myTTOperation;
     450              : 
     451              :     /// @brief whether we are currently operating several route queries in a bulk
     452              :     bool myBulkMode;
     453              : 
     454              :     /// @brief whether we are currently trying to detect bulk mode automatically
     455              :     bool myAutoBulkMode;
     456              : 
     457              :     /// @brief whether we are already initialized
     458              :     bool myAmClean;
     459              : 
     460              :     /// @brief whether edge permissions need to be considered
     461              :     const bool myHavePermissions;
     462              : 
     463              :     /// @brief whether edge restrictions need to be considered
     464              :     const bool myHaveRestrictions;
     465              : 
     466              :     /// The list of explicitly prohibited edges and estimated end time of prohibition
     467              :     Prohibitions myProhibited;
     468              : 
     469              :     /// The container of edge information
     470              :     std::vector<typename SUMOAbstractRouter<E, V>::EdgeInfo> myEdgeInfos;
     471              : 
     472              :     /// A container for reusage of the min edge heap
     473              :     std::vector<typename SUMOAbstractRouter<E, V>::EdgeInfo*> myFrontierList;
     474              :     /// @brief list of visited Edges (for resetting)
     475              :     std::vector<typename SUMOAbstractRouter<E, V>::EdgeInfo*> myFound;
     476              : 
     477              : private:
     478              :     /// @brief the type of this router
     479              :     const std::string myType;
     480              : 
     481              :     /// @brief counters for performance logging
     482              :     long long int myQueryVisits;
     483              :     long long int myNumQueries;
     484              :     /// @brief the time spent querying in milliseconds
     485              :     long long int myQueryStartTime;
     486              :     long long int myQueryTimeSum;
     487              : 
     488              : private:
     489              :     /// @brief Invalidated assignment operator
     490              :     SUMOAbstractRouter& operator=(const SUMOAbstractRouter& s) = delete;
     491              : };
        

Generated by: LCOV version 2.0-1