@@ -67,7 +67,7 @@ namespace dsm {
6767 requires (is_numeric_v<Delay>)
6868 class Dynamics {
6969 protected:
70- std::unordered_map<Id, std::unique_ptr<Itinerary>> m_itineraries;
70+ std::vector< std::unique_ptr<Itinerary>> m_itineraries;
7171 std::map<Id, std::unique_ptr<Agent<Delay>>> m_agents;
7272 TimePoint m_time, m_previousSpireTime;
7373 Graph m_graph;
@@ -154,9 +154,8 @@ namespace dsm {
154154 }
155155 if (path.size () == 0 ) {
156156 throw std::runtime_error (
157- buildLog (std::format (" Path with id {} and destination {} is empty. Please "
157+ buildLog (std::format (" Path destination {} is empty. Please "
158158 " check the adjacency matrix." ,
159- pItinerary->id (),
160159 pItinerary->destination ())));
161160 }
162161 pItinerary->setPath (path);
@@ -232,9 +231,11 @@ namespace dsm {
232231 // / @brief Get the graph
233232 // / @return const Graph&, The graph
234233 const Graph& graph () const { return m_graph; };
234+
235+ const std::unique_ptr<Itinerary>& itinerary (Id destination) const ;
235236 // / @brief Get the itineraries
236237 // / @return const std::unordered_map<Id, Itinerary>&, The itineraries
237- const std::unordered_map<Id, std::unique_ptr<Itinerary>>& itineraries () const {
238+ const std::vector< std::unique_ptr<Itinerary>>& itineraries () const {
238239 return m_itineraries;
239240 }
240241 // / @brief Get the agents
@@ -703,25 +704,38 @@ namespace dsm {
703704 requires (is_numeric_v<Delay>)
704705 void Dynamics<Delay>::setDestinationNodes(const std::span<Id>& destinationNodes,
705706 bool updatePaths) {
707+ m_itineraries.clear ();
708+ m_itineraries.reserve (destinationNodes.size ());
706709 for (const auto & nodeId : destinationNodes) {
707710 if (!m_graph.nodeSet ().contains (nodeId)) {
708711 throw std::invalid_argument (
709712 buildLog (std::format (" Node with id {} not found" , nodeId)));
710713 }
711- this ->addItinerary (Itinerary{nodeId, nodeId });
714+ this ->addItinerary (Itinerary{nodeId});
712715 }
713716 if (updatePaths) {
714717 this ->updatePaths ();
715718 }
716719 }
717720
721+ template <typename Delay>
722+ requires (is_numeric_v<Delay>)
723+ const std::unique_ptr<Itinerary>& Dynamics<Delay>::itinerary(Id destination) const {
724+ for (auto const & pItinerary : m_itineraries) {
725+ if (pItinerary->destination () == destination) {
726+ return pItinerary;
727+ }
728+ }
729+ return nullptr ;
730+ }
731+
718732 template <typename Delay>
719733 requires (is_numeric_v<Delay>)
720734 void Dynamics<Delay>::updatePaths() {
721735 std::vector<std::thread> threads;
722736 threads.reserve (m_itineraries.size ());
723737 std::exception_ptr pThreadException;
724- for (const auto & [itineraryId, itinerary] : m_itineraries) {
738+ for (const auto & itinerary : m_itineraries) {
725739 threads.emplace_back (std::thread ([this , &itinerary, &pThreadException] {
726740 try {
727741 this ->m_updatePath (itinerary);
@@ -937,47 +951,49 @@ namespace dsm {
937951 }
938952 template <typename Delay>
939953 requires (is_numeric_v<Delay>)
940- void Dynamics<Delay>::addAgent(Id srcNodeId, Id itineraryId ) {
954+ void Dynamics<Delay>::addAgent(Id srcNodeId, Id dstNodeId ) {
941955 if (this ->m_agents .size () + 1 > this ->m_graph .maxCapacity ()) {
942956 throw std::overflow_error (buildLog (
943957 std::format (" Graph its already holding the max possible number of agents ({})" ,
944958 this ->m_graph .maxCapacity ())));
945959 }
946- if (!(srcNodeId < this ->m_graph .nodeSet ().size ())) {
947- throw std::invalid_argument (
948- buildLog (std::format (" Node with id {} not found" , srcNodeId)));
949- }
950- if (!(this ->m_itineraries .contains (itineraryId))) {
951- throw std::invalid_argument (
952- buildLog (std::format (" Itinerary with id {} not found" , itineraryId)));
953- }
960+ assert ((void (" Nodes indexes out of range." ),
961+ srcNodeId < this ->m_graph .nodeSet ().size () &&
962+ dstNodeId < this ->m_graph .nodeSet ().size ()));
963+ assert ((void (" No itineray associated with the destination node." ),
964+ std::find_if (m_itineraries.begin (),
965+ m_itineraries.end (),
966+ [dstNodeId](const std::unique_ptr<Itinerary>& itinerary) {
967+ return itinerary->destination () == dstNodeId;
968+ }) != m_itineraries.end ()));
954969 Size agentId{0 };
955970 if (!this ->m_agents .empty ()) {
956971 agentId = this ->m_agents .rbegin ()->first + 1 ;
957972 }
958- this ->addAgent (Agent<Delay>{agentId, itineraryId , srcNodeId});
973+ this ->addAgent (Agent<Delay>{agentId, dstNodeId , srcNodeId});
959974 }
960975 template <typename Delay>
961976 requires (is_numeric_v<Delay>)
962- void Dynamics<Delay>::addAgents(Id itineraryId ,
977+ void Dynamics<Delay>::addAgents(Id dstNodeId ,
963978 Size nAgents,
964979 std::optional<Id> srcNodeId) {
965980 if (this ->m_agents .size () + nAgents > this ->m_graph .maxCapacity ()) {
966981 throw std::overflow_error (buildLog (
967982 std::format (" Graph its already holding the max possible number of agents ({})" ,
968983 this ->m_graph .maxCapacity ())));
969984 }
970- auto itineraryIt{m_itineraries.find (itineraryId)};
971- if (itineraryIt == m_itineraries.end ()) {
972- throw std::invalid_argument (
973- buildLog (std::format (" Itinerary with id {} not found" , itineraryId)));
974- }
985+ assert ((void (" No itineray associated with the destination node." ),
986+ std::find_if (m_itineraries.begin (),
987+ m_itineraries.end (),
988+ [dstNodeId](const std::unique_ptr<Itinerary>& itinerary) {
989+ return itinerary->destination () == dstNodeId;
990+ }) != m_itineraries.end ()));
975991 Size agentId{0 };
976992 if (!this ->m_agents .empty ()) {
977993 agentId = this ->m_agents .rbegin ()->first + 1 ;
978994 }
979995 for (Size i{0 }; i < nAgents; ++i, ++agentId) {
980- this ->addAgent (Agent<Delay>{agentId, itineraryId });
996+ this ->addAgent (Agent<Delay>{agentId, dstNodeId });
981997 if (srcNodeId.has_value ()) {
982998 this ->m_agents [agentId]->setSourceNodeId (srcNodeId.value ());
983999 }
@@ -1032,9 +1048,7 @@ namespace dsm {
10321048 0 , static_cast <Size>(this ->m_graph .streetSet ().size () - 1 )};
10331049 for (Size i{0 }; i < nAgents; ++i) {
10341050 if (randomItinerary) {
1035- auto itineraryIt{this ->m_itineraries .begin ()};
1036- std::advance (itineraryIt, itineraryDist (this ->m_generator ));
1037- itineraryId = itineraryIt->first ;
1051+ itineraryId = m_itineraries[itineraryDist (m_generator)]->destination ();
10381052 }
10391053 Id agentId{0 };
10401054 if (!this ->m_agents .empty ()) {
@@ -1111,16 +1125,13 @@ namespace dsm {
11111125 }
11121126 }
11131127 }
1114- // find the itinerary with the given destination as destination
1115- auto itineraryIt{std::find_if (
1116- m_itineraries.begin (), m_itineraries.end (), [dstId](const auto & itinerary) {
1117- return itinerary.second ->destination () == dstId;
1118- })};
1119- if (itineraryIt == m_itineraries.end ()) {
1120- throw std::invalid_argument (
1121- buildLog (std::format (" Itinerary with destination {} not found." , dstId)));
1122- }
1123- this ->addAgent (srcId, itineraryIt->first );
1128+ assert ((void (" No itineray associated with the destination node." ),
1129+ std::find_if (m_itineraries.begin (),
1130+ m_itineraries.end (),
1131+ [dstId](const std::unique_ptr<Itinerary>& itinerary) {
1132+ return itinerary->destination () == dstId;
1133+ }) != m_itineraries.end ()));
1134+ this ->addAgent (srcId, dstId);
11241135 --nAgents;
11251136 }
11261137 }
@@ -1146,13 +1157,13 @@ namespace dsm {
11461157 template <typename Delay>
11471158 requires (is_numeric_v<Delay>)
11481159 void Dynamics<Delay>::addItinerary(const Itinerary& itinerary) {
1149- m_itineraries.emplace (itinerary. id (), std::make_unique<Itinerary>(itinerary));
1160+ m_itineraries.emplace_back ( std::make_unique<Itinerary>(itinerary));
11501161 }
11511162
11521163 template <typename Delay>
11531164 requires (is_numeric_v<Delay>)
11541165 void Dynamics<Delay>::addItinerary(std::unique_ptr<Itinerary> itinerary) {
1155- m_itineraries.emplace (itinerary-> id (), std::move (itinerary));
1166+ m_itineraries.emplace_back ( std::move (itinerary));
11561167 }
11571168
11581169 template <typename Delay>
0 commit comments