Skip to content

Commit

Permalink
Via search (#122)
Browse files Browse the repository at this point in the history
  • Loading branch information
pablohoch authored Aug 15, 2024
1 parent d17caf7 commit 6c19c96
Show file tree
Hide file tree
Showing 30 changed files with 2,552 additions and 418 deletions.
2 changes: 1 addition & 1 deletion .pkg
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
[utl]
url=git@github.com:motis-project/utl.git
branch=master
commit=258cfaca690f09304e30ae54a437ceb70186e1a5
commit=77aac494c45d2b070e65fe712abc34ac74a91d0f
[miniz]
url=git@github.com:motis-project/miniz.git
branch=master
Expand Down
4 changes: 2 additions & 2 deletions .pkg.lock
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
849379850243393724
9303410224856167959
cista f52a62c4d83377acd398227ab4fcd6c946bdbd70
PEGTL 0091112da4f0a0004186cab12eb3b2e866c71750
res 7d97784ba785ce8a2677ea77164040fde484fb04
date d84b23ca2432e17f3f04a3e0cc96b096b99c39a2
googletest 7b64fca6ea0833628d6f86255a81424365f7cc0c
fmt 09e6cf8fa61ff44509595005631f9da5d917ed3f
utl 258cfaca690f09304e30ae54a437ceb70186e1a5
utl 77aac494c45d2b070e65fe712abc34ac74a91d0f
oh 469a148f731a0434dd34c4bc525f6f3c5bc43fda
zlib fe8e13ffca867612951bc6baf114e5ac8b00f305
boost 60cae66449fa3c9599b2b7d3d5d44c65301ed3a3
Expand Down
33 changes: 22 additions & 11 deletions exe/benchmark.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "nigiri/logging.h"
#include "nigiri/query_generator/generator.h"
#include "nigiri/routing/raptor/raptor.h"
#include "nigiri/routing/raptor_search.h"
#include "nigiri/routing/search.h"
#include "nigiri/timetable.h"
#include "nigiri/types.h"
Expand Down Expand Up @@ -86,7 +87,8 @@ struct benchmark_result {
<< std::chrono::duration_cast<std::chrono::hours>(
br.routing_result_.interval_.size())
.count()
<< "h" << ", #jrny: " << std::setfill(' ') << std::setw(2)
<< "h"
<< ", #jrny: " << std::setfill(' ') << std::setw(2)
<< br.journeys_.size() << ")";
return out;
}
Expand Down Expand Up @@ -127,10 +129,8 @@ nigiri::pareto_set<nigiri::routing::journey> raptor_search(
static auto search_state = routing::search_state{};
static auto algo_state = algo_state_t{};

using algo_t = routing::raptor<nigiri::direction::kForward, false>;
return *(routing::search<nigiri::direction::kForward, algo_t>{
tt, nullptr, search_state, algo_state, std::move(q)}
.execute()
return *(routing::raptor_search(tt, nullptr, search_state, algo_state,
std::move(q), nigiri::direction::kForward)
.journeys_);
}

Expand All @@ -154,12 +154,9 @@ void process_queries(
queries.size(), [&](auto& query_state, auto const q_idx) {
try {
auto const total_time_start = std::chrono::steady_clock::now();
auto const result =
routing::search<direction::kForward,
routing::raptor<direction::kForward, false>>{
tt, nullptr, query_state.ss_, query_state.rs_,
queries[q_idx].q_}
.execute();
auto const result = routing::raptor_search(
tt, nullptr, query_state.ss_, query_state.rs_,
queries[q_idx].q_, direction::kForward);
auto const total_time_stop = std::chrono::steady_clock::now();
auto const guard = std::lock_guard{mutex};
results.emplace_back(benchmark_result{
Expand Down Expand Up @@ -335,6 +332,7 @@ int main(int argc, char* argv[]) {
auto start_loc_val = location_idx_t::value_t{0U};
auto dest_loc_val = location_idx_t::value_t{0U};
auto seed = std::int64_t{-1};
auto min_transfer_time = duration_t::rep{};

bpo::options_description desc("Allowed options");
desc.add_options()("help,h", "produce this help message") //
Expand Down Expand Up @@ -388,6 +386,15 @@ int main(int argc, char* argv[]) {
bpo::value<clasz_mask_t>(&gs.allowed_claszes_)
->default_value(routing::all_clasz_allowed()),
"") //
("min_transfer_time",
bpo::value<duration_t::rep>(&min_transfer_time)->default_value(0U),
"minimum transfer time in minutes") //
("transfer_time_factor",
bpo::value<float>(&gs.transfer_time_settings_.factor_)
->default_value(1.0F),
"multiply all transfer times by this factor") //
("vias", bpo::value<unsigned>(&gs.n_vias_)->default_value(0U),
"number of via stops") //
("start_coord", bpo::value<std::string>(&start_coord_str),
"start coordinate for random queries, format: \"(LAT, LON)\", " //
"where LAT/LON are given in decimal degrees") //
Expand Down Expand Up @@ -466,6 +473,10 @@ int main(int argc, char* argv[]) {
? std::numeric_limits<std::uint8_t>::max()
: max_transfers;

gs.transfer_time_settings_.min_transfer_time_ = duration_t{min_transfer_time};
gs.transfer_time_settings_.default_ =
min_transfer_time == 0U && gs.transfer_time_settings_.factor_ == 1.0F;

if (vm.count("profile_idx") != 0) {
if (prf_idx >= kMaxProfiles) {
std::cout << "Error: profile idx exceeds numeric limits\n";
Expand Down
114 changes: 114 additions & 0 deletions include/nigiri/common/flat_matrix_view.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
#pragma once

#include <cassert>

namespace nigiri {

template <typename Span>
struct base_flat_matrix_view {
using value_type = typename Span::value_type;
using size_type = typename Span::size_type;

struct row {
row(base_flat_matrix_view& matrix, size_type const i)
: matrix_(matrix), i_(i) {}

using iterator = typename Span::iterator;
// libc++ doesn't have std::span::const_iterator
using const_iterator = iterator;

const_iterator begin() const {
return std::next(matrix_.entries_, matrix_.n_columns_ * i_);
}
const_iterator end() const {
return std::next(matrix_.entries_, matrix_.n_columns_ * (i_ + 1));
}
iterator begin() {
return std::next(matrix_.entries_, matrix_.n_columns_ * i_);
}
iterator end() {
return std::next(matrix_.entries_, matrix_.n_columns_ * (i_ + 1));
}
friend const_iterator begin(row const& r) { return r.begin(); }
friend const_iterator end(row const& r) { return r.end(); }
friend iterator begin(row& r) { return r.begin(); }
friend iterator end(row& r) { return r.end(); }

value_type& operator[](size_type const j) {
assert(j < matrix_.n_columns_);
auto const pos = matrix_.n_columns_ * i_ + j;
return matrix_.entries_[pos];
}

base_flat_matrix_view& matrix_;
size_type i_;
};

struct const_row {
const_row(base_flat_matrix_view const& matrix, size_type const i)
: matrix_(matrix), i_(i) {}

using iterator = typename Span::iterator;

iterator begin() const {
return std::next(matrix_.entries_, matrix_.n_columns_ * i_);
}
iterator end() const {
return std::next(matrix_.entries_, matrix_.n_columns_ * (i_ + 1));
}
friend iterator begin(const_row const& r) { return r.begin(); }
friend iterator end(const_row const& r) { return r.end(); }

value_type const& operator[](size_type const j) const {
assert(j < matrix_.n_columns_);
auto const pos = matrix_.n_columns_ * i_ + j;
return matrix_.entries_[pos];
}

base_flat_matrix_view const& matrix_;
size_type i_;
};

base_flat_matrix_view() = default;

base_flat_matrix_view(Span entries, size_type n_rows, size_type n_columns)
: n_rows_{n_rows}, n_columns_{n_columns}, entries_{entries} {
assert(entries_.size() == n_rows_ * n_columns_);
}

row operator[](size_type i) {
assert(i < n_rows_);
return {*this, i};
}
const_row operator[](size_type i) const {
assert(i < n_rows_);
return {*this, i};
}

value_type& operator()(size_type const i, size_type const j) {
assert(i < n_rows_ && j < n_columns_);
return entries_[n_columns_ * i + j];
}

row at(size_type const i) {
verify(i < n_rows_, "matrix::at: index out of range");
return {*this, i};
}

const_row at(size_type const i) const {
verify(i < n_rows_, "matrix::at: index out of range");
return {*this, i};
}

void reset(value_type const& t) {
std::fill(begin(entries_), end(entries_), t);
}

size_type n_rows_{0U}, n_columns_{0U};
Span entries_;
};

template <typename T>
using flat_matrix_view = base_flat_matrix_view<std::span<T>>;

} // namespace nigiri
4 changes: 3 additions & 1 deletion include/nigiri/query_generator/generator.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ struct generator {
std::uniform_int_distribution<std::uint32_t> start_mode_range_d_;
std::uniform_int_distribution<std::uint32_t> dest_mode_range_d_;
std::uniform_int_distribution<std::uint16_t> bearing_d_{0, 359};
std::discrete_distribution<unsigned> stay_d_{40, 5, 5, 5, 5, 10,
5, 5, 5, 5, 10};
};

} // namespace nigiri::query_generation
} // namespace nigiri::query_generation
11 changes: 9 additions & 2 deletions include/nigiri/query_generator/generator_settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "nigiri/routing/clasz_mask.h"
#include "nigiri/routing/limits.h"
#include "nigiri/routing/location_match_mode.h"
#include "nigiri/routing/transfer_time_settings.h"
#include "nigiri/timetable.h"

#include "transport_mode.h"
Expand Down Expand Up @@ -40,7 +41,11 @@ struct generator_settings {
<< "\nextend_interval_later: "
<< (gs.extend_interval_later_ ? "true" : "false")
<< "\nprf_idx: " << std::uint32_t{gs.prf_idx_}
<< "\nallowed_claszes: " << gs.allowed_claszes_;
<< "\nallowed_claszes: " << gs.allowed_claszes_
<< "\nmin_transfer_time: "
<< gs.transfer_time_settings_.min_transfer_time_
<< "\ntransfer_time_factor: " << gs.transfer_time_settings_.factor_
<< "\nvias: " << gs.n_vias_;

auto const visit_loc = [](location_idx_t const loc_idx) {
std::stringstream ss;
Expand Down Expand Up @@ -82,6 +87,8 @@ struct generator_settings {
bool extend_interval_later_{false};
profile_idx_t prf_idx_{0};
routing::clasz_mask_t allowed_claszes_{routing::all_clasz_allowed()};
routing::transfer_time_settings transfer_time_settings_{};
unsigned n_vias_{0U};
};

} // namespace nigiri::query_generation
} // namespace nigiri::query_generation
1 change: 1 addition & 0 deletions include/nigiri/routing/journey.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ struct journey {
unixtime_t dest_time_;
location_idx_t dest_;
std::uint8_t transfers_{0U};
bool error_{false};
};

} // namespace nigiri::routing
1 change: 1 addition & 0 deletions include/nigiri/routing/limits.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@ static constexpr auto const kMaxTravelTime = 1_days;
static constexpr auto const kMaxSearchIntervalSize =
date::days{std::numeric_limits<duration_t::rep>::max() / 1440} -
(kMaxTravelTime + 2_days);
static constexpr auto const kMaxVias = 2;

} // namespace nigiri::routing
8 changes: 8 additions & 0 deletions include/nigiri/routing/query.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,13 @@ struct td_offset {
transport_mode_id_t transport_mode_id_;
};

struct via_stop {
friend bool operator==(via_stop const&, via_stop const&) = default;

location_idx_t location_{};
duration_t stay_{};
};

using start_time_t = std::variant<unixtime_t, interval<unixtime_t>>;

struct query {
Expand All @@ -71,6 +78,7 @@ struct query {
clasz_mask_t allowed_claszes_{all_clasz_allowed()};
bool require_bike_transport_{false};
transfer_time_settings transfer_time_settings_{};
std::vector<via_stop> via_stops_{};
};

} // namespace nigiri::routing
67 changes: 35 additions & 32 deletions include/nigiri/routing/raptor/debug.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@
#define trace_reconstruct(...)
#endif

#define trace_print_state(...) \
fmt::print(__VA_ARGS__); \
state_.print(tt_, base(), kInvalid); \
#define trace_print_state(...) \
fmt::print(__VA_ARGS__); \
state_.print<Vias>(tt_, base(), kInvalid); \
fmt::print("\n")

#define trace_print_state_after_round() \
Expand Down Expand Up @@ -73,12 +73,13 @@
location{tt, leg_start_location}, leg_start_time, j.start_time_, \
o.duration())

#define trace_rc_intermodal_fp_start_found \
trace_reconstruct( \
" --> start: FP+INTERMODAL START -> {} leg_start_time={}, " \
"j_start_time={}, offset={}, footpath=({}, {})\n", \
location{tt, o.target()}, leg_start_time, j.start_time_, o.duration(), \
adjusted_transfer_time(q.transfer_time_settings_, fp.duration()), \
#define trace_rc_intermodal_fp_start_found \
trace_reconstruct( \
" --> start: FP+INTERMODAL START -> {} leg_start_location={}, " \
"leg_start_time={}, j_start_time={}, offset={}, footpath=({}, {})\n", \
location{tt, o.target()}, location{tt, leg_start_location}, \
leg_start_time, j.start_time_, o.duration(), \
adjusted_transfer_time(q.transfer_time_settings_, fp.duration()), \
location{tt, fp.target()})

#define trace_rc_intermodal_fp_no_match \
Expand Down Expand Up @@ -134,30 +135,31 @@
#define trace_rc_transport_not_found \
trace_reconstruct(" -> no entry found\n")

#define trace_rc_transport_entry_not_possible \
trace_reconstruct( \
" ENTRY NOT POSSIBLE AT {}: k={} k-1={}, best_at_stop=min({}, " \
"{})={}={} > event_time={}={}\n", \
location{tt, l}, k, k - 1, raptor_state.best_[to_idx(l)], \
raptor_state.round_times_[k - 1][to_idx(l)], best(k - 1, l), \
delta_to_unix(base, best(k - 1, l)), event_time, \
#define trace_rc_transport_entry_not_possible \
trace_reconstruct( \
" ENTRY NOT POSSIBLE AT {}: k={} k-1={}, v={}->{}, " \
"best_at_stop=min({}, {})={}={} > event_time={}={}\n", \
location{tt, l}, k, k - 1, v, new_v, best_state[to_idx(l)][new_v], \
round_times[k - 1][to_idx(l)][new_v], best(k - 1, l), \
delta_to_unix(base, best(k - 1, l)), event_time, \
fr[stop_idx].time(kFwd ? event_type::kDep : event_type::kArr))

#define trace_rc_transport_entry_found \
trace_reconstruct( \
" FOUND ENTRY AT name={}, dbg={}, location={}: {} <= {}\n", \
fr.name(), fr.dbg(), location{tt, l}, \
delta_to_unix(base, best(k - 1, l)), delta_to_unix(base, event_time))
#define trace_rc_transport_entry_found \
trace_reconstruct( \
" FOUND ENTRY AT name={}, dbg={}, location={}: {} <= {}, " \
"v={}->{}\n", \
fr.name(), fr.dbg(), location{tt, l}, \
delta_to_unix(base, best(k - 1, l)), delta_to_unix(base, event_time), v, \
new_v)

#define trace_rc_fp_intermodal_dest_mismatch \
trace_reconstruct( \
" BAD intermodal+footpath dest offset: {}@{} --{}--> " \
"{}@{} --{}--> END@{} (type={})\n", \
location{tt, fp.target()}, \
raptor_state.round_times_[k][to_idx(fp.target())], \
adjusted_transfer_time(q.transfer_time_settings_, fp.duration()), \
location{tt, eq}, raptor_state.round_times_[k][to_idx(eq)], \
dest_offset.duration_, curr_time, dest_offset.type())
#define trace_rc_fp_intermodal_dest_mismatch \
trace_reconstruct( \
" BAD intermodal+footpath dest offset: {}@{} --{}--> " \
"{}@{} --{}--> END@{} (type={})\n", \
location{tt, fp.target()}, round_times[k][to_idx(fp.target())], \
adjusted_transfer_time(q.transfer_time_settings_, fp.duration()), \
location{tt, eq}, round_times[k][to_idx(eq)], dest_offset.duration_, \
curr_time, dest_offset.type())

#define trace_rc_fp_intermodal_dest_match \
trace_reconstruct( \
Expand Down Expand Up @@ -189,10 +191,11 @@
#define trace_rc_check_fp \
trace_reconstruct( \
"round {}: searching for transports at {} with curr_time={} --{}--> " \
"fp_start={}\n ", \
"fp_start={}, v={}, stay_l={}, stay_fp_target={}, stay_start={}\n ", \
k, location{tt, fp.target()}, delta_to_unix(base, curr_time), \
adjusted_transfer_time(q.transfer_time_settings_, fp.duration()), \
delta_to_unix(base, fp_start))
delta_to_unix(base, fp_start), v, stay_l, stay_fp_target, \
delta_to_unix(base, stay_start))

#else
#define trace_reconstruct(...)
Expand Down
Loading

0 comments on commit 6c19c96

Please sign in to comment.