diff --git a/engine/src/teacher/teacher.cpp b/engine/src/teacher/teacher.cpp index 83bfee6..93781ec 100644 --- a/engine/src/teacher/teacher.cpp +++ b/engine/src/teacher/teacher.cpp @@ -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 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; @@ -804,11 +825,14 @@ std::vector Teacher::run(std::size_t total_budget, std::span> 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 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) { @@ -840,11 +864,14 @@ std::vector Teacher::run(std::size_t total_budget, std::span 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"); } } @@ -869,17 +896,20 @@ std::vector Teacher::run(std::size_t total_budget, std::span 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; 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", @@ -890,18 +920,21 @@ std::vector Teacher::run(std::size_t total_budget, std::span {:.4f}\n", round, current_value, next_value); diff --git a/engine/tests/teacher/teacher_test.cpp b/engine/tests/teacher/teacher_test.cpp index 50a5b97..59e7c97 100644 --- a/engine/tests/teacher/teacher_test.cpp +++ b/engine/tests/teacher/teacher_test.cpp @@ -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 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.