Skip to content

Commit

Permalink
Bike sharing levels (#24)
Browse files Browse the repository at this point in the history
  • Loading branch information
felixguendling authored Nov 5, 2024
1 parent f0a1808 commit 60eb1ee
Show file tree
Hide file tree
Showing 10 changed files with 80 additions and 41 deletions.
2 changes: 1 addition & 1 deletion .pkg
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
[geo]
url=git@github.com:motis-project/geo.git
branch=master
commit=5d99aeb10674a41a82d7c78f850abfd9605bf6e1
commit=de5a3586871b8b76a487d42fcc673c9487a1e233
[cista]
url=git@github.com:felixguendling/cista.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 @@
8900990051304848502
cista 5b2ca78d1af99941ea41e195dc3a29b4f05e1b77
15309325614644894217
cista 52577def055e4bdf90eaa461872fc1f7b5b1131d
zlib-ng 68ab3e2d80253ec5dc3c83691d9ff70477b32cd3
boost 73549ebca677fe6214202a1ab580362b4f80e653
conf f9bf4bd83bf55a2170725707e526cbacc45dcc66
Expand All @@ -8,7 +8,7 @@ fmt edb385ac526c24bc917ec4a41bb0edb28f0ca59e
doctest 70e8f76437b76dd5e9c0a2eb9b907df190ab71a0
googletest 34a46558609e05865c197f0260ab36daa7cbbb6e
utl 538a11dec577de7238c7640491b3d4e091a33e08
geo 5d99aeb10674a41a82d7c78f850abfd9605bf6e1
geo de5a3586871b8b76a487d42fcc673c9487a1e233
libosmium 6e6d6b3081cc8bdf25dda89730e25c36eb995516
mimalloc 2a557cafb2e9e7c872358a83a63c62a7e14330b3
libressl 39c1bf084d5c179d7bbce7ba902fffbebff0ee15
Expand Down
2 changes: 1 addition & 1 deletion exe/backend/src/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ int main(int argc, char const* argv[]) {
pl->build_rtree(w);
}

auto const l = lookup{w};
auto const l = lookup{w, opt.data_dir_, cista::mmap::protection::READ};

auto ioc = boost::asio::io_context{};
auto pool = boost::asio::io_context{};
Expand Down
8 changes: 5 additions & 3 deletions exe/benchmark.cc
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,11 @@ int main(int argc, char const* argv[]) {
auto const start =
node_idx_t{cista::hash_combine(h, ++n, i.load()) % w.n_nodes()};
d.reset(opt.max_dist_);
d.add_start(car::label{car::node{start, 0, direction::kForward}, 0U});
d.add_start(car::label{car::node{start, 0, direction::kBackward}, 0U});
d.run<direction::kForward, false>(*w.r_, opt.max_dist_, nullptr,
d.add_start(w,
car::label{car::node{start, 0, direction::kForward}, 0U});
d.add_start(w,
car::label{car::node{start, 0, direction::kBackward}, 0U});
d.run<direction::kForward, false>(w, *w.r_, opt.max_dist_, nullptr,
nullptr);
}
});
Expand Down
42 changes: 35 additions & 7 deletions include/osr/routing/dijkstra.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ namespace osr {

struct sharing_data;

constexpr auto const kDebug = false;

template <typename Profile>
struct dijkstra {
using profile_t = Profile;
Expand All @@ -28,9 +30,14 @@ struct dijkstra {
cost_.clear();
}

void add_start(label const l) {
void add_start(ways const& w, label const l) {
if (cost_[l.get_node().get_key()].update(l, l.get_node(), l.cost(),
node::invalid())) {
if constexpr (kDebug) {
std::cout << "START ";
l.get_node().print(std::cout, w);
std::cout << "\n";
}
pq_.push(l);
}
}
Expand All @@ -41,7 +48,8 @@ struct dijkstra {
}

template <direction SearchDir, bool WithBlocked>
void run(ways::routing const& r,
void run(ways const& w,
ways::routing const& r,
cost_t const max,
bitvec<node_idx_t> const* blocked,
sharing_data const* sharing) {
Expand All @@ -51,36 +59,56 @@ struct dijkstra {
continue;
}

if constexpr (kDebug) {
std::cout << "EXTRACT ";
l.get_node().print(std::cout, w);
std::cout << "\n";
}

auto const curr = l.get_node();
Profile::template adjacent<SearchDir, WithBlocked>(
r, curr, blocked, sharing,
[&](node const neighbor, std::uint32_t const cost, distance_t,
way_idx_t const way, std::uint16_t, std::uint16_t) {
if constexpr (kDebug) {
std::cout << " NEIGHBOR ";
neighbor.print(std::cout, w);
}

auto const total = l.cost() + cost;
if (total < max &&
cost_[neighbor.get_key()].update(
l, neighbor, static_cast<cost_t>(total), curr)) {
auto next = label{neighbor, static_cast<cost_t>(total)};
next.track(l, r, way, neighbor.get_node());
pq_.push(std::move(next));

if constexpr (kDebug) {
std::cout << " -> PUSH\n";
}
} else {
if constexpr (kDebug) {
std::cout << " -> DOMINATED\n";
}
}
});
}
}

void run(ways::routing const& r,
void run(ways const& w,
ways::routing const& r,
cost_t const max,
bitvec<node_idx_t> const* blocked,
sharing_data const* sharing,
direction const dir) {
if (blocked == nullptr) {
dir == direction::kForward
? run<direction::kForward, false>(r, max, blocked, sharing)
: run<direction::kBackward, false>(r, max, blocked, sharing);
? run<direction::kForward, false>(w, r, max, blocked, sharing)
: run<direction::kBackward, false>(w, r, max, blocked, sharing);
} else {
dir == direction::kForward
? run<direction::kForward, true>(r, max, blocked, sharing)
: run<direction::kBackward, true>(r, max, blocked, sharing);
? run<direction::kForward, true>(w, r, max, blocked, sharing)
: run<direction::kBackward, true>(w, r, max, blocked, sharing);
}
}

Expand Down
43 changes: 26 additions & 17 deletions include/osr/routing/profiles/bike_sharing.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,28 @@ struct bike_sharing {
}

struct key {
friend bool operator==(key, key) = default;
friend bool operator==(key const a, key const b) {
auto const is_zero = [](level_t const l) {
return l == kNoLevel || l == level_t{0.F};
};
return a.n_ == b.n_ &&
(a.lvl_ == b.lvl_ || (is_zero(a.lvl_) && is_zero(b.lvl_)));
}

node_idx_t n_{node_idx_t::invalid()};
level_t lvl_{};
};

using hash = footp::hash;

struct node {
friend bool operator==(node, node) = default;
friend bool operator==(node const a, node const b) {
auto const is_zero = [](level_t const l) {
return l == kNoLevel || l == level_t{0.F};
};
return a.n_ == b.n_ && a.type_ == b.type_ &&
(a.lvl_ == b.lvl_ || (is_zero(a.lvl_) && is_zero(b.lvl_)));
}

boost::json::object geojson_properties(ways const& w) const {
auto properties =
Expand All @@ -89,8 +103,10 @@ struct bike_sharing {
}

std::ostream& print(std::ostream& out, ways const& w) const {
return out << "(node=" << w.node_to_osm_[n_]
<< ", level=" << lvl_.to_float()
return out << "(node="
<< (n_ >= w.n_nodes() ? osm_node_idx_t{to_idx(n_)}
: w.node_to_osm_[n_])
<< (n_ >= w.n_nodes() ? "*" : "") << ", level=" << lvl_
<< ", type=" << node_type_to_str(type_) << ")";
}

Expand Down Expand Up @@ -197,16 +213,6 @@ struct bike_sharing {
std::array<node_type, kN> pred_type_{};
};

struct hash {
using is_avalanching = void;
auto operator()(key const k) const noexcept -> std::uint64_t {
using namespace ankerl::unordered_dense::detail;
return wyhash::mix(
wyhash::hash(static_cast<std::uint64_t>(to_idx(k.lvl_))),
wyhash::hash(static_cast<std::uint64_t>(to_idx(k.n_))));
}
};

static footp::node to_foot(node const n) {
return {.n_ = n.n_, .lvl_ = n.lvl_};
}
Expand Down Expand Up @@ -244,6 +250,7 @@ struct bike_sharing {
footp::resolve_all(w, n, lvl, [&](footp::node const neighbor) {
f(to_node(neighbor, node_type::kInitialFoot));
f(to_node(neighbor, node_type::kTrailingFoot));
f(to_node(neighbor, node_type::kBike));
});
}

Expand All @@ -257,8 +264,10 @@ struct bike_sharing {

auto const& handle_additional_edge =
[&](additional_edge const& ae, node_type const nt, cost_t const cost) {
fn(node{.n_ = ae.node_, .type_ = nt, .lvl_ = n.lvl_}, cost,
ae.distance_, way_idx_t::invalid(), 0, 1);
fn(node{.n_ = ae.node_,
.type_ = nt,
.lvl_ = nt == node_type::kBike ? kNoLevel : n.lvl_},
cost, ae.distance_, way_idx_t::invalid(), 0, 1);
};

auto const& continue_on_foot = [&](node_type const nt,
Expand Down Expand Up @@ -294,7 +303,7 @@ struct bike_sharing {
[&](bike::node const neighbor, std::uint32_t const cost,
distance_t const dist, way_idx_t const way,
std::uint16_t const from, std::uint16_t const to) {
fn(to_node(neighbor, n.lvl_), cost + switch_penalty, dist, way,
fn(to_node(neighbor, kNoLevel), cost + switch_penalty, dist, way,
from, to);
});
if (include_additional_edges) {
Expand Down
2 changes: 1 addition & 1 deletion include/osr/routing/profiles/foot.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ struct foot {

struct hash {
using is_avalanching = void;
auto operator()(node const n) const noexcept -> std::uint64_t {
auto operator()(auto const n) const noexcept -> std::uint64_t {
using namespace ankerl::unordered_dense::detail;
return wyhash::mix(
wyhash::hash(static_cast<std::uint64_t>(
Expand Down
8 changes: 4 additions & 4 deletions src/route.cc
Original file line number Diff line number Diff line change
Expand Up @@ -301,15 +301,15 @@ std::optional<path> route(ways const& w,
if (nc->valid() && nc->cost_ < max) {
Profile::resolve_start_node(
*w.r_, start.way_, nc->node_, from.lvl_, dir,
[&](auto const node) { d.add_start({node, nc->cost_}); });
[&](auto const node) { d.add_start(w, {node, nc->cost_}); });
}
}

if (d.pq_.empty()) {
continue;
}

d.run(*w.r_, max, blocked, sharing, dir);
d.run(w, *w.r_, max, blocked, sharing, dir);

auto const c = best_candidate(w, d, to.lvl_, to_match, max, dir);
if (c.has_value()) {
Expand Down Expand Up @@ -350,12 +350,12 @@ std::vector<std::optional<path>> route(
*w.r_, start.way_, nc->node_, from.lvl_, dir, [&](auto const node) {
auto label = typename Profile::label{node, nc->cost_};
label.track(label, *w.r_, start.way_, node.get_node());
d.add_start(label);
d.add_start(w, label);
});
}
}

d.run(*w.r_, max, blocked, sharing, dir);
d.run(w, *w.r_, max, blocked, sharing, dir);

auto found = 0U;
for (auto const [m, t, r] : utl::zip(to_match, to, result)) {
Expand Down
4 changes: 2 additions & 2 deletions test/lvl_wildcard_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ TEST(routing, no_lvl_wildcard) {
extract(false, "test/stuttgart.osm.pbf", kTestFolder);

auto w = osr::ways{kTestFolder, cista::mmap::protection::READ};
auto l = osr::lookup{w};
auto l = osr::lookup{w, kTestFolder, cista::mmap::protection::READ};

auto const p = route(w, l, search_profile::kFoot, //
{{48.7829, 9.18212}, level_t{0.F}},
Expand Down Expand Up @@ -87,4 +87,4 @@ TEST(routing, no_lvl_wildcard) {
"4.87844364E1],[9.1832201E0,4.87845268E1],[9.183214598396768E0,4."
"8784528970208854E1],[9.18337E0,4.87847E1]]}}]}",
path);
}
}
4 changes: 2 additions & 2 deletions test/restriction_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ TEST(extract, wa) {
osr::extract(false, "test/map.osm", "/tmp/osr_test");

auto w = osr::ways{"/tmp/osr_test", cista::mmap::protection::READ};
auto l = osr::lookup{w};
auto l = osr::lookup{w, "/tmp/osr_test", cista::mmap::protection::READ};

auto const n = w.find_node_idx(osm_node_idx_t{528944});
auto const rhoenring = w.find_way(osm_way_idx_t{120682496});
Expand All @@ -35,4 +35,4 @@ TEST(extract, wa) {
n.value(), w.r_->get_way_pos(n.value(), rhoenring.value()),
w.r_->get_way_pos(n.value(), arheilger.value()));
EXPECT_TRUE(is_restricted);
}
}

0 comments on commit 60eb1ee

Please sign in to comment.