Dynamical system model
Loading...
Searching...
No Matches
dsm::RoadDynamics< delay_t > Class Template Referenceabstract

The RoadDynamics class represents the dynamics of the network. More...

#include <RoadDynamics.hpp>

Inheritance diagram for dsm::RoadDynamics< delay_t >:
dsm::Dynamics< RoadNetwork >

Public Member Functions

 RoadDynamics (RoadNetwork &graph, bool useCache=false, std::optional< unsigned int > seed=std::nullopt, std::function< double(const RoadNetwork *, Id, Id)> weightFunction=weight_functions::streetTime, double weightTreshold=60.)
 Construct a new RoadDynamics object.
 
void setErrorProbability (double errorProbability)
 Set the error probability.
 
void setPassageProbability (double passageProbability)
 
void setForcePriorities (bool forcePriorities)
 Set the force priorities flag.
 
void setDataUpdatePeriod (delay_t dataUpdatePeriod)
 Set the data update period.
 
void setDestinationNodes (std::initializer_list< Id > destinationNodes, bool updatePaths=true)
 Set the destination nodes.
 
template<typename TContainer>
requires (std::is_convertible_v<typename TContainer::value_type, Id>)
void setDestinationNodes (TContainer const &destinationNodes, bool updatePaths=true)
 Set the destination nodes.
 
virtual void setAgentSpeed (Size agentId)=0
 
void updatePaths ()
 Update the paths of the itineraries based on the given weight function.
 
void addAgentsUniformly (Size nAgents, std::optional< Id > itineraryId=std::nullopt)
 Add a set of agents to the simulation.
 
template<typename TContainer>
requires (std::is_same_v<TContainer, std::unordered_map<Id, double>> || std::is_same_v<TContainer, std::map<Id, double>>)
void addAgentsRandomly (Size nAgents, const TContainer &src_weights, const TContainer &dst_weights, const size_t minNodeDistance=0)
 Add a set of agents to the simulation.
 
void addAgentsRandomly (Size nAgents, const size_t minNodeDistance=0)
 
void addAgent (std::unique_ptr< Agent > agent)
 Add an agent to the simulation.
 
template<typename... TArgs>
requires (std::is_constructible_v<Agent, Time, TArgs...>)
void addAgent (TArgs &&... args)
 
template<typename... TArgs>
requires (std::is_constructible_v<Agent, Time, Id, TArgs...>)
void addAgents (Size nAgents, TArgs &&... args)
 
void removeAgent (Size agentId)
 Remove an agent from the simulation.
 
template<typename T1, typename... Tn>
requires (std::is_convertible_v<T1, Id> && (std::is_convertible_v<Tn, Size> && ...))
void removeAgents (T1 id, Tn... ids)
 
template<typename... TArgs>
requires (std::is_constructible_v<Itinerary, TArgs...>)
void addItinerary (TArgs &&... args)
 Add an itinerary.
 
void addItinerary (std::unique_ptr< Itinerary > itinerary)
 Add an itinerary.
 
void evolve (bool reinsert_agents=false)
 Evolve the simulation.
 
void optimizeTrafficLights (double const threshold=0., TrafficLightOptimization optimizationType=TrafficLightOptimization::DOUBLE_TAIL)
 Optimize the traffic lights by changing the green and red times.
 
const std::unordered_map< Id, std::unique_ptr< Itinerary > > & itineraries () const
 Get the itineraries.
 
const std::map< Id, std::unique_ptr< Agent > > & agents () const
 Get the agents.
 
Size nAgents () const
 Get the number of agents currently in the simulation.
 
Measurement< double > meanTravelTime (bool clearData=false)
 Get the mean travel time of the agents in \(s\).
 
Measurement< double > meanTravelDistance (bool clearData=false)
 Get the mean travel distance of the agents in \(m\).
 
Measurement< double > meanTravelSpeed (bool clearData=false)
 Get the mean travel speed of the agents in \(m/s\).
 
