Dynamical system model
|
The Dynamics class represents the dynamics of the network. More...
#include <Dynamics.hpp>
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 Graph & | graph () 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 . | |
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 . | |
Measurement< double > | streetMeanFlow () const |
Get the mean flow of the streets in . | |
Measurement< double > | streetMeanFlow (double threshold, bool above) const |
Get the mean flow of the streets in . | |
Measurement< double > | meanSpireInputFlow (bool resetValue=true) |
Get the mean spire input flow of the streets in . | |
Measurement< double > | meanSpireOutputFlow (bool resetValue=true) |
Get the mean spire output flow of the streets in . | |
Measurement< double > | meanTravelTime (bool clearData=false) |
Get the mean travel time of the agents in . | |
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 |
The Dynamics class represents the dynamics of the network.
Id,The | type of the graph's id. It must be an unsigned integral type. |
Size,The | type of the graph's capacity. It must be an unsigned integral type. |
dsm::Dynamics< Delay >::Dynamics | ( | Graph & | graph | ) |
Construct a new Dynamics object.
graph | The graph representing the network |
void dsm::Dynamics< Delay >::addAgent | ( | const Agent< Delay > & | agent | ) |
Add an agent to the simulation.
agent | The agent |
void dsm::Dynamics< Delay >::addAgent | ( | Id | srcNodeId, |
Id | itineraryId ) |
Add an agent with given source node and itinerary.
srcNodeId | The id of the source node |
itineraryId | The id of the itinerary |
std::invalid_argument | If the source node or the itinerary are not found |
void dsm::Dynamics< Delay >::addAgent | ( | std::unique_ptr< Agent< Delay > > | agent | ) |
Add an agent to the simulation.
agent | std::unique_ptr to the agent |
void dsm::Dynamics< Delay >::addAgents | ( | Id | itineraryId, |
Size | nAgents = 1, | ||
std::optional< Id > | srcNodeId = std::nullopt ) |
Add a pack of agents to the simulation.
itineraryId | The index of the itinerary |
nAgents | The number of agents to add |
std::invalid_argument | If the itinerary is not found |
adds nAgents agents with the same itinerary of id itineraryId
void dsm::Dynamics< Delay >::addAgents | ( | std::span< Agent< Delay > > | agents | ) |
Add a set of agents to the simulation.
agents | Generic container of agents, represented by an std::span |
void dsm::Dynamics< Delay >::addAgents | ( | T1 | agent, |
Tn... | agents ) |
Add a pack of agents to the simulation.
agent | An agent |
agents | Parameter pack of agents |
void dsm::Dynamics< Delay >::addAgents | ( | Tn... | agents | ) |
Add a pack of agents to the simulation.
agents | Parameter pack of agents |
|
virtual |
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::Dynamics< Delay >::addItineraries | ( | std::span< Itinerary > | itineraries | ) |
Add a set of itineraries.
itineraries | Generic container of itineraries, represented by an std::span |
void dsm::Dynamics< Delay >::addItineraries | ( | T1 | itinerary, |
Tn... | itineraries ) |
Add a pack of itineraries.
T1 | |
...Tn |
itinerary | |
...itineraries |
void dsm::Dynamics< Delay >::addItinerary | ( | const Itinerary & | itinerary | ) |
Add an itinerary.
itinerary | The itinerary |
void dsm::Dynamics< Delay >::addItinerary | ( | std::unique_ptr< Itinerary > | itinerary | ) |
Add an itinerary.
itinerary | std::unique_ptr to the itinerary |
Measurement< double > dsm::Dynamics< Delay >::agentMeanSpeed | ( | ) | const |
Get the mean speed of the agents in .
|
inline |
Get the agents.
|
virtual |
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 graph.
|
inline |
Get the itineraries.
|
protectedvirtual |
Evolve the agents.
Puts all new agents on a street, if possible, decrements all delays and increments all travel times.
|
protectedvirtual |
If possible, removes one agent from the node, putting it on the next street.
pNode | A std::unique_ptr to the node |
|
protectedvirtual |
Evolve a street.
streetId | The id of the street |
pStreet | A std::unique_ptr to the street |
reinsert_agents | If 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)
|
protectedvirtual |
Get the next street id.
agentId | The id of the agent |
NodeId | The id of the node |
streetId | The id of the incoming street |
|
inlineprotected |
Update the path of a single itinerary.
pItinerary | An std::unique_prt to the itinerary |
Measurement< double > dsm::Dynamics< Delay >::meanSpireInputFlow | ( | bool | resetValue = true | ) |
Get the mean spire input flow of the streets in .
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::Dynamics< Delay >::meanSpireOutputFlow | ( | bool | resetValue = true | ) |
Get the mean spire output flow of the streets in .
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::Dynamics< Delay >::meanTravelTime | ( | bool | clearData = false | ) |
Get the mean travel time of the agents in .
clearData | If true, the travel times are cleared after the computation |
void dsm::Dynamics< Delay >::optimizeTrafficLights | ( | Delay | nCycles, |
double | threshold = 0., | ||
double | densityTolerance = 0. ) |
Optimize the traffic lights by changing the green and red times.
nCycles | Delay, The number of cycles (times agents are being added) between two calls of this function |
threshold | double, The percentage of the mean capacity of the streets used as threshold for the delta between the two tails. |
densityTolerance | double, 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.
void dsm::Dynamics< Delay >::removeAgent | ( | Size | agentId | ) |
Remove an agent from the simulation.
agentId | the id of the agent to remove |
|
pure virtual |
Set the speed of an agent.
This is a pure-virtual function, it must be implemented in the derived classes
agentId | The id of the agent |
Implemented in dsm::FirstOrderDynamics< Delay >.
|
inline |
Set the data update period.
dataUpdatePeriod | Delay, 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::Dynamics< Delay >::setDestinationNodes | ( | const std::span< Id > & | destinationNodes, |
bool | updatePaths = true ) |
Set the dynamics destination nodes auto-generating itineraries.
destinationNodes | The destination nodes |
updatePaths | If true, the paths are updated |
std::invalid_argument | Ifone or more destination nodes do not exist |
void dsm::Dynamics< Delay >::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
void dsm::Dynamics< Delay >::setItineraries | ( | std::span< Itinerary > | itineraries | ) |
Set the itineraries.
itineraries | The itineraries |
void dsm::Dynamics< Delay >::setMaxFlowPercentage | ( | double | maxFlowPercentage | ) |
Set the maximum flow percentage.
maxFlowPercentage | The maximum flow percentage |
The maximum flow percentage is the percentage of the maximum flow that a street can transmit. Default is 1 (100%).
std::invalid_argument | If the maximum flow percentage is not between 0 and 1 |
void dsm::Dynamics< Delay >::setMinSpeedRateo | ( | double | minSpeedRateo | ) |
Set the minim speed rateo, i.e. the minim speed with respect to the speed limit.
minSpeedRateo | The minim speed rateo |
std::invalid_argument | If the minim speed rateo is not between 0 and 1 |
|
inline |
Set the seed for the graph's random number generator.
seed | The seed |
Measurement< double > dsm::Dynamics< Delay >::streetMeanDensity | ( | bool | normalized = false | ) | const |
Get the mean density of the streets in .
Measurement< double > dsm::Dynamics< Delay >::streetMeanFlow | ( | ) | const |
Get the mean flow of the streets in .
Measurement< double > dsm::Dynamics< Delay >::streetMeanFlow | ( | double | threshold, |
bool | above ) const |
Get the mean flow of the streets in .
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 |
|
pure virtual |
Implemented in dsm::FirstOrderDynamics< Delay >.
|
pure virtual |
Implemented in dsm::FirstOrderDynamics< Delay >.
|
pure virtual |
Implemented in dsm::FirstOrderDynamics< Delay >.
|
inline |
Get the time.
|
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::Dynamics< Delay >::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