|
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, PathWeight const weightFunction=PathWeight::TRAVELTIME, std::optional< double > weightTreshold=std::nullopt) | |
| Construct a new RoadDynamics object. | |
| void | setErrorProbability (double errorProbability) |
| Set the error probability. | |
| void | setPassageProbability (double passageProbability) |
| Set the passage probability. | |
| void | killStagnantAgents (double timeToleranceFactor=3.) |
| Set the time tolerance factor for killing stagnant agents. An agent will be considered stagnant if it has not moved for timeToleranceFactor * std::ceil(street_length / street_maxSpeed) time units. | |
| void | setWeightFunction (PathWeight const pathWeight, std::optional< double > weightThreshold=std::nullopt) |
| Set the weight function. | |
| void | setForcePriorities (bool forcePriorities) noexcept |
| Set the force priorities flag. | |
| void | setDataUpdatePeriod (delay_t dataUpdatePeriod) noexcept |
| Set the data update period. | |
| void | setMaxDistance (double const maxDistance) |
| Set the maximum distance which a random agent can travel. | |
| void | setMaxTravelTime (std::time_t const maxTravelTime) noexcept |
| Set the maximum travel time which a random agent can travel. | |
| void | setOriginNodes (std::unordered_map< Id, double > const &originNodes) |
| Set the origin nodes. | |
| void | setDestinationNodes (std::unordered_map< Id, double > const &destinationNodes) |
| Set the destination nodes. | |
| void | setDestinationNodes (std::initializer_list< Id > destinationNodes) |
| Set the destination nodes. | |
| template<typename TContainer> requires (std::is_convertible_v<typename TContainer::value_type, Id>) | |
| void | setDestinationNodes (TContainer const &destinationNodes) |
| Set the destination nodes. | |
| virtual void | setAgentSpeed (std::unique_ptr< Agent > const &pAgent)=0 |
| void | initTurnCounts () |
| Initialize the turn counts map. | |
| void | resetTurnCounts () |
| Reset the turn counts map values to zero. | |
| void | updatePaths (bool const throw_on_empty=true) |
| Update the paths of the itineraries based on the given weight function. | |
| void | addAgentsUniformly (Size nAgents, std::optional< Id > itineraryId=std::nullopt) |
| Add agents uniformly on the road network. | |
|
template<typename TContainer> requires (std::is_same_v<TContainer, std::unordered_map<Id, double>> || std::is_same_v<TContainer, std::map<Id, double>>) | |
| void | addRandomAgents (std::size_t nAgents, TContainer const &spawnWeights) |
| void | addRandomAgents (std::size_t nAgents) |
| 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) |
| Add a set of agents to the simulation. | |
| void | addAgentsRandomly (Size nAgents) |
| void | addAgent (std::unique_ptr< Agent > agent) |
| Add an agent to the simulation. | |
|
template<typename... TArgs> requires (std::is_constructible_v<Agent, std::time_t, TArgs...>) | |
| void | addAgent (TArgs &&... args) |
|
template<typename... TArgs> requires (std::is_constructible_v<Agent, std::time_t, TArgs...>) | |
| void | addAgents (Size nAgents, TArgs &&... args) |
| 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 (TrafficLightOptimization optimizationType=TrafficLightOptimization::DOUBLE_TAIL, const std::string &logFile=std::string(), double const percentage=0.3, double const threshold=1.3) |
| Optimize the traffic lights by changing the green and red times. | |
| const std::unordered_map< Id, std::unique_ptr< Itinerary > > & | itineraries () const noexcept |
| Get the itineraries. | |
| std::unordered_map< Id, double > const & | originNodes () const noexcept |
| Get the origin nodes of the graph. | |
| std::unordered_map< Id, double > & | originNodes () noexcept |
| Get the origin nodes of the graph. | |
| std::unordered_map< Id, double > const & | destinationNodes () const noexcept |
| Get the destination nodes of the graph. | |
| std::unordered_map< Id, double > & | destinationNodes () noexcept |
| Get the destination nodes of the graph. | |
| const std::vector< std::unique_ptr< Agent > > & | agents () const noexcept |
| 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\). | |
| std::unordered_map< Id, std::unordered_map< Id, size_t > > const & | turnCounts () const noexcept |
| Get the turn counts of the agents. | |
| std::unordered_map< Id, std::unordered_map< Id, double > > const | normalizedTurnCounts () const noexcept |
| Get the normalized turn counts of the agents. | |
| std::unordered_map< Id, std::array< long, 4 > > | turnMapping () const |
| 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}\). | |
| void | saveStreetDensities (std::string filename=std::string(), bool normalized=true, char const separator=';') const |
| Save the street densities in csv format. | |
| void | saveCoilCounts (const std::string &filename, bool reset=false, char const separator=';') |
| Save the street input counts in csv format. | |
| void | saveTravelData (std::string filename=std::string(), bool reset=false) |
| Save the travel data of the agents in csv format. | |
| void | saveMacroscopicObservables (std::string filename=std::string(), char const separator=';') |
| Save the main macroscopic observables in csv format. | |
Public Member Functions inherited from dsf::Dynamics< RoadNetwork > | |
| Dynamics (RoadNetwork &graph, std::optional< unsigned int > seed=std::nullopt) | |
| Construct a new Dynamics object. | |
| void | setName (const std::string &name) |
| Set the name of the simulation. | |
| void | setInitTime (std::time_t timeEpoch) |
| Set the initial time as epoch time. | |
| const auto & | graph () const |
| Get the graph. | |
| const auto & | name () const |
| Get the name of the simulation. | |
| auto | time () const |
| Get the current simulation time as epoch time. | |
| auto | time_step () const |
| Get the current simulation time-step. | |
| auto | strDateTime () const |
| Get the current simulation time as formatted string (YYYY-MM-DD HH:MM:SS) | |
Protected Attributes | |
| std::unordered_map< Id, std::unordered_map< Id, size_t > > | m_turnCounts |
| std::unordered_map< Id, std::array< long, 4 > > | m_turnMapping |
| tbb::concurrent_unordered_map< Id, std::unordered_map< Direction, double > > | m_queuesAtTrafficLights |
| tbb::concurrent_vector< std::pair< double, double > > | m_travelDTs |
| std::time_t | m_previousOptimizationTime |
Protected Attributes inherited from dsf::Dynamics< RoadNetwork > | |
| tbb::task_arena | m_taskArena |
| std::mt19937_64 | m_generator |
Additional Inherited Members | |
Protected Member Functions inherited from dsf::Dynamics< RoadNetwork > | |
| void | m_evolve () |
| auto | m_safeDateTime () const |
| Get a safe date-time string for filenames (YYYYMMDD_HHMMSS) | |
| auto | m_safeName () const |
| Get a safe name string for filenames (spaces replaced by underscores) | |
The RoadDynamics class represents the dynamics of the network.
| delay_t | The type of the agent's delay |
| dsf::mobility::RoadDynamics< delay_t >::RoadDynamics | ( | RoadNetwork & | graph, |
| bool | useCache = false, | ||
| std::optional< unsigned int > | seed = std::nullopt, | ||
| PathWeight const | weightFunction = PathWeight::TRAVELTIME, | ||
| std::optional< double > | weightTreshold = std::nullopt ) |
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 dsf::PathWeight function to use for the pathfinding (default is dsf::PathWeight::TRAVELTIME) |
| weightTreshold | The weight treshold for updating the paths (default is std::nullopt) |
| void dsf::mobility::RoadDynamics< delay_t >::addAgent | ( | std::unique_ptr< Agent > | agent | ) |
Add an agent to the simulation.
| agent | std::unique_ptr to the agent |
| void dsf::mobility::RoadDynamics< delay_t >::addAgentsRandomly | ( | Size | nAgents, |
| const TContainer & | src_weights, | ||
| const TContainer & | dst_weights ) |
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 dsf::mobility::RoadDynamics< delay_t >::addAgentsUniformly | ( | Size | nAgents, |
| std::optional< Id > | itineraryId = std::nullopt ) |
Add agents uniformly on the road network.
| nAgents | The number of agents to add |
| itineraryId | The id of the itinerary to use (default is std::nullopt) |
| std::runtime_error | If there are no itineraries |
| void dsf::mobility::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 dsf::mobility::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
|
inlinenoexcept |
Get the agents.
|
inlinenoexcept |
Get the destination nodes of the graph.
|
inlinenoexcept |
Get the destination nodes of the graph.
| void dsf::mobility::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 |
| void dsf::mobility::RoadDynamics< delay_t >::initTurnCounts | ( | ) |
Initialize the turn counts map.
| std::runtime_error | if the turn counts map is already initialized |
|
inlinenoexcept |
Get the itineraries.
| void dsf::mobility::RoadDynamics< delay_t >::killStagnantAgents | ( | double | timeToleranceFactor = 3. | ) |
Set the time tolerance factor for killing stagnant agents. An agent will be considered stagnant if it has not moved for timeToleranceFactor * std::ceil(street_length / street_maxSpeed) time units.
| timeToleranceFactor | The time tolerance factor |
| std::invalid_argument | If the time tolerance factor is not positive |
| Measurement< double > dsf::mobility::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 > dsf::mobility::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 > dsf::mobility::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 |
| Size dsf::mobility::RoadDynamics< delay_t >::nAgents | ( | ) | const |
Get the number of agents currently in the simulation.
|
noexcept |
Get the normalized turn counts of the agents.
| void dsf::mobility::RoadDynamics< delay_t >::optimizeTrafficLights | ( | TrafficLightOptimization | optimizationType = TrafficLightOptimization::DOUBLE_TAIL, |
| const std::string & | logFile = std::string(), | ||
| double const | percentage = 0.3, | ||
| double const | threshold = 1.3 ) |
Optimize the traffic lights by changing the green and red times.
| optimizationType | TrafficLightOptimization, The type of optimization. Default is DOUBLE_TAIL |
| logFile | The file into which write the logs (default is empty, meaning no logging) |
| percentage | double, the maximum amount (percentage) of the green time to change (default is 0.3) |
| threshold | double, The ratio between the self-density and neighbour density to trigger the non-local optimization (default is 1.3) |
The local optimization is done by changing the green time of each traffic light, trying to make it proportional to the queue lengths at each phase. The non-local optimization is done by synchronizing the traffic lights which are congested over threshold.
|
inlinenoexcept |
Get the origin nodes of the graph.
|
inlinenoexcept |
Get the origin nodes of the graph.
| void dsf::mobility::RoadDynamics< delay_t >::resetTurnCounts | ( | ) |
Reset the turn counts map values to zero.
| std::runtime_error | if the turn counts map is not initialized |
| void dsf::mobility::RoadDynamics< delay_t >::saveCoilCounts | ( | 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 saved only if the street has a coil on it
| void dsf::mobility::RoadDynamics< delay_t >::saveMacroscopicObservables | ( | std::string | filename = std::string(), |
| char const | separator = ';' ) |
Save the main macroscopic observables in csv format.
| filename | The name of the file (default is "{datetime}_{simulation_name}_macroscopic_observables.csv") |
| 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 dsf::mobility::RoadDynamics< delay_t >::saveStreetDensities | ( | std::string | filename = std::string(), |
| bool | normalized = true, | ||
| char const | separator = ';' ) const |
Save the street densities in csv format.
| filename | The name of the file (default is "{datetime}_{simulation_name}_street_densities.csv") |
| normalized | If true, the densities are normalized in [0, 1] |
| void dsf::mobility::RoadDynamics< delay_t >::saveTravelData | ( | std::string | filename = std::string(), |
| bool | reset = false ) |
Save the travel data of the agents in csv format.
The file contains the following columns:
| filename | The name of the file (default is "{datetime}_{simulation_name}_travel_data.csv") |
| reset | If true, the travel speeds are cleared after the computation |
|
pure virtual |
Implemented in dsf::mobility::FirstOrderDynamics.
|
inlinenoexcept |
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 dsf::mobility::RoadDynamics< delay_t >::setDestinationNodes | ( | std::initializer_list< Id > | destinationNodes | ) |
Set the destination nodes.
| destinationNodes | The destination nodes (as an initializer list) |
| void dsf::mobility::RoadDynamics< delay_t >::setDestinationNodes | ( | std::unordered_map< Id, double > const & | destinationNodes | ) |
Set the destination nodes.
| destinationNodes | The destination nodes |
| void dsf::mobility::RoadDynamics< delay_t >::setDestinationNodes | ( | TContainer const & | destinationNodes | ) |
Set the destination nodes.
| destinationNodes | A container of destination nodes ids |
The container must have a value_type convertible to Id and begin() and end() methods
| void dsf::mobility::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 |
|
inlinenoexcept |
Set the force priorities flag.
| forcePriorities | The flag |
If true, if an agent cannot move to the next street, the whole node is skipped
|
inline |
Set the maximum distance which a random agent can travel.
| maxDistance | The maximum distance |
| std::invalid_argument | If the maximum distance is negative |
|
inlinenoexcept |
Set the maximum travel time which a random agent can travel.
| maxTravelTime | The maximum travel time |
| void dsf::mobility::RoadDynamics< delay_t >::setOriginNodes | ( | std::unordered_map< Id, double > const & | originNodes | ) |
Set the origin nodes.
| originNodes | The origin nodes |
| void dsf::mobility::RoadDynamics< delay_t >::setPassageProbability | ( | double | passageProbability | ) |
Set the passage probability.
| passageProbability | The passage probability |
The passage probability is the probability of passing through a node It is useful in the case of random agents
| void dsf::mobility::RoadDynamics< delay_t >::setWeightFunction | ( | PathWeight const | pathWeight, |
| std::optional< double > | weightThreshold = std::nullopt ) |
Set the weight function.
| pathWeight | The dsf::PathWeight function to use for the pathfinding |
| weightThreshold | The weight threshold for updating the paths (default is std::nullopt) |
| Measurement< double > dsf::mobility::RoadDynamics< delay_t >::streetMeanDensity | ( | bool | normalized = false | ) | const |
Get the mean density of the streets in \(m^{-1}\).
| Measurement< double > dsf::mobility::RoadDynamics< delay_t >::streetMeanFlow | ( | ) | const |
Get the mean flow of the streets in \(s^{-1}\).
| Measurement< double > dsf::mobility::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 dsf::mobility::FirstOrderDynamics.
|
virtual |
Reimplemented in dsf::mobility::FirstOrderDynamics.
|
virtual |
Reimplemented in dsf::mobility::FirstOrderDynamics.
|
inlinenoexcept |
Get the turn counts of the agents.
| void dsf::mobility::RoadDynamics< delay_t >::updatePaths | ( | bool const | throw_on_empty = true | ) |
Update the paths of the itineraries based on the given weight function.
| throw_on_empty | If true, throws an exception if an itinerary has an empty path (default is true) If false, removes the itinerary with empty paths and the associated node from the origin/destination nodes |
| std::runtime_error | if throw_on_empty is true and an itinerary has an empty path |