DevControlBot: movement-prediction features, gate, radar scan window

Rewrite elm_features.nim as a 32-D enemy-motion vector (7 scan-to-scan
displacement legs, speed stats, heading/turn/curvature, plus the single-frame
geometry). Add a WinCap=8 scan buffer to radar.nim and feed it through
buildBelief. elm_gun.nim now uses the physical bullet speed for the feature
(was the power*10 corpus artifact) and applies the trained correction only when
|correction| > GATE_TAU; below it the gun keeps the exact geometric aim. Retrain
and export weights (H=256, gate_tau=0.1).
This commit is contained in:
2026-10-06 09:00:43 +02:00
parent 9f09204c40
commit 6a14b0ffd6
5 changed files with 152 additions and 127 deletions
@@ -1,39 +1,36 @@
## The ELM GUN — a learned aim-error corrector, plus the geometric baseline it
## has to beat.
## The ELM GUN — a movement-prediction corrector on top of the geometric
## intercept.
##
## ## What it predicts
##
## The ELM does NOT predict where the enemy is. It predicts the shooter's own
## aim ERROR: `label = idealInterceptBearing - gunBearing` at the moment the shot
## leaves the barrel. That is the quantity measured in the corpus and it is the
## right target because it composes with everything: the classic geometric
## intercept gives the aim point, the ELM says how far off that aim point the
## opponent's own habit puts it, and the bot adds the two.
## The ELM predicts the ENEMY'S FUTURE POSITION, expressed as a correction to
## the plain geometric intercept: at fire time the classic intercept gives a
## base aim point, the ELM estimates `trueImpactBearing - baseBearing` from the
## enemy's recent motion window, and the gun adds the two. That is a movement
## predictor, NOT the old opponent-aim-bias model (which measured how far the
## OTHER bot's shots missed and is deleted).
##
## ## The baseline
## ## The baseline and the gate
##
## `heuristicAim` is the plain geometric intercept — dead-reckon the target from
## the last two scans, lead it by d/bulletSpeed. No learning, no correction, and
## (deliberately) built from the SAME belief as the ELM, so the A/B in real
## battles isolates the learned correction and nothing else.
## the last two scans, lead it by d/bulletSpeed. The learned correction is
## applied ONLY when `|correction| > gateTau`, the threshold calibrated on
## held-out folds (see `train_predict.nim`); below it the gun returns the exact
## geometric aim, so a small noisy nudge can never make the ELM worse than the
## baseline.
##
## ## Weights file
##
## `elm_gun_weights.txt` next to this file, written by
## `tools/elm_bench/train_elm_gun.nim`. Plain text, one matrix per line, flat:
## `tools/elm_bench/train_predict.nim`. Plain text, one matrix per line, flat:
##
## # comment lines are ignored
## <inputDim> <hiddenDim> <outputs> <lambda> <decay> <seed> <activation>
## the header row, e.g. `22 128 1 0.01 1 12345 tanh`
## W <hiddenDim*inputDim values> hidden weights, ROW-major: hiddenDim
## rows of inputDim, pre-activation a_j . x
## W <hiddenDim*inputDim values> hidden weights, ROW-major
## HS <hiddenDim values> hidden-layer biases, b_j
## BETA <(hiddenDim+1)*outputs vals> output weights, ROW-major: outputs rows
## of hiddenDim+1. The LAST entry of a row is
## the output-layer bias — the ELM carries
## it as a constant 1.0 column, not a
## separate parameter (elm.nim).
## BETA <(hiddenDim+1)*outputs vals> output weights, ROW-major
## TRAIN_RMSE <v> training RMSE at export time; diagnostic only, unused.
## GATE_TAU <v> correction magnitude below which correction is 0.
##
## It is a data file, not code: deleting it costs the bot its learned correction,
## nothing else.
@@ -70,6 +67,7 @@ type
ElmGun* = ref object
model*: Elm ## the loaded model (never refitted in-game)
loaded*: bool
gateTau*: float ## |correction| below this -> geometric aim
lastCorrection*: float ## last raw ELM output, rad — the calibration
## probe the battle harness reads
@@ -124,6 +122,7 @@ proc loadWeightsFile(g: ElmGun, path: string): bool =
of "HS": hs = readFloats(f, cfg.hiddenDim, path)
of "BETA": beta = readFloats(f, (cfg.hiddenDim + 1) * cfg.outputs, path)
of "TRAIN_RMSE": discard
of "GATE_TAU": g.gateTau = parts[1].parseFloat()
else:
# header row
cfg = ElmConfig(inputDim: parts[0].parseInt(), hiddenDim: parts[1].parseInt(),
@@ -168,10 +167,12 @@ proc elmCorrection*(g: ElmGun, b: GunBelief, power: float64 = FirePower): float
if not g.loaded or not b.everScanned: return NaN
var bb = b
bb.power = power
bb.bulletSpeed = power * 10.0 # the corpus' convention, see elm_features.nim
bb.bulletSpeed = 20.0 - 3.0 * power # physical speed, matches heuristicAim
let feat = buildFeatures(bb)
let corr = g.model.infer(feat.toSeq())[0]
g.lastCorrection = corr
if abs(corr) <= g.gateTau:
return 0.0 # gate closed: exact geometric aim
clamp(corr, -MaxAimShift, MaxAimShift)
proc predictAim*(g: ElmGun, b: GunBelief, power: float64 = FirePower,