From 99cdedb2b6f925780eab957dfbdc2ce3d55bf59d Mon Sep 17 00:00:00 2001 From: grufoony Date: Mon, 22 Dec 2025 14:27:22 +0100 Subject: [PATCH 1/3] Init refactoring --- src/dsf/mobility/Road.cpp | 7 +------ src/dsf/mobility/Road.hpp | 9 ++++----- src/dsf/mobility/RoadNetwork.cpp | 20 +++----------------- test/mobility/Test_street.cpp | 6 ++++-- 4 files changed, 12 insertions(+), 30 deletions(-) diff --git a/src/dsf/mobility/Road.cpp b/src/dsf/mobility/Road.cpp index ce219cd3..5a850157 100644 --- a/src/dsf/mobility/Road.cpp +++ b/src/dsf/mobility/Road.cpp @@ -22,8 +22,7 @@ namespace dsf::mobility { m_length{length}, m_maxSpeed{maxSpeed}, m_nLanes{nLanes}, - m_name{std::move(name)}, - m_priority{nLanes * 100} { + m_name{std::move(name)} { if (!(length > 0.)) { throw std::invalid_argument( std::format("The length of a road ({}) must be greater than 0.", length)); @@ -84,10 +83,6 @@ namespace dsf::mobility { } m_transportCapacity = transportCapacity; } - void Road::setPriority(int priority) { - assert(priority >= 0); - m_priority = priority; - } Direction Road::turnDirection(double const& previousStreetAngle) const { auto const deltaAngle{this->deltaAngle(previousStreetAngle)}; if (std::abs(deltaAngle) >= std::numbers::pi) { diff --git a/src/dsf/mobility/Road.hpp b/src/dsf/mobility/Road.hpp index ce3183ef..c5400959 100644 --- a/src/dsf/mobility/Road.hpp +++ b/src/dsf/mobility/Road.hpp @@ -17,7 +17,7 @@ namespace dsf::mobility { int m_capacity; double m_transportCapacity; std::string m_name; - int m_priority; + bool m_hasPriority = false; std::set m_forbiddenTurns; // Stores the forbidden turns (road ids) public: @@ -60,9 +60,8 @@ namespace dsf::mobility { /// @param transportCapacity The transport capacity /// @throws std::invalid_argument If the transport capacity is less or equal to 0 void setTransportCapacity(double transportCapacity); - /// @brief Set the road's priority - /// @param priority The road's priority - void setPriority(int priority); + /// @brief Set the road's priority to true + inline void setPriority() { m_hasPriority = true; } /// @brief Add a road id to the forbidden turns /// @param roadId The road id to add void addForbiddenTurn(Id roadId); @@ -90,7 +89,7 @@ namespace dsf::mobility { inline auto const& name() const noexcept { return m_name; } /// @brief Get the priority /// @return int The priority - inline auto priority() const noexcept { return m_priority; } + inline auto hasPriority() const noexcept { return m_hasPriority; } /// @brief Get the road's forbidden turns /// @return std::set The road's forbidden turns /// @details The forbidden turns are the road ids that are not allowed to be used by the agents diff --git a/src/dsf/mobility/RoadNetwork.cpp b/src/dsf/mobility/RoadNetwork.cpp index b36e0579..6bb5004f 100644 --- a/src/dsf/mobility/RoadNetwork.cpp +++ b/src/dsf/mobility/RoadNetwork.cpp @@ -507,23 +507,10 @@ namespace dsf::mobility { auto const& pNode{pair.second}; auto const& inNeighbours{pNode->ingoingEdges()}; auto const& outNeighbours{pNode->outgoingEdges()}; - int maxPriority{0}; - std::for_each(inNeighbours.cbegin(), - inNeighbours.cend(), - [this, &maxPriority](auto const& edgeId) { - auto const& pStreet{this->edge(edgeId)}; - maxPriority = std::max(maxPriority, pStreet->priority()); - }); - std::for_each(outNeighbours.cbegin(), - outNeighbours.cend(), - [this, &maxPriority](auto const& edgeId) { - auto const& pStreet{this->edge(edgeId)}; - maxPriority = std::max(maxPriority, pStreet->priority()); - }); std::for_each( inNeighbours.cbegin(), inNeighbours.cend(), - [this, &pNode, &outNeighbours, &maxPriority](auto const& edgeId) { + [this, &pNode, &outNeighbours](auto const& edgeId) { auto const& pInStreet{this->edge(edgeId)}; auto const nLanes{pInStreet->nLanes()}; if (nLanes == 1) { @@ -533,7 +520,7 @@ namespace dsf::mobility { std::for_each( outNeighbours.cbegin(), outNeighbours.cend(), - [this, &pInStreet, &allowedTurns, &maxPriority](auto const& edgeId) { + [this, &pInStreet, &allowedTurns](auto const& edgeId) { auto const& pOutStreet{this->edge(edgeId)}; if (pOutStreet->target() == pInStreet->source() || pInStreet->forbiddenTurns().contains(pOutStreet->id())) { @@ -546,8 +533,7 @@ namespace dsf::mobility { return; } // Actually going straight means remain on the same road, thus... - if (((pInStreet->priority() == maxPriority) == - (outOppositeStreet->get()->priority() == maxPriority)) && + if ((pInStreet->priority() == outOppositeStreet->get()->priority()) && !allowedTurns.contains(Direction::STRAIGHT)) { spdlog::debug("Street {} prioritized STRAIGHT", pInStreet->id()); if (allowedTurns.contains(Direction::STRAIGHT) && diff --git a/test/mobility/Test_street.cpp b/test/mobility/Test_street.cpp index d3a0d24d..50e5eca1 100644 --- a/test/mobility/Test_street.cpp +++ b/test/mobility/Test_street.cpp @@ -216,7 +216,9 @@ TEST_CASE("Road") { CHECK_EQ(road.capacity(), static_cast(std::ceil((100.0 * 2) / Road::meanVehicleLength()))); CHECK_EQ(road.transportCapacity(), 1.0); - CHECK_EQ(road.priority(), 200); // 2 * 100 + CHECK_FALSE(road.hasPriority()); + road.setPriority(); + CHECK(road.hasPriority()); } } @@ -403,7 +405,7 @@ TEST_CASE("Road") { static_cast(std::ceil((100.0 * 2) / Road::meanVehicleLength()))); CHECK_EQ(road.transportCapacity(), 1.0); CHECK_EQ(road.name(), "Test Road"); - CHECK_EQ(road.priority(), 200); // 2 * 100 + CHECK_FALSE(road.hasPriority()); CHECK(road.forbiddenTurns().empty()); } From 02545e4726fa6df340496fc48b11f16d30f1ed9c Mon Sep 17 00:00:00 2001 From: grufoony Date: Mon, 22 Dec 2025 17:37:46 +0100 Subject: [PATCH 2/3] Better handling of nlanes --- src/dsf/mobility/RoadNetwork.cpp | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/src/dsf/mobility/RoadNetwork.cpp b/src/dsf/mobility/RoadNetwork.cpp index 6bb5004f..60d48440 100644 --- a/src/dsf/mobility/RoadNetwork.cpp +++ b/src/dsf/mobility/RoadNetwork.cpp @@ -507,10 +507,25 @@ namespace dsf::mobility { auto const& pNode{pair.second}; auto const& inNeighbours{pNode->ingoingEdges()}; auto const& outNeighbours{pNode->outgoingEdges()}; + double maxEstimatedFlow{0.0}; + std::for_each(inNeighbours.cbegin(), + inNeighbours.cend(), + [this, &maxEstimatedFlow](auto const& edgeId) { + auto const& pStreet{this->edge(edgeId)}; + auto const estFlow{pStreet->maxSpeed() * pStreet->nLanes()}; + maxEstimatedFlow = std::max(maxEstimatedFlow, estFlow); + }); + std::for_each(outNeighbours.cbegin(), + outNeighbours.cend(), + [this, &maxEstimatedFlow](auto const& edgeId) { + auto const& pStreet{this->edge(edgeId)}; + auto const estFlow{pStreet->maxSpeed() * pStreet->nLanes()}; + maxEstimatedFlow = std::max(maxEstimatedFlow, estFlow); + }); std::for_each( inNeighbours.cbegin(), inNeighbours.cend(), - [this, &pNode, &outNeighbours](auto const& edgeId) { + [this, &pNode, &outNeighbours, &maxEstimatedFlow](auto const& edgeId) { auto const& pInStreet{this->edge(edgeId)}; auto const nLanes{pInStreet->nLanes()}; if (nLanes == 1) { @@ -520,7 +535,7 @@ namespace dsf::mobility { std::for_each( outNeighbours.cbegin(), outNeighbours.cend(), - [this, &pInStreet, &allowedTurns](auto const& edgeId) { + [this, &pInStreet, &allowedTurns, &maxEstimatedFlow](auto const& edgeId) { auto const& pOutStreet{this->edge(edgeId)}; if (pOutStreet->target() == pInStreet->source() || pInStreet->forbiddenTurns().contains(pOutStreet->id())) { @@ -533,7 +548,11 @@ namespace dsf::mobility { return; } // Actually going straight means remain on the same road, thus... - if ((pInStreet->priority() == outOppositeStreet->get()->priority()) && + auto const inEstFlow{pInStreet->maxSpeed() * pInStreet->nLanes()}; + auto const outEstFlow{outOppositeStreet->maxSpeed() * + outOppositeStreet->nLanes()}; + if (((inEstFlow == maxEstimatedFlow) == + (outEstFlow == maxEstimatedFlow)) && !allowedTurns.contains(Direction::STRAIGHT)) { spdlog::debug("Street {} prioritized STRAIGHT", pInStreet->id()); if (allowedTurns.contains(Direction::STRAIGHT) && From 6182b835751a81cd8a30bb3cce96371bfb84d457 Mon Sep 17 00:00:00 2001 From: grufoony Date: Mon, 22 Dec 2025 17:54:56 +0100 Subject: [PATCH 3/3] Fix --- src/dsf/mobility/RoadNetwork.cpp | 4 ++-- src/dsf/mobility/Street.cpp | 2 +- test/mobility/Test_street.cpp | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/dsf/mobility/RoadNetwork.cpp b/src/dsf/mobility/RoadNetwork.cpp index 60d48440..0601ac92 100644 --- a/src/dsf/mobility/RoadNetwork.cpp +++ b/src/dsf/mobility/RoadNetwork.cpp @@ -549,8 +549,8 @@ namespace dsf::mobility { } // Actually going straight means remain on the same road, thus... auto const inEstFlow{pInStreet->maxSpeed() * pInStreet->nLanes()}; - auto const outEstFlow{outOppositeStreet->maxSpeed() * - outOppositeStreet->nLanes()}; + auto const outEstFlow{outOppositeStreet->get()->maxSpeed() * + outOppositeStreet->get()->nLanes()}; if (((inEstFlow == maxEstimatedFlow) == (outEstFlow == maxEstimatedFlow)) && !allowedTurns.contains(Direction::STRAIGHT)) { diff --git a/src/dsf/mobility/Street.cpp b/src/dsf/mobility/Street.cpp index 5f5dd1d8..d628b625 100644 --- a/src/dsf/mobility/Street.cpp +++ b/src/dsf/mobility/Street.cpp @@ -57,7 +57,7 @@ namespace dsf::mobility { isEqual &= (this->m_maxSpeed == other.m_maxSpeed); isEqual &= (this->m_nLanes == other.m_nLanes); isEqual &= (this->m_name == other.m_name); - isEqual &= (this->m_priority == other.m_priority); + isEqual &= (this->m_hasPriority == other.m_hasPriority); return isEqual; } diff --git a/test/mobility/Test_street.cpp b/test/mobility/Test_street.cpp index 50e5eca1..35564541 100644 --- a/test/mobility/Test_street.cpp +++ b/test/mobility/Test_street.cpp @@ -358,8 +358,8 @@ TEST_CASE("Road") { SUBCASE("setPriority") { WHEN("Priority is set") { - road.setPriority(150); - THEN("Priority is updated") { CHECK_EQ(road.priority(), 150); } + road.setPriority(); + THEN("Priority is updated") { CHECK(road.hasPriority()); } } } }