ChimeraBot_garage: M6.5 — twinsim state-reconstruction fix + horizon sweep + final artifacts
This commit is contained in:
@@ -0,0 +1,65 @@
|
||||
# M6.5 horizon sweep - twin vs persistence vs residual
|
||||
Source: `/tmp/chimera_g6_on` (120 round files; 4 MOVING opponents). Horizons [1, 5, 10, 25, 50], ADE averaged over all scanned origins with >= 50 future ticks. Predictors: **twin** (kernel mixture), **persistence** (constant velocity), **residual** (mixture + online linear residual head).
|
||||
|
||||
## Per-opponent curves (ADE units, ratio = twin/persistence)
|
||||
|
||||
| opponent | horizon | origins | twin ADE | pers ADE | resid ADE | twin/pers | twinWin% |
|
||||
|---|---:|---:|---:|---:|---:|---:|---:|
|
||||
| Corners | 1 | 3224 | 0.757 | 0.055 | 0.570 | 13.815 | 2.0 |
|
||||
| Corners | 5 | 3224 | 3.198 | 0.617 | 2.633 | 5.183 | 5.1 |
|
||||
| Corners | 10 | 3224 | 6.949 | 2.296 | 6.242 | 3.027 | 8.8 |
|
||||
| Corners | 25 | 3224 | 20.774 | 12.742 | 21.873 | 1.630 | 18.5 |
|
||||
| Corners | 50 | 3224 | 48.492 | 40.554 | 55.928 | 1.196 | 25.1 |
|
||||
| Crazy | 1 | 13786 | 2.613 | 0.623 | 0.997 | 4.192 | 14.1 |
|
||||
| Crazy | 5 | 13786 | 9.664 | 2.863 | 4.699 | 3.375 | 14.5 |
|
||||
| Crazy | 10 | 13786 | 20.676 | 8.234 | 12.307 | 2.511 | 19.6 |
|
||||
| Crazy | 25 | 13786 | 59.287 | 41.281 | 49.445 | 1.436 | 37.6 |
|
||||
| Crazy | 50 | 13786 | 118.345 | 120.728 | 125.943 | 0.980 | 55.8 |
|
||||
| Fire | 1 | 4501 | 1.032 | 0.127 | 0.670 | 8.150 | 4.6 |
|
||||
| Fire | 5 | 4501 | 3.233 | 1.302 | 2.923 | 2.484 | 14.1 |
|
||||
| Fire | 10 | 4501 | 6.101 | 4.514 | 6.610 | 1.351 | 21.5 |
|
||||
| Fire | 25 | 4501 | 14.642 | 19.974 | 20.571 | 0.733 | 36.6 |
|
||||
| Fire | 50 | 4501 | 24.414 | 47.173 | 44.086 | 0.518 | 48.8 |
|
||||
| RamFire | 1 | 2959 | 1.341 | 0.117 | 0.834 | 11.456 | 4.3 |
|
||||
| RamFire | 5 | 2959 | 5.116 | 1.128 | 3.921 | 4.536 | 11.3 |
|
||||
| RamFire | 10 | 2959 | 11.166 | 3.954 | 9.431 | 2.824 | 18.0 |
|
||||
| RamFire | 25 | 2959 | 32.759 | 20.484 | 33.082 | 1.599 | 32.6 |
|
||||
| RamFire | 50 | 2959 | 70.715 | 63.782 | 82.805 | 1.109 | 47.0 |
|
||||
| SpinBot | 1 | 9812 | 0.545 | 0.557 | 0.579 | 0.979 | 95.7 |
|
||||
| SpinBot | 5 | 9812 | 1.684 | 1.820 | 1.862 | 0.925 | 95.7 |
|
||||
| SpinBot | 10 | 9812 | 3.046 | 3.564 | 3.542 | 0.855 | 96.1 |
|
||||
| SpinBot | 25 | 9812 | 6.477 | 8.401 | 8.374 | 0.771 | 94.8 |
|
||||
| SpinBot | 50 | 9812 | 11.386 | 12.609 | 13.611 | 0.903 | 84.9 |
|
||||
| TrackFire | 1 | 2337 | 0.045 | 0.000 | 0.140 | inf | 0.0 |
|
||||
| TrackFire | 5 | 2337 | 0.285 | 0.000 | 0.633 | inf | 0.0 |
|
||||
| TrackFire | 10 | 2337 | 0.728 | 0.000 | 1.141 | inf | 0.0 |
|
||||
| TrackFire | 25 | 2337 | 1.819 | 0.000 | 2.762 | inf | 0.0 |
|
||||
| TrackFire | 50 | 2337 | 2.857 | 0.000 | 5.601 | inf | 0.0 |
|
||||
| VelociBot | 1 | 6768 | 3.135 | 0.332 | 0.835 | 9.454 | 7.9 |
|
||||
| VelociBot | 5 | 6768 | 9.880 | 1.823 | 3.577 | 5.420 | 9.1 |
|
||||
| VelociBot | 10 | 6768 | 18.698 | 5.664 | 8.785 | 3.301 | 15.9 |
|
||||
| VelociBot | 25 | 6768 | 42.583 | 30.214 | 32.866 | 1.409 | 36.5 |
|
||||
| VelociBot | 50 | 6768 | 70.855 | 91.558 | 82.617 | 0.774 | 63.1 |
|
||||
| Walls | 1 | 8606 | 1.553 | 0.098 | 0.428 | 15.911 | 2.3 |
|
||||
| Walls | 5 | 8606 | 5.731 | 0.885 | 2.203 | 6.474 | 6.6 |
|
||||
| Walls | 10 | 8606 | 12.533 | 3.197 | 6.203 | 3.921 | 11.5 |
|
||||
| Walls | 25 | 8606 | 39.727 | 18.058 | 28.549 | 2.200 | 25.3 |
|
||||
| Walls | 50 | 8606 | 93.296 | 62.078 | 91.258 | 1.503 | 37.9 |
|
||||
|
||||
## MOVING-subset roll-up
|
||||
|
||||
| horizon | twin ADE | pers ADE | resid ADE | twin/pers |
|
||||
|---:|---:|---:|---:|---:|
|
||||
| 1 | 1.949 | 0.440 | 0.738 | 4.431 |
|
||||
| 5 | 6.824 | 1.983 | 3.239 | 3.441 |
|
||||
| 10 | 14.096 | 5.500 | 8.141 | 2.563 |
|
||||
| 25 | 38.771 | 25.953 | 31.611 | 1.494 |
|
||||
| 50 | 77.637 | 75.490 | 82.478 | 1.028 |
|
||||
|
||||
## Verdict
|
||||
|
||||
- Horizons >= 10 with twin/persistence < 1.0 per moving opponent: Crazy:1, SpinBot:3, VelociBot:1, Walls:0.
|
||||
- MOVING-subset roll-up twin/persistence: h10 2.563, h25 1.494, h50 1.028.
|
||||
- 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.
|
||||
|
||||
**Verdict: idea-falsified-on-suite.**
|
||||
@@ -260,6 +260,7 @@ task test, "Run unit tests":
|
||||
exec "nim c -r --hints:off ../tests/test_oracle_g1.nim"
|
||||
exec "nim c -r --hints:off ../tests/test_observer.nim"
|
||||
exec "nim c -r --hints:off ../tests/test_twinsim.nim"
|
||||
exec "nim c -r --hints:off ../tests/test_replay_oracle.nim"
|
||||
exec "nim c -r --hints:off ../tests/test_probes.nim"
|
||||
exec "nim c -r --hints:off ../tests/test_residual.nim"
|
||||
exec "nim c -r --hints:off ../tests/test_fusion.nim"
|
||||
|
||||
@@ -107,7 +107,9 @@ proc residualTrajectory*(origin: ReplayTick, mix: seq[RolloutPoint],
|
||||
let baseSpeed = (if h < mix.len: mix[h].targetSpeed else: origin.lastSpeed)
|
||||
let ts = clamp(baseSpeed + rp.speed, MaxBackwardSpeed, MaxForwardSpeed)
|
||||
sp = stepSpeed(sp, ts)
|
||||
dir = wrap180(dir + limitTurnRate(baseTurn + rp.turn, sp))
|
||||
# The residual head is trained on `realized - capped(mixture command)` (M6.5),
|
||||
# so cap the base before adding the correction: training/inference symmetry.
|
||||
dir = wrap180(dir + limitTurnRate(baseTurn, sp) + rp.turn)
|
||||
x += sp * cos(degToRad(dir))
|
||||
y += sp * sin(degToRad(dir))
|
||||
result.add (x, y)
|
||||
|
||||
@@ -265,23 +265,34 @@ proc observeScan*(o: Observer, tick: int, x, y, direction, speed, energy: float6
|
||||
energy: o.prev.energy)
|
||||
let my = MyState(x: o.prevOwnX, y: o.prevOwnY)
|
||||
var losses = newSeq[float64](o.kernels.len)
|
||||
var mixTurn, mixSpeed, mixFire = 0.0
|
||||
var mixTurn, mixSpeed, mixFireP = 0.0
|
||||
for i in 0 ..< o.kernels.len:
|
||||
let pred = o.kernels[i].step(e, my, o.kernels[i].defaultParams, o.mems[i])
|
||||
losses[i] = intentLoss(pred, obs, s.speed)
|
||||
let w = o.hedge.weights[i]
|
||||
mixTurn += w * pred.turnRate
|
||||
mixSpeed += w * pred.targetSpeed
|
||||
mixFire += w * pred.fire
|
||||
# Fire is a Bernoulli channel: the fusion scores a PROBABILITY, and
|
||||
# `intentLoss` already binarizes the kernel's predicted power. Using the
|
||||
# weighted POWER here made `mixFire` ~= 1 wherever a high-power kernel had
|
||||
# mass, wrecking the mixture's log-loss and letting the residual absorb
|
||||
# the artefact (M6.5).
|
||||
mixFireP += w * (if pred.fire > 0.0: 1.0 else: 0.0)
|
||||
o.hedge.update(losses)
|
||||
when FusionEnabled:
|
||||
# Residual + fusion training (spec §10): learn the mixture's residual on
|
||||
# THIS realized intent, then score the three predictors. Observation
|
||||
# only — nothing here can change a command.
|
||||
# Compare like with like (M6.5). The realized ledger turn is the REALIZED
|
||||
# (server-capped) turn, while `mixTurn` is the mixture's raw turn
|
||||
# COMMAND. Score the mixture's physical turn — capped at the realized
|
||||
# speed, exactly as `intentLoss` caps its prediction — otherwise the
|
||||
# linear residual trivially absorbs the `limitTurnRate` gap and dominates
|
||||
# the fusion (measured 0.94 weight on an ORBIT_BALLIST oracle stream).
|
||||
let mixTurnReal = limitTurnRate(mixTurn, s.speed)
|
||||
let realizedFireP = (if obs.fireKnown: (if obs.fire > 0.0: 1.0 else: 0.0)
|
||||
else: mixFireP)
|
||||
let realized = PredIntent(
|
||||
turn: (if obs.turnKnown: (obs.turnRate.lo + obs.turnRate.hi) / 2.0 else: mixTurn),
|
||||
turn: (if obs.turnKnown: (obs.turnRate.lo + obs.turnRate.hi) / 2.0 else: mixTurnReal),
|
||||
speed: (if obs.targetKnown: (obs.targetSpeed.lo + obs.targetSpeed.hi) / 2.0 else: mixSpeed),
|
||||
fire: (if obs.fireKnown: obs.fire else: mixFire))
|
||||
fire: realizedFireP)
|
||||
let feat = features(ResidualInput(
|
||||
lastTurn: o.lastRealizedTurn, lastSpeed: o.lastRealizedSpeed,
|
||||
scanAge: float64(s.tick - o.prev.tick),
|
||||
@@ -291,13 +302,13 @@ proc observeScan*(o: Observer, tick: int, x, y, direction, speed, energy: float6
|
||||
let persP = PredIntent(turn: o.lastRealizedTurn,
|
||||
speed: o.lastRealizedSpeed, fire: o.fireRateEma)
|
||||
o.fusion.update(realized, [
|
||||
PredIntent(turn: mixTurn, speed: mixSpeed, fire: mixFire),
|
||||
PredIntent(turn: mixTurn + rpred.turn, speed: mixSpeed + rpred.speed,
|
||||
fire: clamp(mixFire + rpred.fire, 0.0, 1.0)),
|
||||
PredIntent(turn: mixTurnReal, speed: mixSpeed, fire: mixFireP),
|
||||
PredIntent(turn: mixTurnReal + rpred.turn, speed: mixSpeed + rpred.speed,
|
||||
fire: clamp(mixFireP + rpred.fire, 0.0, 1.0)),
|
||||
persP])
|
||||
o.residual.observe(feat, ResidualTarget(
|
||||
turn: realized.turn - mixTurn, speed: realized.speed - mixSpeed,
|
||||
fire: realized.fire - mixFire))
|
||||
turn: realized.turn - mixTurnReal, speed: realized.speed - mixSpeed,
|
||||
fire: realizedFireP - mixFireP))
|
||||
o.lastRealizedTurn = realized.turn
|
||||
o.lastRealizedSpeed = realized.speed
|
||||
if obs.fireKnown:
|
||||
|
||||
@@ -0,0 +1,198 @@
|
||||
## 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()
|
||||
@@ -0,0 +1,100 @@
|
||||
## M6.5 regression: twinsim REPLAY state reconstruction must equal the
|
||||
## ORACLE-path state, tick for tick (spec §8 rollout, §12 G1 harness).
|
||||
##
|
||||
## The M6.5 close-out hypothesis was that `twinsim.replay` reconstructs a
|
||||
## different hidden state than the oracle harness from a telemetry-reconstructed
|
||||
## round, "mis-phasing" the kernels and making the mixture rollout lose to
|
||||
## persistence. This test settles it empirically: generate the ORACLE intent
|
||||
## stream for a kernel, serialise it as a telemetry JSONL, run `replay`, and
|
||||
## compare the per-tick posterior + kernel memory against an oracle-path fold
|
||||
## of the SAME stream. Byte-equal means the reconstruction is faithful; a
|
||||
## mismatch would name the first divergent tick.
|
||||
##
|
||||
## Pure Nim, no Java. Run: from ChimeraBot_garage/src, `nimble test`.
|
||||
|
||||
import std/[os, strutils]
|
||||
import chimera/dsl
|
||||
import chimera/oracle
|
||||
import chimera/hedge
|
||||
import chimera/twinsim
|
||||
|
||||
var failed = 0
|
||||
proc check(name: string, cond: bool, detail: string = "") =
|
||||
if cond: echo " ok ", name
|
||||
else: echo " FAIL ", name, " [", detail, "]"; inc failed
|
||||
|
||||
let kernels = allKernels()
|
||||
|
||||
## Oracle-path fold with the per-tick weights exposed (mirror of
|
||||
## `oracle.evaluate`, which only reports the crossing tick).
|
||||
proc oracleWeights(stream: OracleStream, env: Env): seq[seq[float64]] =
|
||||
let my = MyState(x: env.myX, y: env.myY)
|
||||
var h = initHedge(kernelNames(kernels))
|
||||
var mems = newSeq[KernelMem](kernels.len)
|
||||
for i in 0 ..< kernels.len:
|
||||
mems[i] = defaultMemory(kernels[i].defaultParams)
|
||||
var preds = newSeq[Intent](kernels.len)
|
||||
var losses = newSeq[float64](kernels.len)
|
||||
# weights at tick scans[0] (before any observation): the uniform prior.
|
||||
result.add h.weights
|
||||
for t in 0 ..< stream.scans.len - 1:
|
||||
let e = EnemyState(tick: stream.scans[t].tick, x: stream.scans[t].x,
|
||||
y: stream.scans[t].y, direction: stream.scans[t].direction,
|
||||
speed: stream.scans[t].speed, energy: stream.scans[t].energy)
|
||||
for i in 0 ..< kernels.len:
|
||||
preds[i] = kernels[i].step(e, my, kernels[i].defaultParams, mems[i])
|
||||
let obs = combineObs(stream.scans[t], stream.scans[t + 1])
|
||||
if not obs.censored:
|
||||
for i in 0 ..< kernels.len:
|
||||
losses[i] = intentLoss(preds[i], obs, stream.scans[t + 1].speed)
|
||||
h.update(losses)
|
||||
result.add h.weights
|
||||
|
||||
proc writeTelemetry(stream: OracleStream, env: Env, path: string) =
|
||||
var lines: seq[string]
|
||||
for t, s in stream.scans:
|
||||
lines.add("{\"tick\":" & $t & ",\"scan\":true" &
|
||||
",\"ex\":" & $s.x & ",\"ey\":" & $s.y &
|
||||
",\"edir\":" & $s.direction &
|
||||
",\"espeed\":" & $s.speed &
|
||||
",\"eenergy\":" & $s.energy &
|
||||
",\"ox\":" & $env.myX & ",\"oy\":" & $env.myY &
|
||||
",\"odir\":0.0,\"ospeed\":0.0}")
|
||||
writeFile(path, lines.join("\n") & "\n")
|
||||
|
||||
proc weightsEqual(a, b: seq[float64]): bool =
|
||||
if a.len != b.len: return false
|
||||
for i in 0 ..< a.len:
|
||||
if a[i] != b[i]: return false
|
||||
true
|
||||
|
||||
proc main() =
|
||||
let dir = getTempDir() / ("chimera_replay_oracle_" & $getCurrentProcessId())
|
||||
removeDir(dir); createDir(dir)
|
||||
let env = defaultEnv()
|
||||
# One oracle round per kernel: a faithful reconstruction must hold for every
|
||||
# kernel, not just a moving one.
|
||||
for idx in 0 ..< kernels.len:
|
||||
let name = kernels[idx].name
|
||||
let stream = generateOracle(kernels[idx], kernels[idx].defaultParams, env, 250)
|
||||
let path = dir / ("Oracle" & name & "-1.jsonl")
|
||||
writeTelemetry(stream, env, path)
|
||||
let rec = loadRecord(path)
|
||||
let rp = replay(rec, kernels)
|
||||
let ow = oracleWeights(stream, env)
|
||||
if rp.len != ow.len:
|
||||
check(name & ": replay/oracle tick count", false, $rp.len & " vs " & $ow.len)
|
||||
continue
|
||||
var firstDiv = -1
|
||||
for i in 0 ..< rp.len:
|
||||
if not weightsEqual(rp[i].weights, ow[i]):
|
||||
firstDiv = rp[i].tick; break
|
||||
check(name & ": replay weights == oracle weights (byte-equal)",
|
||||
firstDiv < 0,
|
||||
(if firstDiv < 0: "" else: "first divergence at tick " & $firstDiv))
|
||||
removeDir(dir)
|
||||
|
||||
main()
|
||||
echo ""
|
||||
if failed == 0: echo "all replay/oracle tests passed"
|
||||
else: echo failed, " replay/oracle test(s) FAILED"; quit(1)
|
||||
Reference in New Issue
Block a user