Dynamical system model
Loading...
Searching...
No Matches
dsm::Graph< Id, Size > Class Template Reference

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

#include <Graph.hpp>

Public Member Functions

 Graph (const SparseMatrix< Id, bool > &adj)
 Construct a new Graph object.
 
 Graph (const std::unordered_map< Id, std::unique_ptr< Street< Id, Size > > > &streetSet)
 Construct a new Graph object.
 
 Graph (const Graph< Id, Size > &other)
 
Graphoperator= (const Graph< Id, Size > &other)
 
 Graph (Graph< Id, Size > &&)=default
 
Graphoperator= (Graph< Id, Size > &&)=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 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 addNode (std::unique_ptr< NodeConcept< Id, Size > > node)
 Add a node to the graph.
 
void addNode (const Node< Id, Size > &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>)
void makeTrafficLight (Id nodeId)
 Convert an existing node to a traffic light.
 
void makeRoundabout (Id nodeId)
 Convert an existing node into a roundabout.
 
void makeSpireStreet (Id streetId)
 Convert an existing street into a spire street.
 
void addStreet (std::shared_ptr< Street< Id, Size > > street)
 Add a street to the graph.
 
void addStreet (const Street< Id, Size > &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< Id, bool > & adjMatrix () const
 Get the graph's adjacency matrix.
 
const std::unordered_map< Id, std::unique_ptr< NodeConcept< Id, Size > > > & nodeSet () const
 Get the graph's node map.
 
std::unordered_map< Id, std::unique_ptr< NodeConcept< Id, Size > > > & nodeSet ()
 Get the graph's node map.
 
const std::unordered_map< Id, std::unique_ptr< Street< Id, Size > > > & streetSet () const
 Get the graph's street map.
 
std::unordered_map< Id, std::unique_ptr< Street< Id, Size > > > & streetSet ()
 Get the graph's street map.
 
const std::unique_ptr< Street< Id, Size > > * street (Id source, Id destination) const
 Get a street from the graph.
 
unsigned long long maxCapacity () const
 Get the maximum agent capacity.
 
std::optional< DijkstraResult< Id > > shortestPath (const Node< Id, Size > &source, const Node< Id, Size > &destination) const
 Get the shortest path between two nodes using dijkstra algorithm.
 
std::optional< DijkstraResult< Id > > shortestPath (Id source, Id destination) const
 Get the shortest path between two nodes using dijkstra algorithm.
 

Detailed Description

template<typename Id, typename Size>
requires (std::unsigned_integral<Id> && std::unsigned_integral<Size>)
class dsm::Graph< Id, Size >

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]

template<typename Id , typename Size >
requires (std::unsigned_integral<Id> && std::unsigned_integral<Size>)
dsm::Graph< Id, Size >::Graph ( const SparseMatrix< Id, bool > & adj)

Construct a new Graph object.

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

◆ Graph() [2/2]

template<typename Id , typename Size >
requires (std::unsigned_integral<Id> && std::unsigned_integral<Size>)
dsm::Graph< Id, Size >::Graph ( const std::unordered_map< Id, std::unique_ptr< Street< Id, Size > > > & streetSet)

Construct a new Graph object.

Parameters
streetSetA map of streets representing the graph's streets

Member Function Documentation

◆ addNode() [1/2]

template<typename Id , typename Size >
requires (std::unsigned_integral<Id> && std::unsigned_integral<Size>)
void dsm::Graph< Id, Size >::addNode ( const Node< Id, Size > & node)

Add a node to the graph.

Parameters
nodeA reference to the node to add

◆ addNode() [2/2]

template<typename Id , typename Size >
requires (std::unsigned_integral<Id> && std::unsigned_integral<Size>)
void dsm::Graph< Id, Size >::addNode ( std::unique_ptr< NodeConcept< Id, Size > > node)

Add a node to the graph.

Parameters
nodeA std::shared_ptr to the node to add

◆ addStreet() [1/2]

template<typename Id , typename Size >
requires (std::unsigned_integral<Id> && std::unsigned_integral<Size>)
void dsm::Graph< Id, Size >::addStreet ( const Street< Id, Size > & street)

Add a street to the graph.

Parameters
streetA reference to the street to add

◆ addStreet() [2/2]

