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
59 changes: 46 additions & 13 deletions engine/src/teacher/teacher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,27 @@ Methods best_response(const core::TrajectoryDb &db, const Objective &objective,
return Search(db, objective, candidates, method).run(seed, update);
}

// The loop accepts switches on the tracked J_T, whose kNN term only watches each query's top k
// plus its nearest others in the start and single-recipe states. In a mixed state an unwatched
// trajectory can enter a top k unseen, so the result is checked exactly against the start.
// before is the start's exact J_T when the caller already has it
struct Checked {
Methods methods;
double value; // exact J_T of methods
};
Checked checked_loop(const Objective &objective, const Candidates &candidates, const Methods &start,
std::optional<double> before, Methods looped, std::string_view name) {
if (!before)
before = objective.total(assemble(candidates, start));
const double after = objective.total(assemble(candidates, looped));
const bool lost = after < *before;
std::cout << std::format("teacher: {} exact check, J_T {:.6f} -> {:.6f}{}\n", name, *before,
after, lost ? ", lost: keeping the start" : "");
if (lost)
return {start, *before};
return {std::move(looped), after};
}

struct Traded {
KeptSets kept;
double value;
Expand Down Expand Up @@ -804,11 +825,14 @@ std::vector<Arm> Teacher::run(std::size_t total_budget, std::span<const std::str
caps[i] = std::min(std::max(cap, equal_budgets[i]), db.trajectory(i).size());

std::map<std::string, Arm, std::less<>> outputs;
auto keep = [&](std::string_view name, const Candidates &from, const Methods &method) {
// Returns the arm's exact J_T; known skips recomputing it
auto keep = [&](std::string_view name, const Candidates &from, const Methods &method,
std::optional<double> known = {}) {
auto kept = assemble(from, method);
const double j_t = objective.total(kept);
const double j_t = known ? *known : objective.total(kept);
std::cout << std::format("teacher {}: {}, J_T {:.4f}\n", name, wins(method), j_t);
outputs[std::string(name)] = {std::string(name), core::to_simplification(kept), j_t};
return j_t;
};
auto start = std::chrono::steady_clock::now();
auto timed = [&](std::string_view step) {
Expand Down Expand Up @@ -840,11 +864,14 @@ std::vector<Arm> Teacher::run(std::size_t total_budget, std::span<const std::str
const auto guess = needs_curves ? curve_methods(curves.best, equal_budgets)
: independent_choice(db, objective, equal_candidates,
&equal_candidates[kRdp]);
std::optional<double> guess_value;
if (want_guess)
keep("EQUAL_CHOICE_RDP_PARTNERS", equal_candidates, guess);
guess_value = keep("EQUAL_CHOICE_RDP_PARTNERS", equal_candidates, guess);
if (want_equal_loop) {
keep("EQUAL_LOOP", equal_candidates,
best_response(db, objective, equal_candidates, guess, m_seed));
const auto looped = checked_loop(
objective, equal_candidates, guess, guess_value,
best_response(db, objective, equal_candidates, guess, m_seed), "EQUAL_LOOP");
keep("EQUAL_LOOP", equal_candidates, looped.methods, looped.value);
timed("EQUAL_LOOP");
}
}
Expand All @@ -869,17 +896,20 @@ std::vector<Arm> Teacher::run(std::size_t total_budget, std::span<const std::str
const auto choice = curve_methods(curves.best, budgets);
const auto candidates =
make_candidates(objective, budgets, caps, needs_loop_candidates ? nullptr : &choice);
std::optional<double> choice_value;
if (want_budget_choice)
keep("BUDGET_CHOICE", candidates, choice);
choice_value = keep("BUDGET_CHOICE", candidates, choice);
// The loop and its no-update control start from the same state and share the setup
std::optional<Search> search;
if (want_loop || want_reallocate || want_trade || want_no_update)
search.emplace(db, objective, candidates, choice);
if (want_loop || want_reallocate || want_trade) {
const auto looped = search->run(m_seed);
const auto checked = checked_loop(objective, candidates, choice, choice_value,
search->run(m_seed), "BUDGET_LOOP");
const auto &looped = checked.methods;
timed("BUDGET_LOOP");
if (want_loop)
keep("BUDGET_LOOP", candidates, looped);
keep("BUDGET_LOOP", candidates, looped, checked.value);
if (want_trade) {
auto traded = trade_budgets(db, objective, caps, assemble(candidates, looped));
outputs["BUDGET_LOOP_TRADE"] = {"BUDGET_LOOP_TRADE",
Expand All @@ -890,18 +920,21 @@ std::vector<Arm> Teacher::run(std::size_t total_budget, std::span<const std::str
// Each round is kept only if J_T rises
auto current = assemble(candidates, looped);
auto current_methods = looped;
double current_value = objective.total(current);
double current_value = checked.value;
for (std::size_t round = 1; round <= kMaxAllocationRounds; ++round) {
const auto round_curves = build_curves(db, objective, current, caps, false);
double round_value = 0;
const auto round_budgets =
allocate(round_curves.best, total_budget, &round_value);
const auto next_candidates = make_candidates(objective, round_budgets, caps);
auto next_methods = best_response(
db, objective, next_candidates,
curve_methods(round_curves.best, round_budgets), m_seed + round);
const auto round_start = curve_methods(round_curves.best, round_budgets);
auto round_loop = checked_loop(
objective, next_candidates, round_start, std::nullopt,
best_response(db, objective, next_candidates, round_start, m_seed + round),
std::format("reallocation round {}", round));
auto next_methods = std::move(round_loop.methods);
auto next = assemble(next_candidates, next_methods);
const double next_value = objective.total(next);
const double next_value = round_loop.value;
std::cout << std::format(
"teacher: reallocation round {}, J_T {:.4f} -> {:.4f}\n", round,
current_value, next_value);
Expand Down
18 changes: 18 additions & 0 deletions engine/tests/teacher/teacher_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -859,6 +859,24 @@ TEST_F(TeacherTest, ArmsKeepTheBudgetAndBestResponseOnlyImproves) {
}
}

// With 200 trajectories and k = 3 the kNN watch lists cover a small part of the database, so the
// tracked J_T can miss a trajectory entering a top k; the exact check must still hold the loops
TEST_F(TeacherTest, LoopsNeverEndBelowTheirStartOnALargeDatabase) {
const auto db = random_db(5, 200, 14);
auto workloads = teacher::load_or_build_workloads(db, small_config(), 13, m_dir);
const teacher::Teacher model(db, std::move(workloads),
{.join_mode = query::JoinMode::HeldOut, .ranking_knn = true}, 13);
const std::vector<std::string> arms{"EQUAL_CHOICE_RDP_PARTNERS", "EQUAL_LOOP", "BUDGET_CHOICE",
"BUDGET_LOOP"};
for (const std::size_t extra : {100u, 400u}) {
SCOPED_TRACE(extra);
const auto result = model.run(2 * db.size() + extra, arms);
ASSERT_EQ(result.size(), arms.size());
EXPECT_GE(result[1].objective, result[0].objective - 1e-12);
EXPECT_GE(result[3].objective, result[2].objective - 1e-12);
}
}

TEST(TradeMatching, SelfSellerRemainsAvailableAfterAnUnprofitableBuyer) {
// Buyer 0 cannot sell to itself, and seller 2 is too expensive. Buyer 1 can still profitably
// buy from seller 0; neither skipping seller 0 nor ending the buyer sweep is valid.
Expand Down
Loading