const std::unordered_map< Id, std::array< unsigned long long, 4 > > & turnCounts () const
 Get the turn counts of the agents.
 
std::unordered_map< Id, std::array< double, 4 > > turnProbabilities (bool reset=true)
 Get the turn probabilities of the agents.
 
std::unordered_map< Id, std::array< long, 4 > > turnMapping () const
 
Measurement< double > agentMeanSpeed () const
 Get the mean speed of the agents in \(m/s\).
 
virtual double streetMeanSpeed (Id streetId) const
 
virtual Measurement< double > streetMeanSpeed () const
 
virtual Measurement< double > streetMeanSpeed (double, bool) const
 
Measurement< double > streetMeanDensity (bool normalized=false) const
 Get the mean density of the streets in \(m^{-1}\).
 
Measurement< double > streetMeanFlow () const
 Get the mean flow of the streets in \(s^{-1}\).
 
Measurement< double > streetMeanFlow (double threshold, bool above) const
 Get the mean flow of the streets in \(s^{-1}\).
 
Measurement< double > meanSpireInputFlow (bool resetValue=true)
 Get the mean spire input flow of the streets in \(s^{-1}\).
 
Measurement< double > meanSpireOutputFlow (bool resetValue=true)
 Get the mean spire output flow of the streets in \(s^{-1}\).
 
void saveStreetDensities (const std::string &filename, bool normalized=true, char const separator=';') const
 Save the street densities in csv format.
 
void saveInputStreetCounts (const std::string &filename, bool reset=false, char const separator=';')
 Save the street input counts in csv format.
 
void saveOutputStreetCounts (const std::string &filename, bool reset=false, char const separator=';')
 Save the street output counts in csv format.
 
void saveTravelSpeeds (const std::string &filename, bool reset=false)
 Save the travel speeds of the agents in csv format.
 
void saveMacroscopicObservables (const std::string &filename, char const separator=';')
 Save the main macroscopic observables in csv format.
 
- Public Member Functions inherited from dsm::Dynamics< RoadNetwork >
 Dynamics (RoadNetwork &graph, std::optional< unsigned int > seed=std::nullopt)
 Construct a new Dynamics object.
 
void resetTime ()
 Reset the simulation time to 0.
 
const RoadNetworkgraph () const
 Get the graph.
 
Time time () const
 Get the current simulation time-step.
 

Protected Attributes

std::unordered_map< Id, std::array< unsigned long long, 4 > > m_turnCounts
 
std::unordered_map< Id, std::array< long, 4 > > m_turnMapping
 
std::unordered_map< Id, double > m_streetTails
 
std::vector< std::pair< double, double > > m_travelDTs
 
std::vector< Id > m_agentsToRemove
 
Time m_previousOptimizationTime
 
Time m_previousSpireTime
 
- Protected Attributes inherited from dsm::Dynamics< RoadNetwork >
Time m_time
 
std::mt19937_64 m_generator
 

Additional Inherited Members

- Protected Member Functions inherited from dsm::Dynamics< RoadNetwork >
void m_evolve ()
 

Detailed Description

template<typename delay_t>
requires (is_numeric_v<delay_t>)
class dsm::RoadDynamics< delay_t >

The RoadDynamics class represents the dynamics of the network.

Template Parameters
delay_tThe type of the agent's delay

Constructor & Destructor Documentation

◆ RoadDynamics()

template<typename delay_t>
requires (is_numeric_v<delay_t>)
dsm::RoadDynamics< delay_t >::RoadDynamics ( RoadNetwork & graph,
bool useCache = false,
std::optional< unsigned int > seed = std::nullopt,
std::function< double(const RoadNetwork *, Id, Id)> weightFunction = weight_functions::streetTime,
double weightTreshold = 60. )

Construct a new RoadDynamics object.

