LCOV - code coverage report
Current view: top level - src/microsim - MSEdgeControl.h (source / functions) Coverage Total Hit
Test: lcov.info Lines: 100.0 % 13 13
Test Date: 2024-10-24 15:46:30 Functions: 100.0 % 2 2

            Line data    Source code
       1              : /****************************************************************************/
       2              : // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
       3              : // Copyright (C) 2001-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    MSEdgeControl.h
      15              : /// @author  Christian Roessel
      16              : /// @author  Daniel Krajzewicz
      17              : /// @author  Jakob Erdmann
      18              : /// @author  Christoph Sommer
      19              : /// @author  Sascha Krieg
      20              : /// @author  Michael Behrisch
      21              : /// @date    Mon, 09 Apr 2001
      22              : ///
      23              : // Stores edges and lanes, performs moving of vehicle
      24              : /****************************************************************************/
      25              : #pragma once
      26              : #include <config.h>
      27              : 
      28              : #include <vector>
      29              : #include <map>
      30              : #include <string>
      31              : #include <iostream>
      32              : #include <list>
      33              : #include <set>
      34              : #include <queue>
      35              : #include <utils/common/SUMOTime.h>
      36              : #include <utils/common/Named.h>
      37              : #include <utils/common/StopWatch.h>
      38              : #include <utils/router/SUMOAbstractRouter.h>
      39              : #include <utils/router/RouterProvider.h>
      40              : #include <utils/vehicle/SUMOVehicle.h>
      41              : #include <microsim/MSRouterDefs.h>
      42              : 
      43              : #include <utils/foxtools/MFXSynchQue.h>
      44              : #include <utils/foxtools/MFXSynchSet.h>
      45              : //#define THREAD_POOL
      46              : #ifdef THREAD_POOL
      47              : #include <utils/threadpool/WorkStealingThreadPool.h>
      48              : #else
      49              : #ifdef HAVE_FOX
      50              : #include <utils/foxtools/MFXWorkerThread.h>
      51              : #endif
      52              : #endif
      53              : 
      54              : 
      55              : // ===========================================================================
      56              : // class declarations
      57              : // ===========================================================================
      58              : class OutputDevice;
      59              : 
      60              : 
      61              : // ===========================================================================
      62              : // class definitions
      63              : // ===========================================================================
      64              : /**
      65              :  * @class MSEdgeControl
      66              :  * @brief Stores edges and lanes, performs moving of vehicle
      67              :  *
      68              :  * In order to avoid touching all lanes, even the empty ones, this class stores
      69              :  *  and updates the information about "active" lanes, those that have at least
      70              :  *  one vehicle on them. During longitudinal movement, this can be simply
      71              :  *  achieved through return values of the MSLane-methods, signalling either
      72              :  *  that the lane got active or inactive. This is but not possible when
      73              :  *  changing lanes, we have to go through the lanes, here. Also, we have to
      74              :  *  add lanes on which a vehicle was inserted, separately, doing this into
      75              :  *  ("myChangedStateLanes") which entries are integrated at the begin of is step
      76              :  *  in "patchActiveLanes".
      77              :  */
      78              : class MSEdgeControl {
      79              : 
      80              : public:
      81              :     /** @brief Constructor
      82              :      *
      83              :      * Builds LaneUsage information for each lane and assigns them to lanes.
      84              :      *
      85              :      * @param[in] edges The loaded edges
      86              :      * @todo Assure both containers are not 0
      87              :      */
      88              :     MSEdgeControl(const std::vector< MSEdge* >& edges);
      89              : 
      90              : 
      91              :     /// @brief Destructor.
      92              :     ~MSEdgeControl();
      93              : 
      94              : 
      95              :     /** @brief Resets information whether a lane is active for all lanes
      96              :      *
      97              :      * For each lane in "myChangedStateLanes": if the lane has at least one vehicle
      98              :      *  and is not marked as being active, it is added to the list og active lanes
      99              :      *  and marked as being active.
     100              :      */
     101              :     void patchActiveLanes();
     102              : 
     103              : 
     104              :     /// @name Interfaces for longitudinal vehicle movement
     105              :     /// @{
     106              : 
     107              :     /** @brief Compute safe velocities for all vehicles based on positions and
     108              :      * speeds from the last time step. Also registers
     109              :      * ApproachingVehicleInformation for all links
     110              :      *
     111              :      * This method goes through all active lanes calling their "planMovements" method.
     112              :      * @see MSLane::planMovements
     113              :      */
     114              :     void planMovements(SUMOTime t);
     115              : 
     116              :     /** @brief Register junction approaches for all vehicles after velocities
     117              :      * have been planned. This is a prerequisite for executeMovements
     118              :      *
     119              :      * This method goes through all active lanes calling their "setJunctionApproaches" method.
     120              :      */
     121              :     void setJunctionApproaches(SUMOTime t);
     122              : 
     123              : 
     124              :     /** @brief Executes planned vehicle movements with regards to right-of-way
     125              :      *
     126              :      * This method goes through all active lanes calling their executeMovements
     127              :      * method which causes vehicles to update their positions and speeds.
     128              :      * Lanes which receive new vehicles are stored in myWithVehicles2Integrate
     129              :      * After movements are executed the vehicles in myWithVehicles2Integrate are
     130              :      * put onto their new lanes
     131              :      * This method also updates the "active" status of lanes
     132              :      *
     133              :      * @see MSLane::executeMovements
     134              :      * @see MSLane::integrateNewVehicle
     135              :      */
     136              :     void executeMovements(SUMOTime t);
     137              : 
     138              :     void needsVehicleIntegration(MSLane* const l) {
     139     29744817 :         myWithVehicles2Integrate.push_back(l);
     140     15389770 :     }
     141              :     /// @}
     142              : 
     143              : 
     144              :     /** @brief Moves (precomputes) critical vehicles
     145              :      *
     146              :      * Calls "changeLanes" of each of the multi-lane edges. Check then for this
     147              :      *  edge whether a lane got active, adding it to "myActiveLanes" and marking
     148              :      *  it as active in such cases.
     149              :      *
     150              :      * @see MSEdge::changeLanes
     151              :      */
     152              :     void changeLanes(const SUMOTime t);
     153              : 
     154              : 
     155              :     /** @brief Detect collisions
     156              :      *
     157              :      * Calls "detectCollisions" of each lane.
     158              :      * Shouldn't be necessary if model-implementation is correct.
     159              :      * The parameter is simply passed to the lane-instance for reporting.
     160              :      *
     161              :      * @param[in] timestep The current time step
     162              :      * @param[in] stage The current stage within the simulation step
     163              :      * @note see MSNet::simulationStep
     164              :      */
     165              :     void detectCollisions(SUMOTime timestep, const std::string& stage);
     166              : 
     167              : 
     168              :     /** @brief Returns loaded edges
     169              :      *
     170              :      * @return the container storing one-lane edges
     171              :      * @todo Check: Is this secure?
     172              :      */
     173              :     const MSEdgeVector& getEdges() const {
     174              :         return myEdges;
     175              :     }
     176              : 
     177              : 
     178              :     /** @brief Informs the control that the given lane got active
     179              :      *
     180              :      * @param[in] l The activated lane
     181              :      * @todo Check for l==0?
     182              :      */
     183              :     void gotActive(MSLane* l);
     184              : 
     185              :     /// @brief trigger collision checking for inactive lane
     186              :     void checkCollisionForInactive(MSLane* l);
     187              : 
     188              :     /// @brief apply additional restrictions
     189              :     void setAdditionalRestrictions();
     190              : 
     191              :     /// @brief update meso edge type parameters
     192              :     void setMesoTypes();
     193              : 
     194              :     /** @brief Saves the current state into the given stream
     195              :      */
     196              :     void saveState(OutputDevice& out);
     197              : 
     198              :     /** @brief Reconstruct the current state
     199              :      */
     200              :     void setActiveLanes(std::list<MSLane*> lanes);
     201              : 
     202              : 
     203              : #ifndef THREAD_POOL
     204              : #ifdef HAVE_FOX
     205              :     MFXWorkerThread::Pool& getThreadPool() {
     206    166829851 :         return myThreadPool;
     207              :     }
     208              : #endif
     209              : #endif
     210              : 
     211              : public:
     212              :     /**
     213              :      * @struct LaneUsage
     214              :      * @brief A structure holding some basic information about a simulated lane
     215              :      *
     216              :      * To fasten up speed, this structure holds the number of vehicles using
     217              :      *  a lane and the lane's neighbours. Only lanes that are occupied are
     218              :      *  forced to compute the vehicles longitunidal movement.
     219              :      *
     220              :      * The information about a lane's neighbours speed up the computation
     221              :      *  of the lane changing.
     222              :      */
     223              :     struct LaneUsage {
     224              :         /// @brief The described lane
     225              :         MSLane* lane;
     226              :         /// @brief Information whether this lane is active
     227              :         bool amActive;
     228              :         /// @brief Information whether this lane belongs to a multi-lane edge
     229              :         bool haveNeighbors;
     230              :     };
     231              : 
     232              : #ifdef HAVE_FOX
     233              :     /**
     234              :      * @class WorkerThread
     235              :      * @brief the thread which provides the router instance as context
     236              :      */
     237              :     class WorkerThread : public MFXWorkerThread {
     238              :     public:
     239              :         WorkerThread(MFXWorkerThread::Pool& pool)
     240        16162 :             : MFXWorkerThread(pool), myRouterProvider(nullptr) {}
     241              : 
     242              :         bool setRouterProvider(MSRouterProvider* routerProvider) {
     243         3108 :             if (myRouterProvider == nullptr) {
     244          800 :                 myRouterProvider = routerProvider;
     245         2308 :                 return true;
     246              :             }
     247              :             return false;
     248              :         }
     249              :         MSVehicleRouter& getRouter(SUMOVehicleClass svc) const {
     250       211851 :             return myRouterProvider->getVehicleRouter(svc);
     251              :         }
     252              :         MSTransportableRouter& getIntermodalRouter() const {
     253           11 :             return myRouterProvider->getIntermodalRouter();
     254              :         }
     255        31780 :         virtual ~WorkerThread() {
     256        15890 :             stop();
     257        15890 :             delete myRouterProvider;
     258        31780 :         }
     259              :     private:
     260              :         MSRouterProvider* myRouterProvider;
     261              :     };
     262              : #endif
     263              : 
     264              : private:
     265              :     /// @brief Loaded edges
     266              :     MSEdgeVector myEdges;
     267              : 
     268              :     /// @brief Definition of a container about a lane's number of vehicles and neighbors
     269              :     typedef std::vector<LaneUsage> LaneUsageVector;
     270              : 
     271              :     /// @brief Information about lanes' number of vehicles and neighbors
     272              :     LaneUsageVector myLanes;
     273              : 
     274              :     /// @brief The list of active (not empty) lanes
     275              :     std::list<MSLane*> myActiveLanes;
     276              : 
     277              :     /// @brief A storage for lanes which shall be integrated because vehicles have moved onto them
     278              :     MFXSynchQue<MSLane*, std::vector<MSLane*> > myWithVehicles2Integrate;
     279              : 
     280              :     /// @brief Lanes which changed the state without informing the control
     281              :     std::set<MSLane*, ComparatorNumericalIdLess> myChangedStateLanes;
     282              : 
     283              :     /// @brief The list of active (not empty) lanes
     284              :     std::vector<SUMOTime> myLastLaneChange;
     285              : 
     286              :     /// @brief Additional lanes for which collision checking must be performed
     287              :     MFXSynchSet<MSLane*, std::set<MSLane*, ComparatorNumericalIdLess> > myInactiveCheckCollisions;
     288              : 
     289              :     double myMinLengthGeometryFactor;
     290              : 
     291              : #ifdef THREAD_POOL
     292              :     WorkStealingThreadPool<> myThreadPool;
     293              : #else
     294              : #ifdef HAVE_FOX
     295              :     MFXWorkerThread::Pool myThreadPool;
     296              : #endif
     297              : #endif
     298              : 
     299              :     std::vector<StopWatch<std::chrono::nanoseconds> > myStopWatch;
     300              : 
     301              : private:
     302              :     /// @brief Copy constructor.
     303              :     MSEdgeControl(const MSEdgeControl&);
     304              : 
     305              :     /// @brief Assignment operator.
     306              :     MSEdgeControl& operator=(const MSEdgeControl&);
     307              : 
     308              : };
        

Generated by: LCOV version 2.0-1