Skip to content

Commit 0bf5a03

Browse files
committed
Add a check for Delay boundaries
1 parent 8cf43ab commit 0bf5a03

File tree

6 files changed

+50
-31
lines changed

6 files changed

+50
-31
lines changed

examples/slow_charge_rb.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ int main(int argc, char** argv) {
120120

121121
std::cout << "Creating dynamics...\n";
122122

123-
Dynamics dynamics{graph, SEED};
123+
Dynamics dynamics{graph, SEED, 0.95};
124124
Unit n{0};
125125
{
126126
std::vector<Unit> destinationNodes;
@@ -138,7 +138,6 @@ int main(int argc, char** argv) {
138138
dynamics.setMaxFlowPercentage(0.7707);
139139
// dynamics.setForcePriorities(true);
140140
dynamics.setSpeedFluctuationSTD(0.1);
141-
dynamics.setMinSpeedRateo(0.95);
142141

143142
std::cout << "Done." << std::endl;
144143
std::cout << "Running simulation...\n";

examples/slow_charge_tl.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ int main(int argc, char** argv) {
216216

217217
std::cout << "Creating dynamics...\n";
218218

219-
Dynamics dynamics{graph, SEED};
219+
Dynamics dynamics{graph, SEED, 0.95};
220220
Unit n{0};
221221
{
222222
std::vector<Unit> destinationNodes;
@@ -234,7 +234,6 @@ int main(int argc, char** argv) {
234234
// dynamics.setMaxFlowPercentage(0.69);
235235
// dynamics.setForcePriorities(false);
236236
dynamics.setSpeedFluctuationSTD(0.1);
237-
dynamics.setMinSpeedRateo(0.95);
238237
if (OPTIMIZE)
239238
dynamics.setDataUpdatePeriod(30); // Store data every 30 time steps
240239

examples/stalingrado.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,7 @@ int main() {
8282
std::cout << "Streets: " << graph.nEdges() << '\n';
8383

8484
// Create the dynamics
85-
Dynamics dynamics{graph, 69};
86-
dynamics.setMinSpeedRateo(0.95);
85+
Dynamics dynamics{graph, 69, 0.95};
8786
dynamics.setSpeedFluctuationSTD(0.2);
8887
Itinerary itinerary{0, 4};
8988
dynamics.addItinerary(itinerary);

src/dsm/headers/FirstOrderDynamics.hpp

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,15 @@ namespace dsm {
66
template <typename delay_t>
77
requires(std::unsigned_integral<delay_t>)
88
class FirstOrderDynamics : public RoadDynamics<delay_t> {
9+
double m_alpha;
910
double m_speedFluctuationSTD;
1011

1112
public:
1213
/// @brief Construct a new First Order Dynamics object
1314
/// @param graph, The graph representing the network
14-
FirstOrderDynamics(Graph& graph, std::optional<unsigned int> seed = std::nullopt)
15-
: RoadDynamics<delay_t>(graph, seed), m_speedFluctuationSTD{0.} {};
15+
FirstOrderDynamics(Graph& graph,
16+
std::optional<unsigned int> seed = std::nullopt,
17+
double minSpeedRateo = 0.);
1618
/// @brief Set the speed of an agent
1719
/// @param agentId The id of the agent
1820
/// @throw std::invalid_argument, If the agent is not found
@@ -38,18 +40,49 @@ namespace dsm {
3840
Measurement<double> streetMeanSpeed(double threshold, bool above) const override;
3941
};
4042

43+
template <typename delay_t>
44+
requires(std::unsigned_integral<delay_t>)
45+
FirstOrderDynamics<delay_t>::FirstOrderDynamics(Graph& graph,
46+
std::optional<unsigned int> seed,
47+
double minSpeedRateo)
48+
: RoadDynamics<delay_t>(graph, seed),
49+
m_alpha{0.},
50+
m_speedFluctuationSTD{0.} {
51+
if (minSpeedRateo < 0. || minSpeedRateo > 1.) {
52+
throw std::invalid_argument(buildLog(
53+
std::format("The minimum speed rateo must be between 0 and 1, but it is {}",
54+
minSpeedRateo)));
55+
} else {
56+
m_alpha = minSpeedRateo;
57+
}
58+
double globMaxTimePenalty{0.};
59+
for (const auto& [streetId, street] : this->m_graph.streetSet()) {
60+
globMaxTimePenalty =
61+
std::max(globMaxTimePenalty,
62+
std::ceil(street->length() /
63+
((1. - m_alpha) * street->maxSpeed())));
64+
}
65+
if (globMaxTimePenalty > static_cast<double>(std::numeric_limits<delay_t>::max())) {
66+
throw std::overflow_error(
67+
buildLog(std::format("The maximum time penalty ({}) is greater than the "
68+
"maximum value of delay_t ({})",
69+
globMaxTimePenalty,
70+
std::numeric_limits<delay_t>::max())));
71+
}
72+
}
73+
4174
template <typename delay_t>
4275
requires(std::unsigned_integral<delay_t>)
4376
void FirstOrderDynamics<delay_t>::setAgentSpeed(Size agentId) {
4477
const auto& agent{this->m_agents[agentId]};
4578
const auto& street{this->m_graph.streetSet()[agent->streetId().value()]};
4679
double speed{street->maxSpeed() *
47-
(1. - this->m_minSpeedRateo * street->density(true))};
80+
(1. - m_alpha * street->density(true))};
4881
if (m_speedFluctuationSTD > 0.) {
4982
std::normal_distribution<double> speedDist{speed, speed * m_speedFluctuationSTD};
5083
speed = speedDist(this->m_generator);
5184
}
52-
speed < 0. ? agent->setSpeed(street->maxSpeed() * (1. - this->m_minSpeedRateo))
85+
speed < 0. ? agent->setSpeed(street->maxSpeed() * (1. - m_alpha))
5386
: agent->setSpeed(speed);
5487
}
5588

@@ -74,7 +107,7 @@ namespace dsm {
74107
Size n{0};
75108
if (street->nExitingAgents() == 0) {
76109
n = static_cast<Size>(street->waitingAgents().size());
77-
double alpha{this->m_minSpeedRateo / street->capacity()};
110+
double alpha{m_alpha / street->capacity()};
78111
meanSpeed = street->maxSpeed() * n * (1. - 0.5 * alpha * (n - 1.));
79112
} else {
80113
for (const auto& agentId : street->waitingAgents()) {

src/dsm/headers/RoadDynamics.hpp

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ namespace dsm {
4343
protected:
4444
Time m_previousOptimizationTime;
4545
double m_errorProbability;
46-
double m_minSpeedRateo;
4746
double m_maxFlowPercentage;
4847
std::vector<double> m_travelTimes;
4948
std::unordered_map<Id, Id> m_agentNextStreetId;
@@ -82,12 +81,12 @@ namespace dsm {
8281
public:
8382
/// @brief Construct a new RoadDynamics object
8483
/// @param graph The graph representing the network
85-
RoadDynamics(Graph& graph, std::optional<unsigned int> seed);
84+
/// @param seed The seed for the random number generator
85+
/// @param minSpeedRateo The minimum speed rateo
86+
RoadDynamics(Graph& graph,
87+
std::optional<unsigned int> seed,
88+
double minSpeedRateo = 0.);
8689

87-
/// @brief Set the minim speed rateo, i.e. the minim speed with respect to the speed limit
88-
/// @param minSpeedRateo The minim speed rateo
89-
/// @throw std::invalid_argument If the minim speed rateo is not between 0 and 1
90-
void setMinSpeedRateo(double minSpeedRateo);
9190
/// @brief Set the error probability
9291
/// @param errorProbability The error probability
9392
/// @throw std::invalid_argument If the error probability is not between 0 and 1
@@ -151,11 +150,12 @@ namespace dsm {
151150

152151
template <typename delay_t>
153152
requires(is_numeric_v<delay_t>)
154-
RoadDynamics<delay_t>::RoadDynamics(Graph& graph, std::optional<unsigned int> seed)
153+
RoadDynamics<delay_t>::RoadDynamics(Graph& graph,
154+
std::optional<unsigned int> seed,
155+
double minSpeedRateo)
155156
: Dynamics<delay_t>(graph, seed),
156157
m_previousOptimizationTime{0},
157158
m_errorProbability{0.},
158-
m_minSpeedRateo{0.},
159159
m_maxFlowPercentage{1.},
160160
m_forcePriorities{false} {
161161
for (const auto& [streetId, street] : this->m_graph.streetSet()) {
@@ -429,16 +429,6 @@ namespace dsm {
429429
}
430430
}
431431

432-
template <typename delay_t>
433-
requires(is_numeric_v<delay_t>)
434-
void RoadDynamics<delay_t>::setMinSpeedRateo(double minSpeedRateo) {
435-
if (minSpeedRateo < 0. || minSpeedRateo > 1.) {
436-
throw std::invalid_argument(buildLog(std::format(
437-
"The minimum speed rateo ({}) must be between 0 and 1", minSpeedRateo)));
438-
}
439-
m_minSpeedRateo = minSpeedRateo;
440-
}
441-
442432
template <typename delay_t>
443433
requires(is_numeric_v<delay_t>)
444434
void RoadDynamics<delay_t>::setErrorProbability(double errorProbability) {

test/Test_dynamics.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -912,8 +912,7 @@ TEST_CASE("Dynamics") {
912912
node->setCapacity(4);
913913
node->setTransportCapacity(4);
914914
}
915-
Dynamics dynamics{graph2, 69};
916-
dynamics.setMinSpeedRateo(0.5);
915+
Dynamics dynamics{graph2, 69, 0.5};
917916
Itinerary itinerary{0, 2};
918917
dynamics.addItinerary(itinerary);
919918
dynamics.updatePaths();

0 commit comments

Comments
 (0)