77 lines
3.0 KiB
Nim
77 lines
3.0 KiB
Nim
## Tier A unit tests for the CHIMERA residual head (chimera/residual.nim).
|
|
## Run: from ChimeraBot_garage/src, `nimble test`.
|
|
|
|
import std/math
|
|
import chimera/residual
|
|
|
|
var failed = 0
|
|
proc check(name: string, cond: bool, detail: string = "") =
|
|
if cond: echo " ok ", name
|
|
else: echo " FAIL ", name, " [", detail, "]"; inc failed
|
|
|
|
proc finite(x: float64): bool = x == x and abs(x) < 1e12
|
|
|
|
block:
|
|
echo "test 1: online determinism"
|
|
var a = initResidualHead()
|
|
var b = initResidualHead()
|
|
let f = features(ResidualInput(lastTurn: 3.0, lastSpeed: -2.0, scanAge: 1.0,
|
|
dist: 250.0, ownTurn: 4.0, ownSpeed: 5.0))
|
|
for i in 0 ..< 50:
|
|
let t = ResidualTarget(turn: 0.1 * float(i mod 5), speed: -0.05,
|
|
fire: 0.001 * float(i))
|
|
a.observe(f, t)
|
|
b.observe(f, t)
|
|
let pa = a.predict(f)
|
|
let pb = b.predict(f)
|
|
check("identical heads agree bit-for-bit",
|
|
pa.turn == pb.turn and pa.speed == pb.speed and pa.fire == pb.fire)
|
|
check("samples counted", a.samples == 50, $a.samples)
|
|
|
|
block:
|
|
echo "test 2: monotone improvement on a synthetic residual stream"
|
|
# Deterministic stream: target.turn = a linear function of the features.
|
|
var head = initResidualHead()
|
|
var state = 0x12345678'u64
|
|
proc u01(): float64 =
|
|
state = state * 6364136223846793005'u64 + 1442695040888963407'u64
|
|
float64(state shr 11) / 9007199254740992.0
|
|
var firstSum, lastSum = 0.0
|
|
for i in 0 ..< 2000:
|
|
let inp = ResidualInput(lastTurn: (u01() * 2.0 - 1.0) * 10.0,
|
|
lastSpeed: (u01() * 2.0 - 1.0) * 8.0,
|
|
scanAge: 1.0 + u01() * 3.0,
|
|
dist: u01() * 800.0,
|
|
ownTurn: (u01() * 2.0 - 1.0) * 10.0,
|
|
ownSpeed: (u01() * 2.0 - 1.0) * 8.0)
|
|
let ff = features(inp)
|
|
# true residual: 0.2*f0 + 0.1*f1 - 0.3*f4
|
|
let tgt = ResidualTarget(turn: 0.2 * ff[0] + 0.1 * ff[1] - 0.3 * ff[4],
|
|
speed: 0.0, fire: 0.0)
|
|
let l = head.meanSquaredLoss(ff, tgt)
|
|
if i < 100: firstSum += l
|
|
if i >= 1900: lastSum += l
|
|
head.observe(ff, tgt)
|
|
check("early loss > late loss (learns)", lastSum < firstSum,
|
|
$lastSum & " vs " & $firstSum)
|
|
|
|
block:
|
|
echo "test 3: no crash / finite on degenerate input"
|
|
var head = initResidualHead()
|
|
let zeros: array[NFeatures, float64] = default(array[NFeatures, float64])
|
|
head.observe(zeros, ResidualTarget())
|
|
let big = features(ResidualInput(dist: 1e9, lastTurn: 1e9, ownSpeed: -1e9))
|
|
head.observe(big, ResidualTarget(turn: 1e9, speed: -1e9, fire: 1.0))
|
|
let p = head.predict(big)
|
|
check("prediction is finite", finite(p.turn) and finite(p.speed) and finite(p.fire))
|
|
check("fire prediction clamped to [-1,1]", p.fire >= -1.0 and p.fire <= 1.0,
|
|
$p.fire)
|
|
var empty = initResidualHead()
|
|
empty.reset()
|
|
check("reset zeroes samples", empty.samples == 0 and
|
|
empty.predict(zeros).turn == 0.0)
|
|
|
|
echo ""
|
|
if failed == 0: echo "all residual tests passed"
|
|
else: echo failed, " residual test(s) FAILED"; quit(1)
|