/****************************************************************************/1// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo2// Copyright (C) 2002-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 RONet.h14/// @author Daniel Krajzewicz15/// @author Michael Behrisch16/// @author Jakob Erdmann17/// @author Yun-Pang Floetteroed18/// @date Sept 200219///20// The router's network representation21/****************************************************************************/22#pragma once23#include <config.h>2425#include <vector>26#include <utils/common/MsgHandler.h>27#include <utils/common/NamedObjectCont.h>28#include <utils/distribution/RandomDistributor.h>29#include <utils/vehicle/SUMOVehicleParameter.h>30#include <utils/vehicle/SUMOVTypeParameter.h>31#include "ROLane.h"32#include "RORoutable.h"33#include "RORouteDef.h"3435#ifdef HAVE_FOX36#include <utils/foxtools/MFXWorkerThread.h>37#endif383940// ===========================================================================41// class declarations42// ===========================================================================43class ROEdge;44class RONode;45class ROPerson;46class ROVehicle;47class ROAbstractEdgeBuilder;48class OptionsCont;49class OutputDevice;5051typedef MapMatcher<ROEdge, ROLane, RONode> ROMapMatcher;5253// ===========================================================================54// class definitions55// ===========================================================================56/**57* @class RONet58* @brief The router's network representation.59*60* A router network is responsible for watching loaded edges, nodes,!!!61*/62class RONet {63public:6465typedef std::map<const SUMOTime, std::vector<RORoutable*> > RoutablesMap;6667/// @brief Constructor68RONet();697071/** @brief Returns the pointer to the unique instance of RONet (singleton).72* @return Pointer to the unique RONet-instance73*/74static RONet* getInstance();757677/// @brief Destructor78virtual ~RONet();798081/** @brief Adds a restriction for an edge type82* @param[in] id The id of the type83* @param[in] svc The vehicle class the restriction refers to84* @param[in] speed The restricted speed85*/86void addRestriction(const std::string& id, const SUMOVehicleClass svc, const double speed);878889/** @brief Returns the restrictions for an edge type90* If no restrictions are present, 0 is returned.91* @param[in] id The id of the type92* @return The mapping of vehicle classes to maximum speeds93*/94const std::map<SUMOVehicleClass, double>* getRestrictions(const std::string& id) const;9596bool hasRestrictions() const {97return !myRestrictions.empty();98}99100/// @name Insertion and retrieval of graph parts101//@{102103/* @brief Adds a read edge to the network104*105* If the edge is already known (another one with the same id exists),106* an error is generated and given to msg-error-handler. The edge107* is deleted in this case and false is returned.108*109* @param[in] edge The edge to add110* @return Whether the edge was added (if not, it was deleted, too)111*/112virtual bool addEdge(ROEdge* edge);113114115/* @brief Adds a district and connecting edges to the network116*117* If the district is already known (another one with the same id exists),118* an error is generated and given to msg-error-handler. The edges119* are deleted in this case and false is returned.120*121* @param[in] id The district to add122* @return Whether the district was added123*/124bool addDistrict(const std::string id, ROEdge* source, ROEdge* sink);125126127/* @brief Adds a district and connecting edges to the network128*129* If the district is already known (another one with the same id exists),130* an error is generated and given to msg-error-handler. The edges131* are deleted in this case and false is returned.132*133* @param[in] id The district to add134* @return Whether the district was added135*/136bool addDistrictEdge(const std::string tazID, const std::string edgeID, const bool isSource);137138/// @brief add a taz for every junction unless a taz with the same id already exists139void addJunctionTaz(ROAbstractEdgeBuilder& eb);140141/// @brief add a taz for every junction unless a taz with the same id already exists142void setBidiEdges(const std::map<ROEdge*, std::string>& bidiMap);143144/** @brief Retrieves all TAZ (districts) from the network145*146* @return The map of all districts147*/148const std::map<std::string, std::pair<std::vector<std::string>, std::vector<std::string> > >& getDistricts() const {149return myDistricts;150}151152/** @brief Retrieves an edge from the network153*154* This is not very pretty, but necessary, though, as routes run155* over instances, not over ids.156*157* @param[in] name The name of the edge to retrieve158* @return The named edge if known, otherwise 0159*/160ROEdge* getEdge(const std::string& name) const {161return myEdges.get(name);162}163164165/** @brief Retrieves an edge from the network when the lane id is given166*167* @param[in] laneID The name of the lane to retrieve the edge for168* @return The edge of the named lane if known, otherwise 0169*/170ROEdge* getEdgeForLaneID(const std::string& laneID) const;171172/** @brief Retrieves a lane rom the network given its id173*174* @param[in] laneID The name of the lane to retrieve the edge for175* @return The lane object176*/177ROLane* getLane(const std::string& laneID) const;178179/* @brief Adds a read node to the network180*181* If the node is already known (another one with the same id exists),182* an error is generated and given to msg-error-handler. The node183* is deleted in this case184*185* @param[in] node The node to add186*/187void addNode(RONode* node);188189190/** @brief Retrieves an node from the network191*192* @param[in] name The name of the node to retrieve193* @return The named node if known, otherwise 0194* @todo Check whether a const pointer may be returned195*/196RONode* getNode(const std::string& id) const {197return myNodes.get(id);198}199200201/* @brief Adds a read stopping place (bus, train, container, parking) to the network202*203* If the place is already known (another one with the same id and category exists),204* an error is generated and given to msg-error-handler. The stop205* is deleted in this case206*207* @param[in] id The name of the stop to add208* @param[in] category The type of stop209* @param[in] stop The detailed stop description210*/211void addStoppingPlace(const std::string& id, const SumoXMLTag category, SUMOVehicleParameter::Stop* stop);212213/** @brief Retrieves a stopping place from the network214*215* @param[in] id The name of the stop to retrieve216* @param[in] category The type of stop217* @return The named stop if known, otherwise 0218*/219const SUMOVehicleParameter::Stop* getStoppingPlace(const std::string& id, const SumoXMLTag category) const {220if (myStoppingPlaces.count(category) > 0) {221return myStoppingPlaces.find(category)->second.get(id);222}223return 0;224}225226/// @brief return the name for the given stopping place id227const std::string getStoppingPlaceName(const std::string& id) const;228229/// @brief return the element name for the given stopping place id230const std::string getStoppingPlaceElement(const std::string& id) const;231//@}232233234235/// @name Insertion and retrieval of vehicle types, vehicles, routes, and route definitions236//@{237238/** @brief Checks whether the vehicle type (distribution) may be added239*240* This method checks also whether the default type may still be replaced241* @param[in] id The id of the vehicle type (distribution) to add242* @return Whether the type (distribution) may be added243*/244bool checkVType(const std::string& id);245246247/** @brief Adds a read vehicle type definition to the network248*249* If the vehicle type definition is already known (another one with250* the same id exists), false is returned, and the vehicle type251* is deleted.252*253* @param[in] def The vehicle type to add254* @return Whether the vehicle type could be added255*/256virtual bool addVehicleType(SUMOVTypeParameter* type);257258259/** @brief Adds a vehicle type distribution260*261* If another vehicle type (or distribution) with the same id exists, false is returned.262* Otherwise, the vehicle type distribution is added to the internal vehicle type distribution263* container "myVTypeDistDict".264*265* This control get responsible for deletion of the added vehicle266* type distribution.267*268* @param[in] id The id of the distribution to add269* @param[in] vehTypeDistribution The vehicle type distribution to add270* @return Whether the vehicle type could be added271*/272bool addVTypeDistribution(const std::string& id, RandomDistributor<SUMOVTypeParameter*>* vehTypeDistribution);273274275/** @brief Retrieves the named vehicle type distribution276*277* If the named vehicle type distribution was not added to the net before278* nullptr is returned279*280* @param[in] id The id of the vehicle type distribution to return281* @return The named vehicle type distribution282*/283const RandomDistributor<SUMOVTypeParameter*>* getVTypeDistribution(const std::string& id) {284const auto it = myVTypeDistDict.find(id);285return it != myVTypeDistDict.end() ? it ->second : nullptr;286}287288289/** @brief Retrieves the named vehicle type290*291* If the name is "" the default type is returned.292* If the named vehicle type (or typeDistribution) was not added to the net before293* nullptr is returned294*295* @param[in] id The id of the vehicle type to return296* @return The named vehicle type297*/298SUMOVTypeParameter* getVehicleTypeSecure(const std::string& id);299300301/* @brief Adds a route definition to the network302*303* If the route definition is already known (another one with304* the same id exists), false is returned, but the route definition305* is not deleted.306*307* @param[in] def The route definition to add308* @return Whether the route definition could be added309* @todo Rename myRoutes to myRouteDefinitions310*/311bool addRouteDef(RORouteDef* def);312313314/** @brief Returns the named route definition315*316* @param[in] name The name of the route definition to retrieve317* @return The named route definition if known, otherwise 0318* @todo Check whether a const pointer may be returned319* @todo Rename myRoutes to myRouteDefinitions320*/321RORouteDef* getRouteDef(const std::string& name) const {322return myRoutes.get(name);323}324325326/* @brief Adds a vehicle to the network327*328* If the vehicle is already known (another one with the same id329* exists), false is returned, but the vehicle is not deleted.330*331* Otherwise, the number of loaded routes ("myReadRouteNo") is increased.332*333* @param[in] id The id of the vehicle to add334* @param[in] veh The vehicle to add335* @return Whether the vehicle could be added336*/337virtual bool addVehicle(const std::string& id, ROVehicle* veh);338339/// @brief returns whether a vehicle with the given id was already loaded340bool knowsVehicle(const std::string& id) const;341342/// @brief returns departure time for the given vehicle id343SUMOTime getDeparture(const std::string& vehID) const;344345/* @brief Adds a flow of vehicles to the network346*347* If the flow is already known (another one with the same id348* exists), false is returned, but the vehicle parameter are not deleted.349*350* Otherwise, the number of loaded routes ("myReadRouteNo") is increased.351*352* @param[in] flow The parameter of the flow to add353* @return Whether the flow could be added354*/355bool addFlow(SUMOVehicleParameter* flow, const bool randomize);356357358/* @brief Adds a person to the network359*360* @param[in] person The person to add361*/362bool addPerson(ROPerson* person);363364365/* @brief Adds a container to the network366*367* @param[in] depart The departure time of the container368* @param[in] desc The xml description of the container369*/370void addContainer(const SUMOTime depart, const std::string desc);371// @}372373374/// @name Processing stored vehicle definitions375//@{376377/** @brief Computes routes described by their definitions and saves them378*379* As long as a vehicle with a departure time smaller than the given380* exists, its route is computed and it is written and removed from381* the internal container.382*383* @param[in] options The options used during this process384* @param[in] provider The router provider for routes computation385* @param[in] time The time until which route definitions shall be processed386* @return The last seen departure time>=time387*/388SUMOTime saveAndRemoveRoutesUntil(OptionsCont& options,389const RORouterProvider& provider, SUMOTime time);390391392/// Returns the information whether further vehicles, persons or containers are stored393bool furtherStored();394//@}395396397/** @brief Opens the output for computed routes398*399* If one of the file outputs can not be build, an IOError is thrown.400*401* @param[in] options The options to be asked for "output-file", "alternatives-output" and "vtype-output"402*/403void openOutput(const OptionsCont& options);404405406/** @brief Writes the intermodal network and weights if requested407*408* If one of the file outputs can not be build, an IOError is thrown.409*410* @param[in] options The options to be asked for "intermodal-network-output" and "intermodal-weight-output"411*/412void writeIntermodal(const OptionsCont& options, ROIntermodalRouter& router) const;413414415/** @brief closes the file output for computed routes and deletes associated threads if necessary */416void cleanup();417418419/// Returns the total number of edges the network contains including internal edges420int getEdgeNumber() const;421422/// Returns the number of internal edges the network contains423int getInternalEdgeNumber() const;424425const NamedObjectCont<ROEdge*>& getEdgeMap() const {426return myEdges;427}428429static void adaptIntermodalRouter(ROIntermodalRouter& router);430431bool hasPermissions() const;432433void setPermissionsFound();434435/// @brief return whether the network contains bidirectional rail edges436bool hasBidiEdges() const {437return myHasBidiEdges;438}439440/// @brief whether efforts were loaded from file441bool hasLoadedEffort() const;442443OutputDevice* getRouteOutput(const bool alternative = false) {444if (alternative) {445return myRouteAlternativesOutput;446}447return myRoutesOutput;448}449450#ifdef HAVE_FOX451MFXWorkerThread::Pool& getThreadPool() {452return myThreadPool;453}454455class WorkerThread : public MFXWorkerThread, public RORouterProvider {456public:457WorkerThread(MFXWorkerThread::Pool& pool,458const RORouterProvider& original)459: MFXWorkerThread(pool), RORouterProvider(original) {}460virtual ~WorkerThread() {461stop();462}463};464465class BulkmodeTask : public MFXWorkerThread::Task {466public:467BulkmodeTask(const bool value) : myValue(value) {}468void run(MFXWorkerThread* context) {469static_cast<WorkerThread*>(context)->setBulkMode(myValue);470}471private:472const bool myValue;473private:474/// @brief Invalidated assignment operator.475BulkmodeTask& operator=(const BulkmodeTask&);476};477#endif478479480private:481void checkFlows(SUMOTime time, MsgHandler* errorHandler);482483void createBulkRouteRequests(const RORouterProvider& provider, const SUMOTime time, const bool removeLoops);484485private:486/// @brief Unique instance of RONet487static RONet* myInstance;488489/// @brief Known vehicle ids and their departure490std::map<std::string, SUMOTime> myVehIDs;491492/// @brief Known person ids493std::set<std::string> myPersonIDs;494495/// @brief Known nodes496NamedObjectCont<RONode*> myNodes;497498/// @brief Known edges499NamedObjectCont<ROEdge*> myEdges;500501/// @brief Known bus / train / container stops and parking areas502std::map<SumoXMLTag, NamedObjectCont<SUMOVehicleParameter::Stop*> > myStoppingPlaces;503504/// @brief Known vehicle types505NamedObjectCont<SUMOVTypeParameter*> myVehicleTypes;506507/// @brief Vehicle type distribution dictionary type508typedef std::map< std::string, RandomDistributor<SUMOVTypeParameter*>* > VTypeDistDictType;509/// @brief A distribution of vehicle types (probability->vehicle type)510VTypeDistDictType myVTypeDistDict;511512/// @brief Whether the default vehicle type was already used or can still be replaced513bool myDefaultVTypeMayBeDeleted;514515/// @brief Whether the default pedestrian type was already used or can still be replaced516bool myDefaultPedTypeMayBeDeleted;517518/// @brief Whether the default bicycle type was already used or can still be replaced519bool myDefaultBikeTypeMayBeDeleted;520521/// @brief Whether the default taxi type was already used or can still be replaced522bool myDefaultTaxiTypeMayBeDeleted;523524/// @brief Whether the default rail type was already used or can still be replaced525bool myDefaultRailTypeMayBeDeleted;526527/// @brief Known routes528NamedObjectCont<RORouteDef*> myRoutes;529530/// @brief Known routables531RoutablesMap myRoutables;532533/// @brief Known flows534NamedObjectCont<SUMOVehicleParameter*> myFlows;535536/// @brief whether any flows are still active537bool myHaveActiveFlows;538539/// @brief Known containers540typedef std::multimap<const SUMOTime, const std::string> ContainerMap;541ContainerMap myContainers;542543/// @brief vehicles to keep for public transport routing544std::vector<const RORoutable*> myPTVehicles;545546/// @brief Departure times for randomized flows547std::map<std::string, std::vector<SUMOTime> > myDepartures;548549/// @brief traffic assignment zones with sources and sinks550std::map<std::string, std::pair<std::vector<std::string>, std::vector<std::string> > > myDistricts;551552/// @brief The file to write the computed routes into553OutputDevice* myRoutesOutput;554555/// @brief The file to write the computed route alternatives into556OutputDevice* myRouteAlternativesOutput;557558/// @brief The file to write the vehicle types into559OutputDevice* myTypesOutput;560561/// @brief The number of read routes562int myReadRouteNo;563564/// @brief The number of discarded routes565int myDiscardedRouteNo;566567/// @brief The number of written routes568int myWrittenRouteNo;569570/// @brief Whether the network contains edges which not all vehicles may pass571bool myHavePermissions;572573/// @brief The vehicle class specific speed restrictions574std::map<std::string, std::map<SUMOVehicleClass, double> > myRestrictions;575576/// @brief The number of internal edges in the dictionary577int myNumInternalEdges;578579/// @brief handler for ignorable error messages580MsgHandler* myErrorHandler;581582/// @brief whether to keep the vtype distribution in output583const bool myKeepVTypeDist;584585/// @brief whether to calculate routes for public transport586const bool myDoPTRouting;587588/// @brief whether the network contains bidirectional railway edges589bool myHasBidiEdges;590591#ifdef HAVE_FOX592private:593class RoutingTask : public MFXWorkerThread::Task {594public:595RoutingTask(RORoutable* v, const bool removeLoops, MsgHandler* errorHandler)596: myRoutable(v), myRemoveLoops(removeLoops), myErrorHandler(errorHandler) {}597void run(MFXWorkerThread* context);598private:599RORoutable* const myRoutable;600const bool myRemoveLoops;601MsgHandler* const myErrorHandler;602private:603/// @brief Invalidated assignment operator.604RoutingTask& operator=(const RoutingTask&);605};606607608private:609/// @brief for multi threaded routing610MFXWorkerThread::Pool myThreadPool;611#endif612613private:614/// @brief Invalidated copy constructor615RONet(const RONet& src);616617/// @brief Invalidated assignment operator618RONet& operator=(const RONet& src);619620};621622623