Parameters
graphThe graph representing the network
useCacheIf true, the cache is used (default is false)
seedThe seed for the random number generator (default is std::nullopt)
weightFunctionThe weight function for the Dijkstra's algorithm (default is weight_functions::streetTime)
weightTresholdThe weight treshold for updating the paths (default is 60.)

Member Function Documentation

◆ addAgent()

template<typename delay_t>
requires (is_numeric_v<delay_t>)
void dsm::RoadDynamics< delay_t >::addAgent ( std::unique_ptr< Agent > agent)

Add an agent to the simulation.

Parameters
agentstd::unique_ptr to the agent

◆ addAgentsRandomly()

template<typename delay_t>
requires (std::is_same_v<TContainer, std::unordered_map<Id, double>> || std::is_same_v<TContainer, std::map<Id, double>>)
template<typename TContainer>
requires (std::is_same_v<TContainer, std::unordered_map<Id, double>> || std::is_same_v<TContainer, std::map<Id, double>>)
void dsm::RoadDynamics< delay_t >::addAgentsRandomly ( Size nAgents,
const TContainer & src_weights,
const TContainer & dst_weights,
const size_t minNodeDistance = 0 )

Add a set of agents to the simulation.

Parameters
nAgentsThe number of agents to add
src_weightsThe weights of the source nodes
dst_weightsThe weights of the destination nodes
Exceptions
std::invalid_argumentIf the source and destination nodes are the same

◆ addAgentsUniformly()

template<typename delay_t>
requires (is_numeric_v<delay_t>)
void dsm::RoadDynamics< delay_t >::addAgentsUniformly ( Size nAgents,
std::optional< Id > itineraryId = std::nullopt )

Add a set of agents to the simulation.

Parameters
nAgentsThe number of agents to add
uniformlyIf true, the agents are added uniformly on the streets
Exceptions
std::runtime_errorIf there are no itineraries

◆ addItinerary() [1/2]

template<typename delay_t>
requires (is_numeric_v<delay_t>)
void dsm::RoadDynamics< delay_t >::addItinerary ( std::unique_ptr< Itinerary > itinerary)

Add an itinerary.

Parameters
itinerarystd::unique_ptr to the itinerary
Exceptions
std::invalid_argumentIf the itinerary already exists
std::invalid_argumentIf the itinerary's destination is not a node of the graph

◆ addItinerary() [2/2]

template<typename delay_t>
requires (std::is_constructible_v<Itinerary, TArgs...>)
template<typename... TArgs>
requires (std::is_constructible_v<Itinerary, TArgs...>)
void dsm::RoadDynamics< delay_t >::addItinerary ( TArgs &&... args)

Add an itinerary.

Parameters
...argsThe arguments to construct the itinerary

The arguments must be compatible with any constructor of the Itinerary class

◆ agentMeanSpeed()

template<typename delay_t>
requires (is_numeric_v<delay_t>)
Measurement< double > dsm::RoadDynamics< delay_t >::agentMeanSpeed ( ) const

Get the mean speed of the agents in \(m/s\).

Returns
Measurement<double> The mean speed of the agents and the standard deviation

◆ agents()

template<typename delay_t>
const std::map< Id, std::unique_ptr< Agent > > & dsm::RoadDynamics< delay_t >::agents ( ) const
inline

Get the agents.

Returns
const std::unordered_map<Id, Agent<Id>>&, The agents

◆ evolve()

template<typename delay_t>
requires (is_numeric_v<delay_t>)
void dsm::RoadDynamics< delay_t >::evolve ( bool reinsert_agents = false)

Evolve the simulation.

Evolve the simulation by moving the agents and updating the travel times. In particular:

  • Move the first agent of each street queue, if possible, putting it in the next node
  • Move the agents from each node, if possible, putting them in the next street and giving them a speed. If the error probability is not zero, the agents can move to a random street. If the agent is in the destination node, it is removed from the simulation (and then reinserted if reinsert_agents is true)
  • Cycle over agents and update their times
    Parameters
    reinsert_agentsIf true, the agents are reinserted in the simulation after they reach their destination

