Skip to content

Commit 8592bb3

Browse files
authored
Enhance Graph adders (#236)
* New addNode function * add addEdge function * Fix benchmarks * Update version * Enhance docs
1 parent 67a06ae commit 8592bb3

File tree

6 files changed

+62
-63
lines changed

6 files changed

+62
-63
lines changed

benchmark/Graph/BenchGraph.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,12 @@ int main() {
2020
Bench b1(n_rep);
2121

2222
std::cout << "Benchmarking addNode\n";
23-
Intersection n1(std::rand());
24-
b1.benchmark([&g1](const Intersection& node) -> void { g1.addNode(node); }, n1);
23+
b1.benchmark([&g1]() -> void { g1.addNode<Intersection>(std::rand()); });
2524
b1.print();
2625
std::cout << "Benchmarking addNodes overhead for a single node\n";
27-
n1 = Intersection(std::rand());
28-
b1.benchmark([&g1](const Intersection& node) -> void { g1.addNodes(node); }, n1);
29-
b1.print();
26+
// n1 = Intersection(std::rand());
27+
// b1.benchmark([&g1](const Intersection& node) -> void { g1.addNodes(node); }, n1);
28+
// b1.print();
3029

3130
// std::cout << "Benchmarking addStreet\n";
3231
// Street s1(std::rand(), std::make_pair(0, 1));

examples/stalingrado.cpp

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -46,34 +46,30 @@ int main() {
4646
const auto MAX_TIME{static_cast<Unit>(timeUnit * vehiclesToInsert.size())};
4747

4848
// Create the graph
49+
Graph graph;
4950

5051
// Street(StreetId, Capacity, Length, vMax, (from, to))
5152
Street s01{1, 2281 / 8, 2281., 13.9, std::make_pair(0, 1)};
5253
Street s12{7, 118 / 8, 118., 13.9, std::make_pair(1, 2)};
5354
Street s23{13, 222 / 8, 222., 13.9, std::make_pair(2, 3)};
5455
Street s34{19, 1, 651., 13.9, std::make_pair(3, 4), 2};
5556
// Viale Aldo Moro
56-
TrafficLight tl1{1, 132};
57+
auto& tl1 = graph.addNode<TrafficLight>(1, 132);
5758
tl1.setCycle(s01.id(), dsm::Direction::ANY, {62, 0});
5859
tl1.setCapacity(1);
5960
// Via Donato Creti
60-
TrafficLight tl2{2, 141};
61+
auto& tl2 = graph.addNode<TrafficLight>(2, 141);
6162
tl2.setCycle(s12.id(), dsm::Direction::ANY, {72, 0});
6263
tl2.setCapacity(1);
6364
// Via del Lavoro
64-
TrafficLight tl3{3, 138};
65+
auto& tl3 = graph.addNode<TrafficLight>(3, 138);
6566
tl3.setCycle(s23.id(), dsm::Direction::ANY, {88, 0});
6667
tl3.setCapacity(1);
6768
// Viali
68-
TrafficLight tl4{4, 131};
69+
auto& tl4 = graph.addNode<TrafficLight>(4, 131);
6970
tl4.setCycle(s34.id(), dsm::Direction::ANY, {81, 0});
7071
tl4.setCapacity(1);
7172

72-
Graph graph;
73-
graph.addNode(std::make_unique<TrafficLight>(tl1));
74-
graph.addNode(std::make_unique<TrafficLight>(tl2));
75-
graph.addNode(std::make_unique<TrafficLight>(tl3));
76-
graph.addNode(std::make_unique<TrafficLight>(tl4));
7773
graph.addStreets(s01, s12, s23, s34);
7874
graph.buildAdj();
7975
graph.adjustNodeCapacities();

src/dsm/dsm.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
static constexpr uint8_t DSM_VERSION_MAJOR = 2;
88
static constexpr uint8_t DSM_VERSION_MINOR = 2;
9-
static constexpr uint8_t DSM_VERSION_PATCH = 7;
9+
static constexpr uint8_t DSM_VERSION_PATCH = 8;
1010

1111
static auto const DSM_VERSION =
1212
std::format("{}.{}.{}", DSM_VERSION_MAJOR, DSM_VERSION_MINOR, DSM_VERSION_PATCH);

src/dsm/headers/Graph.cpp

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -425,10 +425,6 @@ namespace dsm {
425425
m_nodes.emplace(std::make_pair(node->id(), std::move(node)));
426426
}
427427

428-
void Graph::addNode(const Intersection& node) {
429-
m_nodes.emplace(std::make_pair(node.id(), std::make_unique<Intersection>(node)));
430-
}
431-
432428
TrafficLight& Graph::makeTrafficLight(Id const nodeId,
433429
Delay const cycleTime,
434430
Delay const counter) {
@@ -468,7 +464,7 @@ namespace dsm {
468464
return dynamic_cast<SpireStreet&>(*pStreet);
469465
}
470466

471-
void Graph::addStreet(std::shared_ptr<Street> street) {
467+
void Graph::addStreet(std::unique_ptr<Street> street) {
472468
if (m_streets.contains(street->id())) {
473469
throw std::invalid_argument(
474470
buildLog(std::format("Street with id {} already exists.", street->id())));
@@ -483,7 +479,7 @@ namespace dsm {
483479
m_nodes.emplace(dstId, std::make_unique<Intersection>(dstId));
484480
}
485481
// emplace street
486-
m_streets.emplace(street->id(), street.get());
482+
m_streets.emplace(std::make_pair(street->id(), std::move(street)));
487483
}
488484

489485
void Graph::addStreet(const Street& street) {

src/dsm/headers/Graph.hpp

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -155,9 +155,14 @@ namespace dsm {
155155
/// @brief Add a node to the graph
156156
/// @param node A std::unique_ptr to the node to add
157157
void addNode(std::unique_ptr<Node> node);
158-
/// @brief Add a node to the graph
159-
/// @param node A reference to the node to add
160-
void addNode(const Intersection& node);
158+
/// @brief Add a node of type node_t to the graph
159+
/// @param id The node's id
160+
/// @param args The node's arguments to forward to the constructor
161+
/// @return A reference to the added node
162+
template <typename node_t, typename... TArgs>
163+
requires(std::is_base_of_v<Node, node_t>,
164+
std::constructible_from<node_t, Id, TArgs...>)
165+
node_t& addNode(Id id, TArgs&&... args);
161166

162167
template <typename... Tn>
163168
requires(is_node_v<std::remove_reference_t<Tn>> && ...)
@@ -194,9 +199,17 @@ namespace dsm {
194199
/// @throws std::invalid_argument if the node does not exist
195200
Station& makeStation(Id nodeId, const unsigned int managementTime);
196201

202+
/// @brief Add an edge of type edge_t to the graph
203+
/// @param edge A std::unique_ptr to the edge to add
204+
/// @param args The edge's arguments to forward to the constructor
205+
/// @return A reference to the added edge
206+
template <typename edge_t, typename... TArgs>
207+
requires(std::is_base_of_v<Street, edge_t>,
208+
std::constructible_from<edge_t, Id, TArgs...>)
209+
edge_t& addEdge(Id id, TArgs&&... args);
197210
/// @brief Add a street to the graph
198-
/// @param street A std::shared_ptr to the street to add
199-
void addStreet(std::shared_ptr<Street> street);
211+
/// @param street A std::unique_ptr to the street to add
212+
void addStreet(std::unique_ptr<Street> street);
200213
/// @brief Add a street to the graph
201214
/// @param street A reference to the street to add
202215
void addStreet(const Street& street);
@@ -271,10 +284,16 @@ namespace dsm {
271284
Func f = streetLength) const;
272285
};
273286

287+
template <typename node_t, typename... TArgs>
288+
requires(std::is_base_of_v<Node, node_t>,
289+
std::constructible_from<node_t, Id, TArgs...>)
290+
node_t& Graph::addNode(Id id, TArgs&&... args) {
291+
addNode(std::make_unique<node_t>(id, std::forward<TArgs>(args)...));
292+
return dynamic_cast<node_t&>(*m_nodes[id]);
293+
}
274294
template <typename... Tn>
275295
requires(is_node_v<std::remove_reference_t<Tn>> && ...)
276296
void Graph::addNodes(Tn&&... nodes) {}
277-
278297
template <typename T1, typename... Tn>
279298
requires is_node_v<std::remove_reference_t<T1>> &&
280299
(is_node_v<std::remove_reference_t<Tn>> && ...)
@@ -283,6 +302,14 @@ namespace dsm {
283302
addNodes(std::forward<Tn>(nodes)...);
284303
}
285304

305+
template <typename edge_t, typename... TArgs>
306+
requires(std::is_base_of_v<Street, edge_t>,
307+
std::constructible_from<edge_t, Id, TArgs...>)
308+
edge_t& Graph::addEdge(Id id, TArgs&&... args) {
309+
addStreet(std::make_unique<edge_t>(id, std::forward<TArgs>(args)...));
310+
return dynamic_cast<edge_t&>(*m_streets[id]);
311+
}
312+
286313
template <typename T1>
287314
requires is_street_v<std::remove_reference_t<T1>>
288315
void Graph::addStreets(T1&& street) {

test/Test_dynamics.cpp

Lines changed: 17 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -954,33 +954,20 @@ TEST_CASE("Dynamics") {
954954
}
955955
SUBCASE("Intersection priorities") {
956956
GIVEN("A dynamics object with five nodes and eight streets") {
957-
Intersection nodeO{0, std::make_pair(0, 0)};
958-
Intersection nodeA{1, std::make_pair(-1, 1)};
959-
Intersection nodeB{2, std::make_pair(1, 1)};
960-
Intersection nodeC{3, std::make_pair(1, -1)};
961-
Intersection nodeD{4, std::make_pair(-1, -1)};
962-
Street sAO{0, 1, 10., 10., std::make_pair(1, 0)};
963-
Street sBO{1, 1, 10., 10., std::make_pair(2, 0)};
964-
Street sCO{2, 1, 10., 10., std::make_pair(3, 0)};
965-
Street sDO{3, 1, 10., 10., std::make_pair(4, 0)};
966-
Street sOA{4, 1, 10., 10., std::make_pair(0, 1)};
967-
Street sOB{5, 1, 10., 10., std::make_pair(0, 2)};
968-
Street sOC{6, 1, 10., 10., std::make_pair(0, 3)};
969-
Street sOD{7, 1, 10., 10., std::make_pair(0, 4)};
970957
Graph graph2;
971-
graph2.addNode(nodeO);
972-
graph2.addNode(nodeA);
973-
graph2.addNode(nodeB);
974-
graph2.addNode(nodeC);
975-
graph2.addNode(nodeD);
976-
graph2.addStreet(sAO);
977-
graph2.addStreet(sBO);
978-
graph2.addStreet(sCO);
979-
graph2.addStreet(sDO);
980-
graph2.addStreet(sOA);
981-
graph2.addStreet(sOB);
982-
graph2.addStreet(sOC);
983-
graph2.addStreet(sOD);
958+
auto& nodeO = graph2.addNode<Intersection>(0, std::make_pair(0, 0));
959+
auto& nodeA = graph2.addNode<Intersection>(1, std::make_pair(-1, 1));
960+
auto& nodeB = graph2.addNode<Intersection>(2, std::make_pair(1, 1));
961+
auto& nodeC = graph2.addNode<Intersection>(3, std::make_pair(1, -1));
962+
auto& nodeD = graph2.addNode<Intersection>(4, std::make_pair(-1, -1));
963+
auto& sOA = graph2.addEdge<Street>(0, 1, 10., 10., std::make_pair(0, 1));
964+
auto& sOB = graph2.addEdge<Street>(1, 1, 10., 10., std::make_pair(0, 2));
965+
auto& sOC = graph2.addEdge<Street>(2, 1, 10., 10., std::make_pair(0, 3));
966+
auto& sOD = graph2.addEdge<Street>(3, 1, 10., 10., std::make_pair(0, 4));
967+
auto& sAO = graph2.addEdge<Street>(4, 1, 10., 10., std::make_pair(1, 0));
968+
auto& sBO = graph2.addEdge<Street>(5, 1, 10., 10., std::make_pair(2, 0));
969+
auto& sCO = graph2.addEdge<Street>(6, 1, 10., 10., std::make_pair(3, 0));
970+
auto& sDO = graph2.addEdge<Street>(7, 1, 10., 10., std::make_pair(4, 0));
984971
graph2.buildAdj();
985972
Dynamics dynamics{graph2, 69};
986973
dynamics.graph().nodeSet().at(0)->setCapacity(3);
@@ -1038,13 +1025,10 @@ TEST_CASE("Dynamics") {
10381025
}
10391026
SUBCASE("meanSpireFlow") {
10401027
GIVEN("A network with a spireStreet and a normal street") {
1041-
SpireStreet ss{0, 1, 10., 5., std::make_pair(0, 1)};
1042-
Street s{1, 1, 10., 10., std::make_pair(1, 2)};
10431028
Graph graph2;
1044-
graph2.addStreet(ss);
1045-
graph2.addStreet(s);
1029+
graph2.addEdge<SpireStreet>(0, 1, 10., 5., std::make_pair(0, 1));
1030+
graph2.addEdge<Street>(1, 1, 10., 10., std::make_pair(1, 2));
10461031
graph2.buildAdj();
1047-
graph2.makeSpireStreet(1);
10481032
Dynamics dynamics{graph2, 69};
10491033
Itinerary itinerary{0, 2};
10501034
dynamics.addItinerary(itinerary);
@@ -1070,13 +1054,10 @@ TEST_CASE("Dynamics") {
10701054
}
10711055
SUBCASE("meanSpireFlow") {
10721056
GIVEN("A network with a spireStreet and a normal street") {
1073-
SpireStreet ss{0, 1, 10., 5., std::make_pair(0, 1)};
1074-
Street s{1, 1, 10., 10., std::make_pair(1, 2)};
10751057
Graph graph2;
1076-
graph2.addStreet(ss);
1077-
graph2.addStreet(s);
1058+
graph2.addEdge<SpireStreet>(0, 1, 10., 5., std::make_pair(0, 1));
1059+
graph2.addEdge<Street>(1, 1, 10., 10., std::make_pair(1, 2));
10781060
graph2.buildAdj();
1079-
graph2.makeSpireStreet(1);
10801061
Dynamics dynamics{graph2, 69};
10811062
Itinerary itinerary{0, 2};
10821063
dynamics.addItinerary(itinerary);

0 commit comments

Comments
 (0)