Dynamical system model
Loading...
Searching...
No Matches
dsm::Dynamics< Delay > Class Template Referenceabstract

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

#include <Dynamics.hpp>

Inheritance diagram for dsm::Dynamics< Delay >:
dsm::FirstOrderDynamics< Delay >

Public Member Functions

 Dynamics (Graph &graph)
 Construct a new Dynamics object.
 
void setItineraries (std::span< Itinerary > itineraries)
 Set the itineraries.
 
void setSeed (unsigned int seed)
 Set the seed for the graph's random number generator.
 
void setMinSpeedRateo (double minSpeedRateo)
 Set the minim speed rateo, i.e. the minim speed with respect to the speed limit.
 
void setErrorProbability (double errorProbability)
 Set the error probability.
 
void setMaxFlowPercentage (double maxFlowPercentage)
 Set the maximum flow percentage.
 
void setDestinationNodes (const std::span< Id > &destinationNodes, bool updatePaths=true)
 Set the dynamics destination nodes auto-generating itineraries.
 
virtual void setAgentSpeed (Size agentId)=0
 Set the speed of an agent.
 
void setForcePriorities (bool forcePriorities)
 Set the force priorities flag.
 
void setDataUpdatePeriod (Delay dataUpdatePeriod)
 Set the data update period.
 
virtual void updatePaths ()
 Update the paths of the itineraries based on the actual travel times.
 
virtual void evolve (bool reinsert_agents=false)
 Evolve the simulation.
 
void optimizeTrafficLights (Delay nCycles, double threshold=0., double densityTolerance=0.)
 Optimize the traffic lights by changing the green and red times.
 
const Graphgraph () const
 Get the graph.
 
const std::unordered_map< Id, std::unique_ptr< Itinerary > > & itineraries () const
 Get the itineraries.
 
const std::map< Id, std::unique_ptr< Agent< Delay > > > & agents () const
 Get the agents.
 
TimePoint time () const
 Get the time.
 
void addAgent (const Agent< Delay > &agent)
 Add an agent to the simulation.
 
void addAgent (std::unique_ptr< Agent< Delay > > agent)
 Add an agent to the simulation.
 
void addAgent (Id srcNodeId, Id itineraryId)
 Add an agent with given source node and itinerary.
 
void addAgents (Id itineraryId, Size nAgents=1, std::optional< Id > srcNodeId=std::nullopt)
 Add a pack of agents to the simulation.
 
template<typename... Tn>
requires (is_agent_v<Tn> && ...)
void addAgents (Tn... agents)
 Add a pack of agents to the simulation.
 
template<typename T1 , typename... Tn>
requires (is_agent_v<T1> && (is_agent_v<Tn> && ...))
void addAgents (T1 agent, Tn... agents)
 Add a pack of agents to the simulation.
 
void addAgents (std::span< Agent< Delay > > agents)
 Add a set of agents to the simulation.
 
virtual 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)
 
void removeAgent (Size agentId)
 Remove an agent from the simulation.
 
template<typename T1 , typename... Tn>
requires (std::is_convertible_v<T1, Size> && (std::is_convertible_v<Tn, Size> && ...))
void removeAgents (T1 id, Tn... ids)
 
void addItinerary (const Itinerary &itinerary)
 Add an itinerary.
 
void addItinerary (std::unique_ptr< Itinerary > itinerary)
 Add an itinerary.
 
template<typename... Tn>
requires (is_itinerary_v<Tn> && ...)
void addItineraries (Tn... itineraries)
 
template<typename T1 , typename... Tn>
requires (is_itinerary_v<T1> && (is_itinerary_v<Tn> && ...))
void addItineraries (T1 itinerary, Tn... itineraries)
 Add a pack of itineraries.
 
void addItineraries (std::span< Itinerary > itineraries)
 Add a set of itineraries.
 
void resetTime ()
 Reset the simulation time.
 
Measurement< double > agentMeanSpeed () const
 Get the mean speed of the agents in $m/s$.
 
virtual double streetMeanSpeed (Id) const =0
 
virtual Measurement< double > streetMeanSpeed () const =0
 
virtual Measurement< double > streetMeanSpeed (double, bool) const =0
 
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}$.
 
Measurement< double > meanTravelTime (bool clearData=false)
 Get the mean travel time of the agents in $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
 

Protected Member Functions

virtual Id m_nextStreetId (Id agentId, Id NodeId, std::optional< Id > streetId=std::nullopt)
 Get the next street id.
 