◆ itineraries()

template<typename delay_t>
const std::unordered_map< Id, std::unique_ptr< Itinerary > > & dsm::RoadDynamics< delay_t >::itineraries ( ) const
inline

Get the itineraries.

Returns
const std::unordered_map<Id, Itinerary>&, The itineraries

◆ meanSpireInputFlow()

template<typename delay_t>
requires (is_numeric_v<delay_t>)
Measurement< double > dsm::RoadDynamics< delay_t >::meanSpireInputFlow ( bool resetValue = true)

Get the mean spire input flow of the streets in \(s^{-1}\).

Parameters
resetValueIf true, the spire input/output flows are cleared after the computation
Returns
Measurement<double> The mean spire input flow of the streets and the standard deviation

The spire input flow is computed as the sum of counts over the product of the number of spires and the time delta

◆ meanSpireOutputFlow()

template<typename delay_t>
requires (is_numeric_v<delay_t>)
Measurement< double > dsm::RoadDynamics< delay_t >::meanSpireOutputFlow ( bool resetValue = true)

Get the mean spire output flow of the streets in \(s^{-1}\).

Parameters
resetValueIf true, the spire output/input flows are cleared after the computation
Returns
Measurement<double> The mean spire output flow of the streets and the standard deviation

The spire output flow is computed as the sum of counts over the product of the number of spires and the time delta

◆ meanTravelDistance()

template<typename delay_t>
requires (is_numeric_v<delay_t>)
Measurement< double > dsm::RoadDynamics< delay_t >::meanTravelDistance ( bool clearData = false)

Get the mean travel distance of the agents in \(m\).

Parameters
clearDataIf true, the travel distances are cleared after the computation
Returns
Measurement<double> The mean travel distance of the agents and the standard deviation

◆ meanTravelSpeed()

template<typename delay_t>
requires (is_numeric_v<delay_t>)
Measurement< double > dsm::RoadDynamics< delay_t >::meanTravelSpeed ( bool clearData = false)

Get the mean travel speed of the agents in \(m/s\).

Parameters
clearDataIf true, the travel times and distances are cleared after the computation
Returns
Measurement<double> The mean travel speed of the agents and the standard deviation

◆ meanTravelTime()

template<typename delay_t>
requires (is_numeric_v<delay_t>)
Measurement< double > dsm::RoadDynamics< delay_t >::meanTravelTime ( bool clearData = false)

Get the mean travel time of the agents in \(s\).

Parameters
clearDataIf true, the travel times are cleared after the computation
Returns
Measurement<double> The mean travel time of the agents and the standard deviation

◆ nAgents()

template<typename delay_t>
Size dsm::RoadDynamics< delay_t >::nAgents ( ) const
inline

Get the number of agents currently in the simulation.

Returns
Size The number of agents

◆ optimizeTrafficLights()

template<typename delay_t>
requires (is_numeric_v<delay_t>)
void dsm::RoadDynamics< delay_t >::optimizeTrafficLights ( double const threshold = 0.,
TrafficLightOptimization optimizationType = TrafficLightOptimization::DOUBLE_TAIL )

Optimize the traffic lights by changing the green and red times.

Parameters
thresholddouble, The minimum difference between green and red queues to trigger the optimization (n agents - default is 0)
optimizationTypeTrafficLightOptimization, The type of optimization. Default is DOUBLE_TAIL

The function cycles over the traffic lights and, if the difference between the two tails is greater than the threshold multiplied by the mean capacity of the streets, it changes the green and red times of the traffic light, keeping the total cycle time constant. The optimizationType parameter can be set to SINGLE_TAIL to use an algorith which looks only at the incoming street tails or to DOUBLE_TAIL to consider both incoming and outgoing street tails.

◆ removeAgent()

template<typename delay_t>
requires (is_numeric_v<delay_t>)
void dsm::RoadDynamics< delay_t >::removeAgent ( Size agentId)

