Dynamical System Framework
Loading...
Searching...
No Matches
dsf::mobility::RoadNetwork Class Reference

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

#include <RoadNetwork.hpp>

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

Public Member Functions

 RoadNetwork (const RoadNetwork &)=delete
RoadNetwork & operator= (const RoadNetwork &)=delete
 RoadNetwork (RoadNetwork &&)=default
RoadNetwork & operator= (RoadNetwork &&)=default
std::size_t nCoils () const
 Get the graph's number of coil streets.
std::size_t nIntersections () const
 Get the graph's number of intersections.
std::size_t nRoundabouts () const
 Get the graph's number of roundabouts.
std::size_t nTrafficLights () const
 Get the graph's number of traffic lights.
void adjustNodeCapacities ()
 Adjust the nodes' transport capacity.
void autoInitTrafficLights (double const mainRoadPercentage=0.6, dsf::Delay const defaultCycleDuration=90)
 Auto-initialise traffic light phases from street geometry.
void autoMapStreetLanes ()
 Automatically re-maps street lanes basing on network's topology.
void autoAssignRoadPriorities ()
 Automatically assigns road priorities at intersections, basing on road types.
void setEdgeWeight (std::string_view const strv_weight, std::optional< double > const threshold=std::nullopt) final
 Set the edge weight function based on a string identifier.
void describe (std::ostream &os=std::cout) const
 Describe the RoadNetwork.
template<typename... TArgs>
void importEdges (const std::string &fileName, TArgs &&... args)
 Import the graph's streets from a file.
template<typename... TArgs>
void importNodeProperties (const std::string &fileName, TArgs &&... args)
 Import the graph's nodes properties from a file.
void importTrafficLights (const std::string &fileName)
 Import traffic light phases from a legacy CSV 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)
 Convert an existing node to a traffic light.
RoundaboutmakeRoundabout (Id nodeId)
 Convert an existing node into a roundabout.
void addCoil (Id streetId, std::string const &name=std::string())
 Add a coil (dsf::Counter sensor) on the street with streetId.
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)
void setStreetStatusById (Id const streetId, RoadStatus const status)
 Set the street's status by its id.
void setStreetStatusByName (std::string const &name, RoadStatus const status)
 Set the street's status of all streets with the given name.
void changeStreetNLanesById (Id const streetId, int const nLanes, std::optional< double > const speedFactor=std::nullopt)
 Change the street's number of lanes by its id.
void changeStreetNLanesByName (std::string const &name, int const nLanes, std::optional< double > const speedFactor=std::nullopt)
 Change the street's number of lanes of all streets with the given name.
void changeStreetCapacityById (Id const streetId, double const factor)
 Change the street's capacity by its id.
void changeStreetCapacityByName (std::string const &name, double const factor)
 Change the street's capacity of all streets with the given name.
Street const * street (Id source, Id destination) const
 Get a street from the graph.
auto capacity () const noexcept
 Get the maximum agent capacity.
void exportCSV (std::string_view const folder) const
 Export the graph's edges and nodes to two CSV files in the specified folder.
Public Member Functions inherited from dsf::Network< RoadJunction, Street >
 Network ()=default
 Construct a new empty Network object.
auto const & nodes () const noexcept
 Get the nodes as an unordered map.
auto const & edges () const noexcept
 Get the edges as an unordered map.
auto nNodes () const noexcept
 Get the number of nodes.
auto nEdges () const noexcept
 Get the number of edges.
void addNode (TArgs &&... args)
void addNDefaultNodes (std::size_t const n)
void addEdge (TArgs &&... args)
const auto & node (Id const nodeId) const
 Get a node by id.
const auto & edge (Id const edgeId) const
 Get an edge by id.
PathCollection allPathsTo (Id const targetId) const
 Perform a global Dijkstra search to a target node from all other nodes in the graph.
PathCollection shortestPath (Id const sourceId, Id const targetId) const
 Find the shortest path between two nodes using Dijkstra's algorithm.
void computeBetweennessCentralities ()
 Compute node weighted betweenness centralities using Brandes' algorithm.
void computeEdgeBetweennessCentralities ()
 Compute edge weighted betweenness centralities using Brandes' algorithm.
