Dynamical system model
Loading...
Searching...
No Matches
dsf::RoadNetwork Class Reference

The RoadNetwork class represents a graph in the network. More...

#include <RoadNetwork.hpp>

Inheritance diagram for dsf::RoadNetwork:
dsf::Network< RoadJunction, Street >

Public Member Functions

 RoadNetwork (AdjacencyMatrix const &adj)
 Construct a new RoadNetwork object.
 
void adjustNodeCapacities ()
 Adjust the nodes' transport capacity.
 
void initTrafficLights (Delay const minGreenTime=30)
 Initialize the traffic lights with random parameters.
 
void autoMapStreetLanes ()
 Automatically re-maps street lanes basing on network's topology.
 
void importMatrix (const std::string &fileName, bool isAdj=true, double defaultSpeed=13.8888888889)
 Import the graph's adjacency matrix from a file. If the file is not of a supported format, it will read the file as a matrix with the first two elements being the number of rows and columns and the following elements being the matrix elements.
 
void importCoordinates (const std::string &fileName)
 Import the graph's nodes from a file.
 
void importGeoJSON (const std::string &fileName, std::unordered_map< std::string, std::string > const &fields)
 
template<typename... TArgs>
void importNodes (const std::string &fileName, TArgs &&... args)
 Import the graph's nodes from a file.
 
template<typename... TArgs>
void importEdges (const std::string &fileName, TArgs &&... args)
 Import the graph's streets from a file.
 
void importTrafficLights (const std::string &fileName)
 Import the graph's traffic lights from a file.
 
void exportNodes (const std::string &fileName)
 Export the graph's nodes to a csv-like file separated with ';'.
 
void exportEdges (const std::string &fileName)
 Export the graph's edges to a csv-like file separated with ';'.
 
void exportMatrix (std::string path="./matrix.dsm", bool isAdj=true)
 Export the graph's adjacency matrix to a file.
 
template<typename T1, typename... Tn>
requires is_node_v<std::remove_reference_t<T1>> && (is_node_v<std::remove_reference_t<Tn>> && ...)
void addNodes (T1 &&node, Tn &&... nodes)
 
TrafficLightmakeTrafficLight (Id const nodeId, Delay const cycleTime, Delay const counter=0)
 Convert an existing node to a traffic light.
 
RoundaboutmakeRoundabout (Id nodeId)
 Convert an existing node into a roundabout.
 
void makeStochasticStreet (Id streetId, double const flowRate)
 
void makeSpireStreet (Id streetId)
 Convert an existing street into a spire street.
 
StationmakeStation (Id nodeId, const unsigned int managementTime)
 Convert an existing node into a station.
 
void addStreet (Street &&street)
 Add a street to the graph.
 
template<typename T1>
requires is_street_v<std::remove_reference_t<T1>>
void addStreets (T1 &&street)
 
template<typename T1, typename... Tn>
requires is_street_v<std::remove_reference_t<T1>> && (is_street_v<std::remove_reference_t<Tn>> && ...)
void addStreets (T1 &&street, Tn &&... streets)
 
const std::unique_ptr< Street > * street (Id source, Id destination) const
 Get a street from the graph.
 
auto maxCapacity () const noexcept
 Get the maximum agent capacity.
 
template<typename DynamicsFunc>
requires (std::is_invocable_r_v<double, DynamicsFunc, std::unique_ptr<Street> const&>)
std::unordered_map< Id, std::vector< Id > > allPathsTo (Id const targetId, DynamicsFunc getEdgeWeight, double const threshold=1e-9) const
 Perform a global Dijkstra search to a target node from all other nodes in the graph.
 
- Public Member Functions inherited from dsf::Network< RoadJunction, Street >
 Network ()=default
 Construct a new empty Network object.
 
 Network (AdjacencyMatrix const &adj)
 Construct a new Network object.
 
std::unordered_map< Id, std::unique_ptr< RoadJunction > > const & nodes () const
 Get the nodes as an unordered map.
 
std::unordered_map< Id, std::unique_ptr< Street > > const & edges () const
 Get the edges as an unordered map.
 
size_t nNodes () const
 Get the number of nodes.
 
size_t nEdges () const
 Get the number of edges.
 
void addNode (TArgs &&... args)
 Add a node to the network.
 
void addNDefaultNodes (size_t n)
 
void addEdge (TArgs &&... args)
 Add an edge to the network.
 
std::unique_ptr< RoadJunction > const & node (Id nodeId) const
 Get a node by id.
 
std::unique_ptr< RoadJunction > & node (Id nodeId)
 Get a node by id.
 
TNode & node (Id nodeId)
 Get a node by id.
 
std::unique_ptr< Street > const & edge (Id edgeId) const
 Get an edge by id.
 
std::unique_ptr< Street > & edge (Id edgeId)
 Get an edge by id.
 
std::unique_ptr< Street > const & edge (Id source, Id target) const
 
TEdge & edge (Id edgeId)
 Get an edge by id.
 

