Skip to content

Commit 81d5cc3

Browse files
committed
Small code enhancements
1 parent 3e75d34 commit 81d5cc3

File tree

5 files changed

+68
-53
lines changed

5 files changed

+68
-53
lines changed

src/dsm/dsm.hpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@
66

77
static constexpr uint8_t DSM_VERSION_MAJOR = 2;
88
static constexpr uint8_t DSM_VERSION_MINOR = 1;
9-
static constexpr uint8_t DSM_VERSION_PATCH = 9;
9+
static constexpr uint8_t DSM_VERSION_PATCH = 10;
1010

11-
#define DSM_VERSION \
12-
std::format("{}.{}.{}", DSM_VERSION_MAJOR, DSM_VERSION_MINOR, DSM_VERSION_PATCH)
11+
static auto const DSM_VERSION =
12+
std::format("{}.{}.{}", DSM_VERSION_MAJOR, DSM_VERSION_MINOR, DSM_VERSION_PATCH);
1313

1414
namespace dsm {
1515
/// @brief Returns the version of the DSM library
1616
/// @return The version of the DSM library
17-
constexpr auto version() { return DSM_VERSION; };
17+
auto const& version() { return DSM_VERSION; };
1818
} // namespace dsm
1919

2020
#include "headers/Agent.hpp"

src/dsm/headers/Agent.hpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,10 @@ namespace dsm {
6565
/// @brief Increment the agent's delay by 1
6666
/// @throw std::overflow_error, if delay has reached its maximum value
6767
void incrementDelay();
68-
/// @brief Set the agent's delay
68+
/// @brief Increment the agent's delay by a given value
6969
/// @param delay The agent's delay
7070
/// @throw std::overflow_error, if delay has reached its maximum value
71-
void incrementDelay(Delay delay);
71+
void incrementDelay(Delay const delay);
7272
/// @brief Decrement the agent's delay by 1
7373
/// @throw std::underflow_error, if delay has reached its minimum value
7474
void decrementDelay();
@@ -84,7 +84,7 @@ namespace dsm {
8484
/// @brief Increment the agent's time by a given value
8585
/// @param time The value to increment the agent's time by
8686
/// @throw std::overflow_error, if time has reached its maximum value
87-
void incrementTime(unsigned int time);
87+
void incrementTime(unsigned int const time);
8888
/// @brief Reset the agent's time to 0
8989
void resetTime() { m_time = 0; }
9090

@@ -153,11 +153,11 @@ namespace dsm {
153153
}
154154
template <typename Delay>
155155
requires(is_numeric_v<Delay>)
156-
void Agent<Delay>::incrementDelay(Delay delay) {
156+
void Agent<Delay>::incrementDelay(Delay const delay) {
157157
if (m_delay + delay < m_delay) {
158158
throw std::overflow_error(buildLog("Delay has reached its maximum value"));
159159
}
160-
m_delay = delay;
160+
m_delay += delay;
161161
}
162162
template <typename Delay>
163163
requires(is_numeric_v<Delay>)
@@ -187,7 +187,7 @@ namespace dsm {
187187
}
188188
template <typename Delay>
189189
requires(is_numeric_v<Delay>)
190-
void Agent<Delay>::incrementTime(unsigned int time) {
190+
void Agent<Delay>::incrementTime(unsigned int const time) {
191191
if (m_time + time < m_time) {
192192
throw std::overflow_error(buildLog("Time has reached its maximum value"));
193193
}

src/dsm/headers/Dynamics.hpp

Lines changed: 49 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -508,7 +508,7 @@ namespace dsm {
508508
} else {
509509
this->removeAgent(agentId);
510510
}
511-
return;
511+
continue;
512512
}
513513
auto const& nextStreet{m_graph.streetSet()[m_agentNextStreetId[agentId]]};
514514
if (nextStreet->isFull()) {
@@ -1073,43 +1073,64 @@ namespace dsm {
10731073
void Dynamics<Delay>::addAgentsRandomly(Size nAgents,
10741074
const TContainer& src_weights,
10751075
const TContainer& dst_weights) {
1076-
// Check if the weights are normalized
1077-
{
1078-
auto const sum{std::accumulate(
1079-
src_weights.begin(),
1080-
src_weights.end(),
1081-
0.,
1082-
[](double sum, const std::pair<Id, double>& p) { return sum + p.second; })};
1083-
assert((void("The source weights are not normalized"),
1084-
std::abs(sum - 1.) < std::numeric_limits<double>::epsilon()));
1085-
}
1086-
{
1087-
auto const sum{std::accumulate(
1088-
dst_weights.begin(),
1089-
dst_weights.end(),
1090-
0.,
1091-
[](double sum, const std::pair<Id, double>& p) { return sum + p.second; })};
1092-
assert((void("The destination weights are not normalized"),
1093-
std::abs(sum - 1.) < std::numeric_limits<double>::epsilon()));
1094-
}
1076+
if (src_weights.size() == 1 && dst_weights.size() == 1 &&
1077+
src_weights.begin()->first == dst_weights.begin()->first) {
1078+
throw std::invalid_argument(buildLog(
1079+
std::format("The only source node {} is also the only destination node.",
1080+
src_weights.begin()->first)));
1081+
}
1082+
auto const srcSum{std::accumulate(
1083+
src_weights.begin(),
1084+
src_weights.end(),
1085+
0.,
1086+
[](double sum, const std::pair<Id, double>& p) {
1087+
if (p.second < 0.) {
1088+
throw std::invalid_argument(buildLog(std::format(
1089+
"Negative weight ({}) for source node {}.", p.second, p.first)));
1090+
}
1091+
return sum + p.second;
1092+
})};
1093+
auto const dstSum{std::accumulate(
1094+
dst_weights.begin(),
1095+
dst_weights.end(),
1096+
0.,
1097+
[](double sum, const std::pair<Id, double>& p) {
1098+
if (p.second < 0.) {
1099+
throw std::invalid_argument(buildLog(std::format(
1100+
"Negative weight ({}) for destination node {}.", p.second, p.first)));
1101+
}
1102+
return sum + p.second;
1103+
})};
1104+
std::uniform_real_distribution<double> srcUniformDist{0., srcSum};
1105+
std::uniform_real_distribution<double> dstUniformDist{0., dstSum};
10951106
while (nAgents > 0) {
10961107
Id srcId{0}, dstId{0};
1097-
double dRand{this->m_uniformDist(this->m_generator)}, sum{0.};
1098-
for (const auto& [id, weight] : src_weights) {
1099-
sum += weight;
1100-
if (dRand < sum) {
1108+
if (dst_weights.size() == 1) {
1109+
dstId = dst_weights.begin()->first;
1110+
srcId = dstId;
1111+
}
1112+
double dRand, sum;
1113+
while (srcId == dstId) {
1114+
dRand = srcUniformDist(m_generator);
1115+
sum = 0.;
1116+
for (const auto& [id, weight] : src_weights) {
11011117
srcId = id;
1102-
break;
1118+
sum += weight;
1119+
if (dRand < sum) {
1120+
break;
1121+
}
11031122
}
11041123
}
1105-
dstId = srcId;
1124+
if (src_weights.size() > 1) {
1125+
dstId = srcId;
1126+
}
11061127
while (dstId == srcId) {
1107-
dRand = this->m_uniformDist(this->m_generator);
1128+
dRand = dstUniformDist(m_generator);
11081129
sum = 0.;
11091130
for (const auto& [id, weight] : dst_weights) {
1131+
dstId = id;
11101132
sum += weight;
11111133
if (dRand < sum) {
1112-
dstId = id;
11131134
break;
11141135
}
11151136
}

src/dsm/headers/Node.cpp

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ namespace dsm {
1616
}
1717

1818
void Intersection::addAgent(double angle, Id agentId) {
19-
if (m_agents.size() == this->m_capacity) {
19+
if (isFull()) {
2020
throw std::runtime_error(buildLog("Intersection is full."));
2121
}
2222
for (auto const [angle, id] : m_agents) {
@@ -31,21 +31,11 @@ namespace dsm {
3131
}
3232

3333
void Intersection::addAgent(Id agentId) {
34-
if (m_agents.size() == this->m_capacity) {
35-
throw std::runtime_error(buildLog("Intersection is full."));
36-
}
37-
for (auto const [angle, id] : m_agents) {
38-
if (id == agentId) {
39-
throw std::runtime_error(
40-
buildLog(std::format("Agent with id {} is already on the node.", id)));
41-
}
42-
}
4334
int lastKey{0};
4435
if (!m_agents.empty()) {
4536
lastKey = m_agents.rbegin()->first + 1;
4637
}
47-
m_agents.emplace(lastKey, agentId);
48-
++m_agentCounter;
38+
addAgent(static_cast<double>(lastKey), agentId);
4939
}
5040

5141
void Intersection::removeAgent(Id agentId) {
@@ -67,7 +57,7 @@ namespace dsm {
6757
}
6858

6959
void Roundabout::enqueue(Id agentId) {
70-
if (m_agents.size() == this->m_capacity) {
60+
if (isFull()) {
7161
throw std::runtime_error(buildLog("Roundabout is full."));
7262
}
7363
for (const auto id : m_agents) {

src/dsm/headers/SparseMatrix.hpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -559,7 +559,9 @@ namespace dsm {
559559
for (auto& it : row) {
560560
sum += std::abs(it.second);
561561
}
562-
sum < std::numeric_limits<double>::epsilon() ? sum = 1. : sum = sum;
562+
if (sum < std::numeric_limits<double>::epsilon()) {
563+
sum = 1.;
564+
}
563565
for (auto& it : row) {
564566
normRows.insert(it.first, it.second / sum);
565567
}
@@ -576,7 +578,9 @@ namespace dsm {
576578
for (auto& it : col) {
577579
sum += std::abs(it.second);
578580
}
579-
sum < std::numeric_limits<double>::epsilon() ? sum = 1. : sum = sum;
581+
if (sum < std::numeric_limits<double>::epsilon()) {
582+
sum = 1.;
583+
}
580584
for (auto& it : col) {
581585
normCols.insert(it.first, it.second / sum);
582586
}

0 commit comments

Comments
 (0)