DevControlBot: an ELM gun, its geometric baseline, and an honest null result

Firing wired in behind a compile-time A/B switch (-d:baselineGun): both guns go
through the same predictAim() call and differ only by the learned correction, so
the measured difference is attributable. radar.nim now keeps the two most recent
scans (velocity, scan age, own displacement).

Measured over 20 recorded rounds each vs DrussGT, CassiusClay, Komarious and
Phoenix: the ELM gun LOSES to the geometric baseline on three of four opponents
(Phoenix 51.0% vs 99.5% hit rate). README has the table, the diagnosis and the
two in-game-only bugs measuring exposed (degrees/radians, and features whose
in-game range lies outside the corpus).
This commit is contained in:
2026-10-05 23:48:58 +02:00
parent 818bad50fd
commit dacb650692
6 changed files with 670 additions and 2 deletions
@@ -0,0 +1,135 @@
## The ELM gun's FEATURE VECTOR — one definition, shared by the offline trainer
## and the in-game gun, so the model can never drift from its inputs.
##
## It is a line-by-line port of the feature block of
## `tools/elm_bench/bench_realistic.nim`, which is itself a port of the belief
## computation in `tools/elm_bench/extract_realistic.py`. The offline corpus was
## built from OBSERVER ground truth; this module rebuilds exactly the same
## quantities from what the shooter legitimately knows: its own state plus its
## ScannedBotEvent history.
##
## Two features of the benchmark set are deliberately absent: `round` and `tick`.
## They are corpus bookkeeping (which round, how far into it) and are not
## observable as themselves in-game — the bot's tick counter would stand in for
## `tick` and nothing for `round`, so keeping them would train on a feature the
## runtime cannot reproduce. Dropping two of 24 costs little and keeps the
## offline and online feature spaces identical, which is the whole point.
##
## No import of the bot API: the trainer (Nim, offline) and the gun (in-game)
## both use this, so it must stay pure stdlib + math.
import std/math
## ## The four features that were CUT, and why (measured, not guessed)
##
## `scanAge`, `logScan`, `disp` and `gunHeat` are the corpus' four stale-belief
## features, and in game they are structurally OUT OF DOMAIN, not merely shifted:
##
## feature corpus (9 legacy bots) DevControlBot in game
## scanAge 0 .. 0.033 (0-1 ticks) 0.067 .. 0.333 (2-10 ticks)
## logScan 0 .. 0.173 0.275 .. 0.599
## disp 0 .. 0.080 (<= 8px) 0 .. 0.635 (up to 63px)
## gunHeat 0.343 .. 0.533 (always warm) 0.0 (always cooled)
##
## The corpus is ~97% same-tick shots by bots moving slowly; this bot fires when
## ITS GUN IS COOLED, several ticks after a scan, at up to 8px/tick. A tanh ELM
## extrapolates violently outside its training box: with those four features in,
## 95% of in-game shots returned the clamp (|correction| >= 0.5 rad) and the gun
## was WORSE than the geometric baseline it is supposed to improve on. Without
## them the model stays inside the trained box and the remaining 18 features are
## all geometry the bot really does know.
##
## So: they are gone, and the honest cost is recorded here — a bot that fires on
## the tick it scans (gunHeat unconstrained, age 0) can put them back and
## retrain.
const
FeatureDim* = 18 ## length of the vector `buildFeatures` returns
featureNames* = [
"dist", # 0 belief distance / 1000
"invDist", # 1 soft inverse distance 1/(1 + d/100)
"sinRel", # 2 sin of believed current bearing, body-relative
"cosRel", # 3 cos of the same
"tgtSpeed", # 4 estimated target speed / 10
"bulletSpd", # 5 bullet speed / 20
"everScanned", # 6 1.0 if we have ever scanned the target
"scanGap", # 7 ticks between the last two scans, clamped /30
"sinPred", # 8 sin of the believed INTERCEPT bearing
"cosPred", # 9 cos of the same
"predErr", # 10 wrap(pred - rel) / PI <-- the feature that carries
# the signal (see README: a single
# linear feature on it gives -61%
# of the -62.6% the whole ELM gives)
"tgtLatVel", # 11 lateral target velocity (left-positive) / 10
"closing", # 12 closing speed / 10
"losRate", # 13 lateral velocity / distance, rad per tick
"tof", # 14 believed time of flight / 30
"power", # 15 shot power / 3
"ownSpeed", # 16 our speed / 10
"relSin", # 17 sin of the believed current bearing (duplicate of 2,
# kept because the benchmark carried it)
]
type
GunBelief* = object
## Everything the gun needs, all of it legitimately observable.
everScanned*: bool
selfX*, selfY*: float ## our position NOW
selfHeading*: float ## our body heading, radians (0 = east, CCW +)
selfSpeed*: float
ownDisp*: float ## how far WE moved since the last scan (px)
scanAge*: float ## ticks since the last scan
scanGap*: float ## ticks between the last two scans (0 if <2)
ex*, ey*: float ## dead-reckoned target position NOW
vx*, vy*: float ## estimated target velocity (px/tick)
bulletSpeed*: float ## THE CORPUS' bullet-speed field, which is
## `power * 10` (extract_realistic.py: `v =
## bu["power"] * 10.0`) and NOT the physical
## 20 - 3*power. Reproduced verbatim so the
## feature means the same thing offline and in
## game; only `tof` is derived from it.
power*: float ## the power we are about to (or did) fire at
gunHeat*: float
proc wrapAngle(a: float): float =
## wrap to (-pi, pi]
var r = a
while r > PI: r -= 2 * PI
while r <= -PI: r += 2 * PI
r
proc buildFeatures*(b: GunBelief): array[FeatureDim, float64] =
## The feature vector. Mirrors bench_realistic.nim exactly, including the
## all-zero vector when we have never scanned the target.
if not b.everScanned:
return default(array[FeatureDim, float64])
let de = hypot(b.ex - b.selfX, b.ey - b.selfY)
let ang = arctan2(b.ey - b.selfY, b.ex - b.selfX) ## line-of-sight bearing
let rel = wrapAngle(ang - b.selfHeading) ## believed current bearing
let sp = hypot(b.vx, b.vy)
let lat = -b.vx * sin(ang) + b.vy * cos(ang) ## + = target moves left
let rad = b.vx * cos(ang) + b.vy * sin(ang) ## + = target moves away
let tof = de / b.bulletSpeed
let px = b.ex + b.vx * tof
let py = b.ey + b.vy * tof
let pred = wrapAngle(arctan2(py - b.selfY, px - b.selfX) - b.selfHeading)
let predErr = wrapAngle(pred - rel)
let gap = max(b.scanGap, 0.0)
result[0] = de / 1000.0
result[1] = 1.0 / (1.0 + de / 100.0)
result[2] = sin(rel)
result[3] = cos(rel)
result[4] = sp / 10.0
result[5] = b.bulletSpeed / 20.0
result[6] = 1.0
result[7] = min(gap, 30.0) / 30.0
result[8] = sin(pred)
result[9] = cos(pred)
result[10] = predErr / PI
result[11] = lat / 10.0
result[12] = -rad / 10.0
result[13] = lat / max(de, 1.0)
result[14] = tof / 30.0
result[15] = b.power / 3.0
result[16] = b.selfSpeed / 10.0
result[17] = sin(rel)