Additional Inherited Members

- Protected Member Functions inherited from dsf::Network< RoadJunction, Street >
Id m_cantorHash (Id u, Id v) const
 
Id m_cantorHash (std::pair< Id, Id > const &idPair) const
 
- Protected Attributes inherited from dsf::Network< RoadJunction, Street >
std::unordered_map< Id, std::unique_ptr< RoadJunction > > m_nodes
 
std::unordered_map< Id, std::unique_ptr< Street > > m_edges
 

Detailed Description

The RoadNetwork class represents a graph in the network.

Template Parameters
Id,Thetype of the graph's id. It must be an unsigned integral type.
Size,Thetype of the graph's capacity. It must be an unsigned integral type.

Constructor & Destructor Documentation

◆ RoadNetwork()

dsf::RoadNetwork::RoadNetwork ( AdjacencyMatrix const & adj)

Construct a new RoadNetwork object.

Parameters
adjAn adjacency matrix made by a SparseMatrix representing the graph's adjacency matrix

Member Function Documentation

◆ addStreet()

void dsf::RoadNetwork::addStreet ( Street && street)

Add a street to the graph.

Parameters
streetA reference to the street to add

◆ adjustNodeCapacities()

void dsf::RoadNetwork::adjustNodeCapacities ( )

Adjust the nodes' transport capacity.

The nodes' capacity is adjusted using the graph's streets transport capacity, which may vary basing on the number of lanes. The node capacity will be set to the sum of the incoming streets' transport capacity.

◆ allPathsTo()

template<typename DynamicsFunc>
requires (std::is_invocable_r_v<double, DynamicsFunc, std::unique_ptr<Street> const&>)
std::unordered_map< Id, std::vector< Id > > dsf::RoadNetwork::allPathsTo ( Id const targetId,
DynamicsFunc getEdgeWeight,
double const threshold = 1e-9 ) const

Perform a global Dijkstra search to a target node from all other nodes in the graph.

Template Parameters
DynamicsFuncA callable type that takes a const reference to a Street and returns a double representing the edge weight
Parameters
targetIdThe id of the target node
getEdgeWeightA callable that takes a const reference to a Street and returns a double representing the edge weight
thresholdA threshold value to consider alternative paths
Returns
A map where each key is a node id and the value is a vector of next hop node ids toward the target
Exceptions
std::out_of_rangeif the target node does not exist
std::invalid_argumentif the dynamics function is not callable with a const reference

◆ autoMapStreetLanes()

void dsf::RoadNetwork::autoMapStreetLanes ( )

Automatically re-maps street lanes basing on network's topology.

For example, if one street has the right turn forbidden, then the right lane becomes a straight one

◆ exportEdges()

void dsf::RoadNetwork::exportEdges ( const std::string & fileName)

Export the graph's edges to a csv-like file separated with ';'.

Parameters
pathThe path to the file to export the edges to

The file format is csv-like, with the first line being the column names: id;source_id;target_id;name;geometry

◆ exportMatrix()

void dsf::RoadNetwork::exportMatrix ( std::string path = "./matrix.dsm",
bool isAdj = true )

Export the graph's adjacency matrix to a file.

Parameters
pathThe path to the file to export the adjacency matrix to (default: ./matrix.dsm)
isAdjA boolean value indicating if the file contains the adjacency matrix or the distance matrix.
Exceptions
std::invalid_argumentif the file is not found or invalid

◆ exportNodes()

void dsf::RoadNetwork::exportNodes ( const std::string & fileName)

Export the graph's nodes to a csv-like file separated with ';'.

Parameters
pathThe path to the file to export the nodes to

The file format is csv-like, with the first line being the column names: id;lon;lat

◆ importCoordinates()

void dsf::RoadNetwork::importCoordinates ( const std::string & fileName)

Import the graph's nodes from a file.

Parameters
fileNameThe name of the file to import the nodes from.
Exceptions
std::invalid_argumentif the file is not found, invalid or the format is not supported

The file format is deduced from the file extension. Currently only .dsm files are supported. The first input number is the number of nodes, followed by the coordinates of each node. In the i-th row of the file, the (i - 1)-th node's coordinates are expected.

◆ importEdges()

template<typename... TArgs>
void dsf::RoadNetwork::importEdges ( const std::string & fileName,
TArgs &&... args )

Import the graph's streets from a file.

Parameters
fileNameThe name of the file to import the streets from.

Supports csv, json and geojson file formats. The file format is deduced from the file extension. Supported fields:

  • id: The id of the street
  • source: The id of the source node
  • target: The id of the target node
  • length: The length of the street, in meters
  • type: The type of the street (e.g. residential, primary, secondary, etc.)
  • nlanes: The number of lanes of the street
  • maxspeed: The street's speed limit, in km/h
  • name: The name of the street
  • geometry: The geometry of the street, as a LINESTRING

    Next columns are optional (meaning that their absence will not -hopefully- cause any pain):

  • forbiddenTurns: The forbidden turns of the street, encoding information about street into which the street cannot output agents. The format is a string "sourceId1-targetid1, sourceId2-targetid2,..."
  • coilcode: An integer code to identify the coil located on the street
  • customWeight: will be stored in the weight parameter of the Edge class. You can use it for the shortest path via dsf::weight_functions::customWeight.

