Skip to content

Commit 0a921de

Browse files
committed
Rework setDestinationNodes function in Dynamics class
1 parent 5131e98 commit 0a921de

File tree

3 files changed

+39
-44
lines changed

3 files changed

+39
-44
lines changed

benchmark/Dynamics/BenchDynamics.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,12 @@ int main() {
1818
graph.buildAdj();
1919

2020
Dynamics dynamics{graph};
21-
std::vector<dsm::Id> destinations{10, 42, 69, 121, 420, 690, 777, 999, 1020, 1212};
22-
dynamics.setDestinationNodes(destinations);
21+
dynamics.setDestinationNodes({10, 42, 69, 121, 420, 690, 777, 999, 1020, 1212}, false);
2322

2423
const int n_rep{1};
2524
Bench b1(n_rep);
2625
std::cout << "Benchmarking updatePaths\n";
27-
dynamics.updatePaths();
2826
b1.benchmark([&dynamics]() -> void { dynamics.updatePaths(); });
29-
std::cout << "Time elapsed (ms):\n";
30-
b1.print<sb::milliseconds>();
27+
std::cout << "Time elapsed (s):\n";
28+
b1.print<sb::seconds>();
3129
}

src/dsm/headers/Dynamics.hpp

Lines changed: 32 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -200,12 +200,18 @@ namespace dsm {
200200
/// @brief Update the paths of the itineraries based on the actual travel times
201201
virtual void updatePaths();
202202

203-
/// @brief Set the dynamics destination nodes auto-generating itineraries
204-
/// @param destinationNodes The destination nodes
203+
/// @brief Set the destination nodes
204+
/// @param destinationNodes The destination nodes (as an initializer list)
205205
/// @param updatePaths If true, the paths are updated
206-
/// @throws std::invalid_argument Ifone or more destination nodes do not exist
207-
void setDestinationNodes(const std::span<Id>& destinationNodes,
206+
void setDestinationNodes(std::initializer_list<Id> destinationNodes,
208207
bool updatePaths = true);
208+
/// @brief Set the destination nodes
209+
/// @param destinationNodes A container of destination nodes ids
210+
/// @param updatePaths If true, the paths are updated
211+
/// @details The container must have a value_type convertible to Id and begin() and end() methods
212+
template <typename TContainer>
213+
requires(std::is_convertible_v<typename TContainer::value_type, Id>)
214+
void setDestinationNodes(TContainer const& destinationNodes, bool updatePaths = true);
209215

210216
/// @brief Add an agent to the simulation
211217
/// @param agent std::unique_ptr to the agent
@@ -253,6 +259,7 @@ namespace dsm {
253259
/// @brief Add an itinerary
254260
/// @param itinerary std::unique_ptr to the itinerary
255261
/// @throws std::invalid_argument If the itinerary already exists
262+
/// @throws std::invalid_argument If the itinerary's destination is not a node of the graph
256263
void addItinerary(std::unique_ptr<Itinerary> itinerary);
257264
template <typename... Tn>
258265
requires(is_itinerary_v<Tn> && ...)
@@ -373,36 +380,28 @@ namespace dsm {
373380
m_itineraries.cbegin(), m_itineraries.cend(), [this](auto const& pair) -> void {
374381
this->m_updatePath(pair.second);
375382
});
376-
// std::vector<std::thread> threads;
377-
// threads.reserve(m_itineraries.size());
378-
// std::exception_ptr pThreadException;
379-
// for (const auto& [itineraryId, itinerary] : m_itineraries) {
380-
// threads.emplace_back(std::thread([this, &itinerary, &pThreadException] {
381-
// try {
382-
// this->m_updatePath(itinerary);
383-
// } catch (...) {
384-
// if (!pThreadException)
385-
// pThreadException = std::current_exception();
386-
// }
387-
// }));
388-
// }
389-
// for (auto& thread : threads) {
390-
// thread.join();
391-
// }
392-
// // Throw the exception launched first
393-
// if (pThreadException)
394-
// std::rethrow_exception(pThreadException);
395383
}
396384

397385
template <typename agent_t>
398-
void Dynamics<agent_t>::setDestinationNodes(const std::span<Id>& destinationNodes,
386+
void Dynamics<agent_t>::setDestinationNodes(std::initializer_list<Id> destinationNodes,
399387
bool updatePaths) {
400-
for (const auto& nodeId : destinationNodes) {
401-
if (!m_graph.nodeSet().contains(nodeId)) {
402-
Logger::error(std::format("Node with id {} not found", nodeId));
403-
}
404-
this->addItinerary(nodeId, nodeId);
388+
std::for_each(
389+
destinationNodes.begin(),
390+
destinationNodes.end(),
391+
[this](auto const& nodeId) -> void { this->addItinerary(nodeId, nodeId); });
392+
if (updatePaths) {
393+
this->updatePaths();
405394
}
395+
}
396+
template <typename agent_t>
397+
template <typename TContainer>
398+
requires(std::is_convertible_v<typename TContainer::value_type, Id>)
399+
void Dynamics<agent_t>::setDestinationNodes(TContainer const& destinationNodes,
400+
bool updatePaths) {
401+
std::for_each(
402+
destinationNodes.begin(),
403+
destinationNodes.end(),
404+
[this](auto const& nodeId) -> void { this->addItinerary(nodeId, nodeId); });
406405
if (updatePaths) {
407406
this->updatePaths();
408407
}
@@ -488,6 +487,10 @@ namespace dsm {
488487
throw std::invalid_argument(Logger::buildExceptionMessage(
489488
std::format("Itinerary with id {} already exists.", itinerary->id())));
490489
}
490+
if (!m_graph.nodeSet().contains(itinerary->destination())) {
491+
throw std::invalid_argument(Logger::buildExceptionMessage(std::format(
492+
"Destination node with id {} not found", itinerary->destination())));
493+
}
491494
m_itineraries.emplace(itinerary->id(), std::move(itinerary));
492495
}
493496

test/Test_dynamics.cpp

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -512,8 +512,7 @@ TEST_CASE("Dynamics") {
512512
graph2.addStreets(s0_1, s1_0, s1_2, s2_1);
513513
graph2.buildAdj();
514514
Dynamics dynamics{graph2, false, 69};
515-
std::vector<dsm::Id> dsts{1, 2};
516-
dynamics.setDestinationNodes(dsts);
515+
dynamics.setDestinationNodes({1, 2});
517516
std::vector<dsm::Id> trip{2, 1};
518517
dynamics.addAgent(0, trip, 0);
519518
auto const& pAgent{dynamics.agents().at(0)};
@@ -619,9 +618,7 @@ TEST_CASE("Dynamics") {
619618
graph2.buildStreetAngles();
620619

621620
Dynamics dynamics{graph2, false, 69};
622-
623-
std::vector<uint32_t> destinationNodes{0, 2, 3, 4};
624-
dynamics.setDestinationNodes(destinationNodes);
621+
dynamics.setDestinationNodes({0, 2, 3, 4});
625622

626623
WHEN("We add agents and make the system evolve") {
627624
Agent agent1{0, 2, 0};
@@ -688,9 +685,7 @@ TEST_CASE("Dynamics") {
688685
graph2.buildStreetAngles();
689686

690687
Dynamics dynamics{graph2, false, 69};
691-
692-
std::vector<uint32_t> destinationNodes{0, 2, 3, 4};
693-
dynamics.setDestinationNodes(destinationNodes);
688+
dynamics.setDestinationNodes({0, 2, 3, 4});
694689

695690
WHEN("We add agents and make the system evolve") {
696691
Agent agent1{0, 2, 0};
@@ -744,8 +739,7 @@ TEST_CASE("Dynamics") {
744739
tl.setComplementaryCycle(16, 11);
745740
tl.setComplementaryCycle(21, 11);
746741
Dynamics dynamics{graph2, false, 69};
747-
std::vector<dsm::Id> destinationNodes{0, 2, 3, 4};
748-
dynamics.setDestinationNodes(destinationNodes);
742+
dynamics.setDestinationNodes({0, 2, 3, 4});
749743
WHEN("We evolve the dynamics and optimize traffic lights") {
750744
dynamics.addAgents(7, 0, 2);
751745
dynamics.addAgents(7, 2, 0);

0 commit comments

Comments
 (0)