Skip to content

Commit 155add4

Browse files
authored
Simplify Streets class (#243)
* Add Edge base class * Bugfix: capacity sometimes drop to 0 * Resolve deprecation warning * Remove unused functions in Street * Remove unused things * Add u, v getters * Less constructors * Remove now useless function * Adjust tests * Bugfix * Compiling examples * Fix some warnings * Bugfix
1 parent a16cf3e commit 155add4

20 files changed

+384
-484
lines changed

benchmark/Street/BenchStreet.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ using SparseMatrix = dsm::SparseMatrix<bool>;
1313
using Bench = sb::Bench<long long int>;
1414

1515
int main() {
16-
Street street(0, 1000, 10., std::make_pair(0, 1));
16+
Street street(0, std::make_pair(0, 1), 5000.);
1717
Agent agent(0, 0, 0);
1818
Bench b(1000);
1919

examples/slow_charge_rb.cpp

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -83,12 +83,7 @@ int main(int argc, char** argv) {
8383
graph.importMatrix(IN_MATRIX, false);
8484
graph.importCoordinates(IN_COORDS);
8585
std::cout << "Setting street parameters..." << '\n';
86-
for (const auto& [streetId, street] : graph.streetSet()) {
87-
street->setTransportCapacity(1);
88-
street->setMaxSpeed(13.9);
89-
}
9086
graph.buildAdj();
91-
graph.normalizeStreetCapacities();
9287

9388
std::cout << "Number of nodes: " << graph.nNodes() << '\n';
9489
std::cout << "Number of streets: " << graph.nEdges() << '\n';

examples/slow_charge_tl.cpp

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -90,12 +90,7 @@ int main(int argc, char** argv) {
9090
graph.importMatrix(IN_MATRIX, false);
9191
graph.importCoordinates(IN_COORDS);
9292
std::cout << "Setting street parameters..." << '\n';
93-
for (const auto& [streetId, street] : graph.streetSet()) {
94-
street->setTransportCapacity(1);
95-
street->setMaxSpeed(13.9);
96-
}
9793
graph.buildAdj();
98-
graph.normalizeStreetCapacities();
9994
const auto dv = graph.adjMatrix().getDegreeVector();
10095

10196
// graph.addStreet(Street(100002, std::make_pair(0, 108)));

examples/stalingrado.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,11 @@ int main() {
4949
Graph graph;
5050

5151
// Street(StreetId, Capacity, Length, vMax, (from, to))
52-
Street s01{1, 2281 / 8, 2281., 13.9, std::make_pair(0, 1)};
53-
Street s12{7, 118 / 8, 118., 13.9, std::make_pair(1, 2)};
54-
Street s23{13, 222 / 8, 222., 13.9, std::make_pair(2, 3)};
55-
Street s34{19, 1, 651., 13.9, std::make_pair(3, 4), 2};
52+
Street::setMeanVehicleLength(8.);
53+
Street s01{1, std::make_pair(0, 1), 2281.};
54+
Street s12{7, std::make_pair(1, 2), 118.};
55+
Street s23{13, std::make_pair(2, 3), 222.};
56+
Street s34{19, std::make_pair(3, 4), 651., 13.9, 2};
5657
// Viale Aldo Moro
5758
auto& tl1 = graph.addNode<TrafficLight>(1, 132);
5859
tl1.setCycle(s01.id(), dsm::Direction::ANY, {62, 0});
@@ -73,7 +74,6 @@ int main() {
7374
graph.addStreets(s01, s12, s23, s34);
7475
graph.buildAdj();
7576
graph.adjustNodeCapacities();
76-
graph.normalizeStreetCapacities();
7777
auto& spire = graph.makeSpireStreet(19);
7878

7979
std::cout << "Intersections: " << graph.nNodes() << '\n';

src/dsm/dsm.hpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
#include <format>
66

77
static constexpr uint8_t DSM_VERSION_MAJOR = 2;
8-
static constexpr uint8_t DSM_VERSION_MINOR = 2;
9-
static constexpr uint8_t DSM_VERSION_PATCH = 12;
8+
static constexpr uint8_t DSM_VERSION_MINOR = 3;
9+
static constexpr uint8_t DSM_VERSION_PATCH = 0;
1010

1111
static auto const DSM_VERSION =
1212
std::format("{}.{}.{}", DSM_VERSION_MAJOR, DSM_VERSION_MINOR, DSM_VERSION_PATCH);
@@ -24,6 +24,7 @@ namespace dsm {
2424
#include "headers/TrafficLight.hpp"
2525
#include "headers/Roundabout.hpp"
2626
#include "headers/SparseMatrix.hpp"
27+
#include "headers/Edge.hpp"
2728
#include "headers/Street.hpp"
2829
#include "headers/FirstOrderDynamics.hpp"
2930
#include "utility/TypeTraits/is_node.hpp"

src/dsm/headers/Dynamics.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ namespace dsm {
221221
const std::map<Id, std::unique_ptr<agent_t>>& agents() const { return m_agents; }
222222
/// @brief Get the number of agents currently in the simulation
223223
/// @return Size The number of agents
224-
const Size nAgents() const { return m_agents.size(); }
224+
Size nAgents() const { return m_agents.size(); }
225225
/// @brief Get the time
226226
/// @return Time The time
227227
Time time() const { return m_time; }

src/dsm/headers/Edge.cpp

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#include "Edge.hpp"
2+
#include "../utility/Logger.hpp"
3+
4+
#include <stdexcept>
5+
#include <format>
6+
7+
namespace dsm {
8+
Edge::Edge(Id id, std::pair<Id, Id> nodePair, int capacity, int transportCapacity)
9+
: m_id(id),
10+
m_nodePair(nodePair),
11+
m_capacity{capacity},
12+
m_transportCapacity{transportCapacity} {
13+
if (capacity < 1) {
14+
throw std::invalid_argument(
15+
buildLog(std::format("Edge capacity ({}) must be greater than 0.", capacity)));
16+
}
17+
if (transportCapacity < 1) {
18+
throw std::invalid_argument(buildLog(std::format(
19+
"Edge transport capacity ({}) must be greater than 0.", transportCapacity)));
20+
}
21+
}
22+
23+
void Edge::setCapacity(int capacity) {
24+
if (capacity < 1) {
25+
throw std::invalid_argument(
26+
buildLog(std::format("Edge capacity ({}) must be greater than 0.", capacity)));
27+
}
28+
m_capacity = capacity;
29+
}
30+
void Edge::setTransportCapacity(int capacity) {
31+
if (capacity < 1) {
32+
throw std::invalid_argument(buildLog(
33+
std::format("Edge transport capacity ({}) must be greater than 0.", capacity)));
34+
}
35+
m_transportCapacity = capacity;
36+
}
37+
38+
Id Edge::id() const { return m_id; }
39+
Id Edge::u() const { return m_nodePair.first; }
40+
Id Edge::v() const { return m_nodePair.second; }
41+
std::pair<Id, Id> Edge::nodePair() const { return m_nodePair; }
42+
int Edge::capacity() const { return m_capacity; }
43+
int Edge::transportCapacity() const { return m_transportCapacity; }
44+
}; // namespace dsm

src/dsm/headers/Edge.hpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#pragma once
2+
3+
#include <utility>
4+
#include "../utility/Typedef.hpp"
5+
6+
namespace dsm {
7+
class Edge {
8+
protected:
9+
Id m_id;
10+
std::pair<Id, Id> m_nodePair;
11+
int m_capacity;
12+
int m_transportCapacity;
13+
14+
public:
15+
Edge(Id id, std::pair<Id, Id> nodePair, int capacity = 1, int transportCapacity = 1);
16+
17+
void setCapacity(int capacity);
18+
void setTransportCapacity(int capacity);
19+
20+
Id id() const;
21+
Id u() const;
22+
Id v() const;
23+
std::pair<Id, Id> nodePair() const;
24+
int capacity() const;
25+
int transportCapacity() const;
26+
27+
virtual bool isFull() const = 0;
28+
};
29+
}; // namespace dsm

src/dsm/headers/Graph.cpp

Lines changed: 15 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -126,23 +126,13 @@ namespace dsm {
126126
for (const auto& [streetId, _] : m_adjacency.getRow(nodeId, true)) {
127127
value += m_streets[streetId]->nLanes() * m_streets[streetId]->transportCapacity();
128128
}
129-
m_nodes[nodeId]->setTransportCapacity(value);
129+
m_nodes[nodeId]->setTransportCapacity(value == 0 ? 1 : value);
130130
if (m_nodes[nodeId]->capacity() == 0) {
131131
m_nodes[nodeId]->setCapacity(value);
132132
}
133133
}
134134
}
135135

136-
void Graph::normalizeStreetCapacities(double meanVehicleLength) {
137-
m_maxAgentCapacity = 0;
138-
for (const auto& [_, street] : m_streets) {
139-
auto const maxCapacity{
140-
static_cast<Size>(street->length() * street->nLanes() / meanVehicleLength)};
141-
m_maxAgentCapacity += maxCapacity;
142-
street->setCapacity(maxCapacity);
143-
}
144-
}
145-
146136
void Graph::importMatrix(const std::string& fileName, bool isAdj, double defaultSpeed) {
147137
// check the file extension
148138
std::string fileExt = fileName.substr(fileName.find_last_of(".") + 1);
@@ -172,11 +162,11 @@ namespace dsm {
172162
if (!m_nodes.contains(dstId)) {
173163
m_nodes.emplace(dstId, std::make_unique<Intersection>(dstId));
174164
}
175-
m_streets.emplace(index,
176-
std::make_unique<Street>(index, std::make_pair(srcId, dstId)));
177165
assert(index == srcId * n + dstId);
178-
if (!isAdj) {
179-
m_streets[index]->setLength(val);
166+
if (isAdj) {
167+
addEdge<Street>(index, std::make_pair(srcId, dstId));
168+
} else {
169+
addEdge<Street>(index, std::make_pair(srcId, dstId), val);
180170
}
181171
m_streets[index]->setMaxSpeed(defaultSpeed);
182172
}
@@ -218,11 +208,11 @@ namespace dsm {
218208
if (!m_nodes.contains(dstId)) {
219209
m_nodes.emplace(dstId, std::make_unique<Intersection>(dstId));
220210
}
221-
m_streets.emplace(
222-
index, std::make_unique<Street>(index, std::make_pair(srcId, dstId)));
223211
assert(index == srcId * n + dstId);
224-
if (!isAdj) {
225-
m_streets[index]->setLength(value);
212+
if (isAdj) {
213+
addEdge<Street>(index, std::make_pair(srcId, dstId));
214+
} else {
215+
addEdge<Street>(index, std::make_pair(srcId, dstId), value);
226216
}
227217
m_streets[index]->setMaxSpeed(defaultSpeed);
228218
}
@@ -374,11 +364,10 @@ namespace dsm {
374364

375365
Id streetId = std::stoul(sourceId) + std::stoul(targetId) * m_nodes.size();
376366
addEdge<Street>(streetId,
377-
std::stod(length) / 5,
378-
std::stod(maxspeed),
379-
std::stod(length),
380367
std::make_pair(m_nodeMapping[std::stoul(sourceId)],
381368
m_nodeMapping[std::stoul(targetId)]),
369+
std::stod(length),
370+
std::stod(maxspeed),
382371
std::stoul(lanes),
383372
name);
384373
}
@@ -393,14 +382,14 @@ namespace dsm {
393382
throw std::invalid_argument(buildLog("Cannot open file: " + path));
394383
}
395384
if (isAdj) {
396-
file << m_adjacency.getRowDim() << '\t' << m_adjacency.getColDim() << '\n';
385+
file << m_adjacency.getRowDim() << '\t' << m_adjacency.getColDim();
397386
for (const auto& [id, value] : m_adjacency) {
398-
file << id << '\t' << value << '\n';
387+
file << '\n' << id << '\t' << value;
399388
}
400389
} else {
401-
file << m_adjacency.getRowDim() << " " << m_adjacency.getColDim() << '\n';
390+
file << m_adjacency.getRowDim() << '\t' << m_adjacency.getColDim();
402391
for (const auto& [id, street] : m_streets) {
403-
file << id << '\t' << street->length() << '\n';
392+
file << '\n' << id << '\t' << street->length();
404393
}
405394
}
406395
}

src/dsm/headers/Graph.hpp

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -110,11 +110,6 @@ namespace dsm {
110110
/// @brief Adjust the nodes' transport capacity
111111
/// @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.
112112
void adjustNodeCapacities();
113-
/// @brief Normalize the streets' capacities
114-
/// @param meanVehicleLength The mean vehicle length
115-
/// @details The streets' capacities are normalized using the mean vehicle length following the formula:
116-
/// \f$ \text{capacity} = \frac{\text{length} * \text{nLanes}}{\text{meanVehicleLength}} \f$
117-
void normalizeStreetCapacities(double meanVehicleLength = 5.);
118113

119114
/// @brief Import the graph's adjacency matrix from a file.
120115
/// If the file is not of a supported format, it will read the file as a matrix with the first two elements being

0 commit comments

Comments
 (0)