◆ importMatrix()

void dsf::RoadNetwork::importMatrix ( const std::string & fileName,
bool isAdj = true,
double defaultSpeed = 13.8888888889 )

Import the graph's adjacency matrix from a file. If the file is not of a supported format, it will read the file as a matrix with the first two elements being the number of rows and columns and the following elements being the matrix elements.

Parameters
fileNameThe name of the file to import the adjacency matrix from.
isAdjA boolean value indicating if the file contains the adjacency matrix or the distance matrix.
defaultSpeedThe default speed limit for the streets
Exceptions
std::invalid_argumentif the file is not found or invalid The matrix format is deduced from the file extension. Currently only .dsm files are supported.

◆ importNodes()

template<typename... TArgs>
void dsf::RoadNetwork::importNodes ( const std::string & fileName,
TArgs &&... args )

Import the graph's nodes from a file.

Parameters
fileNameThe name of the file to import the nodes from.
Exceptions
std::invalid_argumentif the file is not found, invalid or the format is not supported

Supports csv file format. Please specify the separator as second parameter. Supported fields:

  • id: The id of the node
  • lon: The x coordinate of the node
  • lat: The y coordinate of the node
  • type: The type of the node The node type can be one of the following:
  • traffic_signals: intersection + traffic light
  • roundabout
  • origin/destination: intesection counted as origin, destination or both. To have both, the string must contain both the strings "origin" and "destination".

◆ importTrafficLights()

void dsf::RoadNetwork::importTrafficLights ( const std::string & fileName)

Import the graph's traffic lights from a file.

Parameters
fileNameThe name of the file to import the traffic lights from.

The file format is csv-like with the ';' separator. Supported columns (in order):

  • id: The id of the TrafficLight node
  • sourceId: The id of the source node of the incoming street
  • cycleTime: The traffic light's cycle time, in seconds
  • greenTime: The green time of the considered phase, in time-steps
    Exceptions
    std::invalid_argumentif the file is not found, invalid or the format is not supported
    The traffic lights are imported from the specified file. If the file does not provide sufficient parameters, the behavior of the traffic light initialization is undefined. Ensure the file contains valid and complete data for accurate traffic light configuration. Street priorities may be assigned based on additional parameters such as the number of lanes and the speed limit, if such data is available in the file.

◆ initTrafficLights()

void dsf::RoadNetwork::initTrafficLights ( Delay const minGreenTime = 30)

Initialize the traffic lights with random parameters.

Parameters
minGreenTimeThe minimum green time for the traffic lights cycles (default is 30)

Traffic Lights with no parameters set are initialized with random parameters. Street priorities are assigned considering the number of lanes and the speed limit. Traffic Lights with an input degree lower than 3 are converted to standard intersections.

◆ makeRoundabout()

Roundabout & dsf::RoadNetwork::makeRoundabout ( Id nodeId)

Convert an existing node into a roundabout.

Parameters
nodeIdThe id of the node to convert to a roundabout
Returns
A reference to the roundabout
Exceptions
std::invalid_argumentif the node does not exist

◆ makeSpireStreet()

void dsf::RoadNetwork::makeSpireStreet ( Id streetId)

Convert an existing street into a spire street.

Parameters
streetIdThe id of the street to convert to a spire street
Exceptions
std::invalid_argumentif the street does not exist

◆ makeStation()

Station & dsf::RoadNetwork::makeStation ( Id nodeId,
const unsigned int managementTime )

Convert an existing node into a station.

Parameters
nodeIdThe id of the node to convert to a station
managementTimeThe station's management time
Returns
A reference to the station
Exceptions
std::invalid_argumentif the node does not exist

◆ makeTrafficLight()

TrafficLight & dsf::RoadNetwork::makeTrafficLight ( Id const nodeId,
Delay const cycleTime,
Delay const counter = 0 )

Convert an existing node to a traffic light.

Parameters
nodeIdThe id of the node to convert to a traffic light
cycleTimeThe traffic light's cycle time
counterThe traffic light's counter initial value. Default is 0
Returns
A reference to the traffic light
Exceptions
std::invalid_argumentif the node does not exist

◆ maxCapacity()

auto dsf::RoadNetwork::maxCapacity ( ) const
inlinenoexcept

Get the maximum agent capacity.

Returns
unsigned long long The maximum agent capacity of the graph

◆ street()

const std::unique_ptr< Street > * dsf::RoadNetwork::street ( Id source,
Id destination ) const

Get a street from the graph.

Parameters
sourceThe source node
destinationThe destination node
Returns
A std::unique_ptr to the street if it exists, nullptr otherwise

The documentation for this class was generated from the following file: