Skip to content
Closed
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
14 changes: 7 additions & 7 deletions src/dsm/headers/Intersection.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@

#pragma once

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

#include <map>
#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::set<Id>
Expand All @@ -26,13 +26,13 @@
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.

virtual ~Intersection() = default;

Expand Down Expand Up @@ -69,11 +69,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();

Check warning on line 72 in src/dsm/headers/Intersection.hpp

View check run for this annotation

Codecov / codecov/patch

src/dsm/headers/Intersection.hpp#L72

Added line #L72 was not covered by tests
}
/// @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 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: 5 additions & 4 deletions src/dsm/headers/RoadDynamics.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,7 @@ namespace dsm {
void m_evolveStreet(const std::unique_ptr<Street>& pStreet, bool reinsert_agents);
/// @brief If possible, removes one agent from the node, putting it on the next street.
/// @param pNode A std::unique_ptr to the node
/// @return bool True if the agent has been moved, false otherwise
bool m_evolveNode(const std::unique_ptr<Node>& pNode);
bool m_evolveNode(std::unique_ptr<RoadJunction> const& pNode);
/// @brief Evolve the agents.
/// @details Puts all new agents on a street, if possible, decrements all delays
/// and increments all travel times.
Expand Down Expand Up @@ -643,7 +642,7 @@ namespace dsm {

template <typename delay_t>
requires(is_numeric_v<delay_t>)
bool RoadDynamics<delay_t>::m_evolveNode(const std::unique_ptr<Node>& pNode) {
bool RoadDynamics<delay_t>::m_evolveNode(std::unique_ptr<RoadJunction> const& pNode) {
if (pNode->isIntersection()) {
auto& intersection = dynamic_cast<Intersection&>(*pNode);
if (intersection.agents().empty()) {
Expand Down Expand Up @@ -1134,7 +1133,9 @@ namespace dsm {
this->graph().nodes().cend(),
[&](const auto& pair) {
for (auto i = 0; i < pair.second->transportCapacity(); ++i) {
m_evolveNode(pair.second);
if (!m_evolveNode(pair.second)) {
break;
}
}
if (pair.second->isTrafficLight()) {
auto& tl = dynamic_cast<TrafficLight&>(*pair.second);
Expand Down
47 changes: 47 additions & 0 deletions src/dsm/headers/RoadJunction.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#pragma once

#include "Node.hpp"

#include "../utility/Typedef.hpp"

namespace dsm {
class RoadJunction : public Node {
Size m_capacity;
double m_transportCapacity;

public:
explicit RoadJunction(Id id);
RoadJunction(Id id, std::pair<double, double> coords);
RoadJunction(RoadJunction const& other);

RoadJunction& operator=(RoadJunction const& other) {
if (this != &other) {
Node::operator=(other);

Check notice

Code scanning / Cppcheck (reported by Codacy)

MISRA 17.7 rule Note

MISRA 17.7 rule
m_capacity = other.m_capacity;
m_transportCapacity = other.m_transportCapacity;
}
return *this;
}

/// @brief Set the junction's capacity
/// @param capacity The junction's capacity
virtual void setCapacity(Size capacity);
/// @brief Set the junction's transport capacity
/// @param capacity The junction's transport capacity
void setTransportCapacity(double capacity);

/// @brief Get the junction's capacity
/// @return Size The junction's capacity
Size capacity() const;
/// @brief Get the junction's transport capacity
/// @return Size The junction's transport capacity
double transportCapacity() const;

virtual double density() const;
virtual bool isFull() const;

virtual bool isIntersection() const noexcept;
virtual bool isTrafficLight() const noexcept;
virtual bool isRoundabout() const noexcept;
};
} // namespace dsm
5 changes: 3 additions & 2 deletions src/dsm/headers/RoadNetwork.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include "AdjacencyMatrix.hpp"
#include "DijkstraWeights.hpp"
#include "Network.hpp"
#include "RoadJunction.hpp"
#include "Intersection.hpp"
#include "TrafficLight.hpp"
#include "Roundabout.hpp"
Expand All @@ -45,7 +46,7 @@
/// @brief The RoadNetwork class represents a graph in the network.
/// @tparam Id, The type of the graph's id. It must be an unsigned integral type.
/// @tparam Size, The type of the graph's capacity. It must be an unsigned integral type.
class RoadNetwork : public Network<Node, Street> {
class RoadNetwork : public Network<RoadJunction, Street> {

Check notice

Code scanning / Cppcheck (reported by Codacy)

MISRA 12.3 rule Note

MISRA 12.3 rule

Check warning

Code scanning / Cppcheck (reported by Codacy)

The class 'RoadNetwork' does not have a constructor although it has private member variables. Warning

The class 'RoadNetwork' does not have a constructor although it has private member variables.
private:
std::unordered_map<std::string, Id> m_nodeMapping;
std::vector<Id> m_inputNodes;
Expand Down Expand Up @@ -87,7 +88,7 @@
RoadNetwork& operator=(const RoadNetwork& other) {
std::for_each(other.m_nodes.begin(), other.m_nodes.end(), [this](const auto& pair) {
this->m_nodes.insert_or_assign(pair.first,
std::unique_ptr<Node>(pair.second.get()));
std::unique_ptr<RoadJunction>(pair.second.get()));
});
std::for_each(other.m_edges.begin(), other.m_edges.end(), [this](const auto& pair) {
this->m_edges.insert_or_assign(pair.first,
Expand Down
14 changes: 7 additions & 7 deletions src/dsm/headers/Roundabout.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,28 @@

#pragma once

#include "Node.hpp"
#include "RoadJunction.hpp"
#include "../utility/queue.hpp"

namespace dsm {
/// @brief The Roundabout class represents a roundabout node in the network.
/// @tparam Id The type of the node's id
/// @tparam Size The type of the node's capacity
class Roundabout : public Node {
class Roundabout : public RoadJunction {
protected:
dsm::queue<Id> m_agents;

public:
/// @brief Construct a new Roundabout object
/// @param id The node's id
explicit Roundabout(Id id) : Node{id} {};
explicit Roundabout(Id id) : RoadJunction{id} {};
/// @brief Construct a new Roundabout object
/// @param id The node's id
/// @param coords A std::pair containing the node's coordinates
Roundabout(Id id, std::pair<double, double> coords) : Node{id, coords} {};
Roundabout(Id id, std::pair<double, double> coords) : RoadJunction{id, coords} {};
/// @brief Construct a new Roundabout object
/// @param node An Intersection object
Roundabout(const Node& node);
Roundabout(const RoadJunction& node);

Check warning

Code scanning / Cppcheck (reported by Codacy)

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

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

~Roundabout() = default;

Expand All @@ -45,11 +45,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();

Check warning on line 48 in src/dsm/headers/Roundabout.hpp

View check run for this annotation

Codecov / codecov/patch

src/dsm/headers/Roundabout.hpp#L48

Added line #L48 was not covered by tests
}
/// @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 Returns true if the node is a roundabout
/// @return bool True if the node is a roundabout
bool isRoundabout() const noexcept override { return true; }
Expand Down
10 changes: 5 additions & 5 deletions src/dsm/headers/Station.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@

#pragma once

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

#include <map>

namespace dsm {
class Station : public Node {
class Station : public RoadJunction {
private:
Delay m_managementTime;
std::multimap<train_t, Id, std::greater<train_t>> m_trains;
Expand All @@ -29,7 +29,7 @@
/// @brief Construct a new Station object
/// @param node A Node object representing the station
/// @param managementTime The time it takes between two train departures/arrivals
Station(Node const& node, Delay managementTime);
Station(RoadJunction const& node, Delay managementTime);
/// @brief Construct a new Station object by copying another Station object
/// @param other The Station object to copy
Station(Station const& other);
Expand All @@ -45,10 +45,10 @@
Delay managementTime() const;
/// @brief Get the train density of the station
/// @return The train density of the station
double density() const final;
double density() const;

Check warning

Code scanning / Cppcheck (reported by Codacy)

The function 'density' overrides a function in a base class but is not marked with a 'override' specifier. Warning

The function 'density' overrides a function in a base class but is not marked with a 'override' specifier.
/// @brief Check if the station is full
/// @return True if the station is full, false otherwise
bool isFull() const final;
bool isFull() const;

Check warning

Code scanning / Cppcheck (reported by Codacy)

The function 'isFull' overrides a function in a base class but is not marked with a 'override' specifier. Warning

The function 'isFull' overrides a function in a base class but is not marked with a 'override' specifier.
/// @brief Check if the node is a station
/// @return True
bool isStation() const noexcept final;
Expand Down
2 changes: 1 addition & 1 deletion src/dsm/headers/TrafficLight.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
TrafficLight(Id id, Delay cycleTime, std::pair<double, double> coords)
: Intersection{id, std::move(coords)}, m_cycleTime{cycleTime}, m_counter{0} {}

TrafficLight(Node const& node, Delay const cycleTime, Delay const counter = 0)
TrafficLight(RoadJunction const& node, Delay const cycleTime, Delay const counter = 0)

Check notice

Code scanning / Cppcheck (reported by Codacy)

MISRA 17.8 rule Note

MISRA 17.8 rule
: Intersection{node}, m_cycleTime{cycleTime}, m_counter{counter} {}

~TrafficLight() = default;
Expand Down
2 changes: 1 addition & 1 deletion src/dsm/sources/Intersection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace dsm {
capacity,
m_agents.size())));
}
Node::setCapacity(capacity);
RoadJunction::setCapacity(capacity);
}

void Intersection::addAgent(double angle, Id agentId) {
Expand Down
26 changes: 26 additions & 0 deletions src/dsm/sources/RoadJunction.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#include "../headers/RoadJunction.hpp"

namespace dsm {
RoadJunction::RoadJunction(Id id) : Node(id), m_capacity{1}, m_transportCapacity{1.} {}

Check notice

Code scanning / Cppcheck (reported by Codacy)

MISRA 12.3 rule Note

MISRA 12.3 rule
RoadJunction::RoadJunction(Id id, std::pair<double, double> coords)
: Node(id, coords), m_capacity{1}, m_transportCapacity{1.} {}

Check notice

Code scanning / Cppcheck (reported by Codacy)

MISRA 12.3 rule Note

MISRA 12.3 rule
RoadJunction::RoadJunction(RoadJunction const& other)
: Node(other),
m_capacity{other.m_capacity},

Check notice

Code scanning / Cppcheck (reported by Codacy)

MISRA 12.3 rule Note

MISRA 12.3 rule
m_transportCapacity{other.m_transportCapacity} {}

void RoadJunction::setCapacity(Size capacity) { m_capacity = capacity; }
void RoadJunction::setTransportCapacity(double capacity) {
assert(capacity > 0.);
m_transportCapacity = capacity;
}

Size RoadJunction::capacity() const { return m_capacity; }
double RoadJunction::transportCapacity() const { return m_transportCapacity; }
double RoadJunction::density() const { return 0.; }
bool RoadJunction::isFull() const { return true; }

Check warning on line 21 in src/dsm/sources/RoadJunction.cpp

View check run for this annotation

Codecov / codecov/patch

src/dsm/sources/RoadJunction.cpp#L20-L21

Added lines #L20 - L21 were not covered by tests

bool RoadJunction::isIntersection() const noexcept { return false; }
bool RoadJunction::isTrafficLight() const noexcept { return false; }
bool RoadJunction::isRoundabout() const noexcept { return false; }
} // namespace dsm
8 changes: 4 additions & 4 deletions src/dsm/sources/RoadNetwork.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,9 @@
}

void RoadNetwork::adjustNodeCapacities() {
int16_t value;
double value;

Check warning

Code scanning / Cppcheck (reported by Codacy)

The scope of the variable 'value' can be reduced. Warning

The scope of the variable 'value' can be reduced.
for (Id nodeId = 0; nodeId < m_nodes.size(); ++nodeId) {
value = 0;
value = 0.;
auto const N{nNodes()};
for (const auto& sourceId : m_adjacencyMatrix.getCol(nodeId)) {
auto const streetId{sourceId * N + nodeId};
Expand All @@ -132,13 +132,13 @@
}
auto const& pNode{m_nodes.at(nodeId)};
pNode->setCapacity(value);
value = 0;
value = 0.;
for (const auto& targetId : m_adjacencyMatrix.getRow(nodeId)) {
auto const streetId{nodeId * N + targetId};
auto const& pStreet{m_edges.at(streetId)};
value += pStreet->nLanes() * pStreet->transportCapacity();
}
pNode->setTransportCapacity(value == 0 ? 1 : value);
pNode->setTransportCapacity(value == 0. ? 1. : value);

Check notice

Code scanning / Cppcheck (reported by Codacy)

MISRA 12.1 rule Note

MISRA 12.1 rule
if (pNode->capacity() == 0) {
pNode->setCapacity(value);
}
Expand Down
Loading
Loading