void computeEdgeKBetweennessCentralities (std::size_t const K)
 Compute edge betweenness centralities using Yen's K-shortest paths.

Additional Inherited Members

Protected Member Functions inherited from dsf::Network< RoadJunction, Street >
constexpr auto m_cantorHash (Id u, Id v) 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
std::function< double(Street const &)> m_weightFunction
std::optional< double > m_weightThreshold

Detailed Description

The RoadNetwork class represents a graph in the network.

Member Function Documentation

◆ addCoil()

void dsf::mobility::RoadNetwork::addCoil ( Id streetId,
std::string const & name = std::string() )

Add a coil (dsf::Counter sensor) on the street with streetId.

Parameters
streetIdThe id of the street to add the coil to
nameThe coil name
Exceptions
std::invalid_argumentif the street does not exist

◆ addStreet()

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

Add a street to the graph.

Parameters
streetA reference to the street to add

◆ adjustNodeCapacities()

void dsf::mobility::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.

◆ autoInitTrafficLights()

void dsf::mobility::RoadNetwork::autoInitTrafficLights ( double const mainRoadPercentage = 0.6,
dsf::Delay const defaultCycleDuration = 90 )

Auto-initialise traffic light phases from street geometry.

Parameters
mainRoadPercentageFraction of the cycle time allocated to priority streets (default 0.6).
defaultCycleDurationThe default cycle duration in ticks (default 90).
Exceptions
std::invalid_argumentIf mainRoadPercentage is not in the range (0, 1).

For each TrafficLight node that has no phases yet:

  • Nodes with fewer than 3 ingoing edges are downgraded to plain Intersection nodes.
  • Priority streets are detected by name, speed limit, lane count or angle (in that order of precedence).
  • Two phases are created: Phase 0 (priority streets) — duration = mainRoadPercentage * DEFAULT_CYCLE Phase 1 (non-priority streets) — duration = remaining ticks
  • Every street is added to its phase with Direction::ANY
  • Nodes whose phases have been set manually (via JSON config or importTrafficLights()) are skipped.

◆ autoMapStreetLanes()

void dsf::mobility::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

◆ capacity()

auto dsf::mobility::RoadNetwork::capacity ( ) const
inlinenoexcept

Get the maximum agent capacity.

Returns
std::size_t The maximum agent capacity of the graph

◆ changeStreetCapacityById()

void dsf::mobility::RoadNetwork::changeStreetCapacityById ( Id const streetId,
double const factor )

Change the street's capacity by its id.

Parameters
streetIdThe id of the street
factorThe factor to multiply the capacity by

◆ changeStreetCapacityByName()

void dsf::mobility::RoadNetwork::changeStreetCapacityByName ( std::string const & name,
double const factor )

Change the street's capacity of all streets with the given name.

Parameters
nameThe name to match
factorThe factor to multiply the capacity by

◆ changeStreetNLanesById()

void dsf::mobility::RoadNetwork::changeStreetNLanesById ( Id const streetId,
int const nLanes,
std::optional< double > const speedFactor = std::nullopt )

Change the street's number of lanes by its id.

Parameters
streetIdThe id of the street
nLanesThe new number of lanes
speedFactorOptional, The factor to multiply the max speed of the street

◆ changeStreetNLanesByName()

void dsf::mobility::RoadNetwork::changeStreetNLanesByName ( std::string const & name,
int const nLanes,
std::optional< double > const speedFactor = std::nullopt )

Change the street's number of lanes of all streets with the given name.

Parameters
nameThe name to match
nLanesThe new number of lanes
speedFactorOptional, The factor to multiply the max speed of the street

◆ describe()

void dsf::mobility::RoadNetwork::describe ( std::ostream & os = std::cout) const

Describe the RoadNetwork.

Parameters
osThe output stream to write the description to (default is std::cout)

◆ exportCSV()

void dsf::mobility::RoadNetwork::exportCSV ( std::string_view const folder) const

Export the graph's edges and nodes to two CSV files in the specified folder.

Parameters
folderThe folder to export the files to

◆ importEdges()

