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

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

#include <RoadDynamics.hpp>

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

Public Member Functions

 RoadDynamics (Graph &graph, bool useCache=false, std::optional< unsigned int > seed=std::nullopt)
 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 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 evolve (bool reinsert_agents=false) override
 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.
 
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
 
void saveTravelSpeeds (const std::string &filename, bool reset=false)
 Save the travel speeds of the agents in csv format.
 
- Public Member Functions inherited from dsm::Dynamics< Agent< delay_t > >
 Dynamics (Graph &graph, bool useCache=false, std::optional< unsigned int > seed=std::nullopt)
 Construct a new Dynamics object.
 
virtual void setAgentSpeed (Size agentId)=0
 
virtual void updatePaths ()
 Update the paths of the itineraries based on the actual travel times.
 
void setDestinationNodes (const std::span< Id > &destinationNodes, bool updatePaths=true)
 Set the dynamics destination nodes auto-generating itineraries.
 
void addAgent (std::unique_ptr< Agent< delay_t > > agent)
 Add an agent to the simulation.
 
void addAgent (TArgs &&... args)
 
void addAgents (Size nAgents, TArgs &&... args)
 
void addAgents (Tn... agents)
 Add a pack of agents to the simulation.
 
void addAgents (T1 agent, Tn... agents)
 Add a pack of agents to the simulation.
 
void addAgents (std::span< Agent< delay_t > > agents)
 Add a set of agents to the simulation.
 
void removeAgent (Size agentId)
 Remove an agent from the simulation.
 
void removeAgents (T1 id, Tn... ids)
 
void addItinerary (const Itinerary &itinerary)
 Add an itinerary.
 
void addItinerary (std::unique_ptr< Itinerary > itinerary)
 Add an itinerary.
 
void addItineraries (Tn... itineraries)
 
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.
 
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_t > > > & agents () const
 Get the agents.
 
Size nAgents () const
 Get the number of agents currently in the simulation.
 
Time time () const
 Get the time.
 
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}$.
 
Measurement< double > meanTravelTime (bool clearData=false)
 Get the mean travel time of the agents in $s$.
 
void saveStreetDensities (const std::string &filename, bool normalized=true) const
 Save the street densities in csv format.
 
void saveInputStreetCounts (const std::string &filename, bool reset=false)
 Save the street input counts in csv format.
 
void saveOutputStreetCounts (const std::string &filename, bool reset=false)
 Save the street output counts in csv format.
 

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.
 
void m_evolveStreet (const std::unique_ptr< Street > &pStreet, bool reinsert_agents) override
 Evolve a street.
 
bool m_evolveNode (const std::unique_ptr< Node > &pNode) override
 If possible, removes one agent from the node, putting it on the next street.
 
void m_evolveAgents () override
 Evolve the agents.
 
- Protected Member Functions inherited from dsm::Dynamics< Agent< delay_t > >
void m_updatePath (const std::unique_ptr< Itinerary > &pItinerary)
 Update the path of a single itinerary using Dijsktra's algorithm.
 

Protected Attributes

Time m_previousOptimizationTime
 
std::vector< std::pair< double, double > > m_travelDTs
 
std::unordered_map< Id, Id > m_agentNextStreetId
 
bool m_forcePriorities
 
std::optional< delay_t > 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, double > m_streetTails
 
- Protected Attributes inherited from dsm::Dynamics< Agent< delay_t > >
Graph m_graph
 
Time m_time
 
Time m_previousSpireTime
 
std::mt19937_64 m_generator
 

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

◆ RoadDynamics()

template<typename delay_t>
requires (is_numeric_v<delay_t>)
dsm::RoadDynamics< delay_t >::RoadDynamics ( Graph & graph,
bool useCache = false,
std::optional< unsigned int > seed = std::nullopt )

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)

Member Function Documentation

◆ 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

◆ evolve()

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

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

Implements dsm::Dynamics< Agent< delay_t > >.

◆ m_evolveAgents()

template<typename delay_t>
requires (is_numeric_v<delay_t>)
void dsm::RoadDynamics< delay_t >::m_evolveAgents ( )
overrideprotectedvirtual

Evolve the agents.

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

Implements dsm::Dynamics< Agent< delay_t > >.

◆ m_evolveNode()

template<typename delay_t>
requires (is_numeric_v<delay_t>)
bool dsm::RoadDynamics< delay_t >::m_evolveNode ( const std::unique_ptr< Node > & pNode)
overrideprotectedvirtual

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

Implements dsm::Dynamics< Agent< delay_t > >.

◆ m_evolveStreet()

template<typename delay_t>
requires (is_numeric_v<delay_t>)
void dsm::RoadDynamics< delay_t >::m_evolveStreet ( const std::unique_ptr< Street > & pStreet,
bool reinsert_agents )
overrideprotectedvirtual

Evolve a street.

Parameters
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)

Implements dsm::Dynamics< Agent< delay_t > >.

◆ m_nextStreetId()

template<typename delay_t>
requires (is_numeric_v<delay_t>)
Id dsm::RoadDynamics< delay_t >::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

◆ 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

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

◆ 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

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

◆ 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

◆ 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: