Dynamical system model
|
The RoadDynamics class represents the dynamics of the network. More...
#include <RoadDynamics.hpp>
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. | |
![]() | |
Dynamics (RoadNetwork &graph, std::optional< unsigned int > seed=std::nullopt) | |
Construct a new Dynamics object. | |
void | resetTime () |
Reset the simulation time to 0. | |
const RoadNetwork & | graph () 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 |
![]() | |
Time | m_time |
std::mt19937_64 | m_generator |
Additional Inherited Members | |
![]() | |
void | m_evolve () |
The RoadDynamics class represents the dynamics of the network.
delay_t | The type of the agent's delay |
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.
graph | The graph representing the network |
useCache | If true, the cache is used (default is false) |
seed | The seed for the random number generator (default is std::nullopt) |
weightFunction | The weight function for the Dijkstra's algorithm (default is weight_functions::streetTime) |
weightTreshold | The weight treshold for updating the paths (default is 60.) |
void dsm::RoadDynamics< delay_t >::addAgent | ( | std::unique_ptr< Agent > | agent | ) |
Add an agent to the simulation.
agent | std::unique_ptr to the agent |
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.
nAgents | The number of agents to add |
src_weights | The weights of the source nodes |
dst_weights | The weights of the destination nodes |
std::invalid_argument | If the source and destination nodes are the same |
void dsm::RoadDynamics< delay_t >::addAgentsUniformly | ( | Size | nAgents, |
std::optional< Id > | itineraryId = std::nullopt ) |
Add a set of agents to the simulation.
nAgents | The number of agents to add |
uniformly | If true, the agents are added uniformly on the streets |
std::runtime_error | If there are no itineraries |
void dsm::RoadDynamics< delay_t >::addItinerary | ( | std::unique_ptr< Itinerary > | itinerary | ) |
Add an itinerary.
itinerary | std::unique_ptr to the itinerary |
std::invalid_argument | If the itinerary already exists |
std::invalid_argument | If the itinerary's destination is not a node of the graph |
void dsm::RoadDynamics< delay_t >::addItinerary | ( | TArgs &&... | args | ) |
Add an itinerary.
...args | The arguments to construct the itinerary |
The arguments must be compatible with any constructor of the Itinerary class
Measurement< double > dsm::RoadDynamics< delay_t >::agentMeanSpeed | ( | ) | const |
Get the mean speed of the agents in \(m/s\).
|
inline |
Get the agents.
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:
reinsert_agents | If true, the agents are reinserted in the simulation after they reach their destination |
|
inline |
Get the itineraries.
Measurement< double > dsm::RoadDynamics< delay_t >::meanSpireInputFlow | ( | bool | resetValue = true | ) |
Get the mean spire input flow of the streets in \(s^{-1}\).
resetValue | If true, the spire input/output flows are cleared after the computation |
The spire input flow is computed as the sum of counts over the product of the number of spires and the time delta
Measurement< double > dsm::RoadDynamics< delay_t >::meanSpireOutputFlow | ( | bool | resetValue = true | ) |
Get the mean spire output flow of the streets in \(s^{-1}\).
resetValue | If true, the spire output/input flows are cleared after the computation |
The spire output flow is computed as the sum of counts over the product of the number of spires and the time delta
Measurement< double > dsm::RoadDynamics< delay_t >::meanTravelDistance | ( | bool | clearData = false | ) |
Get the mean travel distance of the agents in \(m\).
clearData | If true, the travel distances are cleared after the computation |
Measurement< double > dsm::RoadDynamics< delay_t >::meanTravelSpeed | ( | bool | clearData = false | ) |
Get the mean travel speed of the agents in \(m/s\).
clearData | If true, the travel times and distances are cleared after the computation |
Measurement< double > dsm::RoadDynamics< delay_t >::meanTravelTime | ( | bool | clearData = false | ) |
Get the mean travel time of the agents in \(s\).
clearData | If true, the travel times are cleared after the computation |
|
inline |
Get the number of agents currently in the simulation.
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.
threshold | double, The minimum difference between green and red queues to trigger the optimization (n agents - default is 0) |
optimizationType | TrafficLightOptimization, 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.
void dsm::RoadDynamics< delay_t >::removeAgent | ( | Size | agentId | ) |
Remove an agent from the simulation.
agentId | the id of the agent to remove |
void dsm::RoadDynamics< delay_t >::saveInputStreetCounts | ( | const std::string & | filename, |
bool | reset = false, | ||
char const | separator = ';' ) |
Save the street input counts in csv format.
filename | The name of the file |
reset | If true, the input counts are cleared after the computation |
NOTE: counts are printed only if the street is a spire
void dsm::RoadDynamics< delay_t >::saveMacroscopicObservables | ( | const std::string & | filename, |
char const | separator = ';' ) |
Save the main macroscopic observables in csv format.
filename | The name of the file |
separator | The separator character (default is ';') |
The file contains the following columns:
NOTE: the mean density is normalized in [0, 1] and reset is true for all observables which have such parameter
void dsm::RoadDynamics< delay_t >::saveOutputStreetCounts | ( | const std::string & | filename, |
bool | reset = false, | ||
char const | separator = ';' ) |
Save the street output counts in csv format.
filename | The name of the file |
reset | If true, the output counts are cleared after the computation |
NOTE: counts are printed only if the street is a spire
void dsm::RoadDynamics< delay_t >::saveStreetDensities | ( | const std::string & | filename, |
bool | normalized = true, | ||
char const | separator = ';' ) const |
Save the street densities in csv format.
filename | The name of the file |
normalized | If true, the densities are normalized in [0, 1] |
void dsm::RoadDynamics< delay_t >::saveTravelSpeeds | ( | const std::string & | filename, |
bool | reset = false ) |
Save the travel speeds of the agents in csv format.
filename | The name of the file |
reset | If true, the travel speeds are cleared after the computation |
|
pure virtual |
Implemented in dsm::FirstOrderDynamics.
|
inline |
Set the data update period.
dataUpdatePeriod | delay_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.
void dsm::RoadDynamics< delay_t >::setDestinationNodes | ( | std::initializer_list< Id > | destinationNodes, |
bool | updatePaths = true ) |
Set the destination nodes.
destinationNodes | The destination nodes (as an initializer list) |
updatePaths | If true, the paths are updated |
void dsm::RoadDynamics< delay_t >::setDestinationNodes | ( | TContainer const & | destinationNodes, |
bool | updatePaths = true ) |
Set the destination nodes.
destinationNodes | A container of destination nodes ids |
updatePaths | If true, the paths are updated |
The container must have a value_type convertible to Id and begin() and end() methods
void dsm::RoadDynamics< delay_t >::setErrorProbability | ( | double | errorProbability | ) |
Set the error probability.
errorProbability | The error probability |
std::invalid_argument | If the error probability is not between 0 and 1 |
|
inline |
Set the force priorities flag.
forcePriorities | The flag |
If true, if an agent cannot move to the next street, the whole node is skipped
Measurement< double > dsm::RoadDynamics< delay_t >::streetMeanDensity | ( | bool | normalized = false | ) | const |
Get the mean density of the streets in \(m^{-1}\).
Measurement< double > dsm::RoadDynamics< delay_t >::streetMeanFlow | ( | ) | const |
Get the mean flow of the streets in \(s^{-1}\).
Measurement< double > dsm::RoadDynamics< delay_t >::streetMeanFlow | ( | double | threshold, |
bool | above ) const |
Get the mean flow of the streets in \(s^{-1}\).
threshold | The density threshold to consider |
above | If true, the function returns the mean flow of the streets with a density above the threshold, otherwise below |
|
virtual |
Reimplemented in dsm::FirstOrderDynamics.
|
virtual |
Reimplemented in dsm::FirstOrderDynamics.
|
virtual |
Reimplemented in dsm::FirstOrderDynamics.
|
inline |
Get the turn counts of the agents.
The array contains the counts of left (0), straight (1), right (2) and U (3) turns
std::unordered_map< Id, std::array< double, 4 > > dsm::RoadDynamics< delay_t >::turnProbabilities | ( | bool | reset = true | ) |
Get the turn probabilities of the agents.
The array contains the probabilities of left (0), straight (1), right (2) and U (3) turns