Skip to content

Commit cb1f4f7

Browse files
committed
End adding random agents
1 parent dc7c70d commit cb1f4f7

File tree

5 files changed

+46
-14
lines changed

5 files changed

+46
-14
lines changed

src/dsm/headers/Dynamics.hpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,9 @@ namespace dsm {
241241
/// @brief Get the agents
242242
/// @return const std::unordered_map<Id, Agent<Id>>&, The agents
243243
const std::map<Id, std::unique_ptr<agent_t>>& agents() const { return m_agents; }
244+
/// @brief Get the number of agents currently in the simulation
245+
/// @return Size The number of agents
246+
const Size nAgents() const { return m_agents.size(); }
244247
/// @brief Get the time
245248
/// @return Time The time
246249
Time time() const { return m_time; }
@@ -569,7 +572,7 @@ namespace dsm {
569572
if (!m_agents.empty()) {
570573
agentId = m_agents.rbegin()->first + 1;
571574
}
572-
for (auto i{0}; i < nAgents; ++i) {
575+
for (auto i{0}; i < nAgents; ++i, ++agentId) {
573576
this->addAgent(agent_t{agentId, srcNodeId});
574577
}
575578
}
@@ -580,7 +583,6 @@ namespace dsm {
580583
}
581584

582585
template <typename agent_t>
583-
584586
template <typename T1, typename... Tn>
585587
requires(std::is_convertible_v<T1, Size> && (std::is_convertible_v<Tn, Size> && ...))
586588
void Dynamics<agent_t>::removeAgents(T1 id, Tn... ids) {

src/dsm/headers/Graph.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ namespace dsm {
143143
}
144144
}
145145

146-
void Graph::importMatrix(const std::string& fileName, bool isAdj) {
146+
void Graph::importMatrix(const std::string& fileName, bool isAdj, double defaultSpeed) {
147147
// check the file extension
148148
std::string fileExt = fileName.substr(fileName.find_last_of(".") + 1);
149149
if (fileExt == "dsm") {
@@ -178,6 +178,7 @@ namespace dsm {
178178
if (!isAdj) {
179179
m_streets[index]->setLength(val);
180180
}
181+
m_streets[index]->setMaxSpeed(defaultSpeed);
181182
}
182183
} else {
183184
// default case: read the file as a matrix with the first two elements being the number of rows and columns and
@@ -223,6 +224,7 @@ namespace dsm {
223224
if (!isAdj) {
224225
m_streets[index]->setLength(value);
225226
}
227+
m_streets[index]->setMaxSpeed(defaultSpeed);
226228
}
227229
++index;
228230
}

src/dsm/headers/Graph.hpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,9 +121,12 @@ namespace dsm {
121121
/// the number of rows and columns and the following elements being the matrix elements.
122122
/// @param fileName The name of the file to import the adjacency matrix from.
123123
/// @param isAdj A boolean value indicating if the file contains the adjacency matrix or the distance matrix.
124+
/// @param defaultSpeed The default speed limit for the streets
124125
/// @throws std::invalid_argument if the file is not found or invalid
125126
/// The matrix format is deduced from the file extension. Currently only .dsm files are supported.
126-
void importMatrix(const std::string& fileName, bool isAdj = true);
127+
void importMatrix(const std::string& fileName,
128+
bool isAdj = true,
129+
double defaultSpeed = 13.8888888889);
127130
/// @brief Import the graph's nodes from a file
128131
/// @param fileName The name of the file to import the nodes from.
129132
/// @throws std::invalid_argument if the file is not found, invalid or the format is not supported

src/dsm/headers/RoadDynamics.hpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,8 @@ namespace dsm {
359359
template <typename delay_t>
360360
requires(is_numeric_v<delay_t>)
361361
void RoadDynamics<delay_t>::m_evolveAgents() {
362+
std::uniform_int_distribution<Id> nodeDist{
363+
0, static_cast<Id>(this->m_graph.nNodes() - 1)};
362364
for (const auto& [agentId, agent] : this->m_agents) {
363365
if (agent->delay() > 0) {
364366
const auto& street{this->m_graph.streetSet()[agent->streetId().value()]};
@@ -418,8 +420,9 @@ namespace dsm {
418420
}
419421
} else if (!agent->streetId().has_value() &&
420422
!m_agentNextStreetId.contains(agentId)) {
421-
assert(agent->srcNodeId().has_value());
422-
const auto& srcNode{this->m_graph.nodeSet()[agent->srcNodeId().value()]};
423+
Id srcNodeId = agent->srcNodeId().has_value() ? agent->srcNodeId().value()
424+
: nodeDist(this->m_generator);
425+
const auto& srcNode{this->m_graph.nodeSet()[srcNodeId]};
423426
if (srcNode->isFull()) {
424427
continue;
425428
}

test/Test_dynamics.cpp

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ TEST_CASE("Dynamics") {
127127
WHEN("We add the agent") {
128128
dynamics.addAgent(0, 2);
129129
THEN("The agent is added") {
130-
CHECK_EQ(dynamics.agents().size(), 1);
130+
CHECK_EQ(dynamics.nAgents(), 1);
131131
const auto& agent = dynamics.agents().at(0);
132132
CHECK_EQ(agent->id(), 0);
133133
CHECK_EQ(agent->srcNodeId().value(), 0);
@@ -163,7 +163,7 @@ TEST_CASE("Dynamics") {
163163
THEN(
164164
"The number of agents is 1 and the destination is the same as the "
165165
"itinerary") {
166-
CHECK_EQ(dynamics.agents().size(), 1);
166+
CHECK_EQ(dynamics.nAgents(), 1);
167167
CHECK_EQ(dynamics.itineraries()
168168
.at(dynamics.agents().at(0)->itineraryId())
169169
->destination(),
@@ -184,7 +184,7 @@ TEST_CASE("Dynamics") {
184184
"The number of agents is 3, the destination and the street is the "
185185
"same as "
186186
"the itinerary") {
187-
CHECK_EQ(dynamics.agents().size(), 3);
187+
CHECK_EQ(dynamics.nAgents(), 3);
188188
CHECK(dynamics.agents().at(0)->streetId().has_value());
189189
CHECK(dynamics.agents().at(1)->streetId().has_value());
190190
CHECK(dynamics.agents().at(2)->streetId().has_value());
@@ -236,7 +236,7 @@ TEST_CASE("Dynamics") {
236236
dynamics.addItinerary(Itinerary{0, 2});
237237
dynamics.addAgentsRandomly(1, src, dst);
238238
THEN("The agents are correctly set") {
239-
CHECK_EQ(dynamics.agents().size(), 1);
239+
CHECK_EQ(dynamics.nAgents(), 1);
240240
CHECK_EQ(dynamics.itineraries()
241241
.at(dynamics.agents().at(0)->itineraryId())
242242
->destination(),
@@ -252,7 +252,7 @@ TEST_CASE("Dynamics") {
252252
dynamics.addItinerary(Itinerary{2, 107});
253253
dynamics.addAgentsRandomly(3, src, dst);
254254
THEN("The agents are correctly set") {
255-
CHECK_EQ(dynamics.agents().size(), 3);
255+
CHECK_EQ(dynamics.nAgents(), 3);
256256
CHECK_EQ(dynamics.itineraries()
257257
.at(dynamics.agents().at(0)->itineraryId())
258258
->destination(),
@@ -279,6 +279,28 @@ TEST_CASE("Dynamics") {
279279
}
280280
}
281281
}
282+
SUBCASE("addRandomAgents") {
283+
GIVEN("A dynamics object") {
284+
auto const p{0.1};
285+
auto const n{100};
286+
auto graph = Graph{};
287+
graph.importMatrix("./data/matrix.dat", false);
288+
graph.buildAdj();
289+
graph.normalizeStreetCapacities();
290+
Dynamics dynamics{graph, 69};
291+
dynamics.setPassageProbability(p);
292+
WHEN("We add some agent") {
293+
dynamics.addRandomAgents(n);
294+
THEN("The number of agents is correct") { CHECK_EQ(dynamics.nAgents(), 100); }
295+
THEN("If we evolve the dynamics agent disappear gradually") {
296+
for (auto i{0}; i < 40; ++i) {
297+
dynamics.evolve(false);
298+
}
299+
CHECK(dynamics.nAgents() < n);
300+
}
301+
}
302+
}
303+
}
282304
SUBCASE("addAgents") {
283305
GIVEN("A dynamics object and one itinerary") {
284306
auto graph = Graph{};
@@ -296,7 +318,7 @@ TEST_CASE("Dynamics") {
296318
THEN(
297319
"The number of agents is 1 and the destination is the same as the "
298320
"itinerary") {
299-
CHECK_EQ(dynamics.agents().size(), 1);
321+
CHECK_EQ(dynamics.nAgents(), 1);
300322
CHECK_EQ(dynamics.itineraries()
301323
.at(dynamics.agents().at(0)->itineraryId())
302324
->destination(),
@@ -305,7 +327,7 @@ TEST_CASE("Dynamics") {
305327
}
306328
WHEN("We add 69 agents with itinerary 0") {
307329
dynamics.addAgents(0, 69);
308-
THEN("The number of agents is 69") { CHECK_EQ(dynamics.agents().size(), 69); }
330+
THEN("The number of agents is 69") { CHECK_EQ(dynamics.nAgents(), 69); }
309331
}
310332
}
311333
}
@@ -524,7 +546,7 @@ TEST_CASE("Dynamics") {
524546
}
525547
dynamics.evolve(true);
526548
THEN("The agent is reinserted") {
527-
CHECK_EQ(dynamics.agents().size(), 1);
549+
CHECK_EQ(dynamics.nAgents(), 1);
528550
CHECK_EQ(dynamics.agents().at(0)->time(), 1);
529551
CHECK_EQ(dynamics.agents().at(0)->delay(), 0);
530552
CHECK_FALSE(dynamics.agents().at(0)->streetId().has_value());

0 commit comments

Comments
 (0)