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
21 changes: 0 additions & 21 deletions benchmark/Bench_Street.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,25 +128,6 @@ static void BM_Street_SetLaneMapping(benchmark::State& state) {
}
}

static void BM_StochasticStreet_SetFlowRate(benchmark::State& state) {
dsf::mobility::Street baseStreet(
0, {0, 1}, 100.0, 13.8888888889, 2, "test", {}, std::nullopt, 1.0);
dsf::mobility::StochasticStreet street(std::move(baseStreet), 0.5);
for (auto _ : state) {
street.setFlowRate(0.8);
}
}

static void BM_StochasticStreet_FlowRate(benchmark::State& state) {
dsf::mobility::Street baseStreet(
0, {0, 1}, 100.0, 13.8888888889, 2, "test", {}, std::nullopt, 1.0);
dsf::mobility::StochasticStreet street(std::move(baseStreet), 0.5);
for (auto _ : state) {
double fr = street.flowRate();
benchmark::DoNotOptimize(fr);
}
}

static void BM_CoilStreet_AddAgent(benchmark::State& state) {
dsf::mobility::Street street(0, {0, 1}, 100.0, 13.8888888889, 2, "test", {}, 100, 1.0);
street.enableCounter();
Expand Down Expand Up @@ -202,8 +183,6 @@ BENCHMARK(BM_Street_Density);
BENCHMARK(BM_Street_nMovingAgents);
BENCHMARK(BM_Street_nExitingAgents);
BENCHMARK(BM_Street_SetLaneMapping);
BENCHMARK(BM_StochasticStreet_SetFlowRate);
BENCHMARK(BM_StochasticStreet_FlowRate);
BENCHMARK(BM_CoilStreet_AddAgent);
BENCHMARK(BM_CoilStreet_MeanFlow);
BENCHMARK(BM_CoilStreet_Dequeue);
Expand Down
4 changes: 2 additions & 2 deletions src/dsf/dsf.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
#include <format>

static constexpr uint8_t DSF_VERSION_MAJOR = 4;
static constexpr uint8_t DSF_VERSION_MINOR = 5;
static constexpr uint8_t DSF_VERSION_PATCH = 5;
static constexpr uint8_t DSF_VERSION_MINOR = 6;
static constexpr uint8_t DSF_VERSION_PATCH = 0;

static auto const DSF_VERSION =
std::format("{}.{}.{}", DSF_VERSION_MAJOR, DSF_VERSION_MINOR, DSF_VERSION_PATCH);
Expand Down
7 changes: 0 additions & 7 deletions src/dsf/mobility/RoadDynamics.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -696,13 +696,6 @@ namespace dsf::mobility {
auto const& transportCapacity{pStreet->transportCapacity()};
std::uniform_real_distribution<double> uniformDist{0., 1.};
for (auto i = 0; i < std::ceil(transportCapacity); ++i) {
if (pStreet->isStochastic() &&
uniformDist(this->m_generator) >
dynamic_cast<StochasticStreet&>(*pStreet).flowRate()) {
spdlog::trace("Skipping due to flow rate {:.2f} < random value",
dynamic_cast<StochasticStreet&>(*pStreet).flowRate());
continue;
}
if (i == std::ceil(transportCapacity) - 1) {
double integral;
double fractional = std::modf(transportCapacity, &integral);
Expand Down
5 changes: 0 additions & 5 deletions src/dsf/mobility/RoadNetwork.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -798,11 +798,6 @@ namespace dsf::mobility {
pNode = std::make_unique<Station>(*pNode, managementTime);
return node<Station>(nodeId);
}
void RoadNetwork::makeStochasticStreet(Id streetId, double const flowRate) {
auto& pStreet = edge(streetId);
pStreet = std::unique_ptr<StochasticStreet>(
new StochasticStreet(std::move(*pStreet), flowRate));
}
void RoadNetwork::addCoil(Id streetId, std::string const& name) {
edge(streetId)->enableCounter(name);
}
Expand Down
1 change: 0 additions & 1 deletion src/dsf/mobility/RoadNetwork.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,6 @@ namespace dsf::mobility {
/// @throws std::invalid_argument if the node does not exist
Roundabout& makeRoundabout(Id nodeId);

void makeStochasticStreet(Id streetId, double const flowRate);
/// @brief Add a coil (dsf::Counter sensor) on the street with streetId
/// @param streetId The id of the street to add the coil to
/// @param name The coil name
Expand Down
37 changes: 2 additions & 35 deletions src/dsf/mobility/Street.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ namespace dsf::mobility {
spdlog::debug("Adding {} on {}", *pAgent, *this);
m_movingAgents.push(std::move(pAgent));
}
void Street::enqueue(size_t const& queueId) {
void Street::enqueue(std::size_t const& queueId) {
assert(!m_movingAgents.empty());
m_movingAgents.top()->incrementDistance(m_length);
m_exitQueues[queueId].push(
Expand All @@ -113,7 +113,7 @@ namespace dsf::mobility {
++(*m_counter);
}
}
std::unique_ptr<Agent> Street::dequeue(size_t index) {
std::unique_ptr<Agent> Street::dequeue(std::size_t const& index) {
assert(!m_exitQueues[index].empty());
auto pAgent{std::move(m_exitQueues[index].front())};
m_exitQueues[index].pop();
Expand Down Expand Up @@ -169,37 +169,4 @@ namespace dsf::mobility {
return nAgents;
}

StochasticStreet::StochasticStreet(Street&& street, double flowRate)
: Street(std::move(street)) {
setFlowRate(flowRate);
}
StochasticStreet::StochasticStreet(Id id,
std::pair<Id, Id> nodePair,
double length,
double maxSpeed,
int nLanes,
std::string name,
geometry::PolyLine geometry,
double flowRate,
std::optional<int> capacity,
double transportCapacity)
: Street(id,
std::move(nodePair),
length,
maxSpeed,
nLanes,
std::move(name),
std::move(geometry),
capacity,
transportCapacity) {
setFlowRate(flowRate);
}
void StochasticStreet::setFlowRate(double const flowRate) {
if (flowRate < 0. || flowRate > 1.) {
throw std::invalid_argument(
std::format("Flow rate ({}) must be in [0, 1]", flowRate));
}
m_flowRate = flowRate;
}

}; // namespace dsf::mobility
50 changes: 7 additions & 43 deletions src/dsf/mobility/Street.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ namespace dsf::mobility {
}
/// @brief Get the number of of moving agents, i.e. agents not yet enqueued
/// @return int The number of moving agents
int nMovingAgents() const override;
int nMovingAgents() const final;
/// @brief Get the number of agents on all queues for a given direction
/// @param direction The direction of the agents (default is ANY)
/// @param normalizeOnNLanes If true, the number of agents is normalized by the number of lanes
Expand All @@ -152,59 +152,23 @@ namespace dsf::mobility {
bool normalizeOnNLanes = false) const final;
/// @brief Get the street's lane mapping
/// @return std::vector<Direction> The street's lane mapping
inline std::vector<Direction> const& laneMapping() const { return m_laneMapping; }
inline auto const& laneMapping() const { return m_laneMapping; }
/// @brief Add an agent to the street
/// @param pAgent The agent to add to the street
virtual void addAgent(std::unique_ptr<Agent> pAgent);
void addAgent(std::unique_ptr<Agent> pAgent);
/// @brief Add an agent to the street's queue
/// @param queueId The id of the queue
/// @throw std::runtime_error If the street's queue is full
void enqueue(size_t const& queueId);
void enqueue(std::size_t const& queueId);
/// @brief Remove an agent from the street's queue
/// @param index The index of the queue
/// @return Id The id of the agent removed from the street's queue
virtual std::unique_ptr<Agent> dequeue(size_t index);
std::unique_ptr<Agent> dequeue(std::size_t const& index);
/// @brief Check if the street has a coil (dsf::Counter sensor) on it
/// @return bool True if the street has a coil, false otherwise
constexpr bool hasCoil() const { return m_counter.has_value(); };
/// @brief Check if the street is stochastic
/// @return bool True if the street is stochastic, false otherwise
virtual constexpr bool isStochastic() const noexcept { return false; };
};

/// @brief A stochastic street is a street with a flow rate parameter
/// @details The Stochastic Street is used to replace traffic lights with a lower level of detail.
/// The idea is to model the flow of agents in a street as a stochastic process, limiting
/// the number of agents that can exit using a parameter in [0, 1].
/// Thus, the flow rate parameter represents the ratio between the green time of the
/// traffic light and the total time of the traffic light cycle.
class StochasticStreet : public Street {
private:
double m_flowRate;

public:
StochasticStreet(Street&&, double flowRate);
StochasticStreet(Id id,
std::pair<Id, Id> nodePair,
double length = Road::meanVehicleLength(),
double maxSpeed = 13.8888888889,
int nLanes = 1,
std::string name = std::string(),
geometry::PolyLine geometry = {},
double flowRate = 1.,
std::optional<int> capacity = std::nullopt,
double transportCapacity = 1.);
/// @brief Set the flow rate of the street, i.e. the probability that an agent can exit the street
/// @param flowRate The flow rate to set
void setFlowRate(double const flowRate);
/// @brief Get the flow rate of the street
/// @return double The flow rate of the street
inline auto flowRate() const { return m_flowRate; }
/// @brief Check if the street is stochastic
/// @return bool True if the street is stochastic, false otherwise
constexpr bool isStochastic() const noexcept final { return true; };
};

}; // namespace dsf::mobility
} // namespace dsf::mobility

// Specialization of std::formatter for dsf::Street
template <>
Expand Down
6 changes: 3 additions & 3 deletions test/mobility/Test_dynamics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -919,13 +919,13 @@ TEST_CASE("FirstOrderDynamics") {
}
}
GIVEN(
"A dynamics with one stochastic street and one normal street network and an "
"agent") {
"A dynamics with one street with limited flow and one normal street network and "
"an agent") {
Street s1{0, std::make_pair(0, 1), 3.};
Street s2{1, std::make_pair(1, 2), 1.};
s1.setTransportCapacity(0.3);
RoadNetwork graph2;
graph2.addStreets(s1, s2);
graph2.makeStochasticStreet(1, 0.3);
FirstOrderDynamics dynamics{graph2, false, 69, 0., dsf::PathWeight::LENGTH};
dynamics.addItinerary(2, 2);
dynamics.updatePaths();
Expand Down
Loading