From 0ca4b69995c3dfd9d6fc71c7babe5729db0ae4c9 Mon Sep 17 00:00:00 2001 From: TB-PC Date: Fri, 20 Nov 2026 23:04:36 +0100 Subject: [PATCH 1/3] Check the loop's result against its start with exact J_T 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 a switch with a positive tracked gain can lower the real J_T. BUDGET_LOOP, EQUAL_LOOP and the loop inside each reallocation round now recompute exact J_T before and after, and keep the start if the loop lost. Adds a 200-trajectory, k = 3 test where the watch lists cover a small part of the database. --- engine/src/teacher/teacher.cpp | 29 ++++++++++++++++++++++----- engine/tests/teacher/teacher_test.cpp | 18 +++++++++++++++++ 2 files changed, 42 insertions(+), 5 deletions(-) diff --git a/engine/src/teacher/teacher.cpp b/engine/src/teacher/teacher.cpp index 83bfee6..2d54be7 100644 --- a/engine/src/teacher/teacher.cpp +++ b/engine/src/teacher/teacher.cpp @@ -445,6 +445,19 @@ 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 +Methods checked_loop(const Objective &objective, const Candidates &candidates, + const Methods &start, Methods looped, std::string_view name) { + const double 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" : ""); + return lost ? start : std::move(looped); +} + struct Traded { KeptSets kept; double value; @@ -844,7 +857,9 @@ std::vector Teacher::run(std::size_t total_budget, std::span Teacher::run(std::size_t total_budget, std::spanrun(m_seed); + const auto looped = + checked_loop(objective, candidates, choice, search->run(m_seed), "BUDGET_LOOP"); timed("BUDGET_LOOP"); if (want_loop) keep("BUDGET_LOOP", candidates, looped); @@ -897,9 +913,12 @@ std::vector Teacher::run(std::size_t total_budget, std::span 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. From 2478f96da62ba4a88f36b055b914569a48271749 Mon Sep 17 00:00:00 2001 From: TB-PC Date: Sat, 21 Nov 2026 13:29:02 +0100 Subject: [PATCH 2/3] Reuse known J_T values in the loop's exact check checked_loop takes the start's exact J_T when the caller has it and returns the result's, and keep() returns each arm's J_T and accepts a known one. With BUDGET_CHOICE (or EQUAL_CHOICE_RDP_PARTNERS) also requested the check needs no extra J_T evaluation, and a reallocation round reuses the check's value. --- engine/src/teacher/teacher.cpp | 61 ++++++++++++++++++++++------------ 1 file changed, 39 insertions(+), 22 deletions(-) diff --git a/engine/src/teacher/teacher.cpp b/engine/src/teacher/teacher.cpp index 2d54be7..e3abf16 100644 --- a/engine/src/teacher/teacher.cpp +++ b/engine/src/teacher/teacher.cpp @@ -447,15 +447,24 @@ Methods best_response(const core::TrajectoryDb &db, const Objective &objective, // 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 -Methods checked_loop(const Objective &objective, const Candidates &candidates, - const Methods &start, Methods looped, std::string_view name) { - const double before = objective.total(assemble(candidates, start)); +// 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, + 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" : ""); - return lost ? start : std::move(looped); + if (lost) + return {start, *before}; + return {std::move(looped), after}; } struct Traded { @@ -817,11 +826,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) { @@ -853,13 +865,15 @@ 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, - checked_loop(objective, equal_candidates, guess, - best_response(db, objective, equal_candidates, guess, m_seed), - "EQUAL_LOOP")); + 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"); } } @@ -884,18 +898,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 = - checked_loop(objective, candidates, choice, search->run(m_seed), "BUDGET_LOOP"); + 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", @@ -906,7 +922,7 @@ std::vector Teacher::run(std::size_t total_budget, std::span Teacher::run(std::size_t total_budget, std::span {:.4f}\n", round, current_value, next_value); From 7055cb4ecfb771189803f374b86a837f00d83fc3 Mon Sep 17 00:00:00 2001 From: TB-PC Date: Sat, 21 Nov 2026 13:37:01 +0100 Subject: [PATCH 3/3] clang format --- engine/src/teacher/teacher.cpp | 15 ++++++--------- engine/tests/teacher/teacher_test.cpp | 4 ++-- 2 files changed, 8 insertions(+), 11 deletions(-) diff --git a/engine/src/teacher/teacher.cpp b/engine/src/teacher/teacher.cpp index e3abf16..93781ec 100644 --- a/engine/src/teacher/teacher.cpp +++ b/engine/src/teacher/teacher.cpp @@ -453,9 +453,8 @@ 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) { +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)); @@ -869,10 +868,9 @@ std::vector Teacher::run(std::size_t total_budget, std::span Teacher::run(std::size_t total_budget, std::span arms{"EQUAL_CHOICE_RDP_PARTNERS", "EQUAL_LOOP", - "BUDGET_CHOICE", "BUDGET_LOOP"}; + 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);