template<typename... TArgs>
void dsf::mobility::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
  • 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):

  • type: The type of the street (e.g. residential, primary, secondary, etc.)
  • 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
  • priority: boolean, whether the street is a priority road or not. This information can be used in the traffic light cycle generation.
  • any additional CSV column or JSON field will be imported as an edge attribute, with automatic type inference among bool, int64, double, string and null.
    Parameters
    argsAdditional arguments

◆ importNodeProperties()

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

Import the graph's nodes properties from a file.

Parameters
fileNameThe name of the file to import the nodes properties from.
argsAdditional arguments

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

  • id: The id of the node
  • type: The type of the node, e.g. roundabout, traffic_signals, etc.
  • geometry: The geometry of the node, as a POINT

◆ importTrafficLights()

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

Import traffic light phases from a legacy CSV file.

Parameters
fileNameThe path to the CSV file.

The file uses ';' as separator. Expected columns (in order):

  • id : The TrafficLight node id
  • sourceId : The source node id of the ingoing street
  • cycleTime : Total cycle duration in ticks
  • greenTime : Green duration for this street in ticks

The importer reconstructs a two-phase TrafficLight from each node's entries:

  • Streets whose greenTime equals the first greenTime seen for that node → Phase 0 (duration = firstGreenTime).
  • Remaining streets → Phase 1 (duration = cycleTime − firstGreenTime).

All streets are added with Direction::ANY. Use the JSON config block under road_network.traffic_lights for direction-level control.

Exceptions
std::runtime_errorif the file cannot be opened.

◆ makeRoundabout()

Roundabout & dsf::mobility::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

◆ makeStation()

Station & dsf::mobility::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::mobility::RoadNetwork::makeTrafficLight ( Id const nodeId)

Convert an existing node to a traffic light.

Parameters
nodeIdThe id of the node to convert.
Returns
A reference to the new TrafficLight node.
Exceptions
std::invalid_argumentif the node does not exist.
Note
Phases are NOT set here. Call autoInitTrafficLights() afterwards for geometry-based auto-deduction, or configure phases explicitly via TrafficLight::setPhases() / TrafficLight::addPhase().

◆ nCoils()

std::size_t dsf::mobility::RoadNetwork::nCoils ( ) const

Get the graph's number of coil streets.

Returns
std::size_t The number of coil streets

◆ nIntersections()

std::size_t dsf::mobility::RoadNetwork::nIntersections ( ) const

Get the graph's number of intersections.

Returns
std::size_t The number of intersections

◆ nRoundabouts()

std::size_t dsf::mobility::RoadNetwork::nRoundabouts ( ) const

Get the graph's number of roundabouts.

Returns
std::size_t The number of roundabouts

◆ nTrafficLights()

std::size_t dsf::mobility::RoadNetwork::nTrafficLights ( ) const

Get the graph's number of traffic lights.

Returns
std::size_t The number of traffic lights

◆ setEdgeWeight()

void dsf::mobility::RoadNetwork::setEdgeWeight ( std::string_view const strv_weight,
std::optional< double > const threshold = std::nullopt )
finalvirtual

Set the edge weight function based on a string identifier.

Parameters
strv_weightThe string identifier of the weight function. Supported values are "travelTime", "length" and any custom attribute name.
thresholdAn optional threshold to apply to the weight function. The effective weight will be weight * (1 + threshold). This can be used to increase the weight of certain paths and thus make them less likely to be chosen by agents when using a weight-based path update strategy.

Implements dsf::Network< RoadJunction, Street >.

◆ setStreetStatusById()

void dsf::mobility::RoadNetwork::setStreetStatusById ( Id const streetId,
RoadStatus const status )

Set the street's status by its id.

Parameters
streetIdThe id of the street
statusThe status to set

◆ setStreetStatusByName()

void dsf::mobility::RoadNetwork::setStreetStatusByName ( std::string const & name,
RoadStatus const status )

Set the street's status of all streets with the given name.

Parameters
nameThe name to match
statusThe status to set

◆ street()

Street const * dsf::mobility::RoadNetwork::street ( Id source,
Id destination ) const

Get a street from the graph.

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

The documentation for this class was generated from the following files:
  • src/dsf/mobility/RoadNetwork.hpp
  • src/dsf/mobility/RoadNetwork.cpp