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

            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    MSInsertionControl.h
      15              : /// @author  Christian Roessel
      16              : /// @author  Daniel Krajzewicz
      17              : /// @author  Michael Behrisch
      18              : /// @author  Jakob Erdmann
      19              : /// @date    Mon, 12 Mar 2001
      20              : ///
      21              : // Inserts vehicles into the network when their departure time is reached
      22              : /****************************************************************************/
      23              : #pragma once
      24              : #include <config.h>
      25              : 
      26              : #include <vector>
      27              : #include <map>
      28              : #include <string>
      29              : #include <set>
      30              : #include <utils/foxtools/MFXSynchSet.h>
      31              : #include <microsim/MSRouterDefs.h>
      32              : #include "MSVehicleContainer.h"
      33              : 
      34              : 
      35              : // ===========================================================================
      36              : // class declarations
      37              : // ===========================================================================
      38              : class MSVehicle;
      39              : class MSVehicleControl;
      40              : class SUMOVehicle;
      41              : class SUMOVehicleParameter;
      42              : 
      43              : 
      44              : // ===========================================================================
      45              : // class definitions
      46              : // ===========================================================================
      47              : /**
      48              :  * @class MSInsertionControl
      49              :  * @brief Inserts vehicles into the network when their departure time is reached
      50              :  *
      51              :  * Holds a list of vehicles which may be filled by vehicles
      52              :  *  read by SUMORouteLoaders. Tries to emit vehicles departing at a time into the
      53              :  *  network as soon this time is reached and keeps them as long the insertion
      54              :  *  fails.
      55              :  *
      56              :  * If a vehicle is emitted, the control about it is given to the lanes.
      57              :  *
      58              :  * Vehicles are not controlled (created, deleted) by this class.
      59              :  *
      60              :  * @todo When a vehicle is deleted due to waiting too long or because of vaporizing, this is not reported anywhere
      61              :  */
      62              : class MSInsertionControl {
      63              : public:
      64              :     /** @brief Constructor
      65              :      *
      66              :      * @param[in] vc The assigned vehicle control (needed for vehicle re-insertion and deletion)
      67              :      * @param[in] maxDepartDelay Vehicles waiting for insertion longer than this time are deleted (-1: no deletion)
      68              :      * @param[in] checkEdgesOnce Whether an edge on which a vehicle could not depart should be ignored in the same step
      69              :      * @param[in] maxVehicleNumber The maximum number of vehicles that should not be exceeded
      70              :      */
      71              :     MSInsertionControl(MSVehicleControl& vc, SUMOTime maxDepartDelay, bool checkEdgesOnce, int maxVehicleNumber, SUMOTime randomDepartOffset);
      72              : 
      73              : 
      74              :     /// @brief Destructor.
      75              :     ~MSInsertionControl();
      76              : 
      77              : 
      78              :     /** @brief Emits vehicles that want to depart at the given time
      79              :      *
      80              :      * All vehicles scheduled for this time are tried to be emitted. This
      81              :      *  includes those with a depart time as the given time and those that
      82              :      *  wait for being emitted due they could not be inserted in previous
      83              :      *  steps.
      84              :      *
      85              :      * For each vehicle, tryInsert is called. If this fails, a vehicle
      86              :      *  keeps within the refused emit containers ("myRefusedEmits1",
      87              :      *  "myRefusedEmits2") so that it may be emitted within the next steps.
      88              :      *
      89              :      * Returns the number of vehicles that could be inserted into the net.
      90              :      *
      91              :      * @param[in] time The current simulation time
      92              :      * @return The number of vehicles that could be inserted into the net
      93              :      */
      94              :     int emitVehicles(SUMOTime time);
      95              : 
      96              : 
      97              :     /** @brief Adds a single vehicle for departure
      98              :      *
      99              :      * The vehicle is added to "myAllVeh".
     100              :      *
     101              :      * @param[in] veh The vehicle to add for later insertion
     102              :      */
     103              :     void add(SUMOVehicle* veh);
     104              : 
     105              : 
     106              :     /** @brief Adds parameter for a vehicle flow for departure
     107              :      *
     108              :      * @param[in] pars The flow parameters to add for later insertion
     109              :      * @param[in] index The current index when loading this flow from a simulation state
     110              :      * @return whether it could be added (no other flow with the same id was present)
     111              :      */
     112              :     bool addFlow(SUMOVehicleParameter* const pars, int index = -1);
     113              : 
     114              : 
     115              :     /** @brief Returns the number of waiting vehicles
     116              :      *
     117              :      * The sizes of refused emits (sum of vehicles in "myRefusedEmits1" and
     118              :      *  "myRefusedEmits2") is returned.
     119              :      *
     120              :      * @return The number of vehicles that could not (yet) be inserted into the net
     121              :      */
     122              :     int getWaitingVehicleNo() const;
     123              : 
     124              :     /// @brief retrieve vehicles waiting for insertion
     125              :     const MSVehicleContainer::VehicleVector& getPendingVehicles() const {
     126              :         return myPendingEmits;
     127              :     }
     128              : 
     129              :     /** @brief Returns the number of flows that are still active
     130              :      *
     131              :      * @return number of active flows
     132              :      */
     133              :     int getPendingFlowCount() const;
     134              : 
     135              :     /// @brief stops trying to emit the given vehicle (because it already departed)
     136              :     void alreadyDeparted(SUMOVehicle* veh);
     137              : 
     138              :     /// @brief stops trying to emit the given vehicle (and delete it)
     139              :     void descheduleDeparture(const SUMOVehicle* veh);
     140              : 
     141              :     /// @brief reverts a previous call to descheduleDeparture (only needed for departPos="random_free")
     142              :     void retractDescheduleDeparture(const SUMOVehicle* veh);
     143              : 
     144              :     /// @brief clears out all pending vehicles from a route, "" for all routes
     145              :     void clearPendingVehicles(const std::string& route);
     146              : 
     147              : 
     148              :     /** @brief Checks for all vehicles whether they can be emitted
     149              :      *
     150              :      * @param[in] time The current simulation time
     151              :      */
     152              :     void determineCandidates(SUMOTime time);
     153              : 
     154              :     /// @brief return the number of pending emits for the given lane
     155              :     int getPendingEmits(const MSLane* lane);
     156              : 
     157              :     void adaptIntermodalRouter(MSTransportableRouter& router) const;
     158              : 
     159              :     /// @brief compute (optional) random offset to the departure time
     160              :     SUMOTime computeRandomDepartOffset() const;
     161              : 
     162              :     /** @brief Saves the current state into the given stream
     163              :      */
     164              :     void saveState(OutputDevice& out);
     165              : 
     166              :     /** @brief Remove all vehicles before quick-loading state */
     167              :     void clearState();
     168              : 
     169              :     /// @brief retrieve internal RNG
     170              :     SumoRNG* getFlowRNG() {
     171           12 :         return &myFlowRNG;
     172              :     }
     173              : 
     174              :     /// @brief checks whether the given flow still exists
     175              :     bool hasFlow(const std::string& id) const {
     176              :         return myFlowIDs.count(id) != 0;
     177              :     }
     178              : 
     179              :     /// @brief return parameters for the given flow
     180              :     const SUMOVehicleParameter* getFlowPars(const std::string& id) const;
     181              : 
     182              :     /// @brief return the last vehicle for the given flow
     183              :     SUMOVehicle* getLastFlowVehicle(const std::string& id) const;
     184              : 
     185              :     /// @brief updates the flow scale value to keep track of TraCI-induced change
     186              :     void updateScale(const std::string vtypeid);
     187              : 
     188              : private:
     189              :     /** @brief Tries to emit the vehicle
     190              :      *
     191              :      * If the insertion fails, it is examined whether the reason was a vaporizing
     192              :      *  edge. If so, the vehicle is deleted. Otherwise, it is checked whether the
     193              :      *  time the vehicle had to wait so far is larger than the maximum allowed
     194              :      *  waiting time. If so, the vehicle is deleted, too. If both does not match,
     195              :      *  the vehicle is reinserted to "refusedEmits" in order to be emitted in
     196              :      *  next steps.
     197              :      *
     198              :      * @param[in] time The current simulation time
     199              :      * @param[in] veh The vehicle to emit
     200              :      * @param[in] refusedEmits Container to insert vehicles that could not be emitted into
     201              :      * @return The number of emitted vehicles (0 or 1)
     202              :      */
     203              :     int tryInsert(SUMOTime time, SUMOVehicle* veh,
     204              :                   MSVehicleContainer::VehicleVector& refusedEmits);
     205              : 
     206              : 
     207              :     /** @brief Adds all vehicles that should have been emitted earlier to the refuse container
     208              :      *
     209              :      * @param[in] time The current simulation time
     210              :      * @todo recheck
     211              :      */
     212              :     void checkCandidates(SUMOTime time, const bool preCheck);
     213              : 
     214              : 
     215              : private:
     216              : 
     217              :     /// @brief init scale value of flow
     218              :     static double initScale(const std::string vtypeid);
     219              : 
     220              : private:
     221              :     /// @brief The assigned vehicle control (needed for vehicle re-insertion and deletion)
     222              :     MSVehicleControl& myVehicleControl;
     223              : 
     224              :     /// @brief All loaded vehicles sorted by their departure time
     225              :     MSVehicleContainer myAllVeh;
     226              : 
     227              :     /// @brief Buffers for vehicles that could not be inserted
     228              :     MSVehicleContainer::VehicleVector myPendingEmits;
     229              : 
     230              :     /// @brief Buffer for vehicles that may be inserted in the current step
     231              :     std::set<SUMOVehicle*> myEmitCandidates;
     232              : 
     233              :     /// @brief Set of vehicles which shall not be inserted anymore
     234              : 
     235              : #ifdef HAVE_FOX
     236              :     MFXSynchSet<const SUMOVehicle*> myAbortedEmits;
     237              : #else
     238              :     std::set<const SUMOVehicle*> myAbortedEmits;
     239              : #endif
     240              : 
     241              :     /** @struct Flow
     242              :      * @brief Definition of vehicle flow with the current index for vehicle numbering
     243              :      */
     244              :     struct Flow {
     245              :         /// @brief The parameters
     246              :         SUMOVehicleParameter* pars;
     247              :         /// @brief the running index
     248              :         int index;
     249              :         /// @brief the type scaling of this flow. Negative value indicates inhomogenous type distribution
     250              :         double scale;
     251              :     };
     252              : 
     253              :     /// @brief Container for periodical vehicle parameters
     254              :     std::vector<Flow> myFlows;
     255              : 
     256              :     /// @brief Cache for periodical vehicle ids and their most recent index for quicker checking
     257              :     std::map<std::string, int> myFlowIDs;
     258              : 
     259              :     /// @brief The maximum waiting time; vehicles waiting longer are deleted (-1: no deletion)
     260              :     SUMOTime myMaxDepartDelay;
     261              : 
     262              :     /// @brief Whether an edge on which a vehicle could not depart should be ignored in the same step
     263              :     bool myEagerInsertionCheck;
     264              : 
     265              :     /// @brief Storage for maximum vehicle number
     266              :     int myMaxVehicleNumber;
     267              : 
     268              :     /// @brief Last time at which pending emits for each edge where counted
     269              :     SUMOTime myPendingEmitsUpdateTime;
     270              : 
     271              :     /// @brief the number of pending emits for each edge in the current time step
     272              :     std::map<const MSLane*, int> myPendingEmitsForLane;
     273              : 
     274              :     /// @brief The maximum random offset to be added to vehicles departure times (non-negative)
     275              :     SUMOTime myMaxRandomDepartOffset;
     276              : 
     277              : private:
     278              :     /// @brief Invalidated copy constructor.
     279              :     MSInsertionControl(const MSInsertionControl&);
     280              : 
     281              :     /// @brief Invalidated assignment operator.
     282              :     MSInsertionControl& operator=(const MSInsertionControl&);
     283              : 
     284              :     /// @brief A random number generator for probabilistic flows
     285              :     SumoRNG myFlowRNG;
     286              : 
     287              : };
        

Generated by: LCOV version 2.0-1