Dynamical system model
Loading...
Searching...
No Matches
dsm::Graph Class Reference

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

#include <Graph.hpp>

Public Member Functions

 Graph (const SparseMatrix< bool > &adj)
 Construct a new Graph object.
 
 Graph (const std::unordered_map< Id, std::unique_ptr< Street > > &streetSet)
 Construct a new Graph object.
 
 Graph (const Graph &other)
 
Graphoperator= (const Graph &other)
 
 Graph (Graph &&)=default
 
Graphoperator= (Graph &&)=default
 
void buildAdj ()
 Build the graph's adjacency matrix and computes max capacity.
 
void buildStreetAngles ()
 Build the graph's street angles using the node's coordinates.
 
void adjustNodeCapacities ()
 Adjust the nodes' transport capacity.
 
void normalizeStreetCapacities (double meanVehicleLength=5.)
 Normalize the streets' capacities.
 
void importMatrix (const std::string &fileName, bool isAdj=true)
 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 importOSMNodes (const std::string &fileName)
 Import the graph's nodes from a file.
 
void importOSMEdges (const std::string &fileName)
 Import the graph's streets from a file.
 
void exportMatrix (std::string path="./matrix.dsm", bool isAdj=true)
 Export the graph's adjacency matrix to a file.
 
void exportCoordinates (std::string const &path="./coordinates.csv")
 Export the nodes' coordinates to a file.
 
void addNode (std::unique_ptr< Node > node)
 Add a node to the graph.
 
void addNode (const Intersection &node)
 Add a node to the graph.
 
template<typename... Tn>
requires (is_node_v<std::remove_reference_t<Tn>> && ...)
void addNodes (Tn &&... nodes)
 
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)
 
template<typename Delay >
requires (std::unsigned_integral<Delay>)
TrafficLight< Delay > & makeTrafficLight (Id nodeId)
 Convert an existing node to a traffic light.
 
RoundaboutmakeRoundabout (Id nodeId)
 Convert an existing node into a roundabout.
 
SpireStreetmakeSpireStreet (Id streetId)
 Convert an existing street into a spire street.
 
void addStreet (std::shared_ptr< Street > street)
 Add a street to the graph.
 
