/****************************************************************************/1// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo2// Copyright (C) 2001-2025 German Aerospace Center (DLR) and others.3// This program and the accompanying materials are made available under the4// terms of the Eclipse Public License 2.0 which is available at5// https://www.eclipse.org/legal/epl-2.0/6// This Source Code may also be made available under the following Secondary7// Licenses when the conditions for such availability set forth in the Eclipse8// Public License 2.0 are satisfied: GNU General Public License, version 29// or later which is available at10// https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html11// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later12/****************************************************************************/13/// @file MSInsertionControl.h14/// @author Christian Roessel15/// @author Daniel Krajzewicz16/// @author Michael Behrisch17/// @author Jakob Erdmann18/// @date Mon, 12 Mar 200119///20// Inserts vehicles into the network when their departure time is reached21/****************************************************************************/22#pragma once23#include <config.h>2425#include <vector>26#include <map>27#include <string>28#include <set>29#include <utils/foxtools/MFXSynchSet.h>30#include <microsim/MSRouterDefs.h>31#include "MSVehicleContainer.h"323334// ===========================================================================35// class declarations36// ===========================================================================37class MSVehicle;38class MSVehicleControl;39class SUMOVehicle;40class SUMOVehicleParameter;414243// ===========================================================================44// class definitions45// ===========================================================================46/**47* @class MSInsertionControl48* @brief Inserts vehicles into the network when their departure time is reached49*50* Holds a list of vehicles which may be filled by vehicles51* read by SUMORouteLoaders. Tries to emit vehicles departing at a time into the52* network as soon this time is reached and keeps them as long the insertion53* fails.54*55* If a vehicle is emitted, the control about it is given to the lanes.56*57* Vehicles are not controlled (created, deleted) by this class.58*59* @todo When a vehicle is deleted due to waiting too long or because of vaporizing, this is not reported anywhere60*/61class MSInsertionControl {62public:63/** @brief Constructor64*65* @param[in] vc The assigned vehicle control (needed for vehicle re-insertion and deletion)66* @param[in] maxDepartDelay Vehicles waiting for insertion longer than this time are deleted (-1: no deletion)67* @param[in] checkEdgesOnce Whether an edge on which a vehicle could not depart should be ignored in the same step68* @param[in] maxVehicleNumber The maximum number of vehicles that should not be exceeded69*/70MSInsertionControl(MSVehicleControl& vc, SUMOTime maxDepartDelay, bool checkEdgesOnce, int maxVehicleNumber, SUMOTime randomDepartOffset);717273/// @brief Destructor.74~MSInsertionControl();757677/** @brief Emits vehicles that want to depart at the given time78*79* All vehicles scheduled for this time are tried to be emitted. This80* includes those with a depart time as the given time and those that81* wait for being emitted due they could not be inserted in previous82* steps.83*84* For each vehicle, tryInsert is called. If this fails, a vehicle85* keeps within the refused emit containers ("myRefusedEmits1",86* "myRefusedEmits2") so that it may be emitted within the next steps.87*88* Returns the number of vehicles that could be inserted into the net.89*90* @param[in] time The current simulation time91* @return The number of vehicles that could be inserted into the net92*/93int emitVehicles(SUMOTime time);949596/** @brief Adds a single vehicle for departure97*98* The vehicle is added to "myAllVeh".99*100* @param[in] veh The vehicle to add for later insertion101*/102void add(SUMOVehicle* veh);103104105/** @brief Adds parameter for a vehicle flow for departure106*107* @param[in] pars The flow parameters to add for later insertion108* @param[in] index The current index when loading this flow from a simulation state109* @return whether it could be added (no other flow with the same id was present)110*/111bool addFlow(SUMOVehicleParameter* const pars, int index = -1);112113114/** @brief Returns the number of waiting vehicles115*116* The sizes of refused emits (sum of vehicles in "myRefusedEmits1" and117* "myRefusedEmits2") is returned.118*119* @return The number of vehicles that could not (yet) be inserted into the net120*/121int getWaitingVehicleNo() const;122123/// @brief retrieve vehicles waiting for insertion124const MSVehicleContainer::VehicleVector& getPendingVehicles() const {125return myPendingEmits;126}127128/** @brief Returns the number of flows that are still active129*130* @return number of active flows131*/132int getPendingFlowCount() const;133134/// @brief stops trying to emit the given vehicle (because it already departed)135void alreadyDeparted(SUMOVehicle* veh);136137/// @brief stops trying to emit the given vehicle (and delete it)138void descheduleDeparture(const SUMOVehicle* veh);139140/// @brief reverts a previous call to descheduleDeparture (only needed for departPos="random_free")141void retractDescheduleDeparture(const SUMOVehicle* veh);142143/// @brief clears out all pending vehicles from a route, "" for all routes144void clearPendingVehicles(const std::string& route);145146147/** @brief Checks for all vehicles whether they can be emitted148*149* @param[in] time The current simulation time150*/151void determineCandidates(SUMOTime time);152153/// @brief return the number of pending emits for the given lane154int getPendingEmits(const MSLane* lane);155156void adaptIntermodalRouter(MSTransportableRouter& router) const;157158/// @brief compute (optional) random offset to the departure time159SUMOTime computeRandomDepartOffset() const;160161/** @brief Saves the current state into the given stream162*/163void saveState(OutputDevice& out);164165/** @brief Remove all vehicles before quick-loading state */166void clearState();167168/// @brief retrieve internal RNG169SumoRNG* getFlowRNG() {170return &myFlowRNG;171}172173/// @brief checks whether the given flow still exists174bool hasFlow(const std::string& id) const {175return myFlowIDs.count(id) != 0;176}177178/// @brief return parameters for the given flow179const SUMOVehicleParameter* getFlowPars(const std::string& id) const;180181/// @brief return the last vehicle for the given flow182SUMOVehicle* getLastFlowVehicle(const std::string& id) const;183184/// @brief updates the flow scale value to keep track of TraCI-induced change185void updateScale(const std::string vtypeid);186187bool hasTaxiFlow() const;188189private:190/** @brief Tries to emit the vehicle191*192* If the insertion fails, it is examined whether the reason was a vaporizing193* edge. If so, the vehicle is deleted. Otherwise, it is checked whether the194* time the vehicle had to wait so far is larger than the maximum allowed195* waiting time. If so, the vehicle is deleted, too. If both does not match,196* the vehicle is reinserted to "refusedEmits" in order to be emitted in197* next steps.198*199* @param[in] time The current simulation time200* @param[in] veh The vehicle to emit201* @param[in] refusedEmits Container to insert vehicles that could not be emitted into202* @return The number of emitted vehicles (0 or 1)203*/204int tryInsert(SUMOTime time, SUMOVehicle* veh,205MSVehicleContainer::VehicleVector& refusedEmits);206207208/** @brief Adds all vehicles that should have been emitted earlier to the refuse container209*210* @param[in] time The current simulation time211* @todo recheck212*/213void checkCandidates(SUMOTime time, const bool preCheck);214215216private:217218/// @brief init scale value of flow219static double initScale(const std::string vtypeid);220221static bool hasTaxiDeviceType(const std::string& vtypeId, SumoRNG& rng);222223private:224/// @brief The assigned vehicle control (needed for vehicle re-insertion and deletion)225MSVehicleControl& myVehicleControl;226227/// @brief All loaded vehicles sorted by their departure time228MSVehicleContainer myAllVeh;229230/// @brief Buffers for vehicles that could not be inserted231MSVehicleContainer::VehicleVector myPendingEmits;232233/// @brief Buffer for vehicles that may be inserted in the current step234std::set<SUMOVehicle*> myEmitCandidates;235236/// @brief Set of vehicles which shall not be inserted anymore237238#ifdef HAVE_FOX239MFXSynchSet<const SUMOVehicle*> myAbortedEmits;240#else241std::set<const SUMOVehicle*> myAbortedEmits;242#endif243244/** @struct Flow245* @brief Definition of vehicle flow with the current index for vehicle numbering246*/247struct Flow {248/// @brief The parameters249SUMOVehicleParameter* pars;250/// @brief the running index251int index;252/// @brief the type scaling of this flow. Negative value indicates inhomogenous type distribution253double scale;254};255256/// @brief Container for periodical vehicle parameters257std::vector<Flow> myFlows;258259/// @brief Cache for periodical vehicle ids and their most recent index for quicker checking260std::map<std::string, int> myFlowIDs;261262/// @brief The maximum waiting time; vehicles waiting longer are deleted (-1: no deletion)263SUMOTime myMaxDepartDelay;264265/// @brief Whether an edge on which a vehicle could not depart should be ignored in the same step266bool myEagerInsertionCheck;267268/// @brief Storage for maximum vehicle number269int myMaxVehicleNumber;270271/// @brief Last time at which pending emits for each edge where counted272SUMOTime myPendingEmitsUpdateTime;273274/// @brief the number of pending emits for each edge in the current time step275std::map<const MSLane*, int> myPendingEmitsForLane;276277/// @brief The maximum random offset to be added to vehicles departure times (non-negative)278SUMOTime myMaxRandomDepartOffset;279280private:281/// @brief Invalidated copy constructor.282MSInsertionControl(const MSInsertionControl&);283284/// @brief Invalidated assignment operator.285MSInsertionControl& operator=(const MSInsertionControl&);286287/// @brief A random number generator for probabilistic flows288SumoRNG myFlowRNG;289290};291292293