Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bitvec #83

Merged
merged 7 commits into from
May 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .pkg
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
[cista]
url=git@github.com:felixguendling/cista.git
branch=master
commit=49090f7891d069e6136efef1e730a4f24a190a81
commit=ebd5eb5cc7f82c414d3e060a3937d497189a103f
[geo]
url=git@github.com:motis-project/geo.git
branch=master
Expand Down
126 changes: 54 additions & 72 deletions include/nigiri/routing/raptor/raptor.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ struct raptor {
raptor(timetable const& tt,
rt_timetable const* rtt,
raptor_state& state,
std::vector<bool>& is_dest,
bitvec& is_dest,
std::vector<std::uint16_t>& dist_to_dest,
std::vector<std::uint16_t>& lb,
day_idx_t const base,
Expand Down Expand Up @@ -84,19 +84,19 @@ struct raptor {
void next_start_time() {
utl::fill(state_.best_, kInvalid);
utl::fill(state_.tmp_, kInvalid);
utl::fill(state_.prev_station_mark_, false);
utl::fill(state_.station_mark_, false);
utl::fill(state_.route_mark_, false);
utl::fill(state_.prev_station_mark_.blocks_, 0U);
utl::fill(state_.station_mark_.blocks_, 0U);
utl::fill(state_.route_mark_.blocks_, 0U);
if constexpr (Rt) {
utl::fill(state_.rt_transport_mark_, false);
utl::fill(state_.rt_transport_mark_.blocks_, 0U);
}
}

void add_start(location_idx_t const l, unixtime_t const t) {
trace_upd("adding start {}: {}\n", location{tt_, l}, t);
state_.best_[to_idx(l)] = unix_to_delta(base(), t);
state_.round_times_[0U][to_idx(l)] = unix_to_delta(base(), t);
state_.station_mark_[to_idx(l)] = true;
state_.station_mark_.set(to_idx(l), true);
}

void execute(unixtime_t const start_time,
Expand All @@ -116,35 +116,33 @@ struct raptor {
for (auto k = 1U; k != end_k; ++k) {
for (auto i = 0U; i != n_locations_; ++i) {
state_.best_[i] = get_best(state_.round_times_[k][i], state_.best_[i]);
if (is_dest_[i]) {
update_time_at_dest(k, state_.best_[i]);
}
}
is_dest_.for_each_set_bit([&](std::uint64_t const i) {
update_time_at_dest(k, state_.best_[i]);
});

auto any_marked = false;
for (auto i = 0U; i != n_locations_; ++i) {
if (state_.station_mark_[i]) {
for (auto const& r : tt_.location_routes_[location_idx_t{i}]) {
state_.station_mark_.for_each_set_bit([&](std::uint64_t const i) {
for (auto const& r : tt_.location_routes_[location_idx_t{i}]) {
any_marked = true;
state_.route_mark_.set(to_idx(r), true);
}
if constexpr (Rt) {
for (auto const& rt_t :
rtt_->location_rt_transports_[location_idx_t{i}]) {
any_marked = true;
state_.route_mark_[to_idx(r)] = true;
}
if constexpr (Rt) {
for (auto const& rt_t :
rtt_->location_rt_transports_[location_idx_t{i}]) {
any_marked = true;
state_.rt_transport_mark_[to_idx(rt_t)] = true;
}
state_.rt_transport_mark_.set(to_idx(rt_t), true);
}
}
}
});

if (!any_marked) {
trace_print_state_after_round();
break;
}

std::swap(state_.prev_station_mark_, state_.station_mark_);
utl::fill(state_.station_mark_, false);
utl::fill(state_.station_mark_.blocks_, 0U);

any_marked = (allowed_claszes_ == all_clasz_allowed())
? loop_routes<false>(k)
Expand All @@ -160,10 +158,10 @@ struct raptor {
break;
}

utl::fill(state_.route_mark_, false);
utl::fill(state_.route_mark_.blocks_, 0U);

std::swap(state_.prev_station_mark_, state_.station_mark_);
utl::fill(state_.station_mark_, false);
utl::fill(state_.station_mark_.blocks_, 0U);

update_transfers(k);
update_footpaths(k, prf_idx);
Expand All @@ -172,12 +170,7 @@ struct raptor {
trace_print_state_after_round();
}

for (auto i = 0U; i != n_locations_; ++i) {
auto const is_dest = is_dest_[i];
if (!is_dest) {
continue;
}

is_dest_.for_each_set_bit([&](auto const i) {
for (auto k = 1U; k != end_k; ++k) {
auto const dest_time = state_.round_times_[k][i];
if (dest_time != kInvalid) {
Expand All @@ -197,7 +190,7 @@ struct raptor {
}
}
}
}
});
}

void reconstruct(query const& q, journey& j) {
Expand All @@ -212,51 +205,44 @@ struct raptor {
template <bool WithClaszFilter>
bool loop_routes(unsigned const k) {
auto any_marked = false;
for (auto r_idx = 0U; r_idx != n_routes_; ++r_idx) {
state_.route_mark_.for_each_set_bit([&](auto const r_idx) {
auto const r = route_idx_t{r_idx};

if (state_.route_mark_[r_idx]) {
if constexpr (WithClaszFilter) {
if (!is_allowed(allowed_claszes_, tt_.route_clasz_[r])) {
continue;
}
if constexpr (WithClaszFilter) {
if (!is_allowed(allowed_claszes_, tt_.route_clasz_[r])) {
return;
}

++stats_.n_routes_visited_;
trace("┊ ├k={} updating route {}\n", k, r);
any_marked |= update_route(k, r);
}
}

++stats_.n_routes_visited_;
trace("┊ ├k={} updating route {}\n", k, r);
any_marked |= update_route(k, r);
});
return any_marked;
}

template <bool WithClaszFilter>
bool loop_rt_routes(unsigned const k) {
auto any_marked = false;
for (auto rt_t_idx = 0U; rt_t_idx != n_rt_transports_; ++rt_t_idx) {
if (state_.rt_transport_mark_[rt_t_idx]) {
auto const rt_t = rt_transport_idx_t{rt_t_idx};
state_.rt_transport_mark_.for_each_set_bit([&](auto const rt_t_idx) {
auto const rt_t = rt_transport_idx_t{rt_t_idx};

if constexpr (WithClaszFilter) {
if (!is_allowed(allowed_claszes_,
rtt_->rt_transport_section_clasz_[rt_t][0])) {
continue;
}
if constexpr (WithClaszFilter) {
if (!is_allowed(allowed_claszes_,
rtt_->rt_transport_section_clasz_[rt_t][0])) {
return;
}

++stats_.n_routes_visited_;
trace("┊ ├k={} updating rt transport {}\n", k, rt_t);
any_marked |= update_rt_transport(k, rt_t);
}
}

++stats_.n_routes_visited_;
trace("┊ ├k={} updating rt transport {}\n", k, rt_t);
any_marked |= update_rt_transport(k, rt_t);
});
return any_marked;
}

void update_transfers(unsigned const k) {
for (auto i = 0U; i != n_locations_; ++i) {
if (!state_.prev_station_mark_[i]) {
continue;
}
state_.prev_station_mark_.for_each_set_bit([&](auto&& i) {
auto const is_dest = is_dest_[i];
auto const transfer_time =
(!is_intermodal_dest() && is_dest)
Expand All @@ -269,26 +255,22 @@ struct raptor {
if (lb_[i] == kUnreachable ||
!is_better(fp_target_time + dir(lb_[i]), time_at_dest_[k])) {
++stats_.fp_update_prevented_by_lower_bound_;
continue;
return;
}

++stats_.n_earliest_arrival_updated_by_footpath_;
state_.round_times_[k][i] = fp_target_time;
state_.best_[i] = fp_target_time;
state_.station_mark_[i] = true;
state_.station_mark_.set(i, true);
if (is_dest) {
update_time_at_dest(k, fp_target_time);
}
}
}
});
}

void update_footpaths(unsigned const k, profile_idx_t const prf_idx) {
for (auto i = 0U; i != n_locations_; ++i) {
if (!state_.prev_station_mark_[i]) {
continue;
}

state_.prev_station_mark_.for_each_set_bit([&](std::uint64_t const i) {
auto const l_idx = location_idx_t{i};
auto const& fps = kFwd ? tt_.locations_.footpaths_out_[prf_idx][l_idx]
: tt_.locations_.footpaths_in_[prf_idx][l_idx];
Expand Down Expand Up @@ -326,7 +308,7 @@ struct raptor {
++stats_.n_earliest_arrival_updated_by_footpath_;
state_.round_times_[k][to_idx(fp.target())] = fp_target_time;
state_.best_[to_idx(fp.target())] = fp_target_time;
state_.station_mark_[to_idx(fp.target())] = true;
state_.station_mark_.set(to_idx(fp.target()), true);
if (is_dest_[to_idx(fp.target())]) {
update_time_at_dest(k, fp_target_time);
}
Expand All @@ -339,7 +321,7 @@ struct raptor {
state_.best_[to_idx(fp.target())], to_unix(time_at_dest_[k]));
}
}
}
});
}

void update_intermodal_footpaths(unsigned const k) {
Expand Down Expand Up @@ -398,7 +380,7 @@ struct raptor {

++stats_.n_earliest_arrival_updated_by_route_;
state_.tmp_[l_idx] = get_best(by_transport, state_.tmp_[l_idx]);
state_.station_mark_[l_idx] = true;
state_.station_mark_.set(l_idx, true);
current_best = by_transport;
any_marked = true;
}
Expand Down Expand Up @@ -471,7 +453,7 @@ struct raptor {

++stats_.n_earliest_arrival_updated_by_route_;
state_.tmp_[l_idx] = get_best(by_transport, state_.tmp_[l_idx]);
state_.station_mark_[l_idx] = true;
state_.station_mark_.set(l_idx, true);
current_best = by_transport;
any_marked = true;
} else {
Expand Down Expand Up @@ -715,7 +697,7 @@ struct raptor {
timetable const& tt_;
rt_timetable const* rtt_{nullptr};
raptor_state& state_;
std::vector<bool>& is_dest_;
bitvec& is_dest_;
std::vector<std::uint16_t>& dist_to_end_;
std::vector<std::uint16_t>& lb_;
std::array<delta_t, kMaxTransfers + 1> time_at_dest_;
Expand Down
9 changes: 5 additions & 4 deletions include/nigiri/routing/raptor/raptor_state.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#include "date/date.h"

#include "cista/containers/bitvec.h"
#include "cista/containers/flat_matrix.h"

#include "nigiri/common/delta_t.h"
Expand Down Expand Up @@ -31,10 +32,10 @@ struct raptor_state {
std::vector<delta_t> tmp_;
std::vector<delta_t> best_;
cista::raw::flat_matrix<delta_t> round_times_;
std::vector<bool> station_mark_;
std::vector<bool> prev_station_mark_;
std::vector<bool> route_mark_;
std::vector<bool> rt_transport_mark_;
bitvec station_mark_;
bitvec prev_station_mark_;
bitvec route_mark_;
bitvec rt_transport_mark_;
};

} // namespace nigiri::routing
2 changes: 1 addition & 1 deletion include/nigiri/routing/search.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ struct search_state {
~search_state() = default;

std::vector<std::uint16_t> travel_time_lower_bound_;
std::vector<bool> is_destination_;
bitvec is_destination_;
std::vector<std::uint16_t> dist_to_dest_;
std::vector<start> starts_;
pareto_set<journey> results_;
Expand Down
2 changes: 1 addition & 1 deletion include/nigiri/routing/start_times.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ void get_starts(direction,
void collect_destinations(timetable const&,
std::vector<offset> const& destinations,
location_match_mode const,
std::vector<bool>& is_destination,
bitvec& is_destination,
std::vector<std::uint16_t>& dist_to_dest);

} // namespace nigiri::routing
8 changes: 4 additions & 4 deletions src/routing/start_times.cc
Original file line number Diff line number Diff line change
Expand Up @@ -263,16 +263,16 @@ void get_starts(direction const search_dir,
void collect_destinations(timetable const& tt,
std::vector<offset> const& destinations,
location_match_mode const match_mode,
std::vector<bool>& is_destination,
bitvec& is_destination,
std::vector<std::uint16_t>& dist_to_dest) {
is_destination.resize(tt.n_locations());
utl::fill(is_destination, false);
utl::fill(is_destination.blocks_, 0U);

static constexpr auto const kIntermodalTarget =
to_idx(get_special_station(special_station::kEnd));

if (match_mode == location_match_mode::kIntermodal) {
is_destination[kIntermodalTarget] = true;
is_destination.set(kIntermodalTarget, true);
dist_to_dest.resize(tt.n_locations());
utl::fill(dist_to_dest, std::numeric_limits<std::uint16_t>::max());
} else {
Expand All @@ -286,7 +286,7 @@ void collect_destinations(timetable const& tt,
dist_to_dest[to_idx(l)] =
static_cast<std::uint16_t>(d.duration_.count());
} else {
is_destination[to_idx(l)] = true;
is_destination.set(to_idx(l), true);
}
trace_start(" DEST META: {}, duration={}\n", location{tt, l},
d.duration_);
Expand Down
Loading