Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
092bac9
Implement < operator for Agents
Grufoony Feb 24, 2025
dab837a
Simplified setStreetId
Grufoony Feb 24, 2025
36fda43
cose sensate
Grufoony Feb 24, 2025
cceb2b3
Cose meno sensate
Grufoony Feb 24, 2025
a29862f
Remove copy constructors from Edges
Grufoony Feb 24, 2025
5d7f725
Merge branch 'noCopyCOnstructors' into ReworkAgentHandling
Grufoony Feb 24, 2025
dabab1f
Working streets
Grufoony Feb 24, 2025
d0f4e5e
Working streets
Grufoony Feb 24, 2025
e6961e5
Working
Grufoony Feb 24, 2025
b629c24
Seems to compile
Grufoony Feb 24, 2025
68169df
Working on...
Grufoony Feb 24, 2025
d9b21d4
Fix one roundabout test
Grufoony Feb 24, 2025
a1fea5d
Remove useless headers
Grufoony Feb 25, 2025
7024fd4
Small refactor
Grufoony Feb 25, 2025
2e9ca1a
Enqueued agents must have null speed
Grufoony Feb 25, 2025
1a6f12b
Fix node evoution
Grufoony Feb 25, 2025
e5e1795
Last intersection test
Grufoony Feb 25, 2025
272c936
Agent loop optimization
Grufoony Feb 25, 2025
f7dce97
More tests!
Grufoony Feb 25, 2025
6929b23
Fix mean speed getter
Grufoony Feb 25, 2025
118128c
More tests...
Grufoony Feb 25, 2025
15eaa3d
Some traffic light's tests
Grufoony Feb 25, 2025
4cea22d
Finish tl tests
Grufoony Feb 25, 2025
d9c0775
One more test
Grufoony Feb 25, 2025
249dd99
Finish dynamic tests
Grufoony Feb 25, 2025
832a1df
Revert Adj tests
Grufoony Feb 25, 2025
4917d7d
Update action
Grufoony Feb 25, 2025
ca4e806
Try to fix macos test
Grufoony Feb 25, 2025
c108350
Update version
Grufoony Feb 25, 2025
09b68ce
Fix macos tests
Grufoony Feb 25, 2025
77331cd
Fix examples
Grufoony Feb 25, 2025
34683e9
Refactor limits
Grufoony Feb 25, 2025
719118e
Merge branch 'main' into ReworkAgentHandling
Grufoony Feb 28, 2025
190fe14
Add ghost agents to `saveMacroscopicObservables` function
Grufoony Feb 28, 2025
fe63fc2
Bugfix and add test
Grufoony Mar 1, 2025
39fc52d
Merge branch 'main' into ReworkAgentHandling
Grufoony Mar 4, 2025
fcd0cfe
Merge branch 'main' into ReworkAgentHandling
Grufoony Mar 4, 2025
e618c41
Merge branch 'main' into ReworkAgentHandling
Grufoony Mar 5, 2025
6a3a2e1
Fix test
Grufoony Mar 5, 2025
51af56a
Merge branch 'main' into ReworkAgentHandling
Grufoony Mar 6, 2025
9a77381
Add RoadJunction and floating point transport capacity
Grufoony Mar 7, 2025
5cdcb52
Working
Grufoony Mar 7, 2025
a029816
Merge branch 'RefactorNodes' into ReworkAgentHandling
Grufoony Mar 7, 2025
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
7 changes: 4 additions & 3 deletions examples/slow_charge_rb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ int main(int argc, char** argv) {
BASE_OUT_FOLDER,
ERROR_PROBABILITY,
std::to_string(SEED))}; // output folder
const auto MAX_TIME{static_cast<unsigned int>(1e6)}; // maximum time of simulation
const auto MAX_TIME{static_cast<unsigned int>(5e5)}; // maximum time of simulation

