Skip to content

Commit

Permalink
Add setting for disposable time between transfers (#153)
Browse files Browse the repository at this point in the history
* Add setting for disposable time between transfers

This adds an option to increase the time between transfers by a fixed
duration. This setting can be used, if passengers want some time for
orientation or to compensate small delays.

* Rename variable

* Constrain input data
  • Loading branch information
MichaelKutzner authored Nov 19, 2024
1 parent ab531a1 commit 6bf2a94
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 8 deletions.
8 changes: 5 additions & 3 deletions include/nigiri/routing/search.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,11 @@ struct search {
kMaxVias);

tts.factor_ = std::max(tts.factor_, 1.0F);
if (tts.factor_ == 1.0F && tts.min_transfer_time_ == 0_minutes) {
tts.default_ = true;
}
tts.min_transfer_time_ = std::max(tts.min_transfer_time_, 0_minutes);
tts.additional_time_ = std::max(tts.additional_time_, 0_minutes);
tts.default_ = tts.factor_ == 1.0F //
&& tts.min_transfer_time_ == 0_minutes //
&& tts.additional_time_ == 0_minutes;

collect_destinations(tt_, q_.destination_, q_.dest_match_mode_,
state_.is_destination_, state_.dist_to_dest_);
Expand Down
13 changes: 8 additions & 5 deletions include/nigiri/routing/transfer_time_settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@ namespace nigiri::routing {
struct transfer_time_settings {
bool operator==(transfer_time_settings const& o) const {
return default_ == o.default_ ||
std::tie(min_transfer_time_, factor_) ==
std::tie(o.min_transfer_time_, o.factor_);
std::tie(min_transfer_time_, additional_time_, factor_) ==
std::tie(o.min_transfer_time_, additional_time_, o.factor_);
}

bool default_{true};
duration_t min_transfer_time_{0};
duration_t additional_time_{0};
float factor_{1.0F};
};

Expand All @@ -22,9 +23,10 @@ inline T adjusted_transfer_time(transfer_time_settings const& settings,
if (settings.default_) {
return duration;
} else {
return std::max(
static_cast<T>(settings.min_transfer_time_.count()),
static_cast<T>(static_cast<float>(duration) * settings.factor_));
return static_cast<T>(settings.additional_time_.count()) +
std::max(
static_cast<T>(settings.min_transfer_time_.count()),
static_cast<T>(static_cast<float>(duration) * settings.factor_));
}
}

Expand All @@ -36,6 +38,7 @@ inline std::chrono::duration<Rep, std::ratio<60>> adjusted_transfer_time(
return duration;
} else {
return std::chrono::duration<Rep, std::ratio<60>>{
static_cast<Rep>(settings.additional_time_.count()) +
std::max(static_cast<Rep>(settings.min_transfer_time_.count()),
static_cast<Rep>(static_cast<float>(duration.count()) *
settings.factor_))};
Expand Down
33 changes: 33 additions & 0 deletions test/routing/transfer_time_settings_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -215,5 +215,38 @@ TEST(routing, transfer_time_settings_test) {
.factor_ = 2.0F});
EXPECT_EQ(expected_A_C_f20, results_to_str(results, tt));
}
{
// A -> C, default transfer time, 2 min additional (= 4 min)
auto const results =
search(tt, nullptr, "A", "C", tt.date_range_, dir,
{.default_ = false, .additional_time_ = duration_t{2}});
EXPECT_EQ(expected_A_C_f20, results_to_str(results, tt));
}
{
// A -> C, 1.5x transfer time, 1 min additional (= 4 min)
auto const results = search(tt, nullptr, "A", "C", tt.date_range_, dir,
{.default_ = false,
.additional_time_ = duration_t{1},
.factor_ = 1.5F});
EXPECT_EQ(expected_A_C_f20, results_to_str(results, tt));
}
{
// A -> C, min 3 min transfer time, 1 min additional (= 4 min)
auto const results = search(tt, nullptr, "A", "C", tt.date_range_, dir,
{.default_ = false,
.min_transfer_time_ = duration_t{3},
.additional_time_ = duration_t{1}});
EXPECT_EQ(expected_A_C_f20, results_to_str(results, tt));
}
{
// A -> C, min 3 min transfer time, 2.5x transfer time, 5 min additional
// (= 10 min)
auto const results = search(tt, nullptr, "A", "C", tt.date_range_, dir,
{.default_ = false,
.min_transfer_time_ = duration_t{3},
.additional_time_ = duration_t{5},
.factor_ = 2.5F});
EXPECT_EQ(expected_A_C_min10, results_to_str(results, tt));
}
}
}

0 comments on commit 6bf2a94

Please sign in to comment.