virtual void m_increaseTurnCounts (Id streetId, double delta)
 Increase the turn counts.
 
virtual void m_evolveStreet (const Id streetId, const std::unique_ptr< Street > &pStreet, bool reinsert_agents)
 Evolve a street.
 
virtual bool m_evolveNode (const std::unique_ptr< Node > &pNode)
 If possible, removes one agent from the node, putting it on the next street.
 
virtual void m_evolveAgents ()
 Evolve the agents.
 
void m_updatePath (const std::unique_ptr< Itinerary > &pItinerary)
 Update the path of a single itinerary.
 

Protected Attributes

std::unordered_map< Id, std::unique_ptr< Itinerary > > m_itineraries
 
std::map< Id, std::unique_ptr< Agent< Delay > > > m_agents
 
TimePoint m_time
 
TimePoint m_previousSpireTime
 
Graph m_graph
 
double m_errorProbability
 
double m_minSpeedRateo
 
double m_maxFlowPercentage
 
std::mt19937_64 m_generator {std::random_device{}()}
 
std::uniform_real_distribution< double > m_uniformDist {0., 1.}
 
std::vector< unsigned int > m_travelTimes
 
std::unordered_map< Id, Id > m_agentNextStreetId
 
bool m_forcePriorities
 
std::optional< Delay > m_dataUpdatePeriod
 
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, unsigned long long > m_streetTails
 

Detailed Description

template<typename Delay>
requires (is_numeric_v<Delay>)
class dsm::Dynamics< Delay >

The Dynamics class represents the dynamics of 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

◆ Dynamics()

template<typename Delay >
requires (is_numeric_v<Delay>)
dsm::Dynamics< Delay >::Dynamics ( Graph & graph)

Construct a new Dynamics object.

Parameters
graphThe graph representing the network

Member Function Documentation

◆ addAgent() [1/3]

template<typename Delay >
requires (is_numeric_v<Delay>)
void dsm::Dynamics< Delay >::addAgent ( const Agent< Delay > & agent)

Add an agent to the simulation.

Parameters
agentThe agent

◆ addAgent() [2/3]

template<typename Delay >
requires (is_numeric_v<Delay>)
void dsm::Dynamics< Delay >::addAgent ( Id srcNodeId,
Id itineraryId )

Add an agent with given source node and itinerary.

Parameters
srcNodeIdThe id of the source node
itineraryIdThe id of the itinerary
Exceptions
std::invalid_argumentIf the source node or the itinerary are not found

◆ addAgent() [3/3]

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

Add an agent to the simulation.

Parameters
agentstd::unique_ptr to the agent

◆ addAgents() [1/4]

template<typename Delay >
requires (is_numeric_v<Delay>)
void dsm::Dynamics< Delay >::addAgents ( Id itineraryId,
Size nAgents = 1,
std::optional< Id > srcNodeId = std::nullopt )

Add a pack of agents to the simulation.

Parameters
itineraryIdThe index of the itinerary
nAgentsThe number of agents to add
Exceptions
std::invalid_argumentIf the itinerary is not found

adds nAgents agents with the same itinerary of id itineraryId

◆ addAgents() [2/4]

template<typename Delay >
requires (is_numeric_v<Delay>)
void dsm::Dynamics< Delay >::addAgents ( std::span< Agent< Delay > > agents)

Add a set of agents to the simulation.

Parameters
agentsGeneric container of agents, represented by an std::span

◆ addAgents() [3/4]

template<typename Delay >
requires (is_agent_v<T1> && (is_agent_v<Tn> && ...))
template<typename T1 , typename... Tn>
requires (is_agent_v<T1> && (is_agent_v<Tn> && ...))
void dsm::Dynamics< Delay >::addAgents ( T1 agent,
Tn... agents )

Add a pack of agents to the simulation.

Parameters
agentAn agent
agentsParameter pack of agents

◆ addAgents() [4/4]

template<typename Delay >
requires (is_agent_v<Tn> && ...)
template<typename... Tn>
requires (is_agent_v<Tn> && ...)
void dsm::Dynamics< Delay >::addAgents ( Tn... agents)

Add a pack of agents to the simulation.

Parameters
agentsParameter pack of agents

◆ addAgentsUniformly()

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

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

◆ addItineraries() [1/2]

template<typename Delay >
requires (is_numeric_v<Delay>)
void dsm::Dynamics< Delay >::addItineraries ( std::span< Itinerary > itineraries)

