199 lines
8.3 KiB
Nim
199 lines
8.3 KiB
Nim
## M6.5 HORIZON SWEEP — the decisive G5' experiment (spec §8, §12).
|
|
##
|
|
## Extends the bench_g5p protocol from a single horizon (5) to
|
|
## {1, 5, 10, 25, 50} on the MOVING subset {Crazy, SpinBot, VelociBot, Walls},
|
|
## over every recorded round of the archived probe-ON telemetry (>=2 full 8x5
|
|
## runs). For each horizon it reports the predicted-position ADE of three
|
|
## predictors, averaged over ALL scanned origins of the subset:
|
|
##
|
|
## * twin — the posterior-weighted kernel mixture (`twinsim.rollout`),
|
|
## * persistence— constant-velocity carry (`twinsim.persistence`),
|
|
## * residual — the mixture + the online linear residual head
|
|
## (`fusion.residualTrajectory`), trained causally one tick
|
|
## ahead as it is in the live observer.
|
|
##
|
|
## Verdict rule (M6.5):
|
|
## * idea-validated-conditionally if twin/persistence < 1.0 on >= 3/4 moving
|
|
## opponents at >= 2 horizons >= 10;
|
|
## * idea-falsified-on-suite if twin/persistence >= 1.0 at ALL horizons >= 10.
|
|
##
|
|
## OFFLINE, no Java. Run:
|
|
## `nim c -r --hints:off -d:release tests/bench_horizonsweep.nim [telemetryDir]`
|
|
|
|
import std/[os, strutils, tables, math, algorithm, sequtils]
|
|
import bench_common
|
|
import chimera/dsl
|
|
import chimera/physics
|
|
import chimera/twinsim
|
|
import chimera/residual
|
|
import chimera/fusion
|
|
|
|
const
|
|
Horizons = [1, 5, 10, 25, 50]
|
|
Hmax = 50
|
|
DefaultDir = getTempDir() / "chimera_g6_on"
|
|
|
|
type
|
|
Cell = object
|
|
n: int
|
|
twinAde, persAde, resAde: float64
|
|
twinWins: int ## twin ADE < persistence ADE on this origin
|
|
Row = array[Horizons.len, Cell]
|
|
Agg = Table[string, Row]
|
|
|
|
proc record(path: string, kernels: seq[Kernel], agg: var Agg) =
|
|
let rec = loadRecord(path)
|
|
let rp = replay(rec, kernels)
|
|
if rp.len < Hmax + 2: return
|
|
var head = initResidualHead()
|
|
var a = agg.getOrDefault(rec.opponent)
|
|
for i in 0 ..< rp.len - Hmax - 1:
|
|
let origin = rp[i]
|
|
var actual: seq[tuple[x, y: float64]]
|
|
var realized: seq[tuple[x, y: float64]]
|
|
for h in 1 .. Hmax:
|
|
actual.add (rp[i + h].ox, rp[i + h].oy)
|
|
realized.add (rp[i + h].ex, rp[i + h].ey)
|
|
let mix = rollout(kernels, origin, Hmax, actualOwn = actual)
|
|
let pers = persistence(origin, Hmax)
|
|
let feat = features(ResidualInput(
|
|
lastTurn: origin.lastTurn, lastSpeed: origin.lastSpeed,
|
|
scanAge: 1.0,
|
|
dist: hypot(origin.ex - origin.ox, origin.ey - origin.oy),
|
|
ownTurn: 0.0, ownSpeed: origin.ospeed))
|
|
let rpHead = head.predict(feat)
|
|
let res = residualTrajectory(origin, mix.points, rpHead, Hmax)
|
|
for hi, k in Horizons:
|
|
var tAde, pAde, rAde = 0.0
|
|
for h in 0 ..< k:
|
|
tAde += hypot(mix.points[h].x - realized[h].x, mix.points[h].y - realized[h].y)
|
|
pAde += hypot(pers[h].x - realized[h].x, pers[h].y - realized[h].y)
|
|
rAde += hypot(res[h].x - realized[h].x, res[h].y - realized[h].y)
|
|
let fk = float(k)
|
|
a[hi].n += 1
|
|
a[hi].twinAde += tAde / fk
|
|
a[hi].persAde += pAde / fk
|
|
a[hi].resAde += rAde / fk
|
|
if tAde < pAde: a[hi].twinWins += 1
|
|
# Causal residual training: target = realized - capped(mixture), one tick
|
|
# ahead — the same target the live observer uses (M6.5).
|
|
let nxt = rp[i + 1]
|
|
let mixTurnReal = limitTurnRate(mix.points[0].turnRate, nxt.espeed)
|
|
head.observe(feat, ResidualTarget(turn: nxt.lastTurn - mixTurnReal,
|
|
speed: nxt.lastSpeed - mix.points[0].targetSpeed,
|
|
fire: 0.0))
|
|
agg[rec.opponent] = a
|
|
|
|
proc fmt(x: float64, d = 3): string = formatFloat(x, ffDecimal, d)
|
|
|
|
proc main() =
|
|
let arg = if paramCount() >= 1: paramStr(1) else: getEnv("CHIMERA_HS_DIR", "")
|
|
let dir = if arg.len > 0 and dirExists(arg): arg else: DefaultDir
|
|
if not dirExists(dir):
|
|
echo "No telemetry at ", dir, " (run bench_g6.nim first)."
|
|
quit(0)
|
|
let kernels = allKernels()
|
|
var agg = initTable[string, Row]()
|
|
var files: seq[string]
|
|
for f in walkDirRec(dir):
|
|
if f.endsWith(".jsonl"): files.add f
|
|
files.sort()
|
|
var rounds = initCountTable[string]()
|
|
for f in files:
|
|
let r = loadRecord(f)
|
|
rounds.inc(r.opponent)
|
|
record(f, kernels, agg)
|
|
|
|
let art = currentSourcePath().parentDir.parentDir / "benchmarks" / "m6_5_horizonsweep.md"
|
|
var lines: seq[string]
|
|
var src = "# M6.5 horizon sweep - twin vs persistence vs residual\n"
|
|
src.add("Source: `" & dir & "` (" & $files.len & " round files; " & $MovingOpponents.len & " MOVING opponents).")
|
|
src.add(" Horizons " & $Horizons & ", ADE averaged over all scanned origins with >= " & $Hmax & " future ticks.")
|
|
src.add(" Predictors: **twin** (kernel mixture), **persistence** (constant velocity), **residual** (mixture + online linear residual head).\n")
|
|
lines.add(src)
|
|
lines.add("## Per-opponent curves (ADE units, ratio = twin/persistence)\n")
|
|
lines.add("| opponent | horizon | origins | twin ADE | pers ADE | resid ADE | twin/pers | twinWin% |")
|
|
lines.add("|---|---:|---:|---:|---:|---:|---:|---:|")
|
|
echo "opponent horizon origins twinADE persADE resADE twin/pers twinWin%"
|
|
var allNames = toSeq(agg.keys); allNames.sort()
|
|
var verdictCount = initTable[string, int]() # opponent -> horizons>=10 with ratio<1
|
|
var verdictTotal = 0
|
|
for name in allNames:
|
|
let a = agg[name]
|
|
for hi, k in Horizons:
|
|
let c = a[hi]
|
|
if c.n == 0: continue
|
|
let fn = float(c.n)
|
|
let t = c.twinAde / fn
|
|
let p = c.persAde / fn
|
|
let r = c.resAde / fn
|
|
let ratio = t / p
|
|
let win = 100.0 * float(c.twinWins) / fn
|
|
if name in MovingOpponents and k >= 10:
|
|
inc verdictTotal
|
|
if ratio < 1.0:
|
|
verdictCount[name] = verdictCount.getOrDefault(name) + 1
|
|
echo " ", alignLeft(name, 11), align($k, 5), align($c.n, 9),
|
|
" ", align(fmt(t), 7), " ", align(fmt(p), 7), " ", align(fmt(r), 7),
|
|
" ", align(fmt(ratio), 9), " ", align(fmt(win, 1), 8)
|
|
lines.add("| " & name & " | " & $k & " | " & $c.n & " | " & fmt(t) & " | " &
|
|
fmt(p) & " | " & fmt(r) & " | " & fmt(ratio) & " | " & fmt(win, 1) & " |")
|
|
|
|
# Subset roll-up per horizon.
|
|
lines.add("\n## MOVING-subset roll-up\n")
|
|
lines.add("| horizon | twin ADE | pers ADE | resid ADE | twin/pers |")
|
|
lines.add("|---:|---:|---:|---:|---:|")
|
|
echo "\nMOVING subset roll-up:"
|
|
var suiteRatio: array[Horizons.len, float64]
|
|
for hi, k in Horizons:
|
|
var n = 0; var t = 0.0; var p = 0.0; var r = 0.0
|
|
for name in MovingOpponents:
|
|
if name notin agg: continue
|
|
let c = agg[name][hi]
|
|
n += c.n; t += c.twinAde; p += c.persAde; r += c.resAde
|
|
if n == 0: continue
|
|
let fn = float(n)
|
|
let ratio = (t / fn) / (p / fn)
|
|
suiteRatio[hi] = ratio
|
|
echo " h=", align($k, 3), " twin ", align(fmt(t/fn), 7),
|
|
" pers ", align(fmt(p/fn), 7), " res ", align(fmt(r/fn), 7),
|
|
" ratio ", fmt(ratio, 3)
|
|
lines.add("| " & $k & " | " & fmt(t/fn) & " | " & fmt(p/fn) & " | " &
|
|
fmt(r/fn) & " | " & fmt(ratio) & " |")
|
|
|
|
var validatedOpps = 0
|
|
for name in MovingOpponents:
|
|
if verdictCount.getOrDefault(name) >= 2: inc validatedOpps
|
|
let validated = validatedOpps >= 3
|
|
# Falsified-on-suite: the MOVING-subset roll-up twin/persistence ratio is
|
|
# >= 1.0 at EVERY horizon >= 10 (the twin never wins the suite long-run).
|
|
var allLongRatioGe1 = true
|
|
for hi, k in Horizons:
|
|
if k < 10: continue
|
|
if suiteRatio[hi] < 1.0: allLongRatioGe1 = false
|
|
let falsified = (not validated) and allLongRatioGe1
|
|
var verdict: string
|
|
if validated: verdict = "idea-validated-conditionally"
|
|
elif falsified: verdict = "idea-falsified-on-suite"
|
|
else: verdict = "inconclusive"
|
|
lines.add("\n## Verdict\n")
|
|
var counts: seq[string]
|
|
for name in MovingOpponents:
|
|
counts.add name & ":" & $verdictCount.getOrDefault(name)
|
|
lines.add("- Horizons >= 10 with twin/persistence < 1.0 per moving opponent: " &
|
|
counts.join(", ") & ".")
|
|
lines.add("- MOVING-subset roll-up twin/persistence: " &
|
|
"h10 " & fmt(suiteRatio[2]) & ", h25 " & fmt(suiteRatio[3]) &
|
|
", h50 " & fmt(suiteRatio[4]) & ".")
|
|
lines.add("- Rule: **validated-conditionally** if twin/persistence < 1.0 on >= 3/4 " &
|
|
"moving opponents at >= 2 horizons >= 10; **falsified-on-suite** if the subset " &
|
|
"roll-up ratio is >= 1.0 at ALL horizons >= 10.")
|
|
lines.add("\n**Verdict: " & verdict & ".**")
|
|
createDir(art.parentDir)
|
|
writeFile(art, lines.join("\n") & "\n")
|
|
echo "\nVerdict: ", verdict
|
|
echo "Artifact: ", art
|
|
|
|
when isMainModule:
|
|
main()
|