Files

136 lines
5.4 KiB
Nim

## G4 live-identification benchmark — unified M5.5 metric.
##
## Runs `Repeat` full `Rounds`-round battles per ground-truth opponent against
## the ChimeraBot observation sidecar (server on a random port, tps=-1), and
## reports the UNIFIED idTicks metric per opponent: median / p90 / id-fraction
## over ALL rounds in the protocol (every round of every run).
##
## Metric (spec §12, M5.5): per round, the round-relative tick at which
## `max_kernel posterior > 0.8`. argmax correctness is a diagnostic only.
## Pass condition: median <= 300 AND id-fraction >= 80% over >= 3 full 8x5 runs.
##
## Findings are reported, never tuned away. Requires Java + JARs + sample bots.
## Run: `nim c -r --hints:off -d:release tests/bench_g4.nim`
import std/[os, osproc, strutils, sequtils, math, tables, algorithm]
import bench_common
import test_framework/server_manager
const
Repeat = 3
Rounds = 5
Telemetry = getTempDir() / "chimera_twin"
Archive = getTempDir() / "chimera_g4_run"
type
Acc = object
ids: seq[int] # per-round unified idTick (-1 = never)
expIds: seq[int]
correct: int # rounds where argmaxCorrect
finalPost, censor, ade, fde: float64
rounds: int
gapMax, gapViol: int
proc run() =
guardSkip()
let botDir = stageBot("g4")
removeDir(Telemetry)
removeDir(Archive)
createDir(Archive)
var acc = initTable[string, Acc]()
for opp in sorted(toSeq(Expected.keys)):
restartServer()
var url = serverUrl()
putEnv("CHIMERA_OPPONENT", opp)
putEnv("CHIMERA_TELEMETRY_DIR", Telemetry)
let sampleBot = SampleRoot / opp
var a: Acc
for run in 1 .. Repeat:
var ok = false
for attempt in 1 .. MaxAttempts:
removeDir(Telemetry)
sleep(500)
if runBattleHard(url, @[botDir, sampleBot], Rounds, 90000):
ok = true
break
echo " ", opp, " run", run, " attempt", attempt, ": battle failed/timeout"
killStaleBots()
restartServer()
url = serverUrl()
if not ok: continue
let runDir = Archive / (opp & "-run" & $run)
copyDir(Telemetry, runDir)
for f in toSeq(walkFiles(runDir / "*.jsonl")):
let rs = parseFile(f, Expected[opp])
if rs.sumTicks == 0: continue
inc a.rounds
a.ids.add(if rs.identified: rs.idTick else: -1)
a.expIds.add(if rs.expIdTick > 0: rs.expIdTick else: -1)
if rs.argmaxCorrect: inc a.correct
a.finalPost += rs.finalPosterior
a.censor += rs.censorRate
a.ade += rs.ade
a.fde += rs.fde
a.gapMax = max(a.gapMax, rs.gapMax)
a.gapViol += rs.gapViolations
if a.rounds > 0: acc[opp] = a
echo "\n=== G4 unified idTicks (M5.5): ", Repeat, " runs x ", Rounds,
" rounds per opponent ==="
echo "Opponent med p90 id% expMed argmaxOk% finalPost% censor% ade5 gapMax/viol"
var lines: seq[string]
lines.add("# G4 unified idTicks (M5.5)\n")
lines.add("Metric: per round, round-relative tick at which `max_kernel posterior > 0.8`; " &
"median / p90 / id-fraction over all rounds. Pass = median <= 300 AND id% >= 80. " &
$Repeat & " runs x " & $Rounds & " rounds. Generated by `tests/bench_g4.nim`.\n")
lines.add("| opponent | median | p90 | id-fraction% | expMedian | argmaxOk% | finalPost% | censor% | ade5 | pass |")
lines.add("|---|---:|---:|---:|---:|---:|---:|---:|---:|---|")
var passed = 0
var comparable = 0
var pooled: seq[int]
for opp in sorted(toSeq(Expected.keys)):
if opp notin acc: continue
let a = acc[opp]
let s = idStats(a.ids)
let se = idStats(a.expIds)
let n = float(a.rounds)
let correctPct = 100.0 * float(a.correct) / n
let pass = s.median > 0 and s.median <= TargetTicks and s.idFraction >= 80.0
inc comparable
if pass: inc passed
pooled.add a.ids
echo " ", alignLeft(opp, 12), s.fmt,
align($(if se.median < 0: "-" else: $se.median), 7),
align(formatFloat(correctPct, ffDecimal, 0), 10),
align(formatFloat(a.finalPost / n * 100.0, ffDecimal, 0), 11),
align(formatFloat(a.censor / n * 100.0, ffDecimal, 1), 8),
align(formatFloat(a.ade / n, ffDecimal, 2), 6), " ",
$a.gapMax & "/" & $a.gapViol
lines.add "| " & opp & " | " & $(if s.median < 0: -1 else: s.median) & " | " &
$(if s.p90 < 0: -1 else: s.p90) & " | " &
formatFloat(s.idFraction, ffDecimal, 1) & " | " &
$(if se.median < 0: -1 else: se.median) & " | " &
formatFloat(correctPct, ffDecimal, 1) & " | " &
formatFloat(a.finalPost / n * 100.0, ffDecimal, 1) & " | " &
formatFloat(a.censor / n * 100.0, ffDecimal, 2) & " | " &
formatFloat(a.ade / n, ffDecimal, 3) & " | " &
(if pass: "PASS" else: "FAIL") & " |"
let ps = idStats(pooled)
echo "\nPooled (all ", pooled.len, " rounds): median ", ps.median,
" p90 ", ps.p90, " id% ", formatFloat(ps.idFraction, ffDecimal, 1)
echo "G4 pass (median<=300 AND id%>=80): ", passed, "/", comparable,
" over ", Repeat, " full 8x5 runs"
lines.add "\nPooled over " & $pooled.len & " rounds: median " & $ps.median &
", p90 " & $ps.p90 & ", id-fraction " &
formatFloat(ps.idFraction, ffDecimal, 1) & "%. G4 pass: " & $passed & "/" &
$comparable & "."
let art = currentSourcePath().parentDir.parentDir / "benchmarks" / "m5_5_g4.md"
createDir(art.parentDir)
writeFile(art, lines.join("\n") & "\n")
echo "Artifact: ", art
echo "Telemetry archived at: ", Archive
when isMainModule:
run()