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
2 changes: 1 addition & 1 deletion examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ set(EXECUTABLE_OUTPUT_PATH ../)

# add as executable all cpp files into '.' folder
file(GLOB SOURCES "*.cpp")
file(GLOB SRC_SOURCES "../src/dsm/sources/*.cpp", "../src/dsm/utility/*.cpp")
file(GLOB SRC_SOURCES "../src/dsm/sources/*.cpp" "../src/dsm/utility/*.cpp")

# Loop through each source file and create an executable
foreach(SOURCE ${SOURCES})
Expand Down
2 changes: 1 addition & 1 deletion 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, SEED, 0.95};
Dynamics dynamics{graph, true, SEED, 0.95};
Unit n{0};
{
std::vector<Unit> destinationNodes;
Expand Down
2 changes: 1 addition & 1 deletion examples/slow_charge_tl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ int main(int argc, char** argv) {

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

Dynamics dynamics{graph, SEED, 0.95};
Dynamics dynamics{graph, true, SEED, 0.95};
Unit n{0};
{
std::vector<Unit> destinationNodes;
Expand Down
2 changes: 1 addition & 1 deletion examples/stalingrado.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ int main() {
dsm::Logger::info(std::format("Streets: {}", graph.nEdges()));

// Create the dynamics
Dynamics dynamics{graph, 69, 0.95};
Dynamics dynamics{graph, false, 69, 0.95};
dynamics.setSpeedFluctuationSTD(0.2);
Itinerary itinerary{4, 4};
dynamics.addItinerary(itinerary);
Expand Down
2 changes: 1 addition & 1 deletion profiling/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ int main() {

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

Dynamics dynamics{graph, std::nullopt, 0.95};
Dynamics dynamics{graph, false, std::nullopt, 0.95};
dynamics.addItinerary(it1);
dynamics.addItinerary(it2);
dynamics.addItinerary(it3);
Expand Down
2 changes: 1 addition & 1 deletion src/dsm/dsm.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

static constexpr uint8_t DSM_VERSION_MAJOR = 2;
static constexpr uint8_t DSM_VERSION_MINOR = 3;
static constexpr uint8_t DSM_VERSION_PATCH = 16;
static constexpr uint8_t DSM_VERSION_PATCH = 17;

static auto const DSM_VERSION =
std::format("{}.{}.{}", DSM_VERSION_MAJOR, DSM_VERSION_MINOR, DSM_VERSION_PATCH);
Expand Down
42 changes: 39 additions & 3 deletions src/dsm/headers/Dynamics.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
#include <format>
#include <thread>
#include <exception>
#include <fstream>
#include <filesystem>

#include "DijkstraWeights.hpp"
#include "Itinerary.hpp"
Expand All @@ -31,6 +33,8 @@
#include "../utility/Logger.hpp"
#include "../utility/Typedef.hpp"

static auto constexpr g_cacheFolder = "./.dsmcache/";

namespace dsm {
/// @brief The Measurement struct represents the mean of a quantity and its standard deviation
/// @tparam T The type of the quantity
Expand Down Expand Up @@ -67,6 +71,7 @@
private:
std::map<Id, std::unique_ptr<agent_t>> m_agents;
std::unordered_map<Id, std::unique_ptr<Itinerary>> m_itineraries;
bool m_bCacheEnabled;

protected:
Graph m_graph;
Expand All @@ -81,6 +86,18 @@
/// @brief Update the path of a single itinerary using Dijsktra's algorithm
/// @param pItinerary An std::unique_prt to the itinerary
void m_updatePath(const std::unique_ptr<Itinerary>& pItinerary) {
if (m_bCacheEnabled) {
auto const& file =
std::format("{}it{}.dsmcache", g_cacheFolder, pItinerary->id());
if (std::filesystem::exists(file)) {
auto path = SparseMatrix<bool>{};
path.load(file);
pItinerary->setPath(std::move(path));
Logger::info(
std::format("Loaded cached path for itinerary {}", pItinerary->id()));
return;
}
}
Size const dimension = m_graph.adjMatrix().getRowDim();
auto const destinationID = pItinerary->destination();
SparseMatrix<bool> path{dimension, dimension};
Expand Down Expand Up @@ -125,12 +142,22 @@
pItinerary->destination()));
}
pItinerary->setPath(path);
if (m_bCacheEnabled) {
pItinerary->path().cache(
std::format("{}it{}.dsmcache", g_cacheFolder, pItinerary->id()));
Logger::info(
std::format("Saved path in cache for itinerary {}", pItinerary->id()));
}
}

public:
/// @brief Construct a new Dynamics object
/// @param graph The graph representing the network
Dynamics(Graph& graph, std::optional<unsigned int> seed);
/// @param useCache If true, the paths are cached (default is false)
/// @param seed The seed for the random number generator (default is std::nullopt)
Dynamics(Graph& graph,
bool useCache = false,
std::optional<unsigned int> seed = std::nullopt);

virtual void setAgentSpeed(Size agentId) = 0;
virtual void evolve(bool reinsert_agents = false) = 0;
Expand Down Expand Up @@ -276,14 +303,23 @@
};

template <typename agent_t>
Dynamics<agent_t>::Dynamics(Graph& graph, std::optional<unsigned int> seed)
: m_graph{std::move(graph)},
Dynamics<agent_t>::Dynamics(Graph& graph,
bool useCache,
std::optional<unsigned int> seed)
: m_bCacheEnabled{useCache},
m_graph{std::move(graph)},
m_time{0},
m_previousSpireTime{0},
m_generator{std::random_device{}()} {
if (seed.has_value()) {
m_generator.seed(seed.value());
}
if (m_bCacheEnabled) {

Check notice

Code scanning / Cppcheck (reported by Codacy)

MISRA 14.4 rule Note

MISRA 14.4 rule
if (!std::filesystem::exists(g_cacheFolder)) {
std::filesystem::create_directory(g_cacheFolder);
}
Logger::info(std::format("Cache enabled (default folder is {})", g_cacheFolder));
}
}

template <typename agent_t>
Expand Down Expand Up @@ -472,7 +508,7 @@
template <typename agent_t>
Measurement<double> Dynamics<agent_t>::streetMeanDensity(bool normalized) const {
if (m_graph.streetSet().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> densities;
densities.reserve(m_graph.streetSet().size());
Expand Down
6 changes: 5 additions & 1 deletion src/dsm/headers/FirstOrderDynamics.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,12 @@

public:
/// @brief Construct a new First Order Dynamics object
/// @param graph, The graph representing the network
/// @param graph The graph representing the network
/// @param useCache If true, the cache is used (default is false)
/// @param seed The seed for the random number generator (default is std::nullopt)
/// @param alpha The minimum speed rate (default is 0)
explicit FirstOrderDynamics(Graph& graph,
bool useCache = false,

Check notice

Code scanning / Cppcheck (reported by Codacy)

MISRA 17.8 rule Note

MISRA 17.8 rule
std::optional<unsigned int> seed = std::nullopt,
double alpha = 0.);
/// @brief Set the speed of an agent
Expand Down
13 changes: 9 additions & 4 deletions src/dsm/headers/RoadDynamics.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,11 @@
public:
/// @brief Construct a new RoadDynamics object
/// @param graph The graph representing the network
/// @param seed The seed for the random number generator
RoadDynamics(Graph& graph, std::optional<unsigned int> seed);
/// @param useCache If true, the cache is used (default is false)
/// @param seed The seed for the random number generator (default is std::nullopt)
RoadDynamics(Graph& graph,
bool useCache = false,

Check notice

Code scanning / Cppcheck (reported by Codacy)

MISRA 17.8 rule Note

MISRA 17.8 rule
std::optional<unsigned int> seed = std::nullopt);

/// @brief Set the error probability
/// @param errorProbability The error probability
Expand Down Expand Up @@ -180,8 +183,10 @@

template <typename delay_t>
requires(is_numeric_v<delay_t>)
RoadDynamics<delay_t>::RoadDynamics(Graph& graph, std::optional<unsigned int> seed)
: Dynamics<Agent<delay_t>>(graph, seed),
RoadDynamics<delay_t>::RoadDynamics(Graph& graph,
bool useCache,
std::optional<unsigned int> seed)
: Dynamics<Agent<delay_t>>(graph, useCache, seed),

Check notice

Code scanning / Cppcheck (reported by Codacy)

MISRA 12.3 rule Note

MISRA 12.3 rule
m_previousOptimizationTime{0},
m_errorProbability{std::nullopt},
m_passageProbability{std::nullopt},
Expand Down
5 changes: 4 additions & 1 deletion src/dsm/sources/FirstOrderDynamics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@

namespace dsm {
FirstOrderDynamics::FirstOrderDynamics(Graph& graph,
bool useCache,
std::optional<unsigned int> seed,
double alpha)
: RoadDynamics<Delay>(graph, seed), m_alpha{alpha}, m_speedFluctuationSTD{0.} {
: RoadDynamics<Delay>(graph, useCache, seed),

Check notice

Code scanning / Cppcheck (reported by Codacy)

MISRA 12.3 rule Note

MISRA 12.3 rule
m_alpha{alpha},

Check notice

Code scanning / Cppcheck (reported by Codacy)

MISRA 12.3 rule Note

MISRA 12.3 rule
m_speedFluctuationSTD{0.} {
if (alpha < 0. || alpha > 1.) {
Logger::error(std::format("The minimum speed rateo ({}) must be in [0, 1[", alpha));
}
Expand Down
Loading
Loading