Skip to content

Commit 9d263b0

Browse files
authored
Set nextStreetId as Agent class attribute (#261)
* Set `nextStreetId` as Agent class attribute * Fix profiling workflow * Add assertion
1 parent 248ae5f commit 9d263b0

File tree

4 files changed

+30
-15
lines changed

4 files changed

+30
-15
lines changed

.github/workflows/profile_mem_usage.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ jobs:
1717
- uses: actions/checkout@v4
1818

1919
- name: Install valgrind
20-
run: sudo apt install -y valgrind
20+
run: sudo apt update && sudo apt install -y valgrind
2121

2222
- name: Build test simulation
2323
working-directory: ${{github.workspace}}/profiling

src/dsm/dsm.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 DSM_VERSION_MAJOR = 2;
88
static constexpr uint8_t DSM_VERSION_MINOR = 3;
9-
static constexpr uint8_t DSM_VERSION_PATCH = 23;
9+
static constexpr uint8_t DSM_VERSION_PATCH = 24;
1010

1111
static auto const DSM_VERSION =
1212
std::format("{}.{}.{}", DSM_VERSION_MAJOR, DSM_VERSION_MINOR, DSM_VERSION_PATCH);

src/dsm/headers/Agent.hpp

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ namespace dsm {
3434
std::vector<Id> m_trip;
3535
std::optional<Id> m_streetId;
3636
std::optional<Id> m_srcNodeId;
37+
std::optional<Id> m_nextStreetId;
3738
delay_t m_delay;
3839
double m_speed;
3940
double m_distance; // Travelled distance
@@ -55,7 +56,10 @@ namespace dsm {
5556
Agent(Id id, std::vector<Id> const& trip, std::optional<Id> srcNodeId = std::nullopt);
5657
/// @brief Set the street occupied by the agent
5758
/// @param streetId The id of the street currently occupied by the agent
58-
void setStreetId(Id streetId) { m_streetId = streetId; }
59+
void setStreetId(Id streetId);
60+
/// @brief Set the id of the next street
61+
/// @param nextStreetId The id of the next street
62+
void setNextStreetId(Id nextStreetId) { m_nextStreetId = nextStreetId; }
5963
/// @brief Set the agent's speed
6064
/// @param speed, The agent's speed
6165
/// @throw std::invalid_argument, if speed is negative
@@ -114,6 +118,9 @@ namespace dsm {
114118
/// @brief Get the id of the source node of the agent
115119
/// @return The id of the source node of the agent
116120
std::optional<Id> srcNodeId() const { return m_srcNodeId; }
121+
/// @brief Get the id of the next street
122+
/// @return The id of the next street
123+
std::optional<Id> nextStreetId() const { return m_nextStreetId; }
117124
/// @brief Get the agent's speed
118125
/// @return The agent's speed
119126
double speed() const { return m_speed; }
@@ -138,6 +145,7 @@ namespace dsm {
138145
m_trip{itineraryId.has_value() ? std::vector<Id>{itineraryId.value()}
139146
: std::vector<Id>{}},
140147
m_srcNodeId{srcNodeId},
148+
m_nextStreetId{std::nullopt},
141149
m_delay{0},
142150
m_speed{0.},
143151
m_distance{0.},
@@ -150,6 +158,7 @@ namespace dsm {
150158
: m_id{id},
151159
m_trip{trip},
152160
m_srcNodeId{srcNodeId},
161+
m_nextStreetId{std::nullopt},
153162
m_delay{0},
154163
m_speed{0.},
155164
m_distance{0.},
@@ -163,6 +172,14 @@ namespace dsm {
163172
return m_trip[m_itineraryIdx];
164173
}
165174

175+
template <typename delay_t>
176+
requires(is_numeric_v<delay_t>)
177+
void Agent<delay_t>::setStreetId(Id streetId) {
178+
assert(m_nextStreetId.has_value() ? streetId == m_nextStreetId.value() : true);
179+
m_streetId = streetId;
180+
m_nextStreetId = std::nullopt;
181+
}
182+
166183
template <typename delay_t>
167184
requires(is_numeric_v<delay_t>)
168185
void Agent<delay_t>::setSpeed(double speed) {

src/dsm/headers/RoadDynamics.hpp

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ namespace dsm {
5252

5353
protected:
5454
std::vector<std::pair<double, double>> m_travelDTs;
55-
std::unordered_map<Id, Id> m_agentNextStreetId;
5655
bool m_forcePriorities;
5756
std::optional<delay_t> m_dataUpdatePeriod;
5857
std::unordered_map<Id, std::array<unsigned long long, 4>> m_turnCounts;
@@ -306,7 +305,6 @@ namespace dsm {
306305
bool bArrived{false};
307306
if (!bCanPass) {
308307
if (pAgent->isRandom()) {
309-
m_agentNextStreetId.erase(agentId);
310308
bArrived = true;
311309
} else {
312310
continue;
@@ -331,7 +329,8 @@ namespace dsm {
331329
}
332330
continue;
333331
}
334-
auto const& nextStreet{this->m_graph.streetSet()[m_agentNextStreetId[agentId]]};
332+
auto const& nextStreet{
333+
this->m_graph.street(this->agents().at(agentId)->nextStreetId().value())};
335334
if (nextStreet->isFull()) {
336335
continue;
337336
}
@@ -360,7 +359,8 @@ namespace dsm {
360359
return false;
361360
}
362361
for (auto const [angle, agentId] : intersection.agents()) {
363-
auto const& nextStreet{this->m_graph.streetSet()[m_agentNextStreetId[agentId]]};
362+
auto const& nextStreet{
363+
this->m_graph.street(this->agents().at(agentId)->nextStreetId().value())};
364364
if (nextStreet->isFull()) {
365365
if (m_forcePriorities) {
366366
return false;
@@ -373,7 +373,6 @@ namespace dsm {
373373
this->agents().at(agentId)->incrementDelay(
374374
std::ceil(nextStreet->length() / this->agents().at(agentId)->speed()));
375375
nextStreet->addAgent(agentId);
376-
m_agentNextStreetId.erase(agentId);
377376
return true;
378377
}
379378
return false;
@@ -383,7 +382,8 @@ namespace dsm {
383382
return false;
384383
}
385384
auto const agentId{roundabout.agents().front()};
386-
auto const& nextStreet{this->m_graph.streetSet()[m_agentNextStreetId[agentId]]};
385+
auto const& nextStreet{
386+
this->m_graph.street(this->agents().at(agentId)->nextStreetId().value())};
387387
if (!(nextStreet->isFull())) {
388388
if (this->agents().at(agentId)->streetId().has_value()) {
389389
const auto streetId = this->agents().at(agentId)->streetId().value();
@@ -401,7 +401,6 @@ namespace dsm {
401401
this->agents().at(agentId)->incrementDelay(
402402
std::ceil(nextStreet->length() / this->agents().at(agentId)->speed()));
403403
nextStreet->addAgent(agentId);
404-
m_agentNextStreetId.erase(agentId);
405404
} else {
406405
return false;
407406
}
@@ -448,8 +447,8 @@ namespace dsm {
448447
} else {
449448
auto const nextStreetId =
450449
this->m_nextStreetId(agentId, street->nodePair().second, street->id());
451-
auto const& pNextStreet{this->m_graph.streetSet()[nextStreetId]};
452-
m_agentNextStreetId.emplace(agentId, nextStreetId);
450+
auto const& pNextStreet{this->m_graph.street(nextStreetId)};
451+
agent->setNextStreetId(nextStreetId);
453452
if (nLanes == 1) {
454453
street->enqueue(agentId, 0);
455454
} else {
@@ -490,8 +489,7 @@ namespace dsm {
490489
}
491490
}
492491
}
493-
} else if (!agent->streetId().has_value() &&
494-
!m_agentNextStreetId.contains(agentId)) {
492+
} else if (!agent->streetId().has_value() && !agent->nextStreetId().has_value()) {
495493
Id srcNodeId = agent->srcNodeId().has_value() ? agent->srcNodeId().value()
496494
: nodeDist(this->m_generator);
497495
const auto& srcNode{this->m_graph.nodeSet()[srcNodeId]};
@@ -511,7 +509,7 @@ namespace dsm {
511509
auto& roundabout = dynamic_cast<Roundabout&>(*srcNode);
512510
roundabout.enqueue(agentId);
513511
}
514-
m_agentNextStreetId.emplace(agentId, nextStreet->id());
512+
agent->setNextStreetId(nextStreet->id());
515513
} else if (agent->delay() == 0) {
516514
agent->setSpeed(0.);
517515
}

0 commit comments

Comments
 (0)