ChimeraBot_garage: M6 — residual head, predictor fusion, fallback influence, G7 head-to-head
This commit is contained in:
@@ -0,0 +1,94 @@
|
||||
## Tier A unit tests for CHIMERA predictor fusion (chimera/fusion.nim).
|
||||
## Run: from ChimeraBot_garage/src, `nimble test`.
|
||||
|
||||
import std/[math, sequtils]
|
||||
import chimera/dsl
|
||||
import chimera/hedge
|
||||
import chimera/twinsim
|
||||
import chimera/residual
|
||||
import chimera/fusion
|
||||
|
||||
var failed = 0
|
||||
proc check(name: string, cond: bool, detail: string = "") =
|
||||
if cond: echo " ok ", name
|
||||
else: echo " FAIL ", name, " [", detail, "]"; inc failed
|
||||
|
||||
proc sum(f: Fusion): float64 =
|
||||
for w in f.weights(): result += w
|
||||
|
||||
let kernels = allKernels()
|
||||
|
||||
proc demoOrigin(): ReplayTick =
|
||||
let h = initHedge(kernelNames(kernels))
|
||||
result = ReplayTick(tick: 10, ex: 200.0, ey: 300.0, edir: 0.0, espeed: 4.0,
|
||||
eenergy: 100.0, ox: 600.0, oy: 300.0, odir: 180.0,
|
||||
ospeed: 3.0, lastTurn: 0.0, lastSpeed: 4.0)
|
||||
result.weights = h.weights
|
||||
for k in kernels: result.mems.add defaultMemory(k.defaultParams)
|
||||
|
||||
block:
|
||||
echo "test 1: weights normalize"
|
||||
var f = initFusion()
|
||||
check("uniform start sums to 1", abs(sum(f) - 1.0) < 1e-12)
|
||||
for i in 0 ..< 20:
|
||||
f.update(PredIntent(turn: 0.0, speed: 4.0, fire: 0.0),
|
||||
[PredIntent(turn: 9.0, speed: -8.0, fire: 1.0),
|
||||
PredIntent(turn: 5.0, speed: 0.0, fire: 0.5),
|
||||
PredIntent(turn: 0.0, speed: 4.0, fire: 0.0)])
|
||||
check("stays normalized after updates", abs(sum(f) - 1.0) < 1e-12)
|
||||
|
||||
block:
|
||||
echo "test 2: persistence sanity (the consistently-right predictor wins)"
|
||||
var f = initFusion()
|
||||
for i in 0 ..< 300:
|
||||
f.update(PredIntent(turn: 0.0, speed: 4.0, fire: 0.0),
|
||||
[PredIntent(turn: 9.0, speed: -8.0, fire: 1.0), # mixture: bad
|
||||
PredIntent(turn: 5.0, speed: 0.0, fire: 0.5), # residual: bad
|
||||
PredIntent(turn: 0.0, speed: 4.0, fire: 0.0)]) # persistence: good
|
||||
check("persistence weight dominates", f.weights()[PersistenceIdx] > 0.9,
|
||||
$f.weights())
|
||||
check("confidence is in range", f.confidence() >= 0.0 and f.confidence() <= 1.0,
|
||||
$f.confidence())
|
||||
|
||||
block:
|
||||
echo "test 3: fused rollout shape + fire/confidence smoke"
|
||||
let origin = demoOrigin()
|
||||
var f = initFusion()
|
||||
for i in 0 ..< 50:
|
||||
f.update(PredIntent(turn: 0.0, speed: 4.0, fire: 0.0),
|
||||
[PredIntent(turn: 0.0, speed: 4.0, fire: 0.0),
|
||||
PredIntent(turn: 0.0, speed: 4.0, fire: 0.0),
|
||||
PredIntent(turn: 0.0, speed: 4.0, fire: 0.0)])
|
||||
var head = initResidualHead()
|
||||
let rp = head.predict(features(ResidualInput(lastSpeed: 4.0, scanAge: 1.0,
|
||||
dist: 400.0)))
|
||||
let fp = f.fusedPrediction(kernels, origin, rp, 5)
|
||||
check("horizon points = 5", fp.points.len == 5 and fp.pFire.len == 5,
|
||||
$fp.points.len)
|
||||
check("fireWithin in [0,1]", fp.fireWithin >= 0.0 and fp.fireWithin <= 1.0,
|
||||
$fp.fireWithin)
|
||||
check("confidence in [0,1]", fp.confidence >= 0.0 and fp.confidence <= 1.0)
|
||||
check("all points finite",
|
||||
fp.points.allIt(it.x == it.x and abs(it.x) < 1e9 and
|
||||
it.y == it.y and abs(it.y) < 1e9))
|
||||
|
||||
block:
|
||||
echo "test 4: determinism (same state => same fused prediction)"
|
||||
let origin = demoOrigin()
|
||||
var f1 = initFusion()
|
||||
var f2 = initFusion()
|
||||
for i in 0 ..< 30:
|
||||
let r = PredIntent(turn: 1.0, speed: 2.0, fire: 0.0)
|
||||
f1.update(r, [r, r, r])
|
||||
f2.update(r, [r, r, r])
|
||||
let a = f1.fusedPrediction(kernels, origin, ResidualPrediction(), 5)
|
||||
let b = f2.fusedPrediction(kernels, origin, ResidualPrediction(), 5)
|
||||
var same = a.points.len == b.points.len
|
||||
for i in 0 ..< a.points.len:
|
||||
if a.points[i].x != b.points[i].x or a.points[i].y != b.points[i].y:
|
||||
same = false
|
||||
check("bit-identical fused trajectories", same)
|
||||
|
||||
echo ""
|
||||
if failed == 0: echo "all fusion tests passed"
|
||||
else: echo failed, " fusion test(s) FAILED"; quit(1)
|
||||
Reference in New Issue
Block a user