Add a set of itineraries.

Parameters
itinerariesGeneric container of itineraries, represented by an std::span

◆ addItineraries() [2/2]

template<typename Delay >
template<typename T1 , typename... Tn>
requires (is_itinerary_v<T1> && (is_itinerary_v<Tn> && ...))
void dsm::Dynamics< Delay >::addItineraries ( T1 itinerary,
Tn... itineraries )

Add a pack of itineraries.

Template Parameters
T1
...Tn
Parameters
itinerary
...itineraries

◆ addItinerary() [1/2]

template<typename Delay >
requires (is_numeric_v<Delay>)
void dsm::Dynamics< Delay >::addItinerary ( const Itinerary & itinerary)

Add an itinerary.

Parameters
itineraryThe itinerary

◆ addItinerary() [2/2]

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

Add an itinerary.

Parameters
itinerarystd::unique_ptr to the itinerary

◆ agentMeanSpeed()

template<typename Delay >
requires (is_numeric_v<Delay>)
Measurement< double > dsm::Dynamics< Delay >::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 >
const std::map< Id, std::unique_ptr< Agent< Delay > > > & dsm::Dynamics< Delay >::agents ( ) const
inline

Get the agents.

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

◆ evolve()

template<typename Delay >
requires (is_numeric_v<Delay>)
void dsm::Dynamics< Delay >::evolve ( bool reinsert_agents = false)
virtual

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

◆ graph()

template<typename Delay >
const Graph & dsm::Dynamics< Delay >::graph ( ) const
inline

Get the graph.

Returns
const Graph&, The graph

◆ itineraries()

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

Get the itineraries.

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

◆ m_evolveAgents()

template<typename Delay >
requires (is_numeric_v<Delay>)
void dsm::Dynamics< Delay >::m_evolveAgents ( )
protectedvirtual

Evolve the agents.

Puts all new agents on a street, if possible, decrements all delays and increments all travel times.

◆ m_evolveNode()

template<typename Delay >
requires (is_numeric_v<Delay>)
bool dsm::Dynamics< Delay >::m_evolveNode ( const std::unique_ptr< Node > & pNode)
protectedvirtual

If possible, removes one agent from the node, putting it on the next street.

Parameters
pNodeA std::unique_ptr to the node
Returns
bool True if the agent has been moved, false otherwise

◆ m_evolveStreet()

template<typename Delay >
requires (is_numeric_v<Delay>)
void dsm::Dynamics< Delay >::m_evolveStreet ( const Id streetId,
const std::unique_ptr< Street > & pStreet,
bool reinsert_agents )
protectedvirtual

Evolve a street.

Parameters
streetIdThe id of the street
pStreetA std::unique_ptr to the street
reinsert_agentsIf true, the agents are reinserted in the simulation after they reach their destination

If possible, removes the first agent of the street's queue, putting it in the destination node. If the agent is going into the destination node, it is removed from the simulation (and then reinserted if reinsert_agents is true)

◆ m_nextStreetId()

template<typename Delay >
requires (is_numeric_v<Delay>)
Id dsm::Dynamics< Delay >::m_nextStreetId ( Id agentId,
Id NodeId,
std::optional< Id > streetId = std::nullopt )
protectedvirtual

Get the next street id.

Parameters
agentIdThe id of the agent
NodeIdThe id of the node
streetIdThe id of the incoming street
Returns
Id The id of the randomly selected next street

◆ m_updatePath()

template<typename Delay >
void dsm::Dynamics< Delay >::m_updatePath ( const std::unique_ptr< Itinerary > & pItinerary)
inlineprotected

Update the path of a single itinerary.

Parameters
pItineraryAn std::unique_prt to the itinerary

◆ meanSpireInputFlow()

template<typename Delay >
requires (is_numeric_v<Delay>)
Measurement< double > dsm::Dynamics< Delay >::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 >
requires (is_numeric_v<Delay>)
Measurement< double > dsm::Dynamics< Delay >::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

◆ meanTravelTime()

template<typename Delay >
requires (is_numeric_v<Delay>)
Measurement< double > dsm::Dynamics< Delay >::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

◆ optimizeTrafficLights()

template<typename Delay >
requires (is_numeric_v<Delay>)
void dsm::Dynamics< Delay >::optimizeTrafficLights ( Delay nCycles,
double threshold = 0.,
double densityTolerance = 0. )

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

