## Offline tests for the TEMPORAL extensions of the WaveElm gun: ## (A) the lag-window feature map (guns/waveelm.nim, WaveLagK / waveFeatures) ## (B) the RELM recurrent hidden layer (guns/elm.nim, r / hPrev / predictStep) ## covered here, plus the per-enemy / per-episode state reset and the ## train-on-the-stored-activation path. ## ## Everything is offline and bounded: no server, no java, no runBattle, and ## every loop count is a small constant. ## ## Run: nim c -r common_libs/tests/test_waveelm_temporal.nim import std/[math, strformat, tables] import gun_harness/gun_interface import guns/elm import guns/waveelm var failures = 0 var checks = 0 proc check(name: string, ok: bool) = inc checks if ok: echo "PASS: ", name else: echo "FAIL: ", name; inc failures proc fin(x: float): bool = classify(x) notin {fcNan, fcInf, fcNegInf} proc allFinite(s: seq[float]): bool = for v in s: if not fin(v): return false true proc mkState(tick: int, ex, ey, eh, es: float, id = 1, sx = 100.0, sy = 300.0): WorldState = WorldState( enemyX: ex, enemyY: ey, enemyHeading: eh, enemySpeed: es, enemyEnergy: 100.0, selfX: sx, selfY: sy, selfSpeed: 0.0, selfHeading: 0.0, selfRadarHeading: 0.0, selfEnergy: 100.0, arenaWidth: 800.0, arenaHeight: 600.0, tick: tick, enemies: @[EnemyInfo(id: id, x: ex, y: ey, heading: eh, speed: es, energy: 100.0, lastSeenTick: tick)]) # ── (A) the lag window ─────────────────────────────────────────────────────── proc testLagWindow() = check "lag: waveInputDim(2) is the legacy 10", waveInputDim(2) == 10 check "lag: waveInputDim(4) == 10 + 2*(4-2)", waveInputDim(4) == 14 check "lag: waveInputDim(8) == 10 + 2*(8-2)", waveInputDim(8) == 22 check "lag: WaveTrackCap covers the longest window", WaveTrackCap >= WaveLagKMax + 1 check "lag: init clamps the window to [2, WaveLagKMax]", initWaveElmGun(42, lagK = 1).lagK == 2 and initWaveElmGun(42, lagK = 99).lagK == WaveLagKMax block: # A straight run of scans: one tick apart, +6 px in x each tick. Every # window slot must hold THAT tick's displacement / 16 px. var g = initWaveElmGun(42, lagK = 4) check "lag: the net is as wide as the window", g.net.inputDim == 14 var f: seq[float] for t in 0..<6: let st = mkState(t, 300.0 + 6.0 * t.float, 300.0, 0.0, 6.0) discard g.predict(st, 17.0) f = g.waveFeatures(st, 6.0, 0.0, 30.0) check "lag: the feature vector is exactly waveInputDim(4) long", f.len == 14 var ok = true for k in 0..<4: if abs(f[4 + 2 * k] - 6.0 / 16.0) > 1e-12: ok = false if abs(f[5 + 2 * k] - 0.0) > 1e-12: ok = false check &"lag: all 4 window slots hold their own displacement / 16 (t={6})", ok check "lag: the staleness/flight slots moved to the end of the window", abs(f[4 + 2 * 4] - 0.0) < 1e-12 and f[5 + 2 * 4] > 0.0 block: # The base 8 features must not depend on the window: a lagK=4 gun fed the # same scans must agree with a legacy lagK=2 gun on slots 0..7. var g4 = initWaveElmGun(42, lagK = 4) var g2 = initWaveElmGun(42, lagK = 2) for t in 0..<5: let st = mkState(t, 300.0 + 5.0 * t.float, 300.0 + t.float, 12.0, 5.0) discard g4.predict(st, 17.0) discard g2.predict(st, 17.0) let f4 = g4.waveFeatures(mkState(4, 320.0, 304.0, 12.0, 5.0), 5.0, 1.0, 30.0) let f2 = g2.waveFeatures(mkState(4, 320.0, 304.0, 12.0, 5.0), 5.0, 1.0, 30.0) var same = true for i in 0..<8: if abs(f4[i] - f2[i]) > 1e-12: same = false check "lag: the base features are unchanged by the window (slots 0..7)", same block: # Fewer scans than the window: the missing slots must be ZERO, not garbage. var g = initWaveElmGun(42, lagK = 6) discard g.predict(mkState(0, 300.0, 300.0, 0.0, 5.0), 17.0) let f = g.waveFeatures(mkState(0, 300.0, 300.0, 0.0, 5.0), 5.0, 0.0, 30.0) check "lag: a window longer than the scan history reads zeros", f.len == waveInputDim(6) and (block: var ok = true for i in 0.. 0 makes the net recurrent", net.recurrent() check "relm: every |r_j| is inside the contraction range [0, ElmRMax]", (block: var ok = true for v in net.r: if abs(v) > ElmRMax or abs(v) >= 1.0: ok = false ok) check "relm: the recurrence is seeded (not all identical gains)", (block: var varies = false for i in 1..