predictAim folded the learned correction into the bearing and derived x/y once instead of recomputing them per branch. New gunAimErrorDeg carries the degree unit in its name so the FireSlopDeg comparison cannot fall back to the old degrees-vs-radians mix-up; test_online.nim adds a regression block for it.
95 lines
4.4 KiB
Nim
95 lines
4.4 KiB
Nim
## Unit tests for the ONLINE-learning gun (elm_gun.nim): the delayed-label
|
|
## queue, the novelty gate, and the per-opponent reset.
|
|
## Run: nim c -r tests/test_online.nim (from DevControlBot_garage/)
|
|
|
|
import std/math
|
|
import modules/ELM/elm_gun
|
|
import modules/ELM/elm_features
|
|
import modules/ELM/elm # for Elm.infer in the assertions
|
|
|
|
var failed = 0
|
|
proc check(name: string, cond: bool, detail: string = "") =
|
|
if cond: echo " ok ", name
|
|
else: echo " FAIL ", name, " [", detail, "]"; inc failed
|
|
|
|
# ── 1. neutral start: no prior, gate closed ─────────────────────────────────
|
|
block:
|
|
echo "test 1: neutral start"
|
|
let g = initElmGun(priorScale = 0.0)
|
|
check("weights loaded", g.loaded)
|
|
check("prior stored", g.priorBeta.len > 0)
|
|
check("nOnline == 0", g.nOnline == 0)
|
|
check("gate closed at start", not g.gateOpen)
|
|
|
|
# ── 2. online learning opens the gate once it beats zero ────────────────────
|
|
block:
|
|
echo "test 2: online learning closes then opens the novelty gate"
|
|
let g = initElmGun(priorScale = 0.0)
|
|
let feat = newSeq[float64](FeatureDim) # constant feature vector
|
|
for i in 0 ..< 60:
|
|
g.feedSample(feat, 0.3) # constant true correction
|
|
check("accumulated samples", g.nOnline == 60)
|
|
check("model beats zero (modelErr < zeroErr)", g.modelErr < g.zeroErr,
|
|
$g.modelErr & " vs " & $g.zeroErr)
|
|
check("gate OPEN after learning", g.gateOpen)
|
|
let corr = g.model.infer(feat)[0]
|
|
check("correction near label", abs(corr - 0.3) < 0.1, $corr)
|
|
|
|
# ── 3. a model that cannot beat zero keeps the gate shut ────────────────────
|
|
block:
|
|
echo "test 3: no-signal data keeps the gate closed"
|
|
let g = initElmGun(priorScale = 0.0)
|
|
let feat = newSeq[float64](FeatureDim)
|
|
for i in 0 ..< 40:
|
|
g.feedSample(feat, 0.0) # label is exactly zero
|
|
check("gate stays closed on zero labels", not g.gateOpen)
|
|
|
|
# ── 4. delayed-label queue: keyed by dueTick, needs the trace ───────────────
|
|
block:
|
|
echo "test 4: pending queue resolves at dueTick"
|
|
let g = initElmGun(priorScale = 0.0)
|
|
let feat: array[FeatureDim, float64] = default(array[FeatureDim, float64])
|
|
let due = 50
|
|
g.noteEnemyAt(due, 100.0, 0.0) # target straight ahead of (0,0)
|
|
g.recordShot(feat, 0.0, 0.0, 0.0, due) # base bearing 0, due at tick 50
|
|
g.drainPending(due - 1)
|
|
check("nothing resolved before dueTick", g.nOnline == 0 and g.pending.len == 1)
|
|
g.drainPending(due)
|
|
check("resolved at dueTick", g.nOnline == 1 and g.pending.len == 0)
|
|
|
|
# ── 5. reset returns to the neutral start ───────────────────────────────────
|
|
block:
|
|
echo "test 5: resetOnline clears per-opponent state"
|
|
let g = initElmGun(priorScale = 0.0)
|
|
let feat = newSeq[float64](FeatureDim)
|
|
for i in 0 ..< 60: g.feedSample(feat, 0.3)
|
|
g.noteEnemyAt(1, 1.0, 1.0)
|
|
g.recordShot(default(array[FeatureDim, float64]), 0.0, 0.0, 0.0, 5)
|
|
g.resetOnline()
|
|
check("nOnline cleared", g.nOnline == 0)
|
|
check("gate closed after reset", not g.gateOpen)
|
|
check("pending cleared", g.pending.len == 0)
|
|
check("trace cleared", g.traceTick.len == 0)
|
|
|
|
# ── 6. deg/rad fire boundary (regression: the FireSlopDeg mix-up) ───────────
|
|
block:
|
|
echo "test 6: gun-aim error is DEGREES at the fire boundary"
|
|
# aim 0 rad = east; gun at +1 deg -> short-way error -1 deg, NOT -0.0175.
|
|
check("1 deg off gives a -1 deg error",
|
|
abs(gunAimErrorDeg(0.0, 1.0) + 1.0) < 1e-9, $gunAimErrorDeg(0.0, 1.0))
|
|
# short way round: gun 350 deg from an east(0) aim is +10 deg to reach it.
|
|
check("350 deg wraps to +10 deg",
|
|
abs(gunAimErrorDeg(0.0, 350.0) - 10.0) < 1e-6, $gunAimErrorDeg(0.0, 350.0))
|
|
# THE historical bug: a degree error compared against degToRad(FireSlopDeg)
|
|
# (radians) admitted only 0.052 deg. At exactly FireSlopDeg degrees off, the
|
|
# degrees-vs-degrees predicate admits the shot; the radian mix-up rejects it.
|
|
const slop = 1.0'f64
|
|
let err = gunAimErrorDeg(0.0, slop) # exactly `slop` degrees off
|
|
check("|err| <= FireSlopDeg admits the boundary", abs(err) <= slop, $err)
|
|
check("the radian mix-up would REJECT it", abs(err) > degToRad(slop), $err)
|
|
|
|
if failed > 0:
|
|
echo "\n", failed, " check(s) FAILED"
|
|
quit(1)
|
|
echo "\nall online checks passed"
|