Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions benchmark/Dynamics/BenchDynamics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

#include "Graph.hpp"
#include "Itinerary.hpp"
#include "Dynamics.hpp"
#include "FirstOrderDynamics.hpp"
#include "Bench.hpp"

using Graph = dsm::Graph;
Expand All @@ -28,7 +28,6 @@ int main() {
dynamics.addItinerary(it2);
dynamics.addItinerary(it3);
dynamics.addItinerary(it4);
dynamics.setSeed(69);
dynamics.setErrorProbability(0.3);
dynamics.setMinSpeedRateo(0.95);

Expand Down
3 changes: 1 addition & 2 deletions examples/slow_charge_rb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ int main(int argc, char** argv) {

std::cout << "Creating dynamics...\n";

Dynamics dynamics{graph};
Dynamics dynamics{graph, SEED};
Unit n{0};
{
std::vector<Unit> destinationNodes;
Expand All @@ -134,7 +134,6 @@ int main(int argc, char** argv) {
}
std::cout << "Number of exits: " << n << '\n';

dynamics.setSeed(SEED);
dynamics.setErrorProbability(0.05);
dynamics.setMaxFlowPercentage(0.7707);
// dynamics.setForcePriorities(true);
Expand Down
3 changes: 1 addition & 2 deletions examples/slow_charge_tl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ int main(int argc, char** argv) {

std::cout << "Creating dynamics...\n";

Dynamics dynamics{graph};
Dynamics dynamics{graph, SEED};
Unit n{0};
{
std::vector<Unit> destinationNodes;
Expand All @@ -230,7 +230,6 @@ int main(int argc, char** argv) {
}
std::cout << "Number of exits: " << n << '\n';

dynamics.setSeed(SEED);
dynamics.setErrorProbability(ERROR_PROBABILITY);
// dynamics.setMaxFlowPercentage(0.69);
// dynamics.setForcePriorities(false);
Expand Down
3 changes: 1 addition & 2 deletions examples/stalingrado.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,7 @@ int main() {
std::cout << "Streets: " << graph.nEdges() << '\n';

// Create the dynamics
Dynamics dynamics{graph};
dynamics.setSeed(69);
Dynamics dynamics{graph, 69};
dynamics.setMinSpeedRateo(0.95);
dynamics.setSpeedFluctuationSTD(0.2);
Itinerary itinerary{0, 4};
Expand Down
1 change: 0 additions & 1 deletion profiling/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ int main() {
dynamics.addItinerary(it2);
dynamics.addItinerary(it3);
dynamics.addItinerary(it4);
dynamics.setSeed(69);
dynamics.setErrorProbability(0.3);
dynamics.setMinSpeedRateo(0.95);
dynamics.updatePaths();
Expand Down
2 changes: 1 addition & 1 deletion src/dsm/dsm.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
#include "headers/Roundabout.hpp"
#include "headers/SparseMatrix.hpp"
#include "headers/Street.hpp"
#include "headers/Dynamics.hpp"
#include "headers/FirstOrderDynamics.hpp"

Check notice

Code scanning / Cppcheck (reported by Codacy)

MISRA 20.1 rule Note

MISRA 20.1 rule
#include "utility/TypeTraits/is_node.hpp"
#include "utility/TypeTraits/is_street.hpp"
#include "utility/TypeTraits/is_numeric.hpp"
Expand Down
881 changes: 57 additions & 824 deletions src/dsm/headers/Dynamics.hpp

Large diffs are not rendered by default.

149 changes: 149 additions & 0 deletions src/dsm/headers/FirstOrderDynamics.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
#pragma once

#include "RoadDynamics.hpp"

namespace dsm {
template <typename delay_t>
requires(std::unsigned_integral<delay_t>)
class FirstOrderDynamics : public RoadDynamics<delay_t> {
double m_speedFluctuationSTD;

public:
/// @brief Construct a new First Order Dynamics object
/// @param graph, The graph representing the network
FirstOrderDynamics(Graph& graph, std::optional<unsigned int> seed = std::nullopt)
: RoadDynamics<delay_t>(graph, seed), m_speedFluctuationSTD{0.} {};

Check notice

Code scanning / Cppcheck (reported by Codacy)

MISRA 12.3 rule Note

MISRA 12.3 rule
/// @brief Set the speed of an agent
/// @param agentId The id of the agent
/// @throw std::invalid_argument, If the agent is not found
void setAgentSpeed(Size agentId) override;
/// @brief Set the standard deviation of the speed fluctuation
/// @param speedFluctuationSTD The standard deviation of the speed fluctuation
/// @throw std::invalid_argument, If the standard deviation is negative
void setSpeedFluctuationSTD(double speedFluctuationSTD);
/// @brief Get the mean speed of a street in \f$m/s\f$
/// @return double The mean speed of the street or street->maxSpeed() if the street is empty
/// @details The mean speed of a street is given by the formula:
/// \f$ v_{\text{mean}} = v_{\text{max}} \left(1 - \frac{\alpha}{2} \left( n - 1\right) \right) \f$
/// where \f$ v_{\text{max}} \f$ is the maximum speed of the street, \f$ \alpha \f$ is the minimum speed rateo divided by the capacity
/// and \f$ n \f$ is the number of agents in the street
double streetMeanSpeed(Id streetId) const override;
/// @brief Get the mean speed of the streets in \f$m/s\f$
/// @return Measurement The mean speed of the agents and the standard deviation
Measurement<double> streetMeanSpeed() const override;
/// @brief Get the mean speed of the streets with density above or below a threshold in \f$m/s\f$
/// @param threshold The density threshold to consider
/// @param above If true, the function returns the mean speed of the streets with a density above the threshold, otherwise below
/// @return Measurement The mean speed of the agents and the standard deviation
Measurement<double> streetMeanSpeed(double threshold, bool above) const override;
};

template <typename delay_t>
requires(std::unsigned_integral<delay_t>)
void FirstOrderDynamics<delay_t>::setAgentSpeed(Size agentId) {
const auto& agent{this->m_agents[agentId]};
const auto& street{this->m_graph.streetSet()[agent->streetId().value()]};
double speed{street->maxSpeed() *
(1. - this->m_minSpeedRateo * street->density(true))};

Check notice

Code scanning / Cppcheck (reported by Codacy)

MISRA 12.1 rule Note

MISRA 12.1 rule
if (m_speedFluctuationSTD > 0.) {
std::normal_distribution<double> speedDist{speed, speed * m_speedFluctuationSTD};
speed = speedDist(this->m_generator);
}
speed < 0. ? agent->setSpeed(street->maxSpeed() * (1. - this->m_minSpeedRateo))

Check notice

Code scanning / Cppcheck (reported by Codacy)

MISRA 12.1 rule Note

MISRA 12.1 rule
: agent->setSpeed(speed);
}

template <typename delay_t>
requires(std::unsigned_integral<delay_t>)
void FirstOrderDynamics<delay_t>::setSpeedFluctuationSTD(double speedFluctuationSTD) {
if (speedFluctuationSTD < 0.) {
throw std::invalid_argument(
buildLog("The speed fluctuation standard deviation must be positive."));
}
m_speedFluctuationSTD = speedFluctuationSTD;
}

template <typename delay_t>
requires(std::unsigned_integral<delay_t>)
double FirstOrderDynamics<delay_t>::streetMeanSpeed(Id streetId) const {
const auto& street{this->m_graph.streetSet().at(streetId)};
if (street->nAgents() == 0) {
return street->maxSpeed();

Check notice

Code scanning / Cppcheck (reported by Codacy)

MISRA 15.5 rule Note

MISRA 15.5 rule
}
double meanSpeed{0.};
Size n{0};
if (street->nExitingAgents() == 0) {
n = static_cast<Size>(street->waitingAgents().size());
double alpha{this->m_minSpeedRateo / street->capacity()};
meanSpeed = street->maxSpeed() * n * (1. - 0.5 * alpha * (n - 1.));

Check notice

Code scanning / Cppcheck (reported by Codacy)

MISRA 12.1 rule Note

MISRA 12.1 rule
} else {
for (const auto& agentId : street->waitingAgents()) {
meanSpeed += this->m_agents.at(agentId)->speed();
++n;
}
for (auto const& queue : street->exitQueues()) {
for (const auto& agentId : queue) {
meanSpeed += this->m_agents.at(agentId)->speed();
++n;
}
}
}
const auto& node = this->m_graph.nodeSet().at(street->nodePair().second);
if (node->isIntersection()) {

Check notice

Code scanning / Cppcheck (reported by Codacy)

MISRA 14.4 rule Note

MISRA 14.4 rule
auto& intersection = dynamic_cast<Intersection&>(*node);
for (const auto& [angle, agentId] : intersection.agents()) {

Check notice

Code scanning / Cppcheck (reported by Codacy)

MISRA 12.3 rule Note

MISRA 12.3 rule
const auto& agent{this->m_agents.at(agentId)};
if (agent->streetId().has_value() && agent->streetId().value() == streetId) {
meanSpeed += agent->speed();
++n;
}
}
} else if (node->isRoundabout()) {

Check notice

Code scanning / Cppcheck (reported by Codacy)

MISRA 14.4 rule Note

MISRA 14.4 rule
auto& roundabout = dynamic_cast<Roundabout&>(*node);
for (const auto& agentId : roundabout.agents()) {
const auto& agent{this->m_agents.at(agentId)};
if (agent->streetId().has_value() && agent->streetId().value() == streetId) {
meanSpeed += agent->speed();
++n;
}
}
}
return meanSpeed / n;

Check notice

Code scanning / Cppcheck (reported by Codacy)

MISRA 15.5 rule Note

MISRA 15.5 rule
}

template <typename delay_t>
requires(std::unsigned_integral<delay_t>)
Measurement<double> FirstOrderDynamics<delay_t>::streetMeanSpeed() const {
if (this->m_agents.size() == 0) {
return Measurement(0., 0.);

Check notice

Code scanning / Cppcheck (reported by Codacy)

MISRA 15.5 rule Note

MISRA 15.5 rule
}
std::vector<double> speeds;
speeds.reserve(this->m_graph.streetSet().size());
for (const auto& [streetId, street] : this->m_graph.streetSet()) {

Check notice

Code scanning / Cppcheck (reported by Codacy)

MISRA 12.3 rule Note

MISRA 12.3 rule
speeds.push_back(this->streetMeanSpeed(streetId));
}
return Measurement<double>(speeds);

Check notice

Code scanning / Cppcheck (reported by Codacy)

MISRA 15.5 rule Note

MISRA 15.5 rule
}
template <typename delay_t>
requires(std::unsigned_integral<delay_t>)
Measurement<double> FirstOrderDynamics<delay_t>::streetMeanSpeed(double threshold,
bool above) const {
if (this->m_agents.size() == 0) {
return Measurement(0., 0.);

Check notice

Code scanning / Cppcheck (reported by Codacy)

MISRA 15.5 rule Note

MISRA 15.5 rule
}
std::vector<double> speeds;
speeds.reserve(this->m_graph.streetSet().size());
for (const auto& [streetId, street] : this->m_graph.streetSet()) {

Check notice

Code scanning / Cppcheck (reported by Codacy)

MISRA 12.3 rule Note

MISRA 12.3 rule
if (above) {

Check notice

Code scanning / Cppcheck (reported by Codacy)

MISRA 14.4 rule Note

MISRA 14.4 rule
if (street->density(true) > threshold) {
speeds.push_back(this->streetMeanSpeed(streetId));
}
} else {
if (street->density(true) < threshold) {
speeds.push_back(this->streetMeanSpeed(streetId));
}
}
}
return Measurement<double>(speeds);

Check notice

Code scanning / Cppcheck (reported by Codacy)

MISRA 15.5 rule Note

MISRA 15.5 rule
}
} // namespace dsm
Loading
Loading