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
Binary file modified cache/teacher_cache_seed42.bin
Binary file not shown.
1 change: 1 addition & 0 deletions engine/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ add_executable(engine_tests
tests/route_clustering_test.cpp
tests/continuous_frechet_test.cpp
tests/teacher/curves_test.cpp
tests/teacher/certificate_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
25 changes: 24 additions & 1 deletion engine/apps/teacher_eval.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,24 @@ void validate(const Arm &arm, const core::TrajectoryDb &db) {
throw std::runtime_error(std::format("{}: {} points, budget {}", arm.name, total, budget));
}

// FNV-1a over every kept index, so two runs can be checked for identical selections
std::uint64_t kept_hash(const KeptSets &kept) {
std::uint64_t hash = 14695981039346656037ull;
auto mix = [&](std::uint64_t value) {
for (unsigned int byte = 0; byte < 8; ++byte) {
hash ^= value & 0xff;
hash *= 1099511628211ull;
value >>= 8;
}
};
for (const auto &trajectory : kept) {
mix(trajectory.size());
for (const auto index : trajectory)
mix(index);
}
return hash;
}

// Whether the teacher trained on the join at eps_m in mode: one of its thresholds in its own join
// mode, or its held-out term at join.eps_m
bool trained_on(const pipeline::Config &config, const teacher::ObjectiveConfig &objective,
Expand Down Expand Up @@ -342,6 +360,7 @@ int main(int argc, char **argv) try {
"norm_knn,norm_range,norm_where_at,norm_join,norm_self_join");
auto budgets = open_csv(args.out / "budgets.csv", "seed,share,arm,points,budget");
auto timings = open_csv(args.out / "timings.csv", "seed,step,seconds");
auto hashes = open_csv(args.out / "kept_hashes.csv", "seed,share,arm,hash");
std::ofstream knn_check;
if (args.knn_check)
knn_check = open_csv(args.out / "knn_check.csv",
Expand Down Expand Up @@ -380,8 +399,12 @@ int main(int argc, char **argv) try {
for (auto &arm : model.single_recipes(allocation::total_budget(db, share)))
arms.push_back({arm.name, share, core::to_kept(arm.simplification)});
}
for (const auto &arm : arms)
for (const auto &arm : arms) {
validate(arm, db);
hashes << std::format("{},{},{},{:016x}\n", seed, arm.share, arm.name,
kept_hash(arm.kept));
}
hashes.flush();

// Held-out data from here on
start = std::chrono::steady_clock::now();
Expand Down
27 changes: 25 additions & 2 deletions engine/include/backend/engine/teacher/objective.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ class PreparedTrajectories {
std::vector<PreparedTrajectory> m_trajectories;
};

// 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);

inline constexpr std::size_t kTermCount = 4; // kNN, range, where-at, join
using TermWeights = std::array<double, kTermCount>;

Expand Down Expand Up @@ -160,6 +164,10 @@ class Objective {

bool match(const core::TrajectoryView &a, const core::TrajectoryView &b,
std::size_t term = 0) 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
bool held_out_match(std::size_t i, const JoinLink &link, const core::TrajectoryView &candidate,
double &own_error) const;

private:
struct KnnMembership {
Expand Down Expand Up @@ -188,7 +196,8 @@ 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_eps; // threshold of each join 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
std::vector<char> m_term_held_out;
std::size_t m_positive_count = 0;
std::vector<std::vector<JoinLink>> m_links; // per trajectory
Expand All @@ -215,6 +224,9 @@ class RangeState {
std::vector<char> covered; // in boxes(trajectory) order
};
Proposal propose(std::size_t i, const core::TrajectoryView &candidate) const;
// Which of i's boxes the candidate covers; independent of the state
std::vector<char> coverage(std::size_t i, const core::TrajectoryView &candidate) const;
Proposal propose(std::size_t i, std::vector<char> covered) const;
// A proposal is valid until the next apply()
void apply(const Proposal &proposal);
double metric() const;
Expand Down Expand Up @@ -243,6 +255,12 @@ class JoinState {
// Valid until the next apply()
Proposal propose(std::size_t i, const core::TrajectoryView &candidate,
std::span<const core::TrajectoryView> state) const;
// Outcomes of i's links, in links(i) order. Held-out links ignore the state; with
// held_out_known their outcomes are taken from it instead of retested
std::vector<char> outcomes(std::size_t i, const core::TrajectoryView &candidate,
std::span<const core::TrajectoryView> state,
std::span<const char> held_out_known = {}) const;
Proposal propose(std::size_t i, std::vector<char> matched) const;
void apply(const Proposal &proposal);

double metric() const; // as Objective's join metric
Expand Down Expand Up @@ -311,8 +329,12 @@ class KnnState {
struct Watcher {
std::uint32_t query, position; // position in the query's list
};
// True neighbours in e's top k, with the distance at position replaced
// True neighbours in e's top k, with the distance at position replaced. Reads e's ranking
std::size_t hits(std::size_t e, std::size_t position, double distance) const;
// Re-ranks e after its row changed: m_rank[e] and m_hits[e]
void refresh(std::size_t e);
bool ranks_before(std::size_t e, double distance, std::size_t position,
std::size_t other) const;
// The same with e's whole row replaced
std::size_t hits(std::size_t e, std::span<const double> row) const;
// True neighbours in the top k of order (distance, position)
Expand All @@ -328,6 +350,7 @@ class KnnState {
std::vector<std::vector<std::uint32_t>> m_list; // per query, its true top k first
std::vector<std::vector<double>> m_distance; // per query, in list order
std::vector<std::size_t> m_hits; // per query
std::vector<std::vector<std::uint32_t>> m_rank; // per query, positions of its top k + 1
std::size_t m_hit_sum = 0;
std::vector<std::vector<Watcher>> m_watchers; // per trajectory
};
Expand Down
Loading
Loading