diff --git a/src/dsf/bindings.cpp b/src/dsf/bindings.cpp index f42025c1..3ec5a4f5 100644 --- a/src/dsf/bindings.cpp +++ b/src/dsf/bindings.cpp @@ -367,6 +367,7 @@ PYBIND11_MODULE(dsf_cpp, m) { pybind11::arg("alpha") = 0., pybind11::arg("weightFunction") = dsf::PathWeight::TRAVELTIME, pybind11::arg("weightThreshold") = std::nullopt, + pybind11::keep_alive<1, 2>(), dsf::g_docstrings.at("dsf::mobility::FirstOrderDynamics::FirstOrderDynamics") .c_str()) // Note: Constructors with std::function parameters are not exposed to avoid stub generation issues @@ -439,7 +440,7 @@ PYBIND11_MODULE(dsf_cpp, m) { const std::unordered_map& originNodes) { self.setOriginNodes(originNodes); }, - pybind11::arg("originNodes"), + pybind11::arg("originNodes") = std::unordered_map(), dsf::g_docstrings.at("dsf::mobility::RoadDynamics::setOriginNodes").c_str()) .def( "setOriginNodes", diff --git a/src/dsf/dsf.hpp b/src/dsf/dsf.hpp index 5395674a..a6d056df 100644 --- a/src/dsf/dsf.hpp +++ b/src/dsf/dsf.hpp @@ -6,7 +6,7 @@ static constexpr uint8_t DSF_VERSION_MAJOR = 4; static constexpr uint8_t DSF_VERSION_MINOR = 5; -static constexpr uint8_t DSF_VERSION_PATCH = 1; +static constexpr uint8_t DSF_VERSION_PATCH = 2; static auto const DSF_VERSION = std::format("{}.{}.{}", DSF_VERSION_MAJOR, DSF_VERSION_MINOR, DSF_VERSION_PATCH); diff --git a/src/dsf/mobility/RoadDynamics.hpp b/src/dsf/mobility/RoadDynamics.hpp index 4f95bc93..4f1d2af0 100644 --- a/src/dsf/mobility/RoadDynamics.hpp +++ b/src/dsf/mobility/RoadDynamics.hpp @@ -165,9 +165,10 @@ namespace dsf::mobility { inline void setMeanTravelTime(std::time_t const meanTravelTime) noexcept { m_meanTravelTime = meanTravelTime; }; - /// @brief Set the origin nodes + /// @brief Set the origin nodes. If the provided map is empty, the origin nodes are set using the streets' stationary weights. + /// NOTE: the default stationary weights are 1.0 so, if not set, this is equivalent to setting uniform weights. /// @param originNodes The origin nodes - void setOriginNodes(std::unordered_map const& originNodes); + void setOriginNodes(std::unordered_map const& originNodes = {}); /// @brief Set the destination nodes /// @param destinationNodes The destination nodes void setDestinationNodes(std::unordered_map const& destinationNodes); @@ -1140,6 +1141,19 @@ namespace dsf::mobility { std::unordered_map const& originNodes) { m_originNodes.clear(); m_originNodes.reserve(originNodes.size()); + if (originNodes.empty()) { + // If no origin nodes are provided, try to set origin nodes basing on streets' stationary weights + double totalStationaryWeight = 0.0; + for (auto const& [edgeId, pEdge] : this->graph().edges()) { + auto const& weight = pEdge->stationaryWeight(); + m_originNodes[pEdge->source()] += weight; + totalStationaryWeight += weight; + } + for (auto& [nodeId, weight] : m_originNodes) { + weight /= totalStationaryWeight; + } + return; + } auto const sumWeights = std::accumulate( originNodes.begin(), originNodes.end(), 0., [](double sum, auto const& pair) { return sum + pair.second;