/****************************************************************************/1// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo2// Copyright (C) 2002-2026 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;66typedef std::map<const ROEdge*, RouterProhibition> Prohibitions;67typedef std::map<const ROLane*, RouterProhibition> LaneProhibitions;6869/// @brief Constructor70RONet();717273/** @brief Returns the pointer to the unique instance of RONet (singleton).74* @return Pointer to the unique RONet-instance75*/76static RONet* getInstance();777879/// @brief Destructor80virtual ~RONet();818283/** @brief Adds a restriction for an edge type84* @param[in] id The id of the type85* @param[in] svc The vehicle class the restriction refers to86* @param[in] speed The restricted speed87*/88void addSpeedRestriction(const std::string& id, const SUMOVehicleClass svc, const double speed);899091/** @brief Returns the restrictions for an edge type92* If no restrictions are present, 0 is returned.93* @param[in] id The id of the type94* @return The mapping of vehicle classes to maximum speeds95*/96const std::map<SUMOVehicleClass, double>* getRestrictions(const std::string& id) const;9798bool hasSpeedRestrictions() const {99return !mySpeedRestrictions.empty();100}101102bool hasParamRestrictions() const {103return myHaveParamRestrictions;104}105106void setParamRestrictions() {107myHaveParamRestrictions = true;108}109110/// @brief retriefe edge type specific routing preference111double getPreference(const std::string& routingType, const SUMOVTypeParameter& pars) const;112113/// @brief add edge type specific routing preference114void addPreference(const std::string& routingType, SUMOVehicleClass svc, double prio);115/// @brief add edge type specific routing preference116void addPreference(const std::string& routingType, std::string vType, double prio);117118/// @name Insertion and retrieval of graph parts119//@{120121/* @brief Adds a read edge to the network122*123* If the edge is already known (another one with the same id exists),124* an error is generated and given to msg-error-handler. The edge125* is deleted in this case and false is returned.126*127* @param[in] edge The edge to add128* @return Whether the edge was added (if not, it was deleted, too)129*/130virtual bool addEdge(ROEdge* edge);131132133/* @brief Adds a district and connecting edges to the network134*135* If the district is already known (another one with the same id exists),136* an error is generated and given to msg-error-handler. The edges137* are deleted in this case and false is returned.138*139* @param[in] id The district to add140* @return Whether the district was added141*/142bool addDistrict(const std::string id, ROEdge* source, ROEdge* sink);143144145/* @brief Adds a district and connecting edges to the network146*147* If the district is already known (another one with the same id exists),148* an error is generated and given to msg-error-handler. The edges149* are deleted in this case and false is returned.150*151* @param[in] id The district to add152* @return Whether the district was added153*/154bool addDistrictEdge(const std::string tazID, const std::string edgeID, const bool isSource);155156/// @brief add a taz for every junction unless a taz with the same id already exists157void addJunctionTaz(ROAbstractEdgeBuilder& eb);158159/// @brief add a taz for every junction unless a taz with the same id already exists160void setBidiEdges(const std::map<ROEdge*, std::string>& bidiMap);161162/** @brief Retrieves all TAZ (districts) from the network163*164* @return The map of all districts165*/166const std::map<std::string, std::pair<std::vector<std::string>, std::vector<std::string> > >& getDistricts() const {167return myDistricts;168}169170/** @brief Retrieves an edge from the network171*172* This is not very pretty, but necessary, though, as routes run173* over instances, not over ids.174*175* @param[in] name The name of the edge to retrieve176* @return The named edge if known, otherwise 0177*/178ROEdge* getEdge(const std::string& name) const {179return myEdges.get(name);180}181182183/** @brief Retrieves an edge from the network when the lane id is given184*185* @param[in] laneID The name of the lane to retrieve the edge for186* @return The edge of the named lane if known, otherwise 0187*/188ROEdge* getEdgeForLaneID(const std::string& laneID) const;189190/** @brief Retrieves a lane rom the network given its id191*192* @param[in] laneID The name of the lane to retrieve the edge for193* @return The lane object194*/195ROLane* getLane(const std::string& laneID) const;196197/* @brief Adds a read node to the network198*199* If the node is already known (another one with the same id exists),200* an error is generated and given to msg-error-handler. The node201* is deleted in this case202*203* @param[in] node The node to add204*/205void addNode(RONode* node);206207208/** @brief Retrieves an node from the network209*210* @param[in] name The name of the node to retrieve211* @return The named node if known, otherwise 0212* @todo Check whether a const pointer may be returned213*/214RONode* getNode(const std::string& id) const {215return myNodes.get(id);216}217218219/* @brief Adds a read stopping place (bus, train, container, parking) to the network220*221* If the place is already known (another one with the same id and category exists),222* an error is generated and given to msg-error-handler. The stop223* is deleted in this case224*225* @param[in] id The name of the stop to add226* @param[in] category The type of stop227* @param[in] stop The detailed stop description228*/229void addStoppingPlace(const std::string& id, const SumoXMLTag category, SUMOVehicleParameter::Stop* stop);230231/** @brief Retrieves a stopping place from the network232*233* @param[in] id The name of the stop to retrieve234* @param[in] category The type of stop235* @return The named stop if known, otherwise 0236*/237const SUMOVehicleParameter::Stop* getStoppingPlace(const std::string& id, const SumoXMLTag category) const {238if (myStoppingPlaces.count(category) > 0) {239return myStoppingPlaces.find(category)->second.get(id);240}241return 0;242}243244/// @brief return the name for the given stopping place id245const std::string getStoppingPlaceName(const std::string& id) const;246247/// @brief return the element name for the given stopping place id248const std::string getStoppingPlaceElement(const std::string& id) const;249//@}250251252253/// @name Insertion and retrieval of vehicle types, vehicles, routes, and route definitions254//@{255256/** @brief Checks whether the vehicle type (distribution) may be added257*258* This method checks also whether the default type may still be replaced259* @param[in] id The id of the vehicle type (distribution) to add260* @return Whether the type (distribution) may be added261*/262bool checkVType(const std::string& id);263264265/** @brief Adds a read vehicle type definition to the network266*267* If the vehicle type definition is already known (another one with268* the same id exists), false is returned, and the vehicle type269* is deleted.270*271* @param[in] def The vehicle type to add272* @return Whether the vehicle type could be added273*/274virtual bool addVehicleType(SUMOVTypeParameter* type);275276277/** @brief Adds a vehicle type distribution278*279* If another vehicle type (or distribution) with the same id exists, false is returned.280* Otherwise, the vehicle type distribution is added to the internal vehicle type distribution281* container "myVTypeDistDict".282*283* This control get responsible for deletion of the added vehicle284* type distribution.285*286* @param[in] id The id of the distribution to add287* @param[in] vehTypeDistribution The vehicle type distribution to add288* @return Whether the vehicle type could be added289*/290bool addVTypeDistribution(const std::string& id, RandomDistributor<SUMOVTypeParameter*>* vehTypeDistribution);291292293/** @brief Retrieves the named vehicle type distribution294*295* If the named vehicle type distribution was not added to the net before296* nullptr is returned297*298* @param[in] id The id of the vehicle type distribution to return299* @return The named vehicle type distribution300*/301const RandomDistributor<SUMOVTypeParameter*>* getVTypeDistribution(const std::string& id) {302const auto it = myVTypeDistDict.find(id);303return it != myVTypeDistDict.end() ? it ->second : nullptr;304}305306307/** @brief Retrieves the named vehicle type308*309* If the name is "" the default type is returned.310* If the named vehicle type (or typeDistribution) was not added to the net before311* nullptr is returned312*313* @param[in] id The id of the vehicle type to return314* @return The named vehicle type315*/316SUMOVTypeParameter* getVehicleTypeSecure(const std::string& id);317318319/* @brief Adds a route definition to the network320*321* If the route definition is already known (another one with322* the same id exists), false is returned, but the route definition323* is not deleted.324*325* @param[in] def The route definition to add326* @return Whether the route definition could be added327* @todo Rename myRoutes to myRouteDefinitions328*/329bool addRouteDef(RORouteDef* def);330331332/** @brief Returns the named route definition333*334* @param[in] name The name of the route definition to retrieve335* @return The named route definition if known, otherwise 0336* @todo Check whether a const pointer may be returned337* @todo Rename myRoutes to myRouteDefinitions338*/339RORouteDef* getRouteDef(const std::string& name) const {340return myRoutes.get(name);341}342343344/* @brief Adds a vehicle to the network345*346* If the vehicle is already known (another one with the same id347* exists), false is returned, but the vehicle is not deleted.348*349* Otherwise, the number of loaded routes ("myReadRouteNo") is increased.350*351* @param[in] id The id of the vehicle to add352* @param[in] veh The vehicle to add353* @return Whether the vehicle could be added354*/355virtual bool addVehicle(const std::string& id, ROVehicle* veh);356357/// @brief returns whether a vehicle with the given id was already loaded358bool knowsVehicle(const std::string& id) const;359360/// @brief returns departure time for the given vehicle id361SUMOTime getDeparture(const std::string& vehID) const;362363/* @brief Adds a flow of vehicles to the network364*365* If the flow is already known (another one with the same id366* exists), false is returned, but the vehicle parameter are not deleted.367*368* Otherwise, the number of loaded routes ("myReadRouteNo") is increased.369*370* @param[in] flow The parameter of the flow to add371* @return Whether the flow could be added372*/373bool addFlow(SUMOVehicleParameter* flow, const bool randomize);374375376/* @brief Adds a person to the network377*378* @param[in] person The person to add379*/380bool addPerson(ROPerson* person);381382383/* @brief Adds a container to the network384*385* @param[in] depart The departure time of the container386* @param[in] desc The xml description of the container387*/388void addContainer(const SUMOTime depart, const std::string desc);389// @}390391392/// @name Processing stored vehicle definitions393//@{394395/** @brief Computes routes described by their definitions and saves them396*397* As long as a vehicle with a departure time smaller than the given398* exists, its route is computed and it is written and removed from399* the internal container.400*401* @param[in] options The options used during this process402* @param[in] provider The router provider for routes computation403* @param[in] time The time until which route definitions shall be processed404* @return The last seen departure time>=time405*/406SUMOTime saveAndRemoveRoutesUntil(OptionsCont& options,407const RORouterProvider& provider, SUMOTime time);408409410/// Returns the information whether further vehicles, persons or containers are stored411bool furtherStored();412//@}413414415/** @brief Opens the output for computed routes416*417* If one of the file outputs can not be build, an IOError is thrown.418*419* @param[in] options The options to be asked for "output-file", "alternatives-output" and "vtype-output"420*/421void openOutput(const OptionsCont& options);422423424/** @brief Writes the intermodal network and weights if requested425*426* If one of the file outputs can not be build, an IOError is thrown.427*428* @param[in] options The options to be asked for "intermodal-network-output" and "intermodal-weight-output"429*/430void writeIntermodal(const OptionsCont& options, ROIntermodalRouter& router) const;431432433/** @brief closes the file output for computed routes and deletes associated threads if necessary */434void cleanup();435436437/// Returns the total number of edges the network contains including internal edges438int getEdgeNumber() const;439440/// Returns the number of internal edges the network contains441int getInternalEdgeNumber() const;442443const NamedObjectCont<ROEdge*>& getEdgeMap() const {444return myEdges;445}446447static void adaptIntermodalRouter(ROIntermodalRouter& router);448449bool hasPermissions() const;450451void setPermissionsFound();452453/// @brief return whether the network contains bidirectional rail edges454bool hasBidiEdges() const {455return myHasBidiEdges;456}457458/// @brief whether efforts were loaded from file459bool hasLoadedEffort() const;460461double getMaxTraveltime() const {462return myMaxTraveltime;463}464465const Prohibitions& getProhibitions() const {466return myProhibitions;467}468469void updateLaneProhibitions(SUMOTime begin);470471void addProhibition(const ROEdge* edge, const RouterProhibition& prohibition);472void addLaneProhibition(const ROLane* lane, const RouterProhibition& prohibition);473474OutputDevice* getRouteOutput(const bool alternative = false) {475if (alternative) {476return myRouteAlternativesOutput;477}478return myRoutesOutput;479}480481#ifdef HAVE_FOX482MFXWorkerThread::Pool& getThreadPool() {483return myThreadPool;484}485486class WorkerThread : public MFXWorkerThread, public RORouterProvider {487public:488WorkerThread(MFXWorkerThread::Pool& pool,489const RORouterProvider& original)490: MFXWorkerThread(pool), RORouterProvider(original) {}491virtual ~WorkerThread() {492stop();493}494};495496class BulkmodeTask : public MFXWorkerThread::Task {497public:498BulkmodeTask(const bool value) : myValue(value) {}499void run(MFXWorkerThread* context) {500static_cast<WorkerThread*>(context)->setBulkMode(myValue);501}502private:503const bool myValue;504private:505/// @brief Invalidated assignment operator.506BulkmodeTask& operator=(const BulkmodeTask&);507};508#endif509510511private:512void checkFlows(SUMOTime time, MsgHandler* errorHandler);513514void createBulkRouteRequests(const RORouterProvider& provider, const SUMOTime time, const bool removeLoops);515516private:517/// @brief Unique instance of RONet518static RONet* myInstance;519520/// @brief Known vehicle ids and their departure521std::map<std::string, SUMOTime> myVehIDs;522523/// @brief Known person ids524std::set<std::string> myPersonIDs;525526/// @brief Known nodes527NamedObjectCont<RONode*> myNodes;528529/// @brief Known edges530NamedObjectCont<ROEdge*> myEdges;531532/// @brief Known bus / train / container stops and parking areas533std::map<SumoXMLTag, NamedObjectCont<SUMOVehicleParameter::Stop*> > myStoppingPlaces;534535/// @brief Known vehicle types536NamedObjectCont<SUMOVTypeParameter*> myVehicleTypes;537538/// @brief Vehicle type distribution dictionary type539typedef std::map< std::string, RandomDistributor<SUMOVTypeParameter*>* > VTypeDistDictType;540/// @brief A distribution of vehicle types (probability->vehicle type)541VTypeDistDictType myVTypeDistDict;542543/// @brief Whether the default vehicle type was already used or can still be replaced544bool myDefaultVTypeMayBeDeleted;545546/// @brief Whether the default pedestrian type was already used or can still be replaced547bool myDefaultPedTypeMayBeDeleted;548549/// @brief Whether the default bicycle type was already used or can still be replaced550bool myDefaultBikeTypeMayBeDeleted;551552/// @brief Whether the default taxi type was already used or can still be replaced553bool myDefaultTaxiTypeMayBeDeleted;554555/// @brief Whether the default rail type was already used or can still be replaced556bool myDefaultRailTypeMayBeDeleted;557558/// @brief Known routes559NamedObjectCont<RORouteDef*> myRoutes;560561/// @brief Known routables562RoutablesMap myRoutables;563564/// @brief Known flows565NamedObjectCont<SUMOVehicleParameter*> myFlows;566567/// @brief whether any flows are still active568bool myHaveActiveFlows;569570/// @brief Known containers571typedef std::multimap<const SUMOTime, const std::string> ContainerMap;572ContainerMap myContainers;573574/// @brief vehicles to keep for public transport routing575std::vector<const RORoutable*> myPTVehicles;576577/// @brief Departure times for randomized flows578std::map<std::string, std::vector<SUMOTime> > myDepartures;579580/// @brief traffic assignment zones with sources and sinks581std::map<std::string, std::pair<std::vector<std::string>, std::vector<std::string> > > myDistricts;582583/// @brief The file to write the computed routes into584OutputDevice* myRoutesOutput;585586/// @brief The file to write the computed route alternatives into587OutputDevice* myRouteAlternativesOutput;588589/// @brief The file to write the vehicle types into590OutputDevice* myTypesOutput;591592/// @brief The number of read routes593int myReadRouteNo;594595/// @brief The number of discarded routes596int myDiscardedRouteNo;597598/// @brief The number of written routes599int myWrittenRouteNo;600601/// @brief Whether the network contains edges which not all vehicles may pass602bool myHavePermissions;603604/// @brief The vehicle class specific speed restrictions605std::map<std::string, std::map<SUMOVehicleClass, double> > mySpeedRestrictions;606607/// @brief whether parameter-based access restrictions are configured608bool myHaveParamRestrictions;609610/// @brief Preferences for routing611std::map<SUMOVehicleClass, std::map<std::string, double> > myVClassPreferences;612std::map<std::string, std::map<std::string, double> > myVTypePreferences;613614/// @brief The number of internal edges in the dictionary615int myNumInternalEdges;616617/// @brief handler for ignorable error messages618MsgHandler* myErrorHandler;619620/// @brief whether to keep the vtype distribution in output621const bool myKeepVTypeDist;622623/// @brief whether to calculate routes for public transport624const bool myDoPTRouting;625626/// @brief whether to preserve flows627const bool myKeepFlows;628629/// @brief whether the network contains bidirectional railway edges630bool myHasBidiEdges;631632/// @brief the maximum traveltime beyond which routing is considered a failure633const double myMaxTraveltime;634635/// @brief temporary edge closing (rerouters)636Prohibitions myProhibitions;637/// @brief temporary lane closing (rerouters)638LaneProhibitions myLaneProhibitions;639std::map<double, std::set<const ROLane*> > myLaneProhibitionTimes;640641#ifdef HAVE_FOX642private:643class RoutingTask : public MFXWorkerThread::Task {644public:645RoutingTask(RORoutable* v, const bool removeLoops, MsgHandler* errorHandler)646: myRoutable(v), myRemoveLoops(removeLoops), myErrorHandler(errorHandler) {}647void run(MFXWorkerThread* context);648private:649RORoutable* const myRoutable;650const bool myRemoveLoops;651MsgHandler* const myErrorHandler;652private:653/// @brief Invalidated assignment operator.654RoutingTask& operator=(const RoutingTask&);655};656657658private:659/// @brief for multi threaded routing660MFXWorkerThread::Pool myThreadPool;661#endif662663private:664/// @brief Invalidated copy constructor665RONet(const RONet& src);666667/// @brief Invalidated assignment operator668RONet& operator=(const RONet& src);669670};671672673