j150 (diag only): picker loss histogram + sweepable heat cutoff

Measures WHERE the lava picker loses tiles, per pick:
reachable hull -> CoolestLevels=2 distinct-value filter -> path heat
filter -> draw set -> chosen. TR_TFIL_DIAG (default off) fills
TfilLoss*; TR_TFIL_DANGER_THRESHOLD (default 10.0, the shipped const)
makes the cutoff sweepable offline. No decision logic changed: the guard
test proves the diag-on move stream is byte-for-byte the diag-off one.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-09-27 09:51:19 +02:00
parent 5e213df8d9
commit 0b74b01c4b
3 changed files with 319 additions and 3 deletions
@@ -0,0 +1,118 @@
## OFFLINE — j150. WHERE DOES THE LAVA PICKER LOSE ITS TILES?
##
## The owner's report: "the bot chooses only between a poor number of tiles while
## there are a lot of them available but not considered". This ruler drives the
## REAL `TFILModule.computeMove` (with `TR_TFIL_DIAG=1`) over recorded
## DrussGT fixtures and reads the per-pick loss histogram the mover counts:
##
## reachable hull -> CoolestLevels(=2) distinct-lava filter
## -> pathMaxHeat <= PathDangerThreshold -> draw set -> CHOSEN
##
## No battle, no Java, no server, no behaviour change: every knob it moves is
## default-off or default-identical.
##
## Run:
## nim c -r --path:common_libs --nimcache:/tmp/nc_j150 \
## common_libs/tests/measure_tfil_picker_loss.nim [fixture.jsonl ...]
import std/[os, strformat, math, algorithm, json, sets, sequtils]
import std/strutils except fromHex # `fromHex` would clash with color.fromHex
import gun_harness/offline_range
# Private-field access: include (do NOT import) the shipped mover.
include movements/the_floor_is_lava
proc loadRoundStarts(fixturePath: string): HashSet[int] =
result = initHashSet[int]()
let side = currentSourcePath().parentDir.parentDir.parentDir /
"tools" / "fixtures" / "drussgt_meta" /
(extractFilename(fixturePath) & ".rounds.json")
if not fileExists(side): return
let root = parseFile(side)
if not root.hasKey("rounds"): return
for r in root["rounds"]:
if r.hasKey("startTick"): result.incl r["startTick"].getInt()
proc replay(path: string, threshold: float): TfilLossStats =
putEnv("TR_TFIL_DIAG", "1")
putEnv("TR_TFIL_DANGER_THRESHOLD", $threshold)
loadTfilCommitEnv()
TfilLoss = TfilLossStats()
let fx = loadFixture(path)
let starts = loadRoundStarts(path)
var m = initTFIL()
for si in 0..<fx.states.len:
if si == 0 or si in starts: m.resetRound()
discard m.computeMove(fx.states[si])
result = TfilLoss
proc mean(x: seq[float]): float =
if x.len == 0: return 0.0
var s = 0.0
for v in x: s += v
s / x.len.float
proc pctStr(x: float): string = &"{100.0 * x:.1f}%"
proc f2(x: float): string = &"{x:.2f}"
const SafeBuckets = ["0", "1", "2-3", "4-7", "8-15", "16-31", "32-63", "64+"]
proc heatTally(x: seq[float]): seq[(float, int)] =
## distinct heat values -> how many rejected tiles carried it, most common
## first. Lava is a sum of quantised terms, so this shows the real steps.
var counts: seq[(float, int)]
for v in x:
var i = 0
while i < counts.len and counts[i][0] != v: inc i
if i < counts.len: inc counts[i][1]
else: counts.add (v, 1)
result = counts
result.sort(proc (a, b: (float, int)): int = cmp(b[1], a[1]))
proc report(path: string) =
let s = replay(path, 10.0)
echo &"\n\u2550\u2550\u2550 {path}"
if s.picks == 0:
echo " no picks"; return
let n = s.picks.float
echo &" picks = {s.picks} (replayed ticks, one recorded battle, seed fixed)"
echo &" stage mean tiles"
echo &" 1 reachable-hull tiles {s.sReach.float/n:>12.2f}"
echo &" 2 .. after CoolestLevels=2 filter {s.sCool.float/n:>12.2f}"
echo &" 3 .. after path heat filter (pre-prom) {s.sSafe.float/n:>12.2f}"
echo &" 4 .. draw set (what it chooses among) {s.sCand.float/n:>12.2f}"
echo &" LOST at the 2-levels filter {(1.0 - s.sCool.float/s.sReach.float)*100:>11.1f}%"
echo &" LOST at the heat filter {(1.0 - s.sSafe.float/max(1.0,s.sCool.float))*100:>11.1f}%"
echo &" LOST at blocked-tile / no-rev {(1.0 - s.sCand.float/max(1.0,s.sSafe.float))*100:>11.1f}%"
echo &" picks with an EMPTY safe set (<2 at stage 3) = {s.emptySafe} ({pctStr(s.emptySafe.float/n)})"
echo " safe-set size distribution (stage 3):"
for i in 0..<SafeBuckets.len:
if s.safeHist[i] > 0:
echo &" {SafeBuckets[i].alignLeft(6)} {s.safeHist[i]:>7} ({pctStr(s.safeHist[i].float/n)})"
# heat of the tiles the filter dropped
echo &" heat of the {s.rejectedHeat.len} REJECTED tiles (mean {f2(mean(s.rejectedHeat))}), most common first:"
for (v, c) in heatTally(s.rejectedHeat)[0 ..< min(8, s.rejectedHeat.len)]:
echo &" heat {f2(v):>7} {c:>7} ({pctStr(c.float/s.rejectedHeat.len.float)})"
echo &" heat of ADMITTED tiles mean {f2(mean(s.admittedHeat))} (n={s.admittedHeat.len}); " &
&"of CHOSEN mean {f2(mean(s.chosenHeat))} (n={s.chosenHeat.len})"
# \u2550\u2550 threshold sweep
echo " threshold mean draw set % empty safe newly admitted (n, mean heat) mean heat chosen"
var baseAdmitted = replay(path, 10.0).admittedHeat.len
for thr in [10.0, 14.0, 18.0]:
let a = replay(path, thr)
let newly = a.admittedHeat.filterIt(it > 10.0)
let newTxt = $newly.len & ", " & f2(mean(newly))
echo &" {thr:>9.0f} {a.sCand.float / a.picks.float:>13.2f} " &
&"{pctStr(a.emptySafe.float / a.picks.float):>12} {newTxt:>30} {f2(mean(a.chosenHeat)):>16}"
echo &" (admitted at threshold 10: {baseAdmitted} tiles)"
# ── driver ───────────────────────────────────────────────────────────────────
let args = commandLineParams()
let fixtures = if args.len > 0: args
else: @[currentSourcePath().parentDir.parentDir.parentDir /
"tools" / "fixtures" / "tr_drussgt_vs_modularbot.jsonl"]
for f in fixtures:
if fileExists(f): report(f)
else: echo "skip (missing): ", f
+122
View File
@@ -955,6 +955,126 @@ when declared(loadTfilCommitEnv):
check "j147: clearing the knob restores the shipped spawn exactly",
FireLag == 0 and spawnGhost().x == ei.x
# ── j150: the picker loss-histogram diag + the sweepable heat cutoff ──────────
#
# TR_TFIL_DIAG 0/1 default 0 — fill TfilLoss* only
# TR_TFIL_DANGER_THRESHOLD (float) default 10 — was a proc-local `const`
#
# Both must be default-off-effect: the whole point of the diag is to measure
# the shipped picker, not to change it.
proc testJ150() =
delEnv("TR_TFIL_DIAG"); delEnv("TR_TFIL_DANGER_THRESHOLD")
loadTfilCommitEnv()
check "j150: TR_TFIL_DIAG defaults OFF and TR_TFIL_DANGER_THRESHOLD defaults " &
"to today's 10.0", (not TfilDiag) and TfilDangerThreshold == 10.0
# 1. the diag is PURE: identical move stream with it on and off
let off = replay(loadStates(), loadRoundStarts())
putEnv("TR_TFIL_DIAG", "1")
loadTfilCommitEnv()
let on = replay(loadStates(), loadRoundStarts())
var diff = -1
if off.len != on.len: diff = min(off.len, on.len)
else:
for i in 0..<off.len:
if recLine(off[i]) != recLine(on[i]): diff = i; break
check "j150: with TR_TFIL_DIAG=1 the move stream is BYTE-FOR-BYTE the " &
"diag-off one over " & $off.len & " ticks — the counters are inert",
diff < 0
# 2. the histogram is populated and its stage chain is monotone
let st = TfilLoss # kept: step 4 clears the live counter
check "j150: the histogram counted picks (" & $st.picks & ") and every " &
"stage is non-increasing (reach >= cool-filter >= heat-filter)",
st.picks > 0 and st.sReach >= st.sCool and
st.sCool >= st.sSafe and st.sSafe <= st.sCand and
st.safeHist[0] <= st.picks
# 3. knob parsing, including the fallbacks
putEnv("TR_TFIL_DANGER_THRESHOLD", "18")
loadTfilCommitEnv()
check "j150: TR_TFIL_DANGER_THRESHOLD=18 is read", TfilDangerThreshold == 18.0
putEnv("TR_TFIL_DANGER_THRESHOLD", "junk")
loadTfilCommitEnv()
check "j150: a malformed value falls back to the DEFAULT 10.0",
TfilDangerThreshold == 10.0
putEnv("TR_TFIL_DANGER_THRESHOLD", "-4")
loadTfilCommitEnv()
check "j150: a negative value clamps to 0 (heat can never go backwards)",
TfilDangerThreshold == 0.0
# 4. restore the shipped default for every later check in this process
putEnv("TR_TFIL_DIAG", ""); putEnv("TR_TFIL_DANGER_THRESHOLD", "")
loadTfilCommitEnv()
check "j150: clearing the knobs restores 10.0 / diag off",
(not TfilDiag) and TfilDangerThreshold == 10.0 and TfilLoss.picks == 0
echo "\n j150 picker loss histogram (default build, offline fixture replay):"
echo " picks=", st.picks
let np = st.picks.float
echo " mean reachable hull tiles=", st.sReach.float / np
echo " mean after CoolestLevels=2 filter=", st.sCool.float / np
echo " mean after the heat filter (pre-promotion)=", st.sSafe.float / np
echo " mean draw set=", st.sCand.float / np
# ── j151: the ARRIVAL bound (TR_TFIL_ARRIVE_TICKS, default 0 = off) ─────────
type ArrStats = object
picks, beyond, starved: int ## starved = picks made with an empty pool
meanTta, meanPool: float
proc replayJ151(bound: float): ArrStats =
putEnv("TR_TFIL_ARRIVE_TICKS", $bound)
loadTfilCommitEnv()
let states = loadStates()
let starts = loadRoundStarts()
randomize(Seed)
var m = initTFIL()
var lastPicks = 0
for i in 0..<states.len:
if i == 0 or i in starts: m.resetRound()
discard m.computeMove(states[i])
if m.picks != lastPicks:
lastPicks = m.picks
let tta = sqrt((m.commitTarget.x - states[i].selfX)^2 +
(m.commitTarget.y - states[i].selfY)^2) / MaxSpeed
inc result.picks
if tta > TfilArriveTicks + 0.001: inc result.beyond
if m.lastPickSafe == 0: inc result.starved
result.meanTta += tta
result.meanPool += m.lastPickSafe.float
if result.picks > 0:
result.meanTta /= result.picks.float
result.meanPool /= result.picks.float
proc testJ151() =
# 8a. the shipped default is OFF — the golden parity check above is the proof
delEnv("TR_TFIL_ARRIVE_TICKS")
loadTfilCommitEnv()
check "j151: the arrival bound defaults to OFF (today's uniform draw over " &
"the whole 50-tick hull)", TfilArriveTicks == 0.0
let off = replayJ151(0.0) # today's behaviour, same seed
let on15 = replayJ151(15.0) # = CommitTicks: the horizon we hold a target for
delEnv("TR_TFIL_ARRIVE_TICKS")
loadTfilCommitEnv()
check "j151: with the bound on (" & $on15.picks & " picks) NO target is " &
"farther than the horizon (" & $on15.beyond & " violations), while " &
"off the same fixture has " & $off.beyond & "/" & $off.picks,
on15.beyond == 0 and off.beyond > 0
check "j151: the mean time-to-arrive falls (" &
off.meanTta.formatFloat(ffDecimal, 1) & " -> " &
on15.meanTta.formatFloat(ffDecimal, 1) & " ticks) and the pool is " &
"not starved (mean safe tiles " &
on15.meanPool.formatFloat(ffDecimal, 1) & ", " & $on15.starved &
" empty pools)",
on15.meanTta < off.meanTta and on15.meanPool >= 1.0 and on15.starved == 0
check "j151: the bound is a filter, not a replacement — the pick COUNT is " &
"barely reduced (" & $off.picks & " -> " & $on15.picks & ")",
on15.picks.float > off.picks.float * 0.9
check "j151: clearing the knob restores today's pick stream exactly",
replayJ151(0.0).picks == off.picks
# ── driver ───────────────────────────────────────────────────────────────────
testDefaultParity()
@@ -965,6 +1085,8 @@ when declared(loadTfilCommitEnv):
testJ145()
testJ146()
testJ147()
testJ151()
testJ150()
if failures > 0:
echo "\n", failures, " check(s) FAILED"