template<typename Id , typename Size >
requires (std::unsigned_integral<Id> && std::unsigned_integral<Size>)
void dsm::Graph< Id, Size >::addStreet ( std::shared_ptr< Street< Id, Size > > street)

Add a street to the graph.

Parameters
streetA std::shared_ptr to the street to add

◆ adjMatrix()

template<typename Id , typename Size >
const SparseMatrix< Id, bool > & dsm::Graph< Id, Size >::adjMatrix ( ) const
inline

Get the graph's adjacency matrix.

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

◆ buildAdj()

template<typename Id , typename Size >
requires (std::unsigned_integral<Id> && std::unsigned_integral<Size>)
void dsm::Graph< Id, Size >::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.

◆ exportMatrix()

template<typename Id , typename Size >
requires (std::unsigned_integral<Id> && std::unsigned_integral<Size>)
void dsm::Graph< Id, Size >::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.
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()

template<typename Id , typename Size >
requires (std::unsigned_integral<Id> && std::unsigned_integral<Size>)
void dsm::Graph< Id, Size >::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()

template<typename Id , typename Size >
requires (std::unsigned_integral<Id> && std::unsigned_integral<Size>)
void dsm::Graph< Id, Size >::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()

template<typename Id , typename Size >
requires (std::unsigned_integral<Id> && std::unsigned_integral<Size>)
void dsm::Graph< Id, Size >::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()

template<typename Id , typename Size >
requires (std::unsigned_integral<Id> && std::unsigned_integral<Size>)
void dsm::Graph< Id, Size >::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()

template<typename Id , typename Size >
requires (std::unsigned_integral<Id> && std::unsigned_integral<Size>)
void dsm::Graph< Id, Size >::makeRoundabout ( Id nodeId)

Convert an existing node into a roundabout.

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

◆ makeSpireStreet()

template<typename Id , typename Size >
requires (std::unsigned_integral<Id> && std::unsigned_integral<Size>)
void dsm::Graph< Id, Size >::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

◆ makeTrafficLight()

template<typename Id , typename Size >
requires (std::unsigned_integral<Delay>)
template<typename Delay >
requires (std::unsigned_integral<Delay>)
void dsm::Graph< Id, Size >::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
Exceptions
std::invalid_argumentif the node does not exist

◆ maxCapacity()

template<typename Id , typename Size >
unsigned long long dsm::Graph< Id, Size >::maxCapacity ( ) const
inline

Get the maximum agent capacity.

Returns
unsigned long long The maximum agent capacity of the graph

◆ nodeSet() [1/2]

template<typename Id , typename Size >
std::unordered_map< Id, std::unique_ptr< NodeConcept< Id, Size > > > & dsm::Graph< Id, Size >::nodeSet ( )
inline

Get the graph's node map.

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

◆ nodeSet() [2/2]

template<typename Id , typename Size >
const std::unordered_map< Id, std::unique_ptr< NodeConcept< Id, Size > > > & dsm::Graph< Id, Size >::nodeSet ( ) const
inline

Get the graph's node map.

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

◆ shortestPath() [1/2]

template<typename Id , typename Size >
requires (std::unsigned_integral<Id> && std::unsigned_integral<Size>)
std::optional< DijkstraResult< Id > > dsm::Graph< Id, Size >::shortestPath ( const Node< Id, Size > & source,
const Node< Id, Size > & 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]

template<typename Id , typename Size >
requires (std::unsigned_integral<Id> && std::unsigned_integral<Size>)
std::optional< DijkstraResult< Id > > dsm::Graph< Id, Size >::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()

template<typename Id , typename Size >
requires (std::unsigned_integral<Id> && std::unsigned_integral<Size>)
const std::unique_ptr< Street< Id, Size > > * dsm::Graph< Id, Size >::street ( Id source,
Id destination ) const

Get a street from the graph.

Parameters
sourceThe source node
destinationThe destination node
Returns
A std::optional containing a std::shared_ptr to the street if it exists, otherwise std::nullopt

◆ streetSet() [1/2]

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

Get the graph's street map.

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

◆ streetSet() [2/2]

template<typename Id , typename Size >
const std::unordered_map< Id, std::unique_ptr< Street< Id, Size > > > & dsm::Graph< Id, Size >::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 file: