Skip to content

Commit

Permalink
Add isSpire attribute to Street class (#145)
Browse files Browse the repository at this point in the history
* Add yellow delay to trafficlight class

* New set_delay - wip: no tests

* New `setDelay` for traffic light

* Update version

* Add `importCoords` function to Graph class

* Rework `meanTravelTimes`: now can delete data after get

* Add `isSpire` to Street class

* Update version

* Formatting

---------

Co-authored-by: Simone Balducci <[email protected]>
  • Loading branch information
Grufoony and sbaldu authored Mar 13, 2024
1 parent 62570b2 commit 77e9861
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 7 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
cmake_minimum_required(VERSION 3.16.0)

project(dms VERSION 1.1.3 LANGUAGES CXX)
project(dms VERSION 1.1.4 LANGUAGES CXX)

# set the C++ standard
set(CMAKE_CXX_STANDARD 20)
Expand Down
17 changes: 16 additions & 1 deletion src/dsm/headers/Dynamics.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -740,6 +740,9 @@ namespace dsm {
m_graph->streetSet().cend(),
0.,
[](double sum, const auto& street) {
if (!street.second->isSpire()) {
return sum;
}
return sum + street.second->density();
}) /
m_graph->streetSet().size()};
Expand All @@ -748,6 +751,9 @@ namespace dsm {
m_graph->streetSet().cend(),
0.,
[mean](double sum, const auto& street) {
if (!street.second->isSpire()) {
return sum;
}
return sum + std::pow(street.second->density() - mean, 2);
}) /
(m_graph->streetSet().size() - 1)};
Expand All @@ -760,6 +766,9 @@ namespace dsm {
std::vector<double> flows;
flows.reserve(m_graph->streetSet().size());
for (const auto& [streetId, street] : m_graph->streetSet()) {
if (!street->isSpire()) {
continue;
}
auto speedOpt{this->streetMeanSpeed(streetId)};
if (speedOpt.has_value()) {
double flow{street->density() * speedOpt.value()};
Expand All @@ -778,6 +787,9 @@ namespace dsm {
std::vector<double> flows;
flows.reserve(m_graph->streetSet().size());
for (const auto& [streetId, street] : m_graph->streetSet()) {
if (!street->isSpire()) {
continue;
}
if (above && street->density() > threshold) {
auto speedOpt{this->streetMeanSpeed(streetId)};
if (speedOpt.has_value()) {
Expand Down Expand Up @@ -869,7 +881,7 @@ namespace dsm {
std::optional<double> FirstOrderDynamics<Id, Size, Delay>::streetMeanSpeed(
Id streetId) const {
auto street{this->m_graph->streetSet()[streetId]};
if (street->queue().empty()) {
if (street->queue().empty() || !street->isSpire()) {
return std::nullopt;
}
if (this->m_agents.at(street->queue().front())->delay() > 0) {
Expand Down Expand Up @@ -911,6 +923,9 @@ namespace dsm {
std::vector<double> speeds;
speeds.reserve(this->m_graph->streetSet().size());
for (const auto& [streetId, street] : this->m_graph->streetSet()) {
if (!street->isSpire()) {
continue;
}
if (above) {
if (street->density() > threshold) {
auto speedOpt{this->streetMeanSpeed(streetId)};
Expand Down
31 changes: 27 additions & 4 deletions src/dsm/headers/Street.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ namespace dsm {
Id m_id;
Size m_capacity;
Size m_transportCapacity;
bool m_isSpire;

public:
Street() = delete;
Expand Down Expand Up @@ -110,6 +111,11 @@ namespace dsm {
/// @param angle The street's angle
/// @throw std::invalid_argument If the angle is negative or greater than 2 * pi
void setAngle(double angle);
/// @brief Set the street's spire status
/// @param isSpire The street's spire status
/// @details A spire is a street from which you can extract data, e.g. density. However, this
/// parameter must be managed by the dynamics.
void setIsSpire(bool isSpire);

/// @brief Get the street's id
/// @return Id, The street's id
Expand Down Expand Up @@ -145,6 +151,9 @@ namespace dsm {
void enqueue(Id agentId);
/// @brief Remove an agent from the street's queue
std::optional<Id> dequeue();
/// @brief Check if the street is a spire
/// @return bool True if the street is a spire, false otherwise
bool isSpire() const;
};

template <typename Id, typename Size>
Expand All @@ -156,7 +165,8 @@ namespace dsm {
m_angle{street.angle()},
m_id{id},
m_capacity{street.capacity()},
m_transportCapacity{street.transportCapacity()} {}
m_transportCapacity{street.transportCapacity()},
m_isSpire{true} {}

template <typename Id, typename Size>
requires(std::unsigned_integral<Id> && std::unsigned_integral<Size>)
Expand All @@ -167,7 +177,8 @@ namespace dsm {
m_angle{0.},
m_id{index},
m_capacity{1},
m_transportCapacity{std::numeric_limits<Size>::max()} {}
m_transportCapacity{std::numeric_limits<Size>::max()},
m_isSpire{true} {}

template <typename Id, typename Size>
requires(std::unsigned_integral<Id> && std::unsigned_integral<Size>)
Expand All @@ -178,7 +189,8 @@ namespace dsm {
m_angle{0.},
m_id{id},
m_capacity{capacity},
m_transportCapacity{std::numeric_limits<Size>::max()} {}
m_transportCapacity{std::numeric_limits<Size>::max()},
m_isSpire{true} {}

template <typename Id, typename Size>
requires(std::unsigned_integral<Id> && std::unsigned_integral<Size>)
Expand All @@ -189,7 +201,8 @@ namespace dsm {
m_angle{0.},
m_id{id},
m_capacity{capacity},
m_transportCapacity{std::numeric_limits<Size>::max()} {
m_transportCapacity{std::numeric_limits<Size>::max()},
m_isSpire{true} {
this->setMaxSpeed(maxSpeed);
}

Expand Down Expand Up @@ -266,6 +279,11 @@ namespace dsm {
}
m_angle = angle;
}
template <typename Id, typename Size>
requires(std::unsigned_integral<Id> && std::unsigned_integral<Size>)
void Street<Id, Size>::setIsSpire(bool isSpire) {
m_isSpire = isSpire;
}

template <typename Id, typename Size>
requires(std::unsigned_integral<Id> && std::unsigned_integral<Size>)
Expand Down Expand Up @@ -337,6 +355,11 @@ namespace dsm {
m_queue.pop();
return id;
}
template <typename Id, typename Size>
requires(std::unsigned_integral<Id> && std::unsigned_integral<Size>)
bool Street<Id, Size>::isSpire() const {
return m_isSpire;
}

}; // namespace dsm

Expand Down
2 changes: 1 addition & 1 deletion test/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
cmake_minimum_required(VERSION 3.16.0)

project(dsm_tests VERSION 1.1.2 LANGUAGES CXX)
project(dsm_tests VERSION 1.1.4 LANGUAGES CXX)

# Set the C++ standard
set(CMAKE_CXX_STANDARD 20)
Expand Down
2 changes: 2 additions & 0 deletions test/Test_dynamics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -442,5 +442,7 @@ TEST_CASE("Dynamics") {
CHECK_EQ(dynamics.graph().streetSet().at(1)->queue().size(), 3);
CHECK(dynamics.streetMeanSpeed(1).has_value());
CHECK_EQ(dynamics.streetMeanSpeed(1).value(), meanSpeed);
dynamics.graph().streetSet().at(1)->setIsSpire(false);
CHECK_FALSE(dynamics.streetMeanSpeed(1).has_value());
}
}

0 comments on commit 77e9861

Please sign in to comment.