Skip to content
Open
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
7 changes: 1 addition & 6 deletions src/dsf/mobility/Road.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down Expand Up @@ -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) {
Expand Down
9 changes: 4 additions & 5 deletions src/dsf/mobility/Road.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<Id> m_forbiddenTurns; // Stores the forbidden turns (road ids)

public:
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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<Id> The road's forbidden turns
/// @details The forbidden turns are the road ids that are not allowed to be used by the agents
Expand Down
23 changes: 14 additions & 9 deletions src/dsf/mobility/RoadNetwork.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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())) {
Expand All @@ -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) &&
Expand Down
2 changes: 1 addition & 1 deletion src/dsf/mobility/Street.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
10 changes: 6 additions & 4 deletions test/mobility/Test_street.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,9 @@ TEST_CASE("Road") {
CHECK_EQ(road.capacity(),
static_cast<int>(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());
}
}

Expand Down Expand Up @@ -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()); }
}
}
}
Expand Down Expand Up @@ -403,7 +405,7 @@ TEST_CASE("Road") {
static_cast<int>(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());
}

Expand Down
Loading