LCOV - code coverage report
Current view: top level - src/microsim/devices - MSRoutingEngine.h (source / functions) Coverage Total Hit
Test: lcov.info Lines: 100.0 % 6 6
Test Date: 2024-11-22 15:46:21 Functions: 100.0 % 1 1

            Line data    Source code
       1              : /****************************************************************************/
       2              : // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
       3              : // Copyright (C) 2007-2024 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    MSRoutingEngine.h
      15              : /// @author  Michael Behrisch
      16              : /// @author  Daniel Krajzewicz
      17              : /// @author  Jakob Erdmann
      18              : /// @date    Tue, 04 Dec 2007
      19              : ///
      20              : // A device that performs vehicle rerouting based on current edge speeds
      21              : /****************************************************************************/
      22              : #pragma once
      23              : #include <config.h>
      24              : 
      25              : #include <set>
      26              : #include <vector>
      27              : #include <map>
      28              : #include <thread>
      29              : #include <utils/common/SUMOTime.h>
      30              : #include <utils/common/WrappingCommand.h>
      31              : #include <utils/router/AStarRouter.h>
      32              : #include <microsim/MSRouterDefs.h>
      33              : 
      34              : #ifdef HAVE_FOX
      35              : #include <utils/foxtools/MFXWorkerThread.h>
      36              : #endif
      37              : 
      38              : 
      39              : // ===========================================================================
      40              : // class declarations
      41              : // ===========================================================================
      42              : class MSTransportable;
      43              : 
      44              : 
      45              : // ===========================================================================
      46              : // class definitions
      47              : // ===========================================================================
      48              : /**
      49              :  * @class MSRoutingEngine
      50              :  * @brief A device that performs vehicle rerouting based on current edge speeds
      51              :  *
      52              :  * The routing-device system consists of in-vehicle devices that perform a routing
      53              :  *  and a simulation-wide (static) methods for colecting edge weights.
      54              :  *
      55              :  * The edge weights container "myEdgeSpeeds" is pre-initialised as soon as one
      56              :  *  device is built and is kept updated via an event that adapts it to the current
      57              :  *  mean speed on the simulated network's edges.
      58              :  *
      59              :  * A device is assigned to a vehicle using the common explicit/probability - procedure.
      60              :  *
      61              :  * A device computes a new route for a vehicle as soon as the vehicle is inserted
      62              :  *  (within "enterLaneAtInsertion") - and, if the given period is larger than 0 - each
      63              :  *  x time steps where x is the period. This is triggered by an event that executes
      64              :  *  "wrappedRerouteCommandExecute".
      65              :  */
      66              : class MSRoutingEngine {
      67              : public:
      68              :     /// @brief intialize period edge weight update
      69              :     static void initWeightUpdate();
      70              : 
      71              :     /// @brief initialize the edge weights if not done before
      72              :     static void initEdgeWeights(SUMOVehicleClass svc);
      73              : 
      74              :     /// @brief returns whether any edge weight updates will take place
      75              :     static bool hasEdgeUpdates() {
      76      1513892 :         return myEdgeWeightSettingCommand != nullptr;
      77              :     }
      78              : 
      79              :     /// @brief Information when the last edge weight adaptation occurred
      80              :     static SUMOTime getLastAdaptation() {
      81      2202374 :         return myLastAdaptation;
      82              :     }
      83              : 
      84              :     /// @brief return the cached route or nullptr on miss
      85              :     static ConstMSRoutePtr getCachedRoute(const std::pair<const MSEdge*, const MSEdge*>& key);
      86              : 
      87              :     static void initRouter(SUMOVehicle* vehicle = nullptr);
      88              : 
      89              :     /// @brief initiate the rerouting, create router / thread pool on first use
      90              :     static void reroute(SUMOVehicle& vehicle, const SUMOTime currentTime, const std::string& info,
      91              :                         const bool onInit = false, const bool silent = false, const MSEdgeVector& prohibited = MSEdgeVector());
      92              : 
      93              :     /// @brief initiate the person rerouting, create router / thread pool on first use
      94              :     static void reroute(MSTransportable& t, const SUMOTime currentTime, const std::string& info,
      95              :                         const bool onInit = false, const bool silent = false, const MSEdgeVector& prohibited = MSEdgeVector());
      96              : 
      97              :     /// @brief adapt the known travel time for an edge
      98              :     static void setEdgeTravelTime(const MSEdge* const edge, const double travelTime);
      99              : 
     100              :     /// @brief deletes the router instance
     101              :     static void cleanup();
     102              : 
     103              :     /// @brief returns whether any routing actions take place
     104              :     static bool isEnabled() {
     105    183543721 :         return !myWithTaz && myAdaptationInterval >= 0;
     106              :     }
     107              : 
     108              :     /// @brief return the vehicle router instance
     109              :     static MSVehicleRouter& getRouterTT(const int rngIndex,
     110              :                                         SUMOVehicleClass svc,
     111              :                                         const MSEdgeVector& prohibited = MSEdgeVector());
     112              : 
     113              :     /// @brief return the person router instance
     114              :     static MSTransportableRouter& getIntermodalRouterTT(const int rngIndex,
     115              :             const MSEdgeVector& prohibited = MSEdgeVector());
     116              : 
     117              :     /** @brief Returns the effort to pass an edge
     118              :     *
     119              :     * This method is given to the used router in order to obtain the efforts
     120              :     *  to pass an edge from the internal edge weights container.
     121              :     *
     122              :     * The time is not used, here, as the current simulation state is
     123              :     *  used in an aggregated way.
     124              :     *
     125              :     * @param[in] e The edge for which the effort to be passed shall be returned
     126              :     * @param[in] v The vehicle that is rerouted
     127              :     * @param[in] t The time for which the effort shall be returned
     128              :     * @return The effort (time to pass in this case) for an edge
     129              :     * @see DijkstraRouter_ByProxi
     130              :     */
     131              :     static double getEffort(const MSEdge* const e, const SUMOVehicle* const v, double t);
     132              :     static double getEffortBike(const MSEdge* const e, const SUMOVehicle* const v, double t);
     133              :     static double getEffortExtra(const MSEdge* const e, const SUMOVehicle* const v, double t);
     134              :     static SUMOAbstractRouter<MSEdge, SUMOVehicle>::Operation myEffortFunc;
     135              : 
     136              :     /// @brief return current travel speed assumption
     137              :     static double getAssumedSpeed(const MSEdge* edge, const SUMOVehicle* veh);
     138              : 
     139              :     /// @brief whether taz-routing is enabled
     140              :     static bool withTaz() {
     141       432992 :         return myWithTaz;
     142              :     }
     143              : 
     144              :     /// @brief record actual travel time for an edge
     145              :     static void addEdgeTravelTime(const MSEdge& edge, const SUMOTime travelTime);
     146              : 
     147              : #ifdef HAVE_FOX
     148              :     static void waitForAll();
     149              : #endif
     150              : 
     151              : 
     152              : private:
     153              : #ifdef HAVE_FOX
     154              :     /**
     155              :      * @class RoutingTask
     156              :      * @brief the routing task which mainly calls reroute of the vehicle
     157              :      */
     158              :     class RoutingTask : public MFXWorkerThread::Task {
     159              :     public:
     160       176487 :         RoutingTask(SUMOVehicle& v, const SUMOTime time, const std::string& info,
     161              :                     const bool onInit, const bool silent, const MSEdgeVector& prohibited)
     162       176487 :             : myVehicle(v), myTime(time), myInfo(info), myOnInit(onInit), mySilent(silent), myProhibited(prohibited) {}
     163              :         void run(MFXWorkerThread* context);
     164              :     private:
     165              :         SUMOVehicle& myVehicle;
     166              :         const SUMOTime myTime;
     167              :         const std::string myInfo;
     168              :         const bool myOnInit;
     169              :         const bool mySilent;
     170              :         const MSEdgeVector myProhibited;
     171              :     private:
     172              :         /// @brief Invalidated assignment operator.
     173              :         RoutingTask& operator=(const RoutingTask&) = delete;
     174              :     };
     175              : #endif
     176              : 
     177              :     /// @name Network state adaptation
     178              :     /// @{
     179              : 
     180              :     /** @brief Adapt edge efforts by the current edge states
     181              :      *
     182              :      * This method is called by the event handler at the end of a simulation
     183              :      *  step. The current edge weights are combined with the previously stored.
     184              :      *
     185              :      * @param[in] currentTime The current simulation time
     186              :      * @return The offset to the next call (always 1 in this case - edge weights are updated each time step)
     187              :      * @todo Describe how the weights are adapted
     188              :      * @see MSEventHandler
     189              :      * @see StaticCommand
     190              :      */
     191              :     static SUMOTime adaptEdgeEfforts(SUMOTime currentTime);
     192              : 
     193              :     static double patchSpeedForTurns(const MSEdge* edge, double currSpeed);
     194              :     /// @}
     195              : 
     196              :     /// @brief initialized edge speed storage into the given containers
     197              :     static void _initEdgeWeights(std::vector<double>& edgeSpeeds, std::vector<std::vector<double> >& pastEdgeSpeeds);
     198              : 
     199              :     /// @brief returns RNG associated with the current thread
     200              :     static SumoRNG* getThreadRNG();
     201              : 
     202              : private:
     203              :     /// @brief The weights adaptation/overwriting command
     204              :     static Command* myEdgeWeightSettingCommand;
     205              : 
     206              :     /// @brief Information which weight prior edge efforts have
     207              :     static double myAdaptationWeight;
     208              : 
     209              :     /// @brief At which time interval the edge weights get updated
     210              :     static SUMOTime myAdaptationInterval;
     211              : 
     212              :     /// @brief Information when the last edge weight adaptation occurred
     213              :     static SUMOTime myLastAdaptation;
     214              : 
     215              :     /// @brief The number of steps for averaging edge speeds (ring-buffer)
     216              :     static int myAdaptationSteps;
     217              : 
     218              :     /// @brief The current index in the pastEdgeSpeed ring-buffer
     219              :     static int myAdaptationStepsIndex;
     220              : 
     221              :     typedef std::pair<SUMOTime, int> TimeAndCount;
     222              : 
     223              :     /// @brief The container of edge speeds
     224              :     static std::vector<double> myEdgeSpeeds;
     225              :     static std::vector<double> myEdgeBikeSpeeds;
     226              : 
     227              :     /// @brief Sum of travel times experienced by equipped vehicles for each edge
     228              :     static std::vector<TimeAndCount> myEdgeTravelTimes;
     229              : 
     230              :     /// @brief The container of past edge speeds (when using a simple moving average)
     231              :     static std::vector<std::vector<double> > myPastEdgeSpeeds;
     232              :     static std::vector<std::vector<double> > myPastEdgeBikeSpeeds;
     233              : 
     234              :     /// @brief whether taz shall be used at initial rerouting
     235              :     static bool myWithTaz;
     236              : 
     237              :     /// @brief whether separate speeds for bicycles shall be tracked
     238              :     static bool myBikeSpeeds;
     239              : 
     240              :     /// @brief The router to use
     241              :     static MSRouterProvider* myRouterProvider;
     242              : 
     243              :     static std::map<std::thread::id, SumoRNG*> myThreadRNGs;
     244              :     static bool myHaveRoutingThreads;
     245              : 
     246              :     /// @brief The container of pre-calculated routes
     247              :     static std::map<std::pair<const MSEdge*, const MSEdge*>, ConstMSRoutePtr> myCachedRoutes;
     248              : 
     249              :     /// @brief Coefficient for factoring edge priority into routing weight
     250              :     static double myPriorityFactor;
     251              : 
     252              :     /// @brief Minimum priority for all edges
     253              :     static double myMinEdgePriority;
     254              :     /// @brief the difference between maximum and minimum priority for all edges
     255              :     static double myEdgePriorityRange;
     256              : 
     257              : #ifdef HAVE_FOX
     258              :     /// @brief Mutex for accessing the route cache
     259              :     static FXMutex myRouteCacheMutex;
     260              : #endif
     261              : 
     262              : private:
     263              :     /// @brief Invalidated copy constructor.
     264              :     MSRoutingEngine(const MSRoutingEngine&);
     265              : 
     266              :     /// @brief Invalidated assignment operator.
     267              :     MSRoutingEngine& operator=(const MSRoutingEngine&);
     268              : 
     269              : 
     270              : };
        

Generated by: LCOV version 2.0-1