Parameters
nCyclesDelay, The number of cycles (times agents are being added) between two calls of this function
thresholddouble, The percentage of the mean capacity of the streets used as threshold for the delta between the two tails.
densityTolerancedouble, The algorithm will consider all streets with density up to densityTolerance*meanDensity

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.

◆ removeAgent()

template<typename Delay >
requires (is_numeric_v<Delay>)
void dsm::Dynamics< Delay >::removeAgent ( Size agentId)

Remove an agent from the simulation.

Parameters
agentIdthe id of the agent to remove

◆ setAgentSpeed()

template<typename Delay >
virtual void dsm::Dynamics< Delay >::setAgentSpeed ( Size agentId)
pure virtual

Set the speed of an agent.

This is a pure-virtual function, it must be implemented in the derived classes

Parameters
agentIdThe id of the agent

Implemented in dsm::FirstOrderDynamics< Delay >.

◆ setDataUpdatePeriod()

template<typename Delay >
void dsm::Dynamics< Delay >::setDataUpdatePeriod ( Delay dataUpdatePeriod)
inline

Set the data update period.

Parameters
dataUpdatePeriodDelay, 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()

template<typename Delay >
requires (is_numeric_v<Delay>)
void dsm::Dynamics< Delay >::setDestinationNodes ( const std::span< Id > & destinationNodes,
bool updatePaths = true )

Set the dynamics destination nodes auto-generating itineraries.

Parameters
destinationNodesThe destination nodes
updatePathsIf true, the paths are updated
Exceptions
std::invalid_argumentIfone or more destination nodes do not exist

◆ setErrorProbability()

template<typename Delay >
requires (is_numeric_v<Delay>)
void dsm::Dynamics< Delay >::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 >
void dsm::Dynamics< Delay >::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

◆ setItineraries()

template<typename Delay >
requires (is_numeric_v<Delay>)
void dsm::Dynamics< Delay >::setItineraries ( std::span< Itinerary > itineraries)

Set the itineraries.

Parameters
itinerariesThe itineraries

◆ setMaxFlowPercentage()

template<typename Delay >
requires (is_numeric_v<Delay>)
void dsm::Dynamics< Delay >::setMaxFlowPercentage ( double maxFlowPercentage)

Set the maximum flow percentage.

Parameters
maxFlowPercentageThe maximum flow percentage

The maximum flow percentage is the percentage of the maximum flow that a street can transmit. Default is 1 (100%).

Exceptions
std::invalid_argumentIf the maximum flow percentage is not between 0 and 1

◆ setMinSpeedRateo()

template<typename Delay >
requires (is_numeric_v<Delay>)
void dsm::Dynamics< Delay >::setMinSpeedRateo ( double minSpeedRateo)

Set the minim speed rateo, i.e. the minim speed with respect to the speed limit.

Parameters
minSpeedRateoThe minim speed rateo
Exceptions
std::invalid_argumentIf the minim speed rateo is not between 0 and 1

◆ setSeed()

template<typename Delay >
void dsm::Dynamics< Delay >::setSeed ( unsigned int seed)
inline

Set the seed for the graph's random number generator.

Parameters
seedThe seed

◆ streetMeanDensity()

template<typename Delay >
requires (is_numeric_v<Delay>)
Measurement< double > dsm::Dynamics< Delay >::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 >
requires (is_numeric_v<Delay>)
Measurement< double > dsm::Dynamics< Delay >::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 >
requires (is_numeric_v<Delay>)
Measurement< double > dsm::Dynamics< Delay >::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 >
virtual Measurement< double > dsm::Dynamics< Delay >::streetMeanSpeed ( ) const
pure virtual

◆ streetMeanSpeed() [2/3]

template<typename Delay >
virtual Measurement< double > dsm::Dynamics< Delay >::streetMeanSpeed ( double ,
bool  ) const
pure virtual

◆ streetMeanSpeed() [3/3]

template<typename Delay >
virtual double dsm::Dynamics< Delay >::streetMeanSpeed ( Id ) const
pure virtual

◆ time()

template<typename Delay >
TimePoint dsm::Dynamics< Delay >::time ( ) const
inline

Get the time.

Returns
TimePoint The time

◆ turnCounts()

template<typename Delay >
const std::unordered_map< Id, std::array< unsigned long long, 4 > > & dsm::Dynamics< Delay >::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 >
requires (is_numeric_v<Delay>)
std::unordered_map< Id, std::array< double, 4 > > dsm::Dynamics< Delay >::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: