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 src/dsm/dsm.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

static constexpr uint8_t DSM_VERSION_MAJOR = 2;
static constexpr uint8_t DSM_VERSION_MINOR = 1;
static constexpr uint8_t DSM_VERSION_PATCH = 3;
static constexpr uint8_t DSM_VERSION_PATCH = 4;

#define DSM_VERSION \
std::format("{}.{}.{}", DSM_VERSION_MAJOR, DSM_VERSION_MINOR, DSM_VERSION_PATCH)
Expand Down
6 changes: 4 additions & 2 deletions src/dsm/headers/Graph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -369,20 +369,22 @@ namespace dsm {
m_nodes.emplace(std::make_pair(node.id(), std::make_unique<Intersection>(node)));
}

void Graph::makeRoundabout(Id nodeId) {
Roundabout& Graph::makeRoundabout(Id nodeId) {
if (!m_nodes.contains(nodeId)) {
throw std::invalid_argument(buildLog("Node does not exist."));
}
auto& pNode = m_nodes[nodeId];
pNode = std::make_unique<Roundabout>(*pNode);
return dynamic_cast<Roundabout&>(*pNode);
}
void Graph::makeSpireStreet(Id streetId) {
SpireStreet& Graph::makeSpireStreet(Id streetId) {
if (!m_streets.contains(streetId)) {
throw std::invalid_argument(
buildLog(std::format("Street with id {} does not exist.", streetId)));
}
auto& pStreet = m_streets[streetId];
pStreet = std::make_unique<SpireStreet>(pStreet->id(), *pStreet);
return dynamic_cast<SpireStreet&>(*pStreet);
}

void Graph::addStreet(std::shared_ptr<Street> street) {
Expand Down
12 changes: 8 additions & 4 deletions src/dsm/headers/Graph.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -160,18 +160,21 @@ namespace dsm {
/// @brief Convert an existing node to a traffic light
/// @tparam Delay The type of the traffic light's delay
/// @param nodeId The id of the node to convert to a traffic light
/// @return A reference to the traffic light
/// @throws std::invalid_argument if the node does not exist
template <typename Delay>
requires(std::unsigned_integral<Delay>)
void makeTrafficLight(Id nodeId);
TrafficLight<Delay>& makeTrafficLight(Id nodeId);
/// @brief Convert an existing node into a roundabout
/// @param nodeId The id of the node to convert to a roundabout
/// @return A reference to the roundabout
/// @throws std::invalid_argument if the node does not exist
void makeRoundabout(Id nodeId);
Roundabout& makeRoundabout(Id nodeId);
/// @brief Convert an existing street into a spire street
/// @param streetId The id of the street to convert to a spire street
/// @return A reference to the spire street
/// @throws std::invalid_argument if the street does not exist
void makeSpireStreet(Id streetId);
SpireStreet& makeSpireStreet(Id streetId);

/// @brief Add a street to the graph
/// @param street A std::shared_ptr to the street to add
Expand Down Expand Up @@ -278,12 +281,13 @@ namespace dsm {

template <typename Delay>
requires(std::unsigned_integral<Delay>)
void Graph::makeTrafficLight(Id nodeId) {
TrafficLight<Delay>& Graph::makeTrafficLight(Id nodeId) {
if (!m_nodes.contains(nodeId)) {
throw std::invalid_argument(buildLog("Node does not exist."));
}
auto& pNode = m_nodes[nodeId];
pNode = std::make_unique<TrafficLight<Delay>>(*pNode);
return dynamic_cast<TrafficLight<Delay>&>(*pNode);

Check notice

Code scanning / Cppcheck (reported by Codacy)

MISRA 15.5 rule

MISRA 15.5 rule
}

}; // namespace dsm
32 changes: 10 additions & 22 deletions test/Test_dynamics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -583,13 +583,12 @@ TEST_CASE("Dynamics") {
Street s1_4{9, 1, 30., 15., std::make_pair(1, 4)};

Graph graph2;
graph2.addNode(std::make_unique<TrafficLight>(1));
graph2.addStreets(s0_1, s1_0, s1_2, s2_1, s3_1, s1_3, s4_1, s1_4);
graph2.buildAdj();
auto& tl = graph2.makeTrafficLight<uint16_t>(1);
graph2.adjustNodeCapacities();
graph2.normalizeStreetCapacities();
auto const& nodes = graph2.nodeSet();
auto& tl = dynamic_cast<TrafficLight&>(*nodes.at(1));
tl.setDelay(3);
tl.setLeftTurnRatio(0.3);
tl.setPhase(2);
Expand Down Expand Up @@ -648,13 +647,12 @@ TEST_CASE("Dynamics") {
Street s1_4{9, 1, 30., 15., std::make_pair(1, 4)};

Graph graph2;
graph2.addNode(std::make_unique<TrafficLight>(1));
graph2.addStreets(s0_1, s1_0, s1_2, s2_1, s3_1, s1_3, s4_1, s1_4);
graph2.buildAdj();
auto& tl = graph2.makeTrafficLight<uint16_t>(1);
graph2.adjustNodeCapacities();
graph2.normalizeStreetCapacities();
auto const& nodes = graph2.nodeSet();
auto& tl = dynamic_cast<TrafficLight&>(*nodes.at(1));
tl.setDelay(3);
tl.setLeftTurnRatio(0.3);
// NO! Now testing red light
Expand Down Expand Up @@ -703,9 +701,6 @@ TEST_CASE("Dynamics") {
}
SUBCASE("Traffic Lights optimization algorithm") {
GIVEN("A dynamics object with a traffic light intersection") {
TrafficLight tl{1};
tl.setDelay(4);
tl.setPhase(3);
double length{90.}, max_speed{15.};
Street s_01{1, 10, length, max_speed, std::make_pair(0, 1)};
Street s_10{5, 10, length, max_speed, std::make_pair(1, 0)};
Expand All @@ -715,12 +710,14 @@ TEST_CASE("Dynamics") {
Street s_31{16, 10, length, max_speed, std::make_pair(3, 1)};
Street s_14{9, 10, length, max_speed, std::make_pair(1, 4)};
Street s_41{21, 10, length, max_speed, std::make_pair(4, 1)};
tl.addStreetPriority(1);
tl.addStreetPriority(11);
Graph graph2;
graph2.addNode(std::make_unique<TrafficLight>(tl));
graph2.addStreets(s_01, s_10, s_12, s_21, s_13, s_31, s_14, s_41);
graph2.buildAdj();
auto& tl = graph2.makeTrafficLight<uint16_t>(1);
tl.setDelay(4);
tl.setPhase(3);
tl.addStreetPriority(1);
tl.addStreetPriority(11);
Dynamics dynamics{graph2};
Itinerary it_0{0, 0}, it_1{1, 2}, it_2{2, 3}, it_3{3, 4};
dynamics.addItinerary(it_0);
Expand All @@ -737,10 +734,7 @@ TEST_CASE("Dynamics") {
}
dynamics.optimizeTrafficLights(2, 0.1, 0.);
THEN("Green and red time are different") {
const auto timing =
dynamic_cast<TrafficLight&>(*dynamics.graph().nodeSet().at(1))
.delay()
.value();
const auto timing = tl.delay().value();
CHECK(timing.first > timing.second);
}
}
Expand All @@ -757,10 +751,7 @@ TEST_CASE("Dynamics") {
}
dynamics.optimizeTrafficLights(2, 0.1, 0.);
THEN("Green and red time are equal") {
const auto timing =
dynamic_cast<TrafficLight&>(*dynamics.graph().nodeSet().at(1))
.delay()
.value();
const auto timing = tl.delay().value();
CHECK_EQ(timing.first, timing.second);
}
}
Expand All @@ -771,15 +762,14 @@ TEST_CASE("Dynamics") {
"A dynamics object with four streets, one agent for each street, two "
"itineraries "
"and a roundabout") {
Roundabout roundabout{1};
Street s1{0, 1, 10., 10., std::make_pair(0, 1)};
Street s2{1, 1, 10., 10., std::make_pair(2, 1)};
Street s3{2, 1, 10., 10., std::make_pair(1, 0)};
Street s4{3, 1, 10., 10., std::make_pair(1, 2)};
Graph graph2;
graph2.addNode(std::make_unique<Roundabout>(roundabout));
graph2.addStreets(s1, s2, s3, s4);
graph2.buildAdj();
auto& rb = graph2.makeRoundabout(1);
graph2.adjustNodeCapacities();
Dynamics dynamics{graph2};
dynamics.setSeed(69);
Expand All @@ -799,15 +789,13 @@ TEST_CASE("Dynamics") {
dynamics.evolve(false);
dynamics.evolve(false);
THEN("The agents are trapped into the roundabout") {
auto& rb = dynamic_cast<Roundabout&>(*dynamics.graph().nodeSet().at(1));
CHECK_EQ(dynamics.agents().at(0)->streetId().value(), 1);
CHECK_EQ(dynamics.agents().at(1)->streetId().value(), 7);
CHECK_EQ(dynamics.agents().at(2)->streetId().value(), 5);
CHECK_EQ(rb.agents().size(), 1);
}
dynamics.evolve(false);
THEN("The agent with priority leaves the roundabout") {
auto& rb = dynamic_cast<Roundabout&>(*dynamics.graph().nodeSet().at(1));
CHECK_EQ(dynamics.agents().at(0)->streetId().value(), 5);
CHECK_EQ(dynamics.agents().at(1)->streetId().value(), 3);
CHECK_EQ(rb.agents().size(), 0);
Expand Down