Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
9 changes: 4 additions & 5 deletions benchmark/Graph/BenchGraph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,12 @@
Bench b1(n_rep);

std::cout << "Benchmarking addNode\n";
Intersection n1(std::rand());
b1.benchmark([&g1](const Intersection& node) -> void { g1.addNode(node); }, n1);
b1.benchmark([&g1]() -> void { g1.addNode<Intersection>(std::rand()); });

Check notice

Code scanning / Cppcheck (reported by Codacy)

Do not use the rand() function for generating pseudorandom numbers Note test

Do not use the rand() function for generating pseudorandom numbers
b1.print();
std::cout << "Benchmarking addNodes overhead for a single node\n";
n1 = Intersection(std::rand());
b1.benchmark([&g1](const Intersection& node) -> void { g1.addNodes(node); }, n1);
b1.print();
// n1 = Intersection(std::rand());
// b1.benchmark([&g1](const Intersection& node) -> void { g1.addNodes(node); }, n1);
// b1.print();

// std::cout << "Benchmarking addStreet\n";
// Street s1(std::rand(), std::make_pair(0, 1));
Expand Down
14 changes: 5 additions & 9 deletions examples/stalingrado.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,34 +46,30 @@ int main() {
const auto MAX_TIME{static_cast<Unit>(timeUnit * vehiclesToInsert.size())};

// Create the graph
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};
// Viale Aldo Moro
TrafficLight tl1{1, 132};
auto& tl1 = graph.addNode<TrafficLight>(1, 132);
tl1.setCycle(s01.id(), dsm::Direction::ANY, {62, 0});
tl1.setCapacity(1);
// Via Donato Creti
TrafficLight tl2{2, 141};
auto& tl2 = graph.addNode<TrafficLight>(2, 141);
tl2.setCycle(s12.id(), dsm::Direction::ANY, {72, 0});
tl2.setCapacity(1);
// Via del Lavoro
TrafficLight tl3{3, 138};
auto& tl3 = graph.addNode<TrafficLight>(3, 138);
tl3.setCycle(s23.id(), dsm::Direction::ANY, {88, 0});
tl3.setCapacity(1);
// Viali
TrafficLight tl4{4, 131};
auto& tl4 = graph.addNode<TrafficLight>(4, 131);
tl4.setCycle(s34.id(), dsm::Direction::ANY, {81, 0});
tl4.setCapacity(1);

Graph graph;
graph.addNode(std::make_unique<TrafficLight>(tl1));
graph.addNode(std::make_unique<TrafficLight>(tl2));
graph.addNode(std::make_unique<TrafficLight>(tl3));
graph.addNode(std::make_unique<TrafficLight>(tl4));
graph.addStreets(s01, s12, s23, s34);
graph.buildAdj();
graph.adjustNodeCapacities();
Expand Down
8 changes: 2 additions & 6 deletions src/dsm/headers/Graph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -425,10 +425,6 @@ namespace dsm {
m_nodes.emplace(std::make_pair(node->id(), std::move(node)));
}

void Graph::addNode(const Intersection& node) {
m_nodes.emplace(std::make_pair(node.id(), std::make_unique<Intersection>(node)));
}

