147 lines
6.1 KiB
Nim
147 lines
6.1 KiB
Nim
## G5 counterfactual evaluation (CHIMERA M4) — OFFLINE, no Java.
|
|
##
|
|
## Reads the raw per-tick telemetry the G4 bench left under /tmp, replays the
|
|
## ledger + Hedge to recover the causal per-tick posterior, and for every origin
|
|
## tick computes:
|
|
## (a) the TWIN 5-tick rollout under OUR ACTUAL recorded movement, vs the
|
|
## realized enemy positions -> position ADE / FDE;
|
|
## (b) the PERSISTENCE baseline (enemy repeats its last observed intent);
|
|
## (c) per-record and per-opponent aggregates, reported verbatim;
|
|
## (d) fire-channel Brier: twin P(fire within 5 ticks) vs the observed
|
|
## energy-drop fire events; persistence baseline = empirical fire rate.
|
|
##
|
|
## G5 bar (spec §12): twin ADE <= persistence ADE on >= 90% of scenarios.
|
|
## Losing scenarios are listed, not hidden (honesty clause).
|
|
##
|
|
## Run: `nim c -r --hints:off -d:release tests/bench_g5.nim [telemetryDir]`
|
|
|
|
import std/[os, strutils, tables, math, algorithm, sequtils]
|
|
import chimera/dsl
|
|
import chimera/twinsim
|
|
|
|
const Horizon = 5
|
|
const DefaultDir = getTempDir() / "chimera_g4_run"
|
|
const FlatDir = getTempDir() / "chimera_twin"
|
|
|
|
type
|
|
Agg = object
|
|
scenarios: int
|
|
twinAde, persAde, twinFde, persFde: float64
|
|
twinBrier, persBrier: float64
|
|
twinWins: int
|
|
confScen: int ## origins with max posterior >= 0.8
|
|
confTwinAde, confPersAde: float64
|
|
confWins: int
|
|
|
|
proc pickDir(arg: string): string =
|
|
if arg.len > 0 and dirExists(arg): return arg
|
|
if dirExists(DefaultDir): return DefaultDir
|
|
if dirExists(FlatDir): return FlatDir
|
|
""
|
|
|
|
proc main() =
|
|
let arg = if paramCount() >= 1: paramStr(1) else: getEnv("CHIMERA_G5_DIR", "")
|
|
let dir = pickDir(arg)
|
|
if dir.len == 0:
|
|
echo "No telemetry dir found (set CHIMERA_G5_DIR or pass a path)."
|
|
quit(0)
|
|
|
|
let kernels = allKernels()
|
|
let files = loadDir(dir).sortedByIt(it.opponent & "-" & $it.round)
|
|
if files.len == 0:
|
|
echo "No .jsonl records under ", dir
|
|
quit(0)
|
|
|
|
var agg = initTable[string, Agg]()
|
|
var totalScen, totalWins = 0
|
|
var losers: seq[string]
|
|
|
|
for rec in files:
|
|
let rp = replay(rec, kernels)
|
|
if rp.len < Horizon + 1: continue
|
|
let rate = rec.fireRate()
|
|
let persFire = 1.0 - pow(1.0 - rate, float(Horizon))
|
|
var a = agg.getOrDefault(rec.opponent)
|
|
for i in 0 ..< rp.len - Horizon:
|
|
let origin = rp[i]
|
|
var actual: seq[tuple[x, y: float64]]
|
|
var realized: seq[tuple[x, y: float64]]
|
|
var obsFire = false
|
|
for h in 1 .. Horizon:
|
|
actual.add (rp[i + h].ox, rp[i + h].oy)
|
|
realized.add (rp[i + h].ex, rp[i + h].ey)
|
|
# observed fire events live in the raw record, aligned by tick
|
|
for h in 1 .. Horizon:
|
|
for t in rec.ticks:
|
|
if t.tick == rp[i + h].tick and t.fireKnown and t.fireObs > 0.0:
|
|
obsFire = true
|
|
let twin = rollout(kernels, origin, Horizon, actualOwn = actual)
|
|
let pers = persistence(origin, Horizon)
|
|
var tAde, pAde = 0.0
|
|
for h in 0 ..< Horizon:
|
|
tAde += hypot(twin.points[h].x - realized[h].x,
|
|
twin.points[h].y - realized[h].y)
|
|
pAde += hypot(pers[h].x - realized[h].x, pers[h].y - realized[h].y)
|
|
tAde /= float(Horizon); pAde /= float(Horizon)
|
|
let tFde = hypot(twin.points[^1].x - realized[^1].x,
|
|
twin.points[^1].y - realized[^1].y)
|
|
let pFde = hypot(pers[^1].x - realized[^1].x,
|
|
pers[^1].y - realized[^1].y)
|
|
inc a.scenarios
|
|
a.twinAde += tAde; a.persAde += pAde
|
|
a.twinFde += tFde; a.persFde += pFde
|
|
a.twinBrier += (twin.fireWithin - (if obsFire: 1.0 else: 0.0)) ^ 2
|
|
a.persBrier += (persFire - (if obsFire: 1.0 else: 0.0)) ^ 2
|
|
if tAde <= pAde: inc a.twinWins
|
|
inc totalScen
|
|
if tAde <= pAde: inc totalWins
|
|
else: losers.add rec.opponent & "#" & $rec.round & "@" & $origin.tick
|
|
# confident regime: the posterior has actually concentrated
|
|
var maxW = 0.0
|
|
for w in origin.weights: maxW = max(maxW, w)
|
|
if maxW >= 0.8:
|
|
inc a.confScen
|
|
a.confTwinAde += tAde; a.confPersAde += pAde
|
|
if tAde <= pAde: inc a.confWins
|
|
agg[rec.opponent] = a
|
|
|
|
echo "G5 twin-vs-persistence (", dir, "), horizon=", Horizon,
|
|
", scenarios=", totalScen
|
|
echo "\nOpponent scen twinADE persADE margin twinFDE persFDE twinBr persBr twinWin%"
|
|
var names = toSeq(agg.keys); names.sort()
|
|
for name in names:
|
|
let a = agg[name]
|
|
if a.scenarios == 0: continue
|
|
let n = float(a.scenarios)
|
|
echo " ", alignLeft(name, 12), align($a.scenarios, 5),
|
|
" ", align(formatFloat(a.twinAde / n, ffDecimal, 3), 7),
|
|
" ", align(formatFloat(a.persAde / n, ffDecimal, 3), 7),
|
|
" ", align(formatFloat((a.persAde - a.twinAde) / n, ffDecimal, 3), 7),
|
|
" ", align(formatFloat(a.twinFde / n, ffDecimal, 3), 8),
|
|
" ", align(formatFloat(a.persFde / n, ffDecimal, 3), 7),
|
|
" ", align(formatFloat(a.twinBrier / n, ffDecimal, 4), 7),
|
|
" ", align(formatFloat(a.persBrier / n, ffDecimal, 4), 7),
|
|
" ", align(formatFloat(100.0 * float(a.twinWins) / n, ffDecimal, 1), 8)
|
|
echo "\nConfidence-conditioned (max posterior >= 0.8):"
|
|
echo "Opponent confScen twinADE persADE margin twinWin%"
|
|
for name in names:
|
|
let a = agg[name]
|
|
if a.confScen == 0: continue
|
|
let n = float(a.confScen)
|
|
echo " ", alignLeft(name, 12), align($a.confScen, 6),
|
|
" ", align(formatFloat(a.confTwinAde / n, ffDecimal, 3), 7),
|
|
" ", align(formatFloat(a.confPersAde / n, ffDecimal, 3), 7),
|
|
" ", align(formatFloat((a.confPersAde - a.confTwinAde) / n, ffDecimal, 3), 7),
|
|
" ", align(formatFloat(100.0 * float(a.confWins) / n, ffDecimal, 1), 8)
|
|
echo "\nG5 bar: twin ADE <= persistence ADE on ", totalWins, "/", totalScen,
|
|
" = ", formatFloat(100.0 * float(totalWins) / float(max(1, totalScen)),
|
|
ffDecimal, 1), "%"
|
|
echo "G5 (>= 90%): ", (if totalWins * 10 >= totalScen * 9: "PASS" else: "FAIL")
|
|
if losers.len > 0:
|
|
echo "Losing scenarios (", losers.len, "):"
|
|
for i in 0 ..< min(20, losers.len): echo " ", losers[i]
|
|
if losers.len > 20: echo " ... ", losers.len - 20, " more"
|
|
|
|
when isMainModule:
|
|
main()
|