Skip to content

Commit

Permalink
wip: footpath component preprocessing (#76)
Browse files Browse the repository at this point in the history
  • Loading branch information
felixguendling authored Mar 8, 2024
1 parent ea35f57 commit 7f0c17b
Show file tree
Hide file tree
Showing 20 changed files with 632 additions and 518 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
runs-on: ubuntu-latest
container: ghcr.io/motis-project/docker-cpp-build
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4

- name: Format files
run: |
Expand Down Expand Up @@ -53,7 +53,7 @@ jobs:
CLICOLOR_FORCE: 1

steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4

- name: Install ninja
run: choco install ninja
Expand Down Expand Up @@ -119,7 +119,7 @@ jobs:
UBSAN_OPTIONS: halt_on_error=1:abort_on_error=1
ASAN_OPTIONS: alloc_dealloc_mismatch=0
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4

- name: Install ninja
run: brew install ninja
Expand Down Expand Up @@ -197,7 +197,7 @@ jobs:
BUILDCACHE_MAX_CACHE_SIZE: 26843545600
BUILDCACHE_LUA_PATH: ${{ github.workspace }}/tools
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4

- name: Get deps
run: ln -s /deps deps
Expand Down
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=6360fac4f35f4088decb71189adc1337cd28dfaa
commit=4e0e1eb2be611da3e286a306317737071aaf92be
[miniz]
url=git@github.com:motis-project/miniz.git
branch=master
Expand Down
6 changes: 3 additions & 3 deletions .pkg.lock
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
206260688139120269
cista 04f84b0490e0e91ead20ae6eabed5add8f243ba0
281125644813287955
cista 49090f7891d069e6136efef1e730a4f24a190a81
res 7d97784ba785ce8a2677ea77164040fde484fb04
date 26d109612ddb8ba331edba7619a6452667f842bb
fmt edb385ac526c24bc917ec4a41bb0edb28f0ca59e
Expand All @@ -13,5 +13,5 @@ abseil-cpp f2b3825f36e37fddd47c5c395096e9b1e99eca12
protobuf 690e03babf0963d3da9615a2dae0891777842719
unordered_dense 77e91016354e6d8cba24a86c5abb807de2534c02
Catch2 47d56f28a9801911c048d011b375e5631dbb658f
utl b0f35da9628ce6e219ed88c42380271ca2e012b5
utl 4e0e1eb2be611da3e286a306317737071aaf92be
wyhash 1e012b57fc2227a9e583a57e2eacb3da99816d99
29 changes: 23 additions & 6 deletions include/nigiri/common/dial.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,24 @@
namespace nigiri {

template <typename T,
std::size_t MaxBucket,
typename GetBucketFn /* GetBucketFn(T) -> size_t <= MaxBucket */>
struct dial {
explicit dial(GetBucketFn get_bucket = GetBucketFn())
using dist_t =
std::decay_t<decltype(std::declval<GetBucketFn>()(std::declval<T>()))>;

dial() = default;

explicit dial(std::size_t const max_bucket,
GetBucketFn get_bucket = GetBucketFn())
: get_bucket_(std::forward<GetBucketFn>(get_bucket)),
current_bucket_(0),
size_(0),
buckets_(MaxBucket + 1) {}
buckets_(max_bucket + 1) {}

template <typename El>
void push(El&& el) {
auto const dist = get_bucket_(el);
assert(dist <= MaxBucket);
assert(dist < buckets_.size());

buckets_[dist].emplace_back(std::forward<El>(el));
current_bucket_ = std::min(current_bucket_, dist);
Expand All @@ -44,8 +49,20 @@ struct dial {

bool empty() const { return size_ == 0; }

void clear() {
current_bucket_ = 0U;
size_ = 0U;
for (auto& b : buckets_) {
b.clear();
}
}

void n_buckets(dist_t const n) { buckets_.resize(n); }

dist_t n_buckets() const { return static_cast<dist_t>(buckets_.size()); }

private:
std::size_t get_next_bucket() const {
dist_t get_next_bucket() const {
assert(size_ != 0);
auto bucket = current_bucket_;
while (bucket < buckets_.size() && buckets_[bucket].empty()) {
Expand All @@ -55,7 +72,7 @@ struct dial {
}

GetBucketFn get_bucket_;
std::size_t current_bucket_;
dist_t current_bucket_;
std::size_t size_;
std::vector<std::vector<T>> buckets_;
};
Expand Down
7 changes: 7 additions & 0 deletions include/nigiri/constants.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#pragma once

namespace nigiri {

constexpr const auto kWalkSpeed = 1.5; // m/s

} // namespace nigiri
8 changes: 8 additions & 0 deletions include/nigiri/footpath.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,14 @@ struct footpath {
return out << "(" << fp.target() << ", " << fp.duration() << ")";
}

friend bool operator==(footpath const& a, footpath const& b) {
return a.value() == b.value();
}

friend bool operator<(footpath const& a, footpath const& b) {
return a.value() < b.value();
}

location_idx_t::value_t target_ : kTargetBits;
location_idx_t::value_t duration_ : kDurationBits;
};
Expand Down
3 changes: 2 additions & 1 deletion include/nigiri/loader/build_footpaths.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ namespace nigiri::loader {

void build_footpaths(timetable& tt,
bool adjust_footpaths,
bool merge_duplicates);
bool merge_duplicates,
std::uint16_t max_footpath_length);

} // namespace nigiri::loader
30 changes: 0 additions & 30 deletions include/nigiri/loader/floyd_warshall.h

This file was deleted.

7 changes: 6 additions & 1 deletion include/nigiri/loader/init_finish.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
#pragma once

#include <cinttypes>
#include <limits>

namespace nigiri {
struct timetable;
}
Expand All @@ -9,6 +12,8 @@ namespace nigiri::loader {
void register_special_stations(timetable&);
void finalize(timetable&,
bool adjust_footpaths = false,
bool merge_duplicates = false);
bool merge_duplicates = false,
std::uint16_t max_footpath_length =
std::numeric_limits<std::uint16_t>::max());

} // namespace nigiri::loader
11 changes: 11 additions & 0 deletions include/nigiri/loader/link_nearby_stations.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#include "nigiri/loader/match_set.h"

namespace nigiri {
struct timetable;
}

namespace nigiri::loader {

match_set_t link_nearby_stations(timetable&, bool const store_matches);

} // namespace nigiri::loader
12 changes: 12 additions & 0 deletions include/nigiri/loader/match_set.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#include "nigiri/types.h"

namespace nigiri::loader {

using match_set_t = hash_set<pair<location_idx_t, location_idx_t>>;

inline pair<location_idx_t, location_idx_t> make_match_pair(
location_idx_t const a, location_idx_t const b) {
return {std::min(a, b), std::max(a, b)};
}

} // namespace nigiri::loader
16 changes: 16 additions & 0 deletions include/nigiri/loader/merge_duplicates.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#include "nigiri/types.h"

namespace nigiri {
struct timetable;
}

namespace nigiri::loader {

using match_set_t = hash_set<pair<location_idx_t, location_idx_t>>;

unsigned find_duplicates(timetable& tt,
match_set_t const& matches,
location_idx_t const a,
location_idx_t const b);

} // namespace nigiri::loader
10 changes: 5 additions & 5 deletions include/nigiri/logging.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,11 @@ void log(log_lvl const lvl,
fmt::format_string<Args...> fmt_str,
Args&&... args) {
if (lvl >= ::nigiri::s_verbosity) {
fmt::print(
std::clog, "{time} | [{lvl}][{ctx:30}] {msg}\n",
fmt::arg("time", now()), fmt::arg("lvl", to_str(lvl)),
fmt::arg("ctx", ctx),
fmt::arg("msg", fmt::format(fmt_str, std::forward<Args>(args)...)));
fmt::print(std::clog, "{time} | [{lvl}][{ctx:30}] {msg}\n",
fmt::arg("time", now()), fmt::arg("lvl", to_str(lvl)),
fmt::arg("ctx", ctx),
fmt::arg("msg", fmt::format(fmt::runtime(fmt_str),
std::forward<Args>(args)...)));
}
}
#else
Expand Down
47 changes: 46 additions & 1 deletion include/nigiri/routing/dijkstra.h
Original file line number Diff line number Diff line change
@@ -1,16 +1,61 @@
#pragma once

#include "fmt/ranges.h"

#include "nigiri/common/dial.h"
#include "nigiri/footpath.h"
#include "nigiri/types.h"

namespace nigiri {
struct timetable;
struct footpath;
} // namespace nigiri

namespace nigiri::routing {

struct label {
using dist_t = std::uint16_t;
label(location_idx_t const l, dist_t const d) : l_{l}, d_{d} {}
friend bool operator>(label const& a, label const& b) { return a.d_ > b.d_; }
location_idx_t l_;
dist_t d_;
};

inline auto format_as(label const& l) { return std::tuple{l.l_, l.d_}; }

struct get_bucket {
label::dist_t operator()(label const& l) const { return l.d_; }
};

struct query;

template <typename NodeIdx, typename Edge, typename Label, typename GetBucketFn>
void dijkstra(vecvec<NodeIdx, Edge> const& graph,
dial<Label, GetBucketFn>& pq,
std::vector<typename Label::dist_t>& dists,
typename Label::dist_t const max_dist =
std::numeric_limits<typename Label::dist_t>::max()) {
using dist_t = typename Label::dist_t;

while (!pq.empty()) {
auto l = pq.top();
pq.pop();

if (dists[cista::to_idx(l.l_)] < l.d_) {
continue;
}

for (auto const& e : graph[l.l_]) {
auto const edge_target = cista::to_idx(e.target());
auto const new_dist = l.d_ + e.duration().count();
if (new_dist < dists[edge_target] && new_dist < pq.n_buckets() &&
new_dist < max_dist) {
dists[edge_target] = static_cast<dist_t>(new_dist);
pq.push(Label{e.target(), static_cast<dist_t>(new_dist)});
}
}
}
}

void dijkstra(timetable const&,
query const&,
vecvec<location_idx_t, footpath> const& lb_graph,
Expand Down
Loading

0 comments on commit 7f0c17b

Please sign in to comment.