// Clear output folder or create it if it doesn't exist
if (!fs::exists(BASE_OUT_FOLDER)) {
Expand Down Expand Up @@ -205,14 +205,15 @@ int main(int argc, char** argv) {
dynamics.evolve(false);

if (dynamics.time() % 2400 == 0) {
deltaAgents = dynamics.agents().size() - previousAgents;
auto const totalDynamicsAgents{dynamics.nAgents()};
deltaAgents = totalDynamicsAgents - previousAgents;
if (deltaAgents < 0) {
++nAgents;
std::cout << "- Now I'm adding " << nAgents << " agents.\n";
std::cout << "Delta agents: " << deltaAgents << '\n';
std::cout << "At time: " << dynamics.time() << '\n';
}
previousAgents = dynamics.agents().size();
previousAgents = totalDynamicsAgents;
}

if (dynamics.time() % 300 == 0) {
Expand Down
7 changes: 4 additions & 3 deletions examples/slow_charge_tl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ int main(int argc, char** argv) {
BASE_OUT_FOLDER,
ERROR_PROBABILITY,
std::to_string(SEED))}; // output folder
constexpr auto MAX_TIME{static_cast<unsigned int>(1e6)}; // maximum time of simulation
constexpr auto MAX_TIME{static_cast<unsigned int>(5e5)}; // maximum time of simulation

std::cout << "-------------------------------------------------\n";
std::cout << "Input parameters:\n";
Expand Down Expand Up @@ -309,14 +309,15 @@ int main(int argc, char** argv) {
if (dynamics.time() % 2400 == 0 && nAgents > 0) {
// auto meanDelta = std::accumulate(deltas.begin(), deltas.end(), 0) /
// deltas.size();
deltaAgents = dynamics.agents().size() - previousAgents;
auto const totalDynamicsAgents{dynamics.nAgents()};
deltaAgents = totalDynamicsAgents - previousAgents;
if (deltaAgents < 0) {
++nAgents;
std::cout << "- Now I'm adding " << nAgents << " agents.\n";
std::cout << "Delta agents: " << deltaAgents << '\n';
std::cout << "At time: " << dynamics.time() << '\n';
}
previousAgents = dynamics.agents().size();
previousAgents = totalDynamicsAgents;
// deltas.clear();
}

Expand Down
4 changes: 2 additions & 2 deletions src/dsm/dsm.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
#include <format>

static constexpr uint8_t DSM_VERSION_MAJOR = 2;
static constexpr uint8_t DSM_VERSION_MINOR = 5;
static constexpr uint8_t DSM_VERSION_PATCH = 12;
static constexpr uint8_t DSM_VERSION_MINOR = 6;
static constexpr uint8_t DSM_VERSION_PATCH = 0;

static auto const DSM_VERSION =
std::format("{}.{}.{}", DSM_VERSION_MAJOR, DSM_VERSION_MINOR, DSM_VERSION_PATCH);
Expand Down
6 changes: 2 additions & 4 deletions src/dsm/headers/Agent.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,22 +38,20 @@ namespace dsm {
public:
/// @brief Construct a new Agent object
/// @param spawnTime The agent's spawn time
/// @param id The agent's id
/// @param itineraryId Optional, The agent's destination node. If not provided, the agent is a random agent
/// @param srcNodeId Optional, The id of the source node of the agent
Agent(Time const& spawnTime,
Id id,
std::optional<Id> itineraryId = std::nullopt,
std::optional<Id> srcNodeId = std::nullopt);
/// @brief Construct a new Agent object
/// @param spawnTime The agent's spawn time
/// @param id The agent's id
/// @param itineraryIds The agent's itinerary
/// @param srcNodeId Optional, The id of the source node of the agent
Agent(Time const& spawnTime,
Id id,
std::vector<Id> const& trip,
std::optional<Id> srcNodeId = std::nullopt);

void setSrcNodeId(Id srcNodeId);
/// @brief Set the street occupied by the agent
/// @param streetId The id of the street currently occupied by the agent
void setStreetId(std::optional<Id> streetId = std::nullopt);
Expand Down
2 changes: 0 additions & 2 deletions src/dsm/headers/Dynamics.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@
#include <functional>

#include "Network.hpp"
#include "../utility/TypeTraits/is_agent.hpp"
#include "../utility/TypeTraits/is_itinerary.hpp"
#include "../utility/Logger.hpp"
#include "../utility/Typedef.hpp"

Expand Down
4 changes: 4 additions & 0 deletions src/dsm/headers/Edge.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,11 @@
int capacity = 1,
double transportCapacity = 1.,
std::vector<std::pair<double, double>> geometry = {});
Edge(Edge&&) = default;

Check notice

Code scanning / Cppcheck (reported by Codacy)

MISRA 16.3 rule Note

MISRA 16.3 rule
Edge(const Edge&) = delete;
virtual ~Edge() = default;

Check notice

Code scanning / Cppcheck (reported by Codacy)

MISRA 16.3 rule Note

MISRA 16.3 rule

void resetId(Id newId);
void setCapacity(int capacity);
void setTransportCapacity(double capacity);
void setGeometry(std::vector<std::pair<double, double>> geometry);
Expand Down
2 changes: 1 addition & 1 deletion src/dsm/headers/FirstOrderDynamics.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ namespace dsm {
/// @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;
void setAgentSpeed(std::unique_ptr<Agent> const& pAgent) 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
Expand Down
32 changes: 18 additions & 14 deletions src/dsm/headers/Intersection.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,31 +8,35 @@

#pragma once

#include "Node.hpp"
#include "Agent.hpp"
#include "RoadJunction.hpp"

#include <map>
#include <memory>
#include <set>

namespace dsm {
/// @brief The Intersection class represents a node in the network.
/// @tparam Id The type of the node's id. It must be an unsigned integral type.
class Intersection : public Node {
class Intersection : public RoadJunction {
protected:
std::multimap<int16_t, Id> m_agents;
std::multimap<int16_t, std::unique_ptr<Agent>> m_agents;
std::set<Id>
m_streetPriorities; // A set containing the street ids that have priority - like main roads
Size m_agentCounter;

public:
/// @brief Construct a new Intersection object
/// @param id The node's id
explicit Intersection(Id id) : Node{id} {};
explicit Intersection(Id id) : RoadJunction{id} {};

Check notice

Code scanning / Cppcheck (reported by Codacy)

Member variable 'Intersection::m_agentCounter' is not initialized in the constructor. Note

Member variable 'Intersection::m_agentCounter' is not initialized in the constructor.
/// @brief Construct a new Intersection object
/// @param id The node's id
/// @param coords A std::pair containing the node's coordinates
Intersection(Id id, std::pair<double, double> coords) : Node{id, coords} {};
Intersection(Id id, std::pair<double, double> coords) : RoadJunction{id, coords} {};

Check notice

Code scanning / Cppcheck (reported by Codacy)

Member variable 'Intersection::m_agentCounter' is not initialized in the constructor. Note

Member variable 'Intersection::m_agentCounter' is not initialized in the constructor.

Intersection(Node const& node) : Node{node} {};
Intersection(RoadJunction const& node) : RoadJunction{node} {};

Check notice

Code scanning / Cppcheck (reported by Codacy)

Member variable 'Intersection::m_agentCounter' is not initialized in the constructor. Note

Member variable 'Intersection::m_agentCounter' is not initialized in the constructor.

Check warning

Code scanning / Cppcheck (reported by Codacy)

Class 'Intersection' has a constructor with 1 argument that is not explicit. Warning

Class 'Intersection' has a constructor with 1 argument that is not explicit.

Intersection(Intersection const&) = delete;

virtual ~Intersection() = default;

Expand All @@ -47,17 +51,17 @@
/// The agent with the smallest angle difference is the first one to be
/// removed from the node.
/// @throws std::runtime_error if the node is full
void addAgent(double angle, Id agentId);
void addAgent(double angle, std::unique_ptr<Agent> pAgent);
/// @brief Put an agent in the node
/// @param agentId The agent's id
/// @details The agent's angle difference is used to order the agents in the node.
/// The agent with the smallest angle difference is the first one to be
/// removed from the node.
/// @throws std::runtime_error if the node is full
void addAgent(Id agentId);
/// @brief Removes an agent from the node
/// @param agentId The agent's id
void removeAgent(Id agentId);
void addAgent(std::unique_ptr<Agent> pAgent);
// /// @brief Removes an agent from the node
// /// @param agentId The agent's id
// void removeAgent(Id agentId);
/// @brief Set the node streets with priority
/// @param streetPriorities A std::set containing the node's street priorities
void setStreetPriorities(std::set<Id> streetPriorities) {
Expand All @@ -69,11 +73,11 @@
/// @brief Returns the node's density
/// @return double The node's density
double density() const override {
return static_cast<double>(m_agents.size()) / m_capacity;
return static_cast<double>(m_agents.size()) / this->capacity();
}
/// @brief Returns true if the node is full
/// @return bool True if the node is full
bool isFull() const override { return m_agents.size() == this->m_capacity; }
bool isFull() const override { return m_agents.size() == this->capacity(); }

/// @brief Get the node's street priorities
/// @details This function returns a std::set containing the node's street priorities.
Expand All @@ -83,7 +87,7 @@
virtual const std::set<Id>& streetPriorities() const { return m_streetPriorities; };
/// @brief Get the node's agent ids
/// @return std::set<Id> A std::set containing the node's agent ids
const std::multimap<int16_t, Id>& agents() { return m_agents; };
std::multimap<int16_t, std::unique_ptr<Agent>>& agents() { return m_agents; };
/// @brief Returns the number of agents that have passed through the node
/// @return Size The number of agents that have passed through the node
/// @details This function returns the number of agents that have passed through the node
Expand Down
13 changes: 13 additions & 0 deletions src/dsm/headers/Network.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@
requires(std::is_base_of_v<node_t, TNode> &&
std::constructible_from<TNode, Id, TArgs...>)
void addNode(Id nodeId, TArgs&&... args);

template <typename TEdge = edge_t>
requires(std::is_base_of_v<edge_t, TEdge>)
void addEdge(TEdge&& edge);
/// @brief Add an edge to the network
/// @tparam TEdge The type of the edge (default is edge_t)
/// @tparam TArgs The types of the arguments
Expand All @@ -74,11 +78,11 @@
requires(std::is_base_of_v<node_t, TNode>)
TNode& node(Id nodeId);
/// @brief Get an edge by id
/// @tparam TEdge The type of the edge

Check notice

Code scanning / Cppcheck (reported by Codacy)

MISRA 12.3 rule Note

MISRA 12.3 rule
/// @param edgeId The edge's id
/// @return TEdge& A reference to the edge
template <typename TEdge>
requires(std::is_base_of_v<edge_t, TEdge>)

Check notice

Code scanning / Cppcheck (reported by Codacy)

MISRA 12.3 rule Note

MISRA 12.3 rule
TEdge& edge(Id edgeId);
};

Expand Down Expand Up @@ -160,6 +164,15 @@
m_nodes.emplace(std::make_pair(
nodeId, std::make_unique<TNode>(nodeId, std::forward<TArgs>(args)...)));
}
template <typename node_t, typename edge_t>
requires(std::is_base_of_v<Node, node_t> && std::is_base_of_v<Edge, edge_t>)

Check notice

Code scanning / Cppcheck (reported by Codacy)

MISRA 12.3 rule Note

MISRA 12.3 rule
template <typename TEdge>
requires(std::is_base_of_v<edge_t, TEdge>)
void Network<node_t, edge_t>::addEdge(TEdge&& edge) {

Check notice

Code scanning / Cppcheck (reported by Codacy)

MISRA 12.3 rule Note

MISRA 12.3 rule
assert(!m_edges.contains(edge.id()));
m_edges.emplace(std::make_pair(edge.id(), std::make_unique<TEdge>(std::move(edge))));
m_addMissingNodes(edge.id());
}
template <typename node_t, typename edge_t>
requires(std::is_base_of_v<Node, node_t> && std::is_base_of_v<Edge, edge_t>)
template <typename TEdge, typename... TArgs>
Expand All @@ -180,7 +193,7 @@
template <typename node_t, typename edge_t>
requires(std::is_base_of_v<Node, node_t> && std::is_base_of_v<Edge, edge_t>)
std::unique_ptr<edge_t> const& Network<node_t, edge_t>::edge(Id edgeId) const {
return m_edges.at(edgeId);

Check notice

Code scanning / Cppcheck (reported by Codacy)

MISRA 12.3 rule Note

MISRA 12.3 rule
}

template <typename node_t, typename edge_t>
Expand Down
43 changes: 3 additions & 40 deletions src/dsm/headers/Node.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,38 +29,26 @@ namespace dsm {
Id m_id;
std::optional<std::pair<double, double>> m_coords;
std::string m_name;
Size m_capacity;
int m_transportCapacity;

public:
/// @brief Construct a new Node object with capacity 1
/// @param id The node's id
explicit Node(Id id) : m_id{id}, m_name{""}, m_capacity{1}, m_transportCapacity{1} {}
explicit Node(Id id) : m_id{id}, m_name{""} {}
/// @brief Construct a new Node object with capacity 1
/// @param id The node's id
/// @param coords A std::pair containing the node's coordinates (lat, lon)
Node(Id id, std::pair<double, double> coords)
: m_id{id},
m_coords{std::move(coords)},
m_name{""},
m_capacity{1},
m_transportCapacity{1} {}
: m_id{id}, m_coords{std::move(coords)}, m_name{""} {}

Node(Node const& other)
: m_id{other.m_id},
m_coords{other.m_coords},
m_name{other.m_name},
m_capacity{other.m_capacity},
m_transportCapacity{other.m_transportCapacity} {};
: m_id{other.m_id}, m_coords{other.m_coords}, m_name{other.m_name} {};
virtual ~Node() = default;

Node& operator=(Node const& other) {
if (this != &other) {
m_id = other.m_id;
m_coords = other.m_coords;
m_name = other.m_name;
m_capacity = other.m_capacity;
m_transportCapacity = other.m_transportCapacity;
}
return *this;
}
Expand All @@ -74,18 +62,6 @@ namespace dsm {
/// @brief Set the node's name
/// @param name The node's name
void setName(const std::string& name) { m_name = name; }
/// @brief Set the node's capacity
/// @param capacity The node's capacity
virtual void setCapacity(Size capacity) { m_capacity = capacity; }
/// @brief Set the node's transport capacity
/// @param capacity The node's transport capacity
virtual void setTransportCapacity(int capacity) {
if (capacity < 1) {
Logger::error(std::format(
"The transport capacity of a node ({}) must be greater than 0.", capacity));
}
m_transportCapacity = capacity;
}
/// @brief Get the node's id
/// @return Id The node's id
Id id() const { return m_id; }
Expand All @@ -95,19 +71,6 @@ namespace dsm {
/// @brief Get the node's name
/// @return std::string The node's name
const std::string& name() const { return m_name; }
/// @brief Get the node's capacity
/// @return Size The node's capacity
Size capacity() const { return m_capacity; }
/// @brief Get the node's transport capacity
/// @return Size The node's transport capacity
int transportCapacity() const { return m_transportCapacity; }

virtual double density() const { return 0.; };
virtual bool isFull() const { return true; };

virtual bool isIntersection() const noexcept { return false; }
virtual bool isTrafficLight() const noexcept { return false; }
virtual bool isRoundabout() const noexcept { return false; }

virtual bool isStation() const noexcept { return false; }
};
Expand Down
9 changes: 1 addition & 8 deletions src/dsm/headers/Road.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include "Edge.hpp"

#include <memory>
#include <optional>
#include <string>

Expand All @@ -15,12 +16,6 @@ namespace dsm {
std::string m_name;

public:
/// @brief Construct a new Road object starting from an existing road
/// @details The new road has different id but same capacity, length, speed limit, and node pair as the
/// existing road.
/// @param Road The existing road
/// @param id The new road's id
Road(Id id, const Road&);
/// @brief Construct a new Road object
/// @param id The road's id
/// @param nodePair The road's node pair
Expand Down Expand Up @@ -65,8 +60,6 @@ namespace dsm {
/// @return std::string The name
std::string name() const;

virtual void addAgent(Id agentId) = 0;

virtual int nAgents() const = 0;
virtual int nMovingAgents() const = 0;
virtual int nExitingAgents() const = 0;
Expand Down
Loading
Loading