Skip to content
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
42 changes: 21 additions & 21 deletions engine/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -66,29 +66,29 @@ add_executable(teacher_eval
target_link_libraries(teacher_eval PRIVATE engine)

add_executable(engine_tests
tests/parquet_loader_test.cpp
tests/projection_test.cpp
tests/split/time_split_test.cpp
tests/allocation/size_proportional_test.cpp
tests/core/interpolation_test.cpp
tests/core/parallel_test.cpp
tests/core/projection_test.cpp
tests/core/simplification_test.cpp
tests/core/subset_test.cpp
tests/evaluator/combined_test.cpp
tests/evaluator/evaluator_test.cpp
tests/io/parquet_loader_test.cpp
tests/pipeline/config_test.cpp
tests/query/continuous_frechet_test.cpp
tests/query/join_test.cpp
tests/query/knn_test.cpp
tests/query/range_test.cpp
tests/query/route_clustering_test.cpp
tests/query/where_at_test.cpp
tests/selection/rdp_test.cpp
tests/selection/sed_test.cpp
tests/selection/simplify_database_test.cpp
tests/split/random_split_test.cpp
tests/selectors/rdp_test.cpp
tests/selectors/sed_test.cpp
tests/size_proportional_test.cpp
tests/interpolation_test.cpp
tests/where_at_test.cpp
tests/evaluator_test.cpp
tests/subset_test.cpp
tests/simplification_test.cpp
tests/selectors/simplify_database_test.cpp
tests/range_test.cpp
tests/parallel_test.cpp
tests/knn_test.cpp
tests/config_test.cpp
tests/join_test.cpp
tests/combined_test.cpp
tests/route_clustering_test.cpp
tests/continuous_frechet_test.cpp
tests/teacher/curves_test.cpp
tests/split/time_split_test.cpp
tests/teacher/certificate_test.cpp
tests/teacher/curves_test.cpp
tests/teacher/teacher_test.cpp
)
target_link_libraries(engine_tests PRIVATE engine GTest::gtest_main Arrow::arrow_shared Parquet::parquet_shared)
Expand Down
28 changes: 17 additions & 11 deletions engine/apps/teacher_eval.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,7 @@ struct Arm {
std::string name;
double share;
KeptSets kept;
std::optional<teacher::Objective::Terms> metrics; // the teacher's, if it computed them
};

// PARTNER: RDP with weight 1 + lambda for trajectories that have a join partner
Expand All @@ -244,19 +245,24 @@ std::vector<Arm> baseline_arms(const core::TrajectoryDb &db,
db, allocation::AllocatorType::SizeProportional, budget, alpha);
};
const auto equal = geometric(0.0);
arms.push_back({"RDP_GLOBAL",
share,
core::to_kept(selection::rdp::simplify_database(db, budget)),
{}});
arms.push_back({"RDP_a0", share, select(db, AlgorithmType::RDP, equal), {}});
arms.push_back(
{"RDP_GLOBAL", share, core::to_kept(selection::rdp::simplify_database(db, budget))});
arms.push_back({"RDP_a0", share, select(db, AlgorithmType::RDP, equal)});
arms.push_back({"RDP_a025", share, select(db, AlgorithmType::RDP, geometric(kRdpAlpha))});
arms.push_back({"SED_a0", share, select(db, AlgorithmType::SED, equal)});
arms.push_back({"UNI_a0", share, select(db, AlgorithmType::UNIFORM, equal)});
{"RDP_a025", share, select(db, AlgorithmType::RDP, geometric(kRdpAlpha)), {}});
arms.push_back({"SED_a0", share, select(db, AlgorithmType::SED, equal), {}});
arms.push_back({"UNI_a0", share, select(db, AlgorithmType::UNIFORM, equal), {}});
for (const double lambda : kPartnerLambdas) {
std::vector<double> weights(db.size());
for (std::size_t i = 0; i < db.size(); ++i)
weights[i] = 1.0 + (partnered[i] ? lambda : 0.0);
arms.push_back({std::format("PARTNER_l{:g}", lambda), share,
arms.push_back({std::format("PARTNER_l{:g}", lambda),
share,
select(db, AlgorithmType::RDP,
weighted_budgets(db, weights, budget - 2 * db.size()))});
weighted_budgets(db, weights, budget - 2 * db.size())),
{}});
}
}
return arms;
Expand Down Expand Up @@ -541,7 +547,7 @@ int main(int argc, char **argv) try {
for (const double share : config.budget_shares) {
start = std::chrono::steady_clock::now();
for (auto &arm : model.run(allocation::total_budget(db, share), args.arms))
arms.push_back({arm.name, share, core::to_kept(arm.simplification)});
arms.push_back({arm.name, share, core::to_kept(arm.simplification), arm.metrics});
timings << std::format("{},teacher_{:g},{:.2f}\n", seed, share,
core::seconds_since(start));
}
Expand All @@ -550,7 +556,7 @@ int main(int argc, char **argv) try {
arms.push_back(std::move(arm));
for (const double share : config.budget_shares)
for (auto &arm : model.single_recipes(allocation::total_budget(db, share)))
arms.push_back({arm.name, share, core::to_kept(arm.simplification)});
arms.push_back({arm.name, share, core::to_kept(arm.simplification), {}});
}
for (const auto &arm : arms) {
validate(arm, db);
Expand Down Expand Up @@ -614,7 +620,7 @@ int main(int argc, char **argv) try {
evaluator::Evaluator(db, compressed, test, scoring).evaluate().scores;
const auto normalized = evaluator::normalize_scores(scores, floor);
// The teacher's own estimate of each metric, next to the test's
const auto teacher_metrics = objective.metrics(arm.kept);
const auto teacher_metrics = arm.metrics ? *arm.metrics : objective.metrics(arm.kept);
const auto &teacher_floors = objective.floors();
auto norm = [&](teacher::Objective::Term t) {
return (teacher_metrics[t] - teacher_floors[t]) / (1.0 - teacher_floors[t]);
Expand All @@ -632,7 +638,7 @@ int main(int argc, char **argv) try {
scores.join.f1, scores.clustering.score, scores.where_at.within_radius,
scores.join.micro_f1, scores.knn.kendalls_tau_topk, normalized.knn,
normalized.range, normalized.where_at, normalized.join, normalized.clustering,
normalized.combined, objective.total(arm.kept),
normalized.combined, objective.total(teacher_metrics),
scores.clustering.predicted_clusters, core::seconds_since(start),
mode_name(config.join.mode), scores.self_join.f1, normalized.self_join,
normalized.join_f1, normalized.clustering_score, scores.self_knn.recall_at_k,
Expand Down
5 changes: 5 additions & 0 deletions engine/include/backend/engine/query/knn.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#include <cstddef>
#include <cstdint>
#include <utility>
#include <vector>

namespace query {
Expand Down Expand Up @@ -57,5 +58,9 @@ KnnWorkload generate_knn_queries(const core::TrajectoryDb &original,
std::vector<std::size_t> knn(const core::TrajectoryDb &trajectories,
const core::TrajectoryView &query, std::size_t k,
FrechetVariant variant = FrechetVariant::Continuous);
// The same continuous neighbours, each with its knn_distance
std::vector<std::pair<std::size_t, double>>
knn_with_distances(const core::TrajectoryDb &trajectories, const core::TrajectoryView &query,
std::size_t k);

} // namespace query
53 changes: 41 additions & 12 deletions engine/include/backend/engine/teacher/objective.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <cstddef>
#include <cstdint>
#include <limits>
#include <optional>
#include <span>
#include <vector>

Expand Down Expand Up @@ -44,8 +45,10 @@ class PreparedTrajectories {
};

// Upper bound on the continuous Fréchet distance between a trajectory and a simplification
// keeping a subsequence of its points. Infinity if kept is not such a subsequence
double simplification_bound(const core::TrajectoryView &original, const core::TrajectoryView &kept);
// keeping a subsequence of its points. Infinity if kept is not such a subsequence. segments:
// the original's edge lengths, if already known
double simplification_bound(const core::TrajectoryView &original, const core::TrajectoryView &kept,
std::span<const double> segments = {});

inline constexpr std::size_t kTermCount = 6; // kNN, range, where-at, join, shape, margin
using TermWeights = std::array<double, kTermCount>;
Expand Down Expand Up @@ -120,18 +123,21 @@ class Objective {
friend class Objective;
std::size_t trajectory = 0;
core::TrajectoryView candidate{};
std::span<const core::TrajectoryView> partners{};
Terms terms{};
double join = 0.0;
double deviation = 0.0; // simplification_bound of the candidate
};
IndependentScore prepare_independent(std::size_t i, const core::TrajectoryView &candidate,
std::span<const core::TrajectoryView> partners,
std::span<const double> range_weights = {}) const;
double independent_value(const IndependentScore &score,
double beat = -std::numeric_limits<double>::infinity()) const;

// Exact J_T
// Exact J_T, from the kept sets or from their metrics
double total(const KeptSets &kept) const;
double total(const Terms &metrics) const;
// simplification_bound of candidate against original i, with i's edge lengths cached
double deviation(std::size_t i, const core::TrajectoryView &candidate) const;

// Unscaled metrics
Terms metrics(const KeptSets &kept) const;
Expand Down Expand Up @@ -168,19 +174,30 @@ class Objective {

bool match(const core::TrajectoryView &a, const core::TrajectoryView &b,
std::size_t term = 0) const;
// frechet_within(original j, candidate, radius), first on a coarse copy of j whose deviation
// bound decides most tests
bool original_within(std::size_t j, const core::TrajectoryView &candidate, double radius) const;
// match(original of link.other, candidate), skipping the test when the originals' distance
// plus the candidate's own error already proves it. own_error < 0: not yet computed
// bracket and the candidate's own error already decide it. own_error < 0: not yet computed
bool held_out_match(std::size_t i, const JoinLink &link, const core::TrajectoryView &candidate,
double &own_error) const;

private:
struct KnnMembership {
std::uint32_t query;
double cutoff;
// Bracket on the continuous distance from the query to original i; 0, infinity if unknown
double lower = 0.0, upper = std::numeric_limits<double>::infinity();
};

// Knn slot left 0
Terms raw_separable(std::size_t i, const core::TrajectoryView &candidate) const;
// Whether the triangle bound alone decides d(query, candidate) <= cutoff, given a bracket on
// d(query, original) and deviation bounding d(original, candidate); nullopt if it does not
std::optional<bool> certify(double lower, double upper, double deviation, double cutoff) const;
// m_pair_lower and m_pair_upper, once per pair across terms
void bracket_pairs();

// Knn slot left 0. deviation < 0: computed here if needed
Terms raw_separable(std::size_t i, const core::TrajectoryView &candidate,
double deviation = -1.0) const;
double independent_sum(const IndependentScore &score, double knn) const;
double join_metric(std::span<const core::TrajectoryView> state) const;
std::vector<double> join_f1s(std::span<const core::TrajectoryView> state) const;
Expand All @@ -201,15 +218,23 @@ class Objective {

std::vector<std::uint32_t> m_pair_i, m_pair_j; // pairs used, positives first
std::vector<std::uint8_t> m_pair_term;
std::vector<double> m_pair_upper; // held-out positives: bound on the originals' distance
std::vector<double> m_eps; // threshold of each join term
// Per pair, a bracket on the originals' continuous distance; 0, infinity if unknown
std::vector<double> m_pair_lower, m_pair_upper;
std::vector<double> m_eps; // threshold of each join term
std::size_t m_positive_count = 0;
std::vector<std::vector<JoinLink>> m_links; // per trajectory
std::vector<std::size_t> m_truth; // positives per slot
std::vector<std::size_t> m_with_truth; // per join term, slots with any positive
std::vector<double> m_join_floor; // per join term, macro F1 with endpoints only
std::vector<double> m_join_weight; // per join term, 1 / (terms * (1 - floor))

// Continuous Fréchet only: brackets and deviation bounds decide tests, coarse copies screen
// the rest
bool m_certify = false;
std::vector<PreparedTrajectory> m_coarse; // per trajectory, RDP to about kCoarseM
std::vector<double> m_coarse_error; // its simplification_bound; < 0: not used
std::vector<std::vector<double>> m_segments; // per trajectory, its edge lengths

Terms m_floor{};
Terms m_scale{}; // weight / (weight sum * (1 - floor))
std::vector<double> m_endpoint_separable; // scaled separable terms, endpoints only
Expand Down Expand Up @@ -296,11 +321,15 @@ class KnnState {
// From every query watching i, in the order propose and apply expect
std::vector<double> distances(std::size_t i, const core::TrajectoryView &candidate,
bool parallel = false) const;
// i's current distances, as distances() gives them for i's current version
std::vector<double> stored(std::size_t i) const;
// Change in J_T
double propose(std::size_t i, std::span<const double> distances) const;
// The same change from i becoming candidate, computing a distance only near a query's k-th
// place
double propose_bounded(std::size_t i, const core::TrajectoryView &candidate) const;
// place. reach bounds d(i's current version, candidate), e.g. both deviation bounds: then
// the stored distance alone often places the candidate
double propose_bounded(std::size_t i, const core::TrajectoryView &candidate,
double reach = std::numeric_limits<double>::infinity()) const;
// Adds trajectory i to the list of every query that one of versions[i] comes within the query's
// watch radius of, the distance of its watch-th nearest in state
void watch(std::span<const std::vector<core::TrajectoryView>> versions,
Expand Down
3 changes: 2 additions & 1 deletion engine/include/backend/engine/teacher/teacher.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ struct TeacherConfig {
struct Arm {
std::string name;
core::Simplification simplification;
double objective = 0.0; // J_T
double objective = 0.0; // J_T
std::optional<Objective::Terms> metrics; // the raw J_T metrics, when computed on the way
};

// Sees only the database it is given and workloads built from it. Trained for held-out queries
Expand Down
26 changes: 20 additions & 6 deletions engine/src/query/knn.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -268,8 +268,9 @@ struct RankedNeighbor {
}
};

std::vector<std::size_t> continuous_knn(const core::TrajectoryDb &trajectory_db,
const core::TrajectoryView &query, std::size_t k) {
std::vector<std::pair<std::size_t, double>> continuous_knn(const core::TrajectoryDb &trajectory_db,
const core::TrajectoryView &query,
std::size_t k) {
CandidateQueue queue(trajectory_db, query, k);
std::priority_queue<RankedNeighbor> nearest;
std::vector<double> prev, curr;
Expand Down Expand Up @@ -313,16 +314,24 @@ std::vector<std::size_t> continuous_knn(const core::TrajectoryDb &trajectory_db,
nearest.push(next);
}
}
std::vector<std::size_t> result(nearest.size());
std::vector<std::pair<std::size_t, double>> result(nearest.size());
for (std::size_t i = result.size(); i > 0; --i) {
result[i - 1] = nearest.top().idx;
result[i - 1] = {nearest.top().idx, nearest.top().distance};
nearest.pop();
}
return result;
}

} // namespace

std::vector<std::pair<std::size_t, double>>
knn_with_distances(const core::TrajectoryDb &trajectories, const core::TrajectoryView &query,
std::size_t k) {
if (k == 0)
throw std::invalid_argument("knn: k = 0, no neighbors");
return continuous_knn(trajectories, query, k);
}

double knn_distance(const core::TrajectoryView &a, const core::TrajectoryView &b,
FrechetVariant variant) {
const double upper = frechet_distance(a, b);
Expand All @@ -342,8 +351,13 @@ std::vector<std::size_t> knn(const core::TrajectoryDb &trajectory_db,
if (k == 0) {
throw std::invalid_argument("knn: k = 0, no neighbors");
}
return variant == FrechetVariant::Discrete ? discrete_knn(trajectory_db, query_trajectory, k)
: continuous_knn(trajectory_db, query_trajectory, k);
if (variant == FrechetVariant::Discrete)
return discrete_knn(trajectory_db, query_trajectory, k);
const auto ranked = continuous_knn(trajectory_db, query_trajectory, k);
std::vector<std::size_t> result(ranked.size());
for (std::size_t i = 0; i < ranked.size(); ++i)
result[i] = ranked[i].first;
return result;
}

KnnWorkload generate_knn_queries(const core::TrajectoryDb &original,
Expand Down
Loading
Loading