Remove an agent from the simulation.

Parameters
agentIdthe id of the agent to remove

◆ saveInputStreetCounts()

template<typename delay_t>
requires (is_numeric_v<delay_t>)
void dsm::RoadDynamics< delay_t >::saveInputStreetCounts ( const std::string & filename,
bool reset = false,
char const separator = ';' )

Save the street input counts in csv format.

Parameters
filenameThe name of the file
resetIf true, the input counts are cleared after the computation

NOTE: counts are printed only if the street is a spire

◆ saveMacroscopicObservables()

template<typename delay_t>
requires (is_numeric_v<delay_t>)
void dsm::RoadDynamics< delay_t >::saveMacroscopicObservables ( const std::string & filename,
char const separator = ';' )

Save the main macroscopic observables in csv format.

Parameters
filenameThe name of the file
separatorThe separator character (default is ';')

The file contains the following columns:

  • time: the time of the simulation
  • n_agents: the number of agents currently in the simulation
  • mean_speed - mean_speed_std: the mean speed of the agents
  • mean_density - mean_density_std: the (normalized) mean density of the streets
  • mean_flow - mean_flow_std: the mean flow of the streets
  • mean_flow_spires - mean_flow_spires_std: the mean flow of the spires
  • mean_traveltime - mean_traveltime_std: the mean travel time of the agents
  • mean_traveldistance - mean_traveldistance_err: the mean travel distance of the agents
  • mean_travelspeed - mean_travelspeed_std: the mean travel speed of the agents

NOTE: the mean density is normalized in [0, 1] and reset is true for all observables which have such parameter

◆ saveOutputStreetCounts()

template<typename delay_t>
requires (is_numeric_v<delay_t>)
void dsm::RoadDynamics< delay_t >::saveOutputStreetCounts ( const std::string & filename,
bool reset = false,
char const separator = ';' )

Save the street output counts in csv format.

Parameters
filenameThe name of the file
resetIf true, the output counts are cleared after the computation

NOTE: counts are printed only if the street is a spire

◆ saveStreetDensities()

template<typename delay_t>
requires (is_numeric_v<delay_t>)
void dsm::RoadDynamics< delay_t >::saveStreetDensities ( const std::string & filename,
bool normalized = true,
char const separator = ';' ) const

Save the street densities in csv format.

Parameters
filenameThe name of the file
normalizedIf true, the densities are normalized in [0, 1]

◆ saveTravelSpeeds()

template<typename delay_t>
requires (is_numeric_v<delay_t>)
void dsm::RoadDynamics< delay_t >::saveTravelSpeeds ( const std::string & filename,
bool reset = false )

Save the travel speeds of the agents in csv format.

Parameters
filenameThe name of the file
resetIf true, the travel speeds are cleared after the computation

◆ setAgentSpeed()

template<typename delay_t>
virtual void dsm::RoadDynamics< delay_t >::setAgentSpeed ( Size agentId)
pure virtual

Implemented in dsm::FirstOrderDynamics.

◆ setDataUpdatePeriod()

template<typename delay_t>
void dsm::RoadDynamics< delay_t >::setDataUpdatePeriod ( delay_t dataUpdatePeriod)
inline

Set the data update period.

Parameters
dataUpdatePerioddelay_t, The period

Some data, i.e. the street queue lengths, are stored only after a fixed amount of time which is represented by this variable.

◆ setDestinationNodes() [1/2]

template<typename delay_t>
requires (is_numeric_v<delay_t>)
void dsm::RoadDynamics< delay_t >::setDestinationNodes ( std::initializer_list< Id > destinationNodes,
bool updatePaths = true )

Set the destination nodes.

Parameters
destinationNodesThe destination nodes (as an initializer list)
updatePathsIf true, the paths are updated

◆ setDestinationNodes() [2/2]