void addStreet (const 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 SparseMatrix< bool > & adjMatrix () const
 Get the graph's adjacency matrix.
 
const std::unordered_map< Id, std::unique_ptr< Node > > & nodeSet () const
 Get the graph's node map.
 
std::unordered_map< Id, std::unique_ptr< Node > > & nodeSet ()
 Get the graph's node map.
 
const std::unordered_map< Id, std::unique_ptr< Street > > & streetSet () const
 Get the graph's street map.
 
std::unordered_map< Id, std::unique_ptr< Street > > & streetSet ()
 Get the graph's street map.
 
const std::unique_ptr< Street > * street (Id source, Id destination) const
 Get a street from the graph.
 
const std::unique_ptr< Street > * oppositeStreet (Id streetId) const
 Get the opposite street of a street in the graph.
 
unsigned long long maxCapacity () const
 Get the maximum agent capacity.
 
std::optional< DijkstraResult > shortestPath (const Intersection &source, const Intersection &destination) const
 Get the shortest path between two nodes using dijkstra algorithm.
 
std::optional< DijkstraResult > shortestPath (Id source, Id destination) const
 Get the shortest path between two nodes using dijkstra algorithm.
 

Detailed Description

The Graph 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

◆ Graph() [1/2]

dsm::Graph::Graph ( const SparseMatrix< bool > & adj)

Construct a new Graph object.

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

◆ Graph() [2/2]

dsm::Graph::Graph ( const std::unordered_map< Id, std::unique_ptr< Street > > & streetSet)

Construct a new Graph object.

Parameters
streetSetA map of streets representing the graph's streets

Member Function Documentation

◆ addNode() [1/2]

void dsm::Graph::addNode ( const Intersection & node)

Add a node to the graph.

Parameters
nodeA reference to the node to add

◆ addNode() [2/2]

void dsm::Graph::addNode ( std::unique_ptr< Node > node)

Add a node to the graph.

Parameters
nodeA std::unique_ptr to the node to add

◆ addStreet() [1/2]

void dsm::Graph::addStreet ( const Street & street)

Add a street to the graph.

Parameters
streetA reference to the street to add

◆ addStreet() [2/2]

void dsm::Graph::addStreet ( std::shared_ptr< Street > street)

Add a street to the graph.

Parameters
streetA std::shared_ptr to the street to add

◆ adjMatrix()

const SparseMatrix< bool > & dsm::Graph::adjMatrix ( ) const
inline

Get the graph's adjacency matrix.

Returns
A std::shared_ptr to the graph's adjacency matrix

◆ adjustNodeCapacities()

void dsm::Graph::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.

◆ buildAdj()

void dsm::Graph::buildAdj ( )

Build the graph's adjacency matrix and computes max capacity.

The adjacency matrix is built using the graph's streets and nodes. N.B.: The street ids are reassigned using the max node id, i.e. newStreetId = srcId * n + dstId, where n is the max node id.

◆ exportCoordinates()

void dsm::Graph::exportCoordinates ( std::string const & path = "./coordinates.csv")

Export the nodes' coordinates to a file.

Parameters
pathThe path to the file to export the nodes' coordinates to (default: ./nodes.dsm)

◆ exportMatrix()

void dsm::Graph::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

◆ importCoordinates()

void dsm::Graph::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.

◆ importMatrix()

void dsm::Graph::importMatrix ( const std::string & fileName,
bool isAdj = true )

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.
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.

◆ importOSMEdges()

void dsm::Graph::importOSMEdges ( const std::string & fileName)

Import the graph's streets from a file.

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

◆ importOSMNodes()

void dsm::Graph::importOSMNodes ( 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

◆ makeRoundabout()

Roundabout & dsm::Graph::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()

SpireStreet & dsm::Graph::makeSpireStreet ( Id streetId)

Convert an existing street into a spire street.

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

◆ makeTrafficLight()

template<typename Delay >
requires (std::unsigned_integral<Delay>)
TrafficLight< Delay > & dsm::Graph::makeTrafficLight ( Id nodeId)

Convert an existing node to a traffic light.

Template Parameters
DelayThe type of the traffic light's delay
Parameters
nodeIdThe id of the node to convert to a traffic light
Returns
A reference to the traffic light
Exceptions
std::invalid_argumentif the node does not exist

◆ maxCapacity()

unsigned long long dsm::Graph::maxCapacity ( ) const
inline

Get the maximum agent capacity.

Returns
unsigned long long The maximum agent capacity of the graph

◆ nodeSet() [1/2]

std::unordered_map< Id, std::unique_ptr< Node > > & dsm::Graph::nodeSet ( )
inline

Get the graph's node map.

Returns
A std::unordered_map containing the graph's nodes

◆ nodeSet() [2/2]

const std::unordered_map< Id, std::unique_ptr< Node > > & dsm::Graph::nodeSet ( ) const
inline

Get the graph's node map.

Returns
A std::unordered_map containing the graph's nodes

◆ normalizeStreetCapacities()

void dsm::Graph::normalizeStreetCapacities ( double meanVehicleLength = 5.)

Normalize the streets' capacities.

Parameters
meanVehicleLengthThe mean vehicle length

The streets' capacities are normalized using the mean vehicle length following the formula: $ \text{capacity} = \frac{\text{length} * \text{nLanes}}{\text{meanVehicleLength}} $

◆ oppositeStreet()

const std::unique_ptr< Street > * dsm::Graph::oppositeStreet ( Id streetId) const

Get the opposite street of a street in the graph.

Parameters
streetIdThe id of the street
Exceptions
std::invalid_argumentif the street does not exist
Returns
A std::unique_ptr to the street if it exists, nullptr otherwise

◆ shortestPath() [1/2]

std::optional< DijkstraResult > dsm::Graph::shortestPath ( const Intersection & source,
const Intersection & destination ) const

Get the shortest path between two nodes using dijkstra algorithm.

Parameters
sourceThe source node
destinationThe destination node
Returns
A DijkstraResult object containing the path and the distance

◆ shortestPath() [2/2]

std::optional< DijkstraResult > dsm::Graph::shortestPath ( Id source,
Id destination ) const

Get the shortest path between two nodes using dijkstra algorithm.

Parameters
sourceThe source node id
destinationThe destination node id
Returns
A DijkstraResult object containing the path and the distance

◆ street()

const std::unique_ptr< Street > * dsm::Graph::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

◆ streetSet() [1/2]

std::unordered_map< Id, std::unique_ptr< Street > > & dsm::Graph::streetSet ( )
inline

Get the graph's street map.

Returns
A std::unordered_map containing the graph's streets

◆ streetSet() [2/2]

const std::unordered_map< Id, std::unique_ptr< Street > > & dsm::Graph::streetSet ( ) const
inline

Get the graph's street map.

Returns
A std::unordered_map containing the graph's streets

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