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 benchmark/Street/BenchStreet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
using Bench = sb::Bench<long long int>;

int main() {
Street street(0, 1000, 10., std::make_pair(0, 1));
Street street(0, std::make_pair(0, 1), 5000.);

Check notice

Code scanning / Cppcheck (reported by Codacy)

MISRA 12.3 rule Note test

MISRA 12.3 rule
Agent agent(0, 0, 0);
Bench b(1000);

Expand Down
5 changes: 0 additions & 5 deletions examples/slow_charge_rb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,7 @@ int main(int argc, char** argv) {
graph.importMatrix(IN_MATRIX, false);
graph.importCoordinates(IN_COORDS);
std::cout << "Setting street parameters..." << '\n';
for (const auto& [streetId, street] : graph.streetSet()) {
street->setTransportCapacity(1);
street->setMaxSpeed(13.9);
}
graph.buildAdj();
graph.normalizeStreetCapacities();

std::cout << "Number of nodes: " << graph.nNodes() << '\n';
std::cout << "Number of streets: " << graph.nEdges() << '\n';
Expand Down
5 changes: 0 additions & 5 deletions examples/slow_charge_tl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,7 @@ int main(int argc, char** argv) {
graph.importMatrix(IN_MATRIX, false);
graph.importCoordinates(IN_COORDS);
std::cout << "Setting street parameters..." << '\n';
for (const auto& [streetId, street] : graph.streetSet()) {
street->setTransportCapacity(1);
street->setMaxSpeed(13.9);
}
graph.buildAdj();
graph.normalizeStreetCapacities();
const auto dv = graph.adjMatrix().getDegreeVector();

// graph.addStreet(Street(100002, std::make_pair(0, 108)));
Expand Down
10 changes: 5 additions & 5 deletions examples/stalingrado.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,11 @@
Graph graph;

// Street(StreetId, Capacity, Length, vMax, (from, to))
Street s01{1, 2281 / 8, 2281., 13.9, std::make_pair(0, 1)};
Street s12{7, 118 / 8, 118., 13.9, std::make_pair(1, 2)};
Street s23{13, 222 / 8, 222., 13.9, std::make_pair(2, 3)};
Street s34{19, 1, 651., 13.9, std::make_pair(3, 4), 2};
Street::setMeanVehicleLength(8.);
Street s01{1, std::make_pair(0, 1), 2281.};

Check notice

Code scanning / Cppcheck (reported by Codacy)

MISRA 12.3 rule Note

MISRA 12.3 rule
Street s12{7, std::make_pair(1, 2), 118.};

Check notice

Code scanning / Cppcheck (reported by Codacy)

MISRA 12.3 rule Note

MISRA 12.3 rule
Street s23{13, std::make_pair(2, 3), 222.};

Check notice

Code scanning / Cppcheck (reported by Codacy)

MISRA 12.3 rule Note

MISRA 12.3 rule
Street s34{19, std::make_pair(3, 4), 651., 13.9, 2};

Check notice

Code scanning / Cppcheck (reported by Codacy)

MISRA 12.3 rule Note

MISRA 12.3 rule
// Viale Aldo Moro
auto& tl1 = graph.addNode<TrafficLight>(1, 132);
tl1.setCycle(s01.id(), dsm::Direction::ANY, {62, 0});
Expand All @@ -73,7 +74,6 @@
graph.addStreets(s01, s12, s23, s34);
graph.buildAdj();
graph.adjustNodeCapacities();
graph.normalizeStreetCapacities();
auto& spire = graph.makeSpireStreet(19);

std::cout << "Intersections: " << graph.nNodes() << '\n';
Expand Down
5 changes: 3 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 = 2;
static constexpr uint8_t DSM_VERSION_PATCH = 12;
static constexpr uint8_t DSM_VERSION_MINOR = 3;
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 All @@ -24,6 +24,7 @@
#include "headers/TrafficLight.hpp"
#include "headers/Roundabout.hpp"
#include "headers/SparseMatrix.hpp"
#include "headers/Edge.hpp"

Check notice

Code scanning / Cppcheck (reported by Codacy)

MISRA 20.1 rule Note

MISRA 20.1 rule
#include "headers/Street.hpp"
#include "headers/FirstOrderDynamics.hpp"
#include "utility/TypeTraits/is_node.hpp"
Expand Down
2 changes: 1 addition & 1 deletion src/dsm/headers/Dynamics.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ namespace dsm {
const std::map<Id, std::unique_ptr<agent_t>>& agents() const { return m_agents; }
/// @brief Get the number of agents currently in the simulation
/// @return Size The number of agents
const Size nAgents() const { return m_agents.size(); }
Size nAgents() const { return m_agents.size(); }
/// @brief Get the time
/// @return Time The time
Time time() const { return m_time; }
Expand Down
44 changes: 44 additions & 0 deletions src/dsm/headers/Edge.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#include "Edge.hpp"
#include "../utility/Logger.hpp"

#include <stdexcept>
#include <format>

namespace dsm {
Edge::Edge(Id id, std::pair<Id, Id> nodePair, int capacity, int transportCapacity)
: m_id(id),
m_nodePair(nodePair),
m_capacity{capacity},

Check notice

Code scanning / Cppcheck (reported by Codacy)

MISRA 12.3 rule Note

MISRA 12.3 rule
m_transportCapacity{transportCapacity} {
if (capacity < 1) {
throw std::invalid_argument(
buildLog(std::format("Edge capacity ({}) must be greater than 0.", capacity)));
}
if (transportCapacity < 1) {
throw std::invalid_argument(buildLog(std::format(
"Edge transport capacity ({}) must be greater than 0.", transportCapacity)));
}
}

void Edge::setCapacity(int capacity) {
if (capacity < 1) {
throw std::invalid_argument(
buildLog(std::format("Edge capacity ({}) must be greater than 0.", capacity)));
}
m_capacity = capacity;
}
void Edge::setTransportCapacity(int capacity) {
if (capacity < 1) {
throw std::invalid_argument(buildLog(
std::format("Edge transport capacity ({}) must be greater than 0.", capacity)));
}
m_transportCapacity = capacity;
}

Id Edge::id() const { return m_id; }
Id Edge::u() const { return m_nodePair.first; }
Id Edge::v() const { return m_nodePair.second; }
std::pair<Id, Id> Edge::nodePair() const { return m_nodePair; }

Check notice

Code scanning / Cppcheck (reported by Codacy)

MISRA 12.3 rule Note

MISRA 12.3 rule
int Edge::capacity() const { return m_capacity; }
int Edge::transportCapacity() const { return m_transportCapacity; }
}; // namespace dsm
29 changes: 29 additions & 0 deletions src/dsm/headers/Edge.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#pragma once

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

namespace dsm {
class Edge {
protected:
Id m_id;
std::pair<Id, Id> m_nodePair;
int m_capacity;
int m_transportCapacity;

public:
Edge(Id id, std::pair<Id, Id> nodePair, int capacity = 1, int transportCapacity = 1);

void setCapacity(int capacity);
void setTransportCapacity(int capacity);

Id id() const;
Id u() const;
Id v() const;
Comment on lines +21 to +22
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

u and v?

std::pair<Id, Id> nodePair() const;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Debatable but I'd say const&

int capacity() const;
int transportCapacity() const;

virtual bool isFull() const = 0;
};
}; // namespace dsm
41 changes: 15 additions & 26 deletions src/dsm/headers/Graph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,23 +126,13 @@
for (const auto& [streetId, _] : m_adjacency.getRow(nodeId, true)) {
value += m_streets[streetId]->nLanes() * m_streets[streetId]->transportCapacity();
}
m_nodes[nodeId]->setTransportCapacity(value);
m_nodes[nodeId]->setTransportCapacity(value == 0 ? 1 : value);

Check notice

Code scanning / Cppcheck (reported by Codacy)

MISRA 12.1 rule Note

MISRA 12.1 rule
if (m_nodes[nodeId]->capacity() == 0) {
m_nodes[nodeId]->setCapacity(value);
}
}
}

void Graph::normalizeStreetCapacities(double meanVehicleLength) {
m_maxAgentCapacity = 0;
for (const auto& [_, street] : m_streets) {
auto const maxCapacity{
static_cast<Size>(street->length() * street->nLanes() / meanVehicleLength)};
m_maxAgentCapacity += maxCapacity;
street->setCapacity(maxCapacity);
}
}

void Graph::importMatrix(const std::string& fileName, bool isAdj, double defaultSpeed) {
// check the file extension
std::string fileExt = fileName.substr(fileName.find_last_of(".") + 1);
Expand Down Expand Up @@ -172,11 +162,11 @@
if (!m_nodes.contains(dstId)) {
m_nodes.emplace(dstId, std::make_unique<Intersection>(dstId));
}
m_streets.emplace(index,
std::make_unique<Street>(index, std::make_pair(srcId, dstId)));
assert(index == srcId * n + dstId);
if (!isAdj) {
m_streets[index]->setLength(val);
if (isAdj) {
addEdge<Street>(index, std::make_pair(srcId, dstId));
} else {
addEdge<Street>(index, std::make_pair(srcId, dstId), val);

Check notice

Code scanning / Cppcheck (reported by Codacy)

MISRA 12.3 rule Note

MISRA 12.3 rule
}
m_streets[index]->setMaxSpeed(defaultSpeed);
}
Expand Down Expand Up @@ -218,11 +208,11 @@
if (!m_nodes.contains(dstId)) {
m_nodes.emplace(dstId, std::make_unique<Intersection>(dstId));
}
m_streets.emplace(
index, std::make_unique<Street>(index, std::make_pair(srcId, dstId)));
assert(index == srcId * n + dstId);
if (!isAdj) {
m_streets[index]->setLength(value);
if (isAdj) {
addEdge<Street>(index, std::make_pair(srcId, dstId));
} else {
addEdge<Street>(index, std::make_pair(srcId, dstId), value);

Check notice

Code scanning / Cppcheck (reported by Codacy)

MISRA 12.3 rule Note

MISRA 12.3 rule
}
m_streets[index]->setMaxSpeed(defaultSpeed);
}
Expand Down Expand Up @@ -374,11 +364,10 @@

Id streetId = std::stoul(sourceId) + std::stoul(targetId) * m_nodes.size();
addEdge<Street>(streetId,
std::stod(length) / 5,
std::stod(maxspeed),
std::stod(length),
std::make_pair(m_nodeMapping[std::stoul(sourceId)],
m_nodeMapping[std::stoul(targetId)]),
std::stod(length),
std::stod(maxspeed),
std::stoul(lanes),
name);
}
Expand All @@ -393,14 +382,14 @@
throw std::invalid_argument(buildLog("Cannot open file: " + path));
}
if (isAdj) {
file << m_adjacency.getRowDim() << '\t' << m_adjacency.getColDim() << '\n';
file << m_adjacency.getRowDim() << '\t' << m_adjacency.getColDim();

Check notice

Code scanning / Cppcheck (reported by Codacy)

MISRA 10.1 rule Note

MISRA 10.1 rule
for (const auto& [id, value] : m_adjacency) {
file << id << '\t' << value << '\n';
file << '\n' << id << '\t' << value;

Check notice

Code scanning / Cppcheck (reported by Codacy)

MISRA 10.1 rule Note

MISRA 10.1 rule
}
} else {
file << m_adjacency.getRowDim() << " " << m_adjacency.getColDim() << '\n';
file << m_adjacency.getRowDim() << '\t' << m_adjacency.getColDim();

Check notice

Code scanning / Cppcheck (reported by Codacy)

MISRA 10.1 rule Note

MISRA 10.1 rule
for (const auto& [id, street] : m_streets) {
file << id << '\t' << street->length() << '\n';
file << '\n' << id << '\t' << street->length();

Check notice

Code scanning / Cppcheck (reported by Codacy)

MISRA 10.1 rule Note

MISRA 10.1 rule
}
}
}
Expand Down
5 changes: 0 additions & 5 deletions src/dsm/headers/Graph.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,11 +110,6 @@ namespace dsm {
/// @brief Adjust the nodes' transport capacity
/// @details The nodes' capacity is adjusted using the graph's streets transport capacity, which may vary basing on the number of lanes. The node capacity will be set to the sum of the incoming streets' transport capacity.
void adjustNodeCapacities();
/// @brief Normalize the streets' capacities
/// @param meanVehicleLength The mean vehicle length
/// @details The streets' capacities are normalized using the mean vehicle length following the formula:
/// \f$ \text{capacity} = \frac{\text{length} * \text{nLanes}}{\text{meanVehicleLength}} \f$
void normalizeStreetCapacities(double meanVehicleLength = 5.);

/// @brief Import the graph's adjacency matrix from a file.
/// If the file is not of a supported format, it will read the file as a matrix with the first two elements being
Expand Down
12 changes: 9 additions & 3 deletions src/dsm/headers/Node.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ namespace dsm {
std::optional<std::pair<double, double>> m_coords;
std::string m_name;
Size m_capacity;
Size m_transportCapacity;
int m_transportCapacity;

public:
/// @brief Construct a new Node object with capacity 1
Expand Down Expand Up @@ -79,7 +79,13 @@ namespace dsm {
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(Size capacity) { m_transportCapacity = capacity; }
virtual void setTransportCapacity(int capacity) {
if (capacity < 1) {
throw std::invalid_argument(buildLog(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 @@ -94,7 +100,7 @@ namespace dsm {
Size capacity() const { return m_capacity; }
/// @brief Get the node's transport capacity
/// @return Size The node's transport capacity
Size transportCapacity() const { return m_transportCapacity; }
int transportCapacity() const { return m_transportCapacity; }

virtual double density() const = 0;
virtual bool isFull() const = 0;
Expand Down
1 change: 0 additions & 1 deletion src/dsm/headers/RoadDynamics.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,6 @@ namespace dsm {
if (std::abs(deltaAngle) < std::numbers::pi) {
// Lanes are counted as 0 is the far right lane
if (std::abs(deltaAngle) < std::numbers::pi / 4) {
auto const dstNodeId = pNextStreet->nodePair().first;
std::vector<double> weights;
for (auto const& queue : street->exitQueues()) {
weights.push_back(1. / (queue.size() + 1));
Expand Down
Loading
Loading