Skip to content

Commit 2fac69f

Browse files
authored
[Experimental] Add support for real-time transition probabilities (#368)
* Non va eheh * Finish implementation * stash * Add bindings and format * Add `--output-on-failure` suggested flag in test execution * Some checks more * Prova fix * Small changes * Bump version * Constexpr * Product instead of ratio * Small changes * Formatting
1 parent 5fc278f commit 2fac69f

File tree

12 files changed

+336
-85
lines changed

12 files changed

+336
-85
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ cmake --build build -j$(nproc)
9797

9898
To run the tests use the command:
9999
```shell
100-
ctest --test-dir build -j$(nproc)
100+
ctest --test-dir build -j$(nproc) --output-on-failure
101101
```
102102

103103
## Benchmarking

src/dsf/bindings.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -482,6 +482,23 @@ PYBIND11_MODULE(dsf_cpp, m) {
482482
pybind11::arg("nAgents"),
483483
pybind11::arg("itineraryId") = std::nullopt,
484484
dsf::g_docstrings.at("dsf::mobility::RoadDynamics::addAgentsUniformly").c_str())
485+
.def(
486+
"addRandomAgents",
487+
[](dsf::mobility::FirstOrderDynamics& self, std::size_t nAgents) {
488+
self.addRandomAgents(nAgents);
489+
},
490+
pybind11::arg("nAgents"),
491+
dsf::g_docstrings.at("dsf::mobility::RoadDynamics::addRandomAgents").c_str())
492+
.def(
493+
"addRandomAgents",
494+
[](dsf::mobility::FirstOrderDynamics& self,
495+
std::size_t nAgents,
496+
const std::unordered_map<dsf::Id, double>& src_weights) {
497+
self.addRandomAgents(nAgents, src_weights);
498+
},
499+
pybind11::arg("nAgents"),
500+
pybind11::arg("src_weights"),
501+
dsf::g_docstrings.at("dsf::mobility::RoadDynamics::addRandomAgents").c_str())
485502
.def(
486503
"addAgentsRandomly",
487504
[](dsf::mobility::FirstOrderDynamics& self, dsf::Size nAgents) {

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 = 4;
9-
static constexpr uint8_t DSF_VERSION_PATCH = 2;
9+
static constexpr uint8_t DSF_VERSION_PATCH = 3;
1010

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

src/dsf/mobility/Agent.cpp

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,21 @@ namespace dsf::mobility {
3232
void Agent::setSrcNodeId(Id srcNodeId) { m_srcNodeId = srcNodeId; }
3333
void Agent::setStreetId(std::optional<Id> streetId) {
3434
if (!streetId.has_value()) {
35-
assert(m_nextStreetId.has_value());
35+
if (!m_nextStreetId.has_value()) {
36+
throw std::logic_error(std::format(
37+
"Agent {} has no next street id to set the current street id to", m_id));
38+
}
3639
m_streetId = std::exchange(m_nextStreetId, std::nullopt);
3740
return;
3841
}
39-
assert(m_nextStreetId.has_value() ? streetId == m_nextStreetId.value() : true);
42+
if (m_nextStreetId.has_value()) {
43+
throw std::logic_error(std::format(
44+
"Agent {} has a next street id already set, cannot set street id directly",
45+
m_id));
46+
}
4047
m_streetId = streetId;
4148
m_nextStreetId = std::nullopt;
4249
}
43-
void Agent::setNextStreetId(Id nextStreetId) { m_nextStreetId = nextStreetId; }
4450
void Agent::setSpeed(double speed) {
4551
if (speed < 0.) {
4652
throw std::invalid_argument(

src/dsf/mobility/Agent.hpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ namespace dsf::mobility {
5858
void setStreetId(std::optional<Id> streetId = std::nullopt);
5959
/// @brief Set the id of the next street
6060
/// @param nextStreetId The id of the next street
61-
void setNextStreetId(Id nextStreetId);
61+
inline auto setNextStreetId(Id nextStreetId) { m_nextStreetId = nextStreetId; }
6262
/// @brief Set the agent's speed
6363
/// @param speed, The agent's speed
6464
/// @throw std::invalid_argument, if speed is negative
@@ -127,8 +127,6 @@ struct std::formatter<dsf::mobility::Agent> {
127127
constexpr auto parse(std::format_parse_context& ctx) { return ctx.begin(); }
128128
template <typename FormatContext>
129129
auto format(const dsf::mobility::Agent& agent, FormatContext&& ctx) const {
130-
auto const strItinerary = agent.trip().empty() ? std::string("RANDOM")
131-
: std::to_string(agent.itineraryId());
132130
return std::format_to(
133131
ctx.out(),
134132
"Agent(id: {}, streetId: {}, srcNodeId: {}, nextStreetId: {}, "
@@ -139,7 +137,7 @@ struct std::formatter<dsf::mobility::Agent> {
139137
agent.srcNodeId().has_value() ? std::to_string(agent.srcNodeId().value()) : "N/A",
140138
agent.nextStreetId().has_value() ? std::to_string(agent.nextStreetId().value())
141139
: "N/A",
142-
strItinerary,
140+
agent.isRandom() ? std::string("RANDOM") : std::to_string(agent.itineraryId()),
143141
agent.speed(),
144142
agent.distance(),
145143
agent.spawnTime(),

src/dsf/mobility/FirstOrderDynamics.hpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,9 @@ namespace dsf::mobility {
77
double m_alpha;
88
double m_speedFluctuationSTD;
99

10-
double m_speedFactor(double const& density) const;
10+
double m_speedFactor(double const& density) const final;
1111

12-
double m_streetEstimatedTravelTime(
13-
std::unique_ptr<Street> const& pStreet) const override;
12+
double m_streetEstimatedTravelTime(std::unique_ptr<Street> const& pStreet) const final;
1413

1514
public:
1615
/// @brief Construct a new First Order Dynamics object

0 commit comments

Comments
 (0)