Skip to content

Commit c6d6d63

Browse files
authored
Allow automatic source nodes assignment (#380)
* Default behavior for empty origins * Bindings * Fix? * Bump version * Docs
1 parent e77f593 commit c6d6d63

File tree

3 files changed

+19
-4
lines changed

3 files changed

+19
-4
lines changed

src/dsf/bindings.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,7 @@ PYBIND11_MODULE(dsf_cpp, m) {
367367
pybind11::arg("alpha") = 0.,
368368
pybind11::arg("weightFunction") = dsf::PathWeight::TRAVELTIME,
369369
pybind11::arg("weightThreshold") = std::nullopt,
370+
pybind11::keep_alive<1, 2>(),
370371
dsf::g_docstrings.at("dsf::mobility::FirstOrderDynamics::FirstOrderDynamics")
371372
.c_str())
372373
// Note: Constructors with std::function parameters are not exposed to avoid stub generation issues
@@ -439,7 +440,7 @@ PYBIND11_MODULE(dsf_cpp, m) {
439440
const std::unordered_map<dsf::Id, double>& originNodes) {
440441
self.setOriginNodes(originNodes);
441442
},
442-
pybind11::arg("originNodes"),
443+
pybind11::arg("originNodes") = std::unordered_map<dsf::Id, double>(),
443444
dsf::g_docstrings.at("dsf::mobility::RoadDynamics::setOriginNodes").c_str())
444445
.def(
445446
"setOriginNodes",

src/dsf/dsf.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
static constexpr uint8_t DSF_VERSION_MAJOR = 4;
88
static constexpr uint8_t DSF_VERSION_MINOR = 5;
9-
static constexpr uint8_t DSF_VERSION_PATCH = 1;
9+
static constexpr uint8_t DSF_VERSION_PATCH = 2;
1010

1111
static auto const DSF_VERSION =
1212
std::format("{}.{}.{}", DSF_VERSION_MAJOR, DSF_VERSION_MINOR, DSF_VERSION_PATCH);

src/dsf/mobility/RoadDynamics.hpp

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,9 +165,10 @@ namespace dsf::mobility {
165165
inline void setMeanTravelTime(std::time_t const meanTravelTime) noexcept {
166166
m_meanTravelTime = meanTravelTime;
167167
};
168-
/// @brief Set the origin nodes
168+
/// @brief Set the origin nodes. If the provided map is empty, the origin nodes are set using the streets' stationary weights.
169+
/// NOTE: the default stationary weights are 1.0 so, if not set, this is equivalent to setting uniform weights.
169170
/// @param originNodes The origin nodes
170-
void setOriginNodes(std::unordered_map<Id, double> const& originNodes);
171+
void setOriginNodes(std::unordered_map<Id, double> const& originNodes = {});
171172
/// @brief Set the destination nodes
172173
/// @param destinationNodes The destination nodes
173174
void setDestinationNodes(std::unordered_map<Id, double> const& destinationNodes);
@@ -1140,6 +1141,19 @@ namespace dsf::mobility {
11401141
std::unordered_map<Id, double> const& originNodes) {
11411142
m_originNodes.clear();
11421143
m_originNodes.reserve(originNodes.size());
1144+
if (originNodes.empty()) {
1145+
// If no origin nodes are provided, try to set origin nodes basing on streets' stationary weights
1146+
double totalStationaryWeight = 0.0;
1147+
for (auto const& [edgeId, pEdge] : this->graph().edges()) {
1148+
auto const& weight = pEdge->stationaryWeight();
1149+
m_originNodes[pEdge->source()] += weight;
1150+
totalStationaryWeight += weight;
1151+
}
1152+
for (auto& [nodeId, weight] : m_originNodes) {
1153+
weight /= totalStationaryWeight;
1154+
}
1155+
return;
1156+
}
11431157
auto const sumWeights = std::accumulate(
11441158
originNodes.begin(), originNodes.end(), 0., [](double sum, auto const& pair) {
11451159
return sum + pair.second;

0 commit comments

Comments
 (0)