Skip to content

Commit f918de6

Browse files
committed
Init Itineraries rework
1 parent 94ae4d7 commit f918de6

File tree

8 files changed

+130
-242
lines changed

8 files changed

+130
-242
lines changed

src/dsm/headers/Dynamics.hpp

Lines changed: 49 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -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>

src/dsm/headers/Graph.hpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -207,9 +207,7 @@ namespace dsm {
207207
}
208208
/// @brief Get the graph's street map
209209
/// @return A std::unordered_map containing the graph's streets
210-
std::unordered_map<Id, std::unique_ptr<Street>>& streetSet() {
211-
return m_streets;
212-
}
210+
std::unordered_map<Id, std::unique_ptr<Street>>& streetSet() { return m_streets; }
213211
/// @brief Get a street from the graph
214212
/// @param source The source node
215213
/// @param destination The destination node

src/dsm/headers/Itinerary.cpp

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,10 @@
22
#include "Itinerary.hpp"
33

44
namespace dsm {
5-
Itinerary::Itinerary(Id id, Id destination) : m_id{id}, m_destination{destination} {}
5+
Itinerary::Itinerary(Id destination) : m_destination{destination} {}
66

7-
Itinerary::Itinerary(Id id, Id destination, SparseMatrix<bool> path)
8-
: m_id{id}, m_path{std::move(path)}, m_destination{destination} {}
9-
10-
void Itinerary::setDestination(Id destination) {
11-
m_destination = destination;
12-
this->m_path.clear();
13-
}
7+
Itinerary::Itinerary(Id destination, SparseMatrix<bool> path)
8+
: m_path{std::move(path)}, m_destination{destination} {}
149

1510
void Itinerary::setPath(SparseMatrix<bool> path) {
1611
if (path.getRowDim() != path.getColDim()) {

src/dsm/headers/Itinerary.hpp

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,30 +21,23 @@ namespace dsm {
2121
/// @tparam Id The type of the itinerary's id. It must be an unsigned integral type.
2222
class Itinerary {
2323
private:
24-
Id m_id;
2524
SparseMatrix<bool> m_path;
2625
Id m_destination;
2726

2827
public:
2928
/// @brief Construct a new Itinerary object
3029
/// @param destination The itinerary's destination
31-
Itinerary(Id id, Id destination);
30+
Itinerary(Id destination);
3231
/// @brief Construct a new Itinerary object
3332
/// @param destination The itinerary's destination
3433
/// @param path An adjacency matrix made by a SparseMatrix representing the itinerary's path
35-
Itinerary(Id id, Id destination, SparseMatrix<bool> path);
34+
Itinerary(Id destination, SparseMatrix<bool> path);
3635

37-
/// @brief Set the itinerary's destination
38-
/// @param destination The itinerary's destination
39-
void setDestination(Id destination);
4036
/// @brief Set the itinerary's path
4137
/// @param path An adjacency matrix made by a SparseMatrix representing the itinerary's path
4238
/// @throw std::invalid_argument, if the itinerary's source or destination is not in the path's
4339
void setPath(SparseMatrix<bool> path);
4440

45-
/// @brief Get the itinerary's id
46-
/// @return Id, The itinerary's id
47-
Id id() const { return m_id; }
4841
/// @brief Get the itinerary's destination
4942
/// @return Id, The itinerary's destination
5043
Id destination() const { return m_destination; }

src/dsm/headers/Node.hpp

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -52,25 +52,19 @@ namespace dsm {
5252
void setId(Id id) { m_id = id; }
5353
/// @brief Set the node's coordinates
5454
/// @param coords A std::pair containing the node's coordinates (lat, lon)
55-
void setCoords(std::pair<double, double> coords) {
56-
m_coords = std::move(coords);
57-
}
55+
void setCoords(std::pair<double, double> coords) { m_coords = std::move(coords); }
5856
/// @brief Set the node's capacity
5957
/// @param capacity The node's capacity
6058
virtual void setCapacity(Size capacity) { m_capacity = capacity; }
6159
/// @brief Set the node's transport capacity
6260
/// @param capacity The node's transport capacity
63-
virtual void setTransportCapacity(Size capacity) {
64-
m_transportCapacity = capacity;
65-
}
61+
virtual void setTransportCapacity(Size capacity) { m_transportCapacity = capacity; }
6662
/// @brief Get the node's id
6763
/// @return Id The node's id
6864
Id id() const { return m_id; }
6965
/// @brief Get the node's coordinates
7066
/// @return std::optional<std::pair<double, double>> A std::pair containing the node's coordinates
71-
const std::optional<std::pair<double, double>>& coords() const {
72-
return m_coords;
73-
}
67+
const std::optional<std::pair<double, double>>& coords() const { return m_coords; }
7468
/// @brief Get the node's capacity
7569
/// @return Size The node's capacity
7670
Size capacity() const { return m_capacity; }
@@ -146,9 +140,7 @@ namespace dsm {
146140
/// If a street has priority, it means that the agents that are on that street
147141
/// have priority over the agents that are on the other streets.
148142
/// @return std::set<Id> A std::set containing the node's street priorities
149-
virtual const std::set<Id>& streetPriorities() const {
150-
return m_streetPriorities;
151-
};
143+
virtual const std::set<Id>& streetPriorities() const { return m_streetPriorities; };
152144
/// @brief Get the node's agent ids
153145
/// @return std::set<Id> A std::set containing the node's agent ids
154146
const std::multimap<int16_t, Id>& agents() { return m_agents; };

src/dsm/headers/Street.hpp

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -110,9 +110,7 @@ namespace dsm {
110110
/// @brief Set the street's node pair
111111
/// @param node1 The source node of the street
112112
/// @param node2 The destination node of the street
113-
void setNodePair(Id node1, Id node2) {
114-
m_nodePair = std::make_pair(node1, node2);
115-
}
113+
void setNodePair(Id node1, Id node2) { m_nodePair = std::make_pair(node1, node2); }
116114
/// @brief Set the street's node pair
117115
/// @param node1 The source node of the street
118116
/// @param node2 The destination node of the street
@@ -158,14 +156,10 @@ namespace dsm {
158156
const std::set<Id>& waitingAgents() const { return m_waitingAgents; }
159157
/// @brief Get the street's queue
160158
/// @return dsm::queue<Size>, The street's queue
161-
const dsm::queue<Size>& queue(size_t index) const {
162-
return m_exitQueues[index];
163-
}
159+
const dsm::queue<Size>& queue(size_t index) const { return m_exitQueues[index]; }
164160
/// @brief Get the street's queues
165161
/// @return std::vector<dsm::queue<Size>> The street's queues
166-
const std::vector<dsm::queue<Size>>& exitQueues() const {
167-
return m_exitQueues;
168-
}
162+
const std::vector<dsm::queue<Size>>& exitQueues() const { return m_exitQueues; }
169163
/// @brief Get the street's node pair
170164
/// @return std::pair<Id, Id>, The street's node pair
171165
const std::pair<Id, Id>& nodePair() const { return m_nodePair; }
@@ -177,9 +171,7 @@ namespace dsm {
177171
double density() const { return nAgents() / (m_len * m_nLanes); }
178172
/// @brief Get the street's normalized density
179173
/// @return double, The street's normalized density
180-
double normDensity() const {
181-
return nAgents() / static_cast<double>(m_capacity);
182-
}
174+
double normDensity() const { return nAgents() / static_cast<double>(m_capacity); }
183175
/// @brief Check if the street is full
184176
/// @return bool, True if the street is full, false otherwise
185177
bool isFull() const { return nAgents() == m_capacity; }

0 commit comments

Comments
 (0)