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..0601ac92 100644 --- a/src/dsf/mobility/RoadNetwork.cpp +++ b/src/dsf/mobility/RoadNetwork.cpp @@ -507,23 +507,25 @@ namespace dsf::mobility { auto const& pNode{pair.second}; auto const& inNeighbours{pNode->ingoingEdges()}; auto const& outNeighbours{pNode->outgoingEdges()}; - int maxPriority{0}; + double maxEstimatedFlow{0.0}; std::for_each(inNeighbours.cbegin(), inNeighbours.cend(), - [this, &maxPriority](auto const& edgeId) { + [this, &maxEstimatedFlow](auto const& edgeId) { auto const& pStreet{this->edge(edgeId)}; - maxPriority = std::max(maxPriority, pStreet->priority()); + auto const estFlow{pStreet->maxSpeed() * pStreet->nLanes()}; + maxEstimatedFlow = std::max(maxEstimatedFlow, estFlow); }); std::for_each(outNeighbours.cbegin(), outNeighbours.cend(), - [this, &maxPriority](auto const& edgeId) { + [this, &maxEstimatedFlow](auto const& edgeId) { auto const& pStreet{this->edge(edgeId)}; - maxPriority = std::max(maxPriority, pStreet->priority()); + auto const estFlow{pStreet->maxSpeed() * pStreet->nLanes()}; + maxEstimatedFlow = std::max(maxEstimatedFlow, estFlow); }); std::for_each( inNeighbours.cbegin(), inNeighbours.cend(), - [this, &pNode, &outNeighbours, &maxPriority](auto const& edgeId) { + [this, &pNode, &outNeighbours, &maxEstimatedFlow](auto const& edgeId) { auto const& pInStreet{this->edge(edgeId)}; auto const nLanes{pInStreet->nLanes()}; if (nLanes == 1) { @@ -533,7 +535,7 @@ namespace dsf::mobility { std::for_each( outNeighbours.cbegin(), outNeighbours.cend(), - [this, &pInStreet, &allowedTurns, &maxPriority](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())) { @@ -546,8 +548,11 @@ namespace dsf::mobility { return; } // Actually going straight means remain on the same road, thus... - if (((pInStreet->priority() == maxPriority) == - (outOppositeStreet->get()->priority() == maxPriority)) && + auto const inEstFlow{pInStreet->maxSpeed() * pInStreet->nLanes()}; + auto const outEstFlow{outOppositeStreet->get()->maxSpeed() * + outOppositeStreet->get()->nLanes()}; + if (((inEstFlow == maxEstimatedFlow) == + (outEstFlow == maxEstimatedFlow)) && !allowedTurns.contains(Direction::STRAIGHT)) { spdlog::debug("Street {} prioritized STRAIGHT", pInStreet->id()); if (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 d3a0d24d..35564541 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()); } } @@ -356,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()); } } } } @@ -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()); }