ChimeraBot_garage: M5 — probe scheduler, advisory wiring, G6 ablation ON/OFF, G5 interventional eval
This commit is contained in:
@@ -0,0 +1,181 @@
|
||||
## G5' interventional counterfactual eval (CHIMERA M5) — OFFLINE, no Java.
|
||||
##
|
||||
## Source: docs/chimera-spec.md §12 G5'. Reads the probe-ON telemetry that
|
||||
## bench_g6.nim archived under /tmp/chimera_g6_on, replays the ledger + Hedge to
|
||||
## recover the causal posterior, and for every tick that FOLLOWS an executed
|
||||
## probe measures:
|
||||
## * 5-tick enemy-reaction ADE of the twin mixture vs the persistence baseline
|
||||
## (constant-velocity carry), under OUR ACTUAL recorded movement;
|
||||
## * reaction-direction agreement: the angle between the predicted and the
|
||||
## observed net enemy displacement over the window (<= 45 deg counts).
|
||||
##
|
||||
## Gates reported:
|
||||
## * G5' bar: twin ADE <= persistence ADE on >= 70% of probe-following windows;
|
||||
## * stretch row: >= 90% (reported, never the gate);
|
||||
## * observational sanity floor: twin ADE <= 2x persistence ADE per opponent.
|
||||
##
|
||||
## Run: `nim c -r --hints:off -d:release tests/bench_g5p.nim [telemetryDir]`
|
||||
|
||||
import std/[os, strutils, tables, math, algorithm, sequtils]
|
||||
import chimera/dsl
|
||||
import chimera/physics
|
||||
import chimera/twinsim
|
||||
|
||||
const Horizon = 5
|
||||
const DefaultDir = getTempDir() / "chimera_g6_on"
|
||||
const DirAgreeDeg = 45.0
|
||||
|
||||
type
|
||||
Agg = object
|
||||
windows: int
|
||||
twinAde, persAde: float64
|
||||
twinWins: int
|
||||
dirTwinAgree, dirPersAgree, dirN: int
|
||||
allWindows: int
|
||||
allTwinAde, allPersAde: float64
|
||||
allTwinWins: int
|
||||
|
||||
proc probeTicks(path: string): seq[int] =
|
||||
## Only EXECUTED probes (`gate:"ok"`) count as interventional windows: a
|
||||
## gate-blocked trigger issued no probe, so the following ticks are
|
||||
## observation-only and would silently re-import the M4 persistence-bias.
|
||||
for ln in lines(path):
|
||||
if ln.find("\"event\":\"probe\"") < 0: continue
|
||||
if ln.find("\"gate\":\"ok\"") < 0: continue
|
||||
let needle = "\"tick\":"
|
||||
let i = ln.find(needle)
|
||||
if i < 0: continue
|
||||
var j = i + needle.len
|
||||
while j < ln.len and ln[j] in {' ', '\t'}: inc j
|
||||
let s = j
|
||||
while j < ln.len and ln[j] in Digits: inc j
|
||||
if j > s: result.add parseInt(ln[s ..< j])
|
||||
|
||||
proc netDir(sx, sy, ex, ey: float64): float64 =
|
||||
## Bearing of the displacement (sx,sy)->(ex,ey).
|
||||
arctan2(ey - sy, ex - sx) * 180.0 / PI
|
||||
|
||||
proc dirAgree(sx, sy, obsX, obsY, predX, predY: float64): bool =
|
||||
let obsMag = hypot(obsX - sx, obsY - sy)
|
||||
let predMag = hypot(predX - sx, predY - sy)
|
||||
if obsMag <= 1.0: return predMag <= 5.0
|
||||
abs(wrap180(netDir(sx, sy, predX, predY) - netDir(sx, sy, obsX, obsY))) <= DirAgreeDeg
|
||||
|
||||
proc record(path: string, kernels: seq[Kernel], agg: var Table[string, Agg]) =
|
||||
let rec = loadRecord(path)
|
||||
let probes = probeTicks(path)
|
||||
if rec.ticks.len < Horizon + 1: return
|
||||
let rp = replay(rec, kernels)
|
||||
if rp.len < Horizon + 1: return
|
||||
var a = agg.getOrDefault(rec.opponent)
|
||||
# observational floor: every origin window in the same record
|
||||
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]]
|
||||
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)
|
||||
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)
|
||||
inc a.allWindows
|
||||
a.allTwinAde += tAde / float(Horizon)
|
||||
a.allPersAde += pAde / float(Horizon)
|
||||
if tAde <= pAde: inc a.allTwinWins
|
||||
# interventional windows: probe ticks only
|
||||
var byTick = initTable[int, int]()
|
||||
for i, r in rp: byTick[r.tick] = i
|
||||
for p in probes:
|
||||
if not byTick.hasKey(p): continue
|
||||
let i = byTick[p]
|
||||
if i + Horizon >= rp.len: continue
|
||||
let origin = rp[i]
|
||||
var actual: seq[tuple[x, y: float64]]
|
||||
var realized: seq[tuple[x, y: float64]]
|
||||
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)
|
||||
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)
|
||||
inc a.windows
|
||||
a.twinAde += tAde; a.persAde += pAde
|
||||
if tAde <= pAde: inc a.twinWins
|
||||
inc a.dirN
|
||||
if dirAgree(origin.ex, origin.ey, realized[^1].x, realized[^1].y,
|
||||
twin.points[^1].x, twin.points[^1].y): inc a.dirTwinAgree
|
||||
if dirAgree(origin.ex, origin.ey, realized[^1].x, realized[^1].y,
|
||||
pers[^1].x, pers[^1].y): inc a.dirPersAgree
|
||||
agg[rec.opponent] = a
|
||||
|
||||
proc main() =
|
||||
let arg = if paramCount() >= 1: paramStr(1) else: getEnv("CHIMERA_G5P_DIR", "")
|
||||
let dir = if arg.len > 0 and dirExists(arg): arg else: DefaultDir
|
||||
if not dirExists(dir):
|
||||
echo "No probe-ON telemetry at ", dir, " (run bench_g6.nim first)."
|
||||
quit(0)
|
||||
let kernels = allKernels()
|
||||
var agg = initTable[string, Agg]()
|
||||
var files: seq[string]
|
||||
for f in walkDirRec(dir):
|
||||
if f.endsWith(".jsonl"): files.add f
|
||||
files.sort()
|
||||
for f in files: record(f, kernels, agg)
|
||||
|
||||
var totalWin, total = 0
|
||||
var totT, totP = 0.0
|
||||
var dirT, dirP, dirN = 0
|
||||
var floorBad = 0
|
||||
var names = toSeq(agg.keys); names.sort()
|
||||
echo "G5' interventional (probe-following windows), horizon=", Horizon,
|
||||
", dir=", dir
|
||||
echo "\nOpponent probes twinADE persADE margin twinWin% dirTwin% dirPers% obsFloor"
|
||||
for name in names:
|
||||
let a = agg[name]
|
||||
if a.windows == 0: continue
|
||||
let n = float(a.windows)
|
||||
let win = 100.0 * float(a.twinWins) / n
|
||||
totalWin += a.twinWins
|
||||
total += a.windows
|
||||
totT += a.twinAde; totP += a.persAde
|
||||
dirT += a.dirTwinAgree; dirP += a.dirPersAgree; dirN += a.dirN
|
||||
let floorOk = a.allPersAde > 0.0 and a.allTwinAde <= 2.0 * a.allPersAde
|
||||
if not floorOk: inc floorBad
|
||||
echo " ", alignLeft(name, 12), align($a.windows, 6),
|
||||
" ", 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(win, ffDecimal, 1), 8),
|
||||
" ", align(formatFloat(100.0 * float(a.dirTwinAgree) /
|
||||
float(max(1, a.dirN)), ffDecimal, 1), 8),
|
||||
" ", align(formatFloat(100.0 * float(a.dirPersAgree) /
|
||||
float(max(1, a.dirN)), ffDecimal, 1), 8),
|
||||
" ", (if floorOk: "ok" else: "FAIL")
|
||||
echo "\nInterventional twin-ADE <= persistence-ADE: ", totalWin, "/", total,
|
||||
" = ", formatFloat(100.0 * float(totalWin) / float(max(1, total)),
|
||||
ffDecimal, 1), "%"
|
||||
echo "G5' bar (>= 70%): ",
|
||||
(if totalWin * 10 >= total * 7: "PASS" else: "FAIL")
|
||||
echo "Stretch row (>= 90%): ",
|
||||
formatFloat(100.0 * float(totalWin) / float(max(1, total)),
|
||||
ffDecimal, 1), "% ",
|
||||
(if totalWin * 10 >= total * 9: "(met)" else: "(not met)")
|
||||
echo "Reaction-direction agreement: twin ",
|
||||
formatFloat(100.0 * float(dirT) / float(max(1, dirN)), ffDecimal, 1),
|
||||
"%, persistence ",
|
||||
formatFloat(100.0 * float(dirP) / float(max(1, dirN)), ffDecimal, 1), "%"
|
||||
echo "Observational sanity floor (twinADE <= 2x persADE per opponent): ",
|
||||
(if floorBad == 0: "PASS (all opponents)" else: "FAIL (" & $floorBad & ")")
|
||||
|
||||
when isMainModule:
|
||||
main()
|
||||
Reference in New Issue
Block a user