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).
141 lines
5.4 KiB
Nim
141 lines
5.4 KiB
Nim
## 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.
|
|
##
|
|
## This is the MOVEMENT-PREDICTION gun's feature set. The label it is trained on
|
|
## is the correction from the plain geometric intercept to the enemy's TRUE
|
|
## impact position (`tools/elm_bench/extract_predict.py`), so the features are
|
|
## enemy-motion-centric: a window of recent scan-to-scan displacement vectors,
|
|
## plus the single-frame geometry the old gun used.
|
|
##
|
|
## Every quantity comes from the shooter's own state and its ScannedBotEvent
|
|
## history (scans are exact, one per tick in game). No oracle.
|
|
##
|
|
## Stdlib only, so the offline trainer and the in-game gun share it verbatim.
|
|
|
|
import std/math
|
|
|
|
const
|
|
WinCap* = 8 ## motion window: scans kept, newest first
|
|
## Feature vector layout (FeatureDim = 32):
|
|
## 0..13 7 newest-to-oldest displacement vectors, each divided by its own
|
|
## tick gap and by 16 px/tick: [dx0,dy0, dx1,dy1, ... dx6,dy6]
|
|
## 14..16 newest speed, window mean speed, window speed sd (all /10)
|
|
## 17,18 sin/cos of the newest displacement heading
|
|
## 19,20 sin/cos of the turn rate (angle between the two newest legs)
|
|
## 21 curvature |dtheta| / speed, clamped to 1
|
|
## 22..24 belief distance /1000, sin/cos of the body-relative bearing
|
|
## 25 our own speed /10
|
|
## 26 bullet speed /20
|
|
## 27 believed time of flight /30
|
|
## 28 predErr: base-intercept bearing minus current bearing, /PI
|
|
## 29 target lateral velocity /10
|
|
## 30 closing speed /10
|
|
## 31 line-of-sight rate (lat vel / dist)
|
|
FeatureDim* = 32
|
|
featureNames* = [
|
|
"dx0", "dy0", "dx1", "dy1", "dx2", "dy2", "dx3", "dy3",
|
|
"dx4", "dy4", "dx5", "dy5", "dx6", "dy6",
|
|
"speed", "speedMean", "speedSd",
|
|
"sinHead", "cosHead", "sinTurn", "cosTurn", "curve",
|
|
"dist", "sinRel", "cosRel", "ownSpeed", "bulletSpd", "tof",
|
|
"predErr", "tgtLatVel", "closing", "losRate",
|
|
]
|
|
|
|
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 ## physical bullet speed, 20 - 3*power
|
|
power*: float ## the power we are about to (or did) fire at
|
|
gunHeat*: float
|
|
## motion window, newest first. winTick[k] is the tracker tick of the
|
|
## k-th newest scan; winX/winY the scanned position.
|
|
winX*, winY*: array[WinCap, float]
|
|
winTick*: array[WinCap, int]
|
|
winN*: int
|
|
|
|
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 extract_predict.py exactly. All-zero 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)
|
|
let sp = hypot(b.vx, b.vy)
|
|
let lat = -b.vx * sin(ang) + b.vy * cos(ang)
|
|
let rad = b.vx * cos(ang) + b.vy * sin(ang)
|
|
let tof = de / b.bulletSpeed
|
|
let px = b.ex + b.vx * tof
|
|
let py = b.ey + b.vy * tof
|
|
let base = arctan2(py - b.selfY, px - b.selfX)
|
|
let predErr = wrapAngle(base - ang)
|
|
|
|
# --- motion window: displacement of leg k = scan k -> scan k+1 ---
|
|
var spdSum = 0.0
|
|
var spdSq = 0.0
|
|
var nLeg = 0
|
|
var head = 0.0
|
|
var dtheta = 0.0
|
|
if b.winN >= 2:
|
|
var dxs: array[WinCap - 1, float]
|
|
var dys: array[WinCap - 1, float]
|
|
for k in 0 ..< min(b.winN, WinCap) - 1:
|
|
let gap = float(max(b.winTick[k] - b.winTick[k + 1], 1))
|
|
let dx = (b.winX[k] - b.winX[k + 1]) / gap
|
|
let dy = (b.winY[k] - b.winY[k + 1]) / gap
|
|
dxs[k] = dx
|
|
dys[k] = dy
|
|
result[2 * k] = dx / 16.0
|
|
result[2 * k + 1] = dy / 16.0
|
|
let s = hypot(dx, dy)
|
|
spdSum += s
|
|
spdSq += s * s
|
|
inc nLeg
|
|
let nf = float(nLeg)
|
|
let sMean = spdSum / nf
|
|
head = arctan2(dys[0], dxs[0])
|
|
if nLeg >= 2:
|
|
let phead = arctan2(dys[1], dxs[1])
|
|
dtheta = wrapAngle(head - phead)
|
|
result[14] = sp / 10.0
|
|
result[15] = sMean / 10.0
|
|
result[16] = sqrt(max(spdSq / nf - sMean * sMean, 0.0)) / 10.0
|
|
result[17] = sin(head)
|
|
result[18] = cos(head)
|
|
result[19] = sin(dtheta)
|
|
result[20] = cos(dtheta)
|
|
result[21] = min(abs(dtheta) / max(sMean, 1e-6), 1.0)
|
|
else:
|
|
result[14] = sp / 10.0
|
|
result[15] = sp / 10.0
|
|
|
|
result[22] = de / 1000.0
|
|
result[23] = sin(rel)
|
|
result[24] = cos(rel)
|
|
result[25] = b.selfSpeed / 10.0
|
|
result[26] = b.bulletSpeed / 20.0
|
|
result[27] = tof / 30.0
|
|
result[28] = predErr / PI
|
|
result[29] = lat / 10.0
|
|
result[30] = -rad / 10.0
|
|
result[31] = lat / max(de, 1.0)
|
|
|
|
for i in 0 ..< FeatureDim:
|
|
if classify(result[i]) in {fcNan, fcInf, fcNegInf}: result[i] = 0.0
|