TrafficLight& Graph::makeTrafficLight(Id const nodeId,
Delay const cycleTime,
Delay const counter) {
Expand Down Expand Up @@ -468,7 +464,7 @@ namespace dsm {
return dynamic_cast<SpireStreet&>(*pStreet);
}

void Graph::addStreet(std::shared_ptr<Street> street) {
void Graph::addStreet(std::unique_ptr<Street> street) {
if (m_streets.contains(street->id())) {
throw std::invalid_argument(
buildLog(std::format("Street with id {} already exists.", street->id())));
Expand All @@ -483,7 +479,7 @@ namespace dsm {
m_nodes.emplace(dstId, std::make_unique<Intersection>(dstId));
}
// emplace street
m_streets.emplace(street->id(), street.get());
m_streets.emplace(std::make_pair(street->id(), std::move(street)));
}

void Graph::addStreet(const Street& street) {
Expand Down
32 changes: 26 additions & 6 deletions src/dsm/headers/Graph.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -155,9 +155,11 @@
/// @brief Add a node to the graph
/// @param node A std::unique_ptr to the node to add
void addNode(std::unique_ptr<Node> node);
/// @brief Add a node to the graph
/// @param node A reference to the node to add
void addNode(const Intersection& node);

template <typename node_t, typename... TArgs>
requires(std::is_base_of_v<Node, node_t>,
std::constructible_from<node_t, Id, TArgs...>)
node_t& addNode(Id id, TArgs&&... args);

template <typename... Tn>
requires(is_node_v<std::remove_reference_t<Tn>> && ...)
Expand Down Expand Up @@ -194,9 +196,13 @@
/// @throws std::invalid_argument if the node does not exist
Station& makeStation(Id nodeId, const unsigned int managementTime);

template <typename edge_t, typename... TArgs>
requires(std::is_base_of_v<Street, edge_t>,
std::constructible_from<edge_t, Id, TArgs...>)
edge_t& addEdge(Id id, TArgs&&... args);
/// @brief Add a street to the graph
/// @param street A std::shared_ptr to the street to add
void addStreet(std::shared_ptr<Street> street);
/// @param street A std::unique_ptr to the street to add
void addStreet(std::unique_ptr<Street> street);
/// @brief Add a street to the graph
/// @param street A reference to the street to add
void addStreet(const Street& street);
Expand Down Expand Up @@ -271,10 +277,16 @@
Func f = streetLength) const;
};

template <typename node_t, typename... TArgs>

Check notice

Code scanning / Cppcheck (reported by Codacy)

MISRA 12.3 rule Note

MISRA 12.3 rule
requires(std::is_base_of_v<Node, node_t>,

Check notice

Code scanning / Cppcheck (reported by Codacy)

MISRA 12.3 rule Note

MISRA 12.3 rule
std::constructible_from<node_t, Id, TArgs...>)

Check notice

Code scanning / Cppcheck (reported by Codacy)

MISRA 12.3 rule Note

MISRA 12.3 rule
node_t& Graph::addNode(Id id, TArgs&&... args) {
addNode(std::make_unique<node_t>(id, std::forward<TArgs>(args)...));
return dynamic_cast<node_t&>(*m_nodes[id]);

Check notice

Code scanning / Cppcheck (reported by Codacy)

MISRA 15.5 rule Note

MISRA 15.5 rule
}
template <typename... Tn>
requires(is_node_v<std::remove_reference_t<Tn>> && ...)
void Graph::addNodes(Tn&&... nodes) {}

template <typename T1, typename... Tn>
requires is_node_v<std::remove_reference_t<T1>> &&
(is_node_v<std::remove_reference_t<Tn>> && ...)
Expand All @@ -283,6 +295,14 @@
addNodes(std::forward<Tn>(nodes)...);
}

template <typename edge_t, typename... TArgs>

Check notice

Code scanning / Cppcheck (reported by Codacy)

MISRA 12.3 rule Note

MISRA 12.3 rule
requires(std::is_base_of_v<Street, edge_t>,

Check notice

Code scanning / Cppcheck (reported by Codacy)

MISRA 12.3 rule Note

MISRA 12.3 rule
std::constructible_from<edge_t, Id, TArgs...>)

Check notice

Code scanning / Cppcheck (reported by Codacy)

MISRA 12.3 rule Note

MISRA 12.3 rule
edge_t& Graph::addEdge(Id id, TArgs&&... args) {
addStreet(std::make_unique<edge_t>(id, std::forward<TArgs>(args)...));
return dynamic_cast<edge_t&>(*m_streets[id]);

Check notice

Code scanning / Cppcheck (reported by Codacy)

MISRA 15.5 rule Note

MISRA 15.5 rule
}

template <typename T1>
requires is_street_v<std::remove_reference_t<T1>>
void Graph::addStreets(T1&& street) {
Expand Down
53 changes: 17 additions & 36 deletions test/Test_dynamics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -954,33 +954,20 @@ TEST_CASE("Dynamics") {
}
SUBCASE("Intersection priorities") {
GIVEN("A dynamics object with five nodes and eight streets") {
Intersection nodeO{0, std::make_pair(0, 0)};
Intersection nodeA{1, std::make_pair(-1, 1)};
Intersection nodeB{2, std::make_pair(1, 1)};
Intersection nodeC{3, std::make_pair(1, -1)};
Intersection nodeD{4, std::make_pair(-1, -1)};
Street sAO{0, 1, 10., 10., std::make_pair(1, 0)};
Street sBO{1, 1, 10., 10., std::make_pair(2, 0)};
Street sCO{2, 1, 10., 10., std::make_pair(3, 0)};
Street sDO{3, 1, 10., 10., std::make_pair(4, 0)};
Street sOA{4, 1, 10., 10., std::make_pair(0, 1)};
Street sOB{5, 1, 10., 10., std::make_pair(0, 2)};
Street sOC{6, 1, 10., 10., std::make_pair(0, 3)};
Street sOD{7, 1, 10., 10., std::make_pair(0, 4)};
Graph graph2;
graph2.addNode(nodeO);
graph2.addNode(nodeA);
graph2.addNode(nodeB);
graph2.addNode(nodeC);
graph2.addNode(nodeD);
graph2.addStreet(sAO);
graph2.addStreet(sBO);
graph2.addStreet(sCO);
graph2.addStreet(sDO);
graph2.addStreet(sOA);
graph2.addStreet(sOB);
graph2.addStreet(sOC);
graph2.addStreet(sOD);
auto& nodeO = graph2.addNode<Intersection>(0, std::make_pair(0, 0));
auto& nodeA = graph2.addNode<Intersection>(1, std::make_pair(-1, 1));
auto& nodeB = graph2.addNode<Intersection>(2, std::make_pair(1, 1));
auto& nodeC = graph2.addNode<Intersection>(3, std::make_pair(1, -1));
auto& nodeD = graph2.addNode<Intersection>(4, std::make_pair(-1, -1));
auto& sOA = graph2.addEdge<Street>(0, 1, 10., 10., std::make_pair(0, 1));
auto& sOB = graph2.addEdge<Street>(1, 1, 10., 10., std::make_pair(0, 2));
auto& sOC = graph2.addEdge<Street>(2, 1, 10., 10., std::make_pair(0, 3));
auto& sOD = graph2.addEdge<Street>(3, 1, 10., 10., std::make_pair(0, 4));
auto& sAO = graph2.addEdge<Street>(4, 1, 10., 10., std::make_pair(1, 0));
auto& sBO = graph2.addEdge<Street>(5, 1, 10., 10., std::make_pair(2, 0));
auto& sCO = graph2.addEdge<Street>(6, 1, 10., 10., std::make_pair(3, 0));
auto& sDO = graph2.addEdge<Street>(7, 1, 10., 10., std::make_pair(4, 0));
graph2.buildAdj();
Dynamics dynamics{graph2, 69};
dynamics.graph().nodeSet().at(0)->setCapacity(3);
Expand Down Expand Up @@ -1038,13 +1025,10 @@ TEST_CASE("Dynamics") {
}
SUBCASE("meanSpireFlow") {
GIVEN("A network with a spireStreet and a normal street") {
SpireStreet ss{0, 1, 10., 5., std::make_pair(0, 1)};
Street s{1, 1, 10., 10., std::make_pair(1, 2)};
Graph graph2;
graph2.addStreet(ss);
graph2.addStreet(s);
graph2.addEdge<SpireStreet>(0, 1, 10., 5., std::make_pair(0, 1));
graph2.addEdge<Street>(1, 1, 10., 10., std::make_pair(1, 2));
graph2.buildAdj();
graph2.makeSpireStreet(1);
Dynamics dynamics{graph2, 69};
Itinerary itinerary{0, 2};
dynamics.addItinerary(itinerary);
Expand All @@ -1070,13 +1054,10 @@ TEST_CASE("Dynamics") {
}
SUBCASE("meanSpireFlow") {
GIVEN("A network with a spireStreet and a normal street") {
SpireStreet ss{0, 1, 10., 5., std::make_pair(0, 1)};
Street s{1, 1, 10., 10., std::make_pair(1, 2)};
Graph graph2;
graph2.addStreet(ss);
graph2.addStreet(s);
graph2.addEdge<SpireStreet>(0, 1, 10., 5., std::make_pair(0, 1));
graph2.addEdge<Street>(1, 1, 10., 10., std::make_pair(1, 2));
graph2.buildAdj();
graph2.makeSpireStreet(1);
Dynamics dynamics{graph2, 69};
Itinerary itinerary{0, 2};
dynamics.addItinerary(itinerary);
Expand Down
Loading