template<typename delay_t>
requires (std::is_convertible_v<typename TContainer::value_type, Id>)
template<typename TContainer>
requires (std::is_convertible_v<typename TContainer::value_type, Id>)
void dsm::RoadDynamics< delay_t >::setDestinationNodes ( TContainer const & destinationNodes,
bool updatePaths = true )

Set the destination nodes.

Parameters
destinationNodesA container of destination nodes ids
updatePathsIf true, the paths are updated

The container must have a value_type convertible to Id and begin() and end() methods

◆ setErrorProbability()

template<typename delay_t>
requires (is_numeric_v<delay_t>)
void dsm::RoadDynamics< delay_t >::setErrorProbability ( double errorProbability)

Set the error probability.

Parameters
errorProbabilityThe error probability
Exceptions
std::invalid_argumentIf the error probability is not between 0 and 1

◆ setForcePriorities()

template<typename delay_t>
void dsm::RoadDynamics< delay_t >::setForcePriorities ( bool forcePriorities)
inline

Set the force priorities flag.

Parameters
forcePrioritiesThe flag

If true, if an agent cannot move to the next street, the whole node is skipped

◆ streetMeanDensity()

template<typename delay_t>
requires (is_numeric_v<delay_t>)
Measurement< double > dsm::RoadDynamics< delay_t >::streetMeanDensity ( bool normalized = false) const

Get the mean density of the streets in \(m^{-1}\).

Returns
Measurement<double> The mean density of the streets and the standard deviation

◆ streetMeanFlow() [1/2]

template<typename delay_t>
requires (is_numeric_v<delay_t>)
Measurement< double > dsm::RoadDynamics< delay_t >::streetMeanFlow ( ) const

Get the mean flow of the streets in \(s^{-1}\).

Returns
Measurement<double> The mean flow of the streets and the standard deviation

◆ streetMeanFlow() [2/2]

template<typename delay_t>
requires (is_numeric_v<delay_t>)
Measurement< double > dsm::RoadDynamics< delay_t >::streetMeanFlow ( double threshold,
bool above ) const

Get the mean flow of the streets in \(s^{-1}\).

Parameters
thresholdThe density threshold to consider
aboveIf true, the function returns the mean flow of the streets with a density above the threshold, otherwise below
Returns
Measurement<double> The mean flow of the streets and the standard deviation

◆ streetMeanSpeed() [1/3]

template<typename delay_t>
requires (is_numeric_v<delay_t>)
Measurement< double > dsm::RoadDynamics< delay_t >::streetMeanSpeed ( ) const
virtual

Reimplemented in dsm::FirstOrderDynamics.

◆ streetMeanSpeed() [2/3]

template<typename delay_t>
requires (is_numeric_v<delay_t>)
Measurement< double > dsm::RoadDynamics< delay_t >::streetMeanSpeed ( double threshold,
bool above ) const
virtual

Reimplemented in dsm::FirstOrderDynamics.

◆ streetMeanSpeed() [3/3]

template<typename delay_t>
requires (is_numeric_v<delay_t>)
double dsm::RoadDynamics< delay_t >::streetMeanSpeed ( Id streetId) const
virtual

Reimplemented in dsm::FirstOrderDynamics.

◆ turnCounts()

template<typename delay_t>
const std::unordered_map< Id, std::array< unsigned long long, 4 > > & dsm::RoadDynamics< delay_t >::turnCounts ( ) const
inline

Get the turn counts of the agents.

Returns
const std::array<unsigned long long, 3>& The turn counts

The array contains the counts of left (0), straight (1), right (2) and U (3) turns

◆ turnProbabilities()

template<typename delay_t>
requires (is_numeric_v<delay_t>)
std::unordered_map< Id, std::array< double, 4 > > dsm::RoadDynamics< delay_t >::turnProbabilities ( bool reset = true)

Get the turn probabilities of the agents.

Returns
std::array<double, 3> The turn probabilities

The array contains the probabilities of left (0), straight (1), right (2) and U (3) turns


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