## bench_waveelm_temporal.nim — OFFLINE A/B BENCHMARK for the temporal ## modelling of the online ELM gun (guns/waveelm.nim). ## ## WHAT IS MEASURED ## The gun's job is to predict a systematic aim OFFSET that is added to the ## RING RECONSTRUCTION (the base the directed design got from `intercept()`, ## now measured by the ring instead). So the quantity with a ground truth is the ## offset, not the aim point: each tick the harness rebuilds the SAME base the ## gun computed (identical inputs, `ringDecode` over the gun's own ring ## measurement) and compares ## ## error(t) = (gunPred(t) - ice(t)) - trueOffset(t) ## ## before any feedback arrives. That is the aim-offset estimator's prediction ## error, i.e. exactly what a hit/miss decides later. Two metrics: ## * RMS error (px) over the EVAL window (after burn-in) ## * hit rate: fraction of rung-shots whose residual is within BotRadius, ## counted over the WHOLE stream. The gun is only TRAINED on the hits, so ## hit rate is both the score and the amount of supervision available — ## a variant that predicts the offset badly gets fewer labels and can never ## recover. That is the real failure mode being benchmarked. ## ## THE GROUND TRUTH (see `truthOffset`) ## trueOffset(t) = G(t) * (pos(t) - pos(t-L)) + rad(t) ## i.e. "the aim solution that was actually used was L ticks stale, by a gain ## G". This is the turret lag / slew-rate model: the turret points at where the ## target WAS, the ring points at where it WILL be, and the gap is ## the sum of the last L displacements. It is a LINEAR functional of the recent ## displacement history, so any feature set that hands the net the last L ## displacements can represent it exactly — which is precisely the hypothesis ## under test. It is not a strawman: the gun's own doc says the label absorbs ## turret lag. ## ## THE THREE REGIMES (same bias functional form, different temporal character ## of the TARGET, so the estimator is identical and only the input differs) ## a) smooth : constant speed, smooth steady turn (turn-rate quantised, ## as a real bot's heading is), constant gain. ## b) accel : speed and turn rate both ramp with tick, so the offset ## envelope grows with the second derivative. ## c) abrupt : at t = AbruptTick the target flips its strafing direction ## (a 110 deg heading step) AND the gain steps 1.0 -> 1.6: ## the non-stationary case. ## ## Headroom, honestly: regime (a) is nearly linear in the features the gun ## already has, so (a) is expected to show little movement. The signal is in ## (b) and (c). ## ## Run: nim c -r common_libs/tests/bench_waveelm_temporal.nim ## NO battle server, NO runBattle, NO java: pure offline replay, ~1 s. import std/[math, strformat, sequtils] import gun_harness/gun_interface import gun_harness/virtual_bullets as vb import guns/waveelm const BenchTicks* = 300 ## ticks per regime (3 regimes x 3 rungs per tick) BenchBurnIn* = 60 ## ticks of warm-up excluded from the RMS BenchSelfX* = 100.0 BenchSelfY* = 300.0 ArenaW = 800.0 ArenaH = 600.0 LagTicks* = 4 ## L: how stale the aim solution was LongLagTicks* = 12 ## a staleness NO explicit lag window can represent HeadStep = 1.0 ## deg: turn-rate limit of the target's heading AbruptTick = 150 BenchSeeds = [42'i64, 7, 99] ## ELM/hidden-layer seeds averaged over # ── the target trajectories ────────────────────────────────────────────────── type Regime* = enum rSmooth, rAccel, rAbrupt proc regimeName*(r: Regime): string = case r of rSmooth: "smooth" of rAccel: "accel" of rAbrupt: "abrupt" type Traj* = object name*: string px*, py*: seq[float] ## true position per tick hx*, hy*: seq[float] ## true unit velocity direction per tick sp*: seq[float] ## true speed per tick ox*, oy*: seq[float] ## ground-truth systematic offset per tick proc wrap180(x: float): float = result = x for _ in 0..<4: if result > 180.0: result -= 360.0 elif result < -180.0: result += 360.0 else: break proc buildTraj*(r: Regime, jitter: bool, lag = LagTicks): Traj = ## Forward integration of a turn-rate-LIMITED heading: the commanded heading ## turns smoothly, the actual heading is quantised to HeadStep deg. That ## quantisation is the tick-scale structure that makes the L-tick-averaged ## displacement different from the instantaneous one — it is what a lag ## window buys, and it is a real property of Tank Royale bots (they are ## commanded, not continuous). result.name = regimeName(r) result.px = newSeq[float](BenchTicks) result.py = newSeq[float](BenchTicks) result.hx = newSeq[float](BenchTicks) result.hy = newSeq[float](BenchTicks) result.sp = newSeq[float](BenchTicks) var x = 400.0 var y = 300.0 var cmd = degToRad(20.0) ## commanded heading var act = cmd ## actual (quantised) heading ## Irregular burst jitter: a seeded coin per tick, speed x0 or x2, mean x1. ## This is the tick-scale structure a lag window exists to filter: real bots ## do not glide at a constant px/tick (they are tickRate-limited, they stop ## and go, the radar only catches them on some ticks), so the instantaneous ## displacement is a poor estimator of the motion the bias was built from, ## while a 4-tick average is a good one. The seed is fixed, so the burst ## pattern is identical on every run. var js = 0x2545F491'i64 for t in 0.. 6.4 px/tick turn = 2.0 + 0.018 * t.float # 2.0 -> 7.3 deg/tick of rAbrupt: speed = if t < AbruptTick: 4.5 else: 5.5 turn = if t < AbruptTick: 3.0 else: -3.0 if jitter: js = (js * 1103515245 + 12345) and 0x7FFFFFFF speed *= (if ((js shr 12) and 1) == 0: 0.4 else: 1.6) # Keep the target INSIDE the gun's solvable envelope. A target that # outruns the slowest rung (bulletSpeed(3.0) = 11 px/tick) has NO # intercept: t = |p + v t| / spd has no root, the fixed point diverges and # the aim point leaves the arena — a geometry failure of the intercept # solver, not a property of the offset estimator being benchmarked. if speed > 10.0: speed = 10.0 act = degToRad(round(radToDeg(act) / HeadStep) * HeadStep) result.px[t] = x result.py[t] = y result.hx[t] = cos(act) result.hy[t] = sin(act) result.sp[t] = speed x += cos(cmd) * speed y += sin(cmd) * speed # Keep the run inside the arena. A hard position clamp here would fake a # wall bounce (and a velocity that teleports), so instead the COMMANDED # heading is steered back toward the centre before the border is reached. if x < 150 or x > ArenaW - 150 or y < 150 or y > ArenaH - 150: cmd = arctan2(ArenaH / 2.0 - y, ArenaW / 2.0 - x) else: cmd = degToRad(wrap180(radToDeg(cmd) + turn)) # ground truth: the aim solution was L ticks stale, scaled by the gain let gain = proc(t: int): float = case r of rSmooth: 1.0 of rAccel: 0.9 + 0.0015 * t.float # the offset envelope grows of rAbrupt: (if t < AbruptTick: 1.0 else: 1.6) result.ox = newSeq[float](BenchTicks) result.oy = newSeq[float](BenchTicks) for t in 0..