README: movement-prediction gun — label centring, gate, real-battle numbers
Replace the aim-error-ELM section with the movement-prediction gun: the true-impact label and its centred distribution, the 32-D features, held-out training (ELM -27.8% RMSE vs plain intercept, output mean -0.36 deg), the |correction|>0.10 gate (61.4% open), and 20-round RawRecord results showing damage/round up against DrussGT, CassiusClay, Komarious and Phoenix.
This commit is contained in:
+128
-150
@@ -311,162 +311,140 @@ cd DevControlBot_garage && nimble runBot
|
||||
|
||||
There are **no tests** in this garage. The `test` task and the `tests/` directory
|
||||
were removed along with the old build layout; see
|
||||
[AGENTS.md](AGENTS.md) for the convention to follow when tests are added.
|
||||
## The ELM gun (learned aim-error correction) — and an honest null result
|
||||
## Movement-prediction gun
|
||||
|
||||
The bot used to never fire. It now fires, and the gun is an **ELM that predicts
|
||||
the shooter's own aim error**, not a target position: the classic geometric
|
||||
intercept produces the aim point, the ELM says how far off it the shot habitually
|
||||
lands, and the two are added.
|
||||
The gun predicts **where the enemy will actually be when the bullet arrives**.
|
||||
The old model predicted the opponent's aim error (how far *their* shots missed a
|
||||
correct intercept); using that as our own correction made the gun aim like the
|
||||
opponent, which is why it added a constant +1.6°..+11.5° bias and lost to the
|
||||
plain geometric gun. That path is deleted (`train_elm_gun.nim` removed,
|
||||
`extract_realistic.py` marked deprecated). The base geometric intercept is still
|
||||
the base lead (`heuristicAim`); the ELM now refines it with a movement
|
||||
prediction.
|
||||
|
||||
### Files
|
||||
### The label
|
||||
|
||||
For every sampled tick (shooter fixed to DevControlBot, power 2, bullet speed
|
||||
`20 - 3*2 = 14`, so shooter state and bullet speed match the live gun):
|
||||
|
||||
* **base** = the plain geometric intercept bearing from the enemy's current
|
||||
scan-based position/velocity (the same `heuristicAim`);
|
||||
* **true** = bearing to the enemy's ACTUAL position at the tick the bullet
|
||||
reaches it, solved by fixed-point iteration against the observer's true
|
||||
trajectory;
|
||||
* **label** = `wrap(true - base)`.
|
||||
|
||||
Generated by `tools/elm_bench/extract_predict.py` (stride 5) over
|
||||
`data/drussgt_raw` + `data/generalization_raw` into `samples_predict.ndjson`
|
||||
(53 980 samples, 9 opponents, gitignored and reproducible).
|
||||
|
||||
| | mean | sd | p5 | median | p95 |
|
||||
|---|---|---|---|---|---|
|
||||
| label | **-0.329°** | 18.322° | -32.68° | -0.35° | +32.81° |
|
||||
|
||||
**Centred near zero.** The old opponent-bias label had a large systematic offset
|
||||
(+0.042 rad = +2.4°); this one does not (-0.006 rad = -0.33°). On a target that
|
||||
holds its line the label is 0; only real turning/acceleration moves it.
|
||||
|
||||
### Features (`elm_features.nim`, 32-D)
|
||||
|
||||
Enemy-motion-centric: 7 newest-to-oldest scan-to-scan displacement vectors (each
|
||||
divided by its own tick gap, /16), newest/mean/sd speed, heading sin/cos, turn
|
||||
rate sin/cos, curvature, plus the single-frame geometry (distance, relative
|
||||
bearing, own speed, bullet speed, time of flight, `predErr`, lateral velocity,
|
||||
closing speed, LOS rate). The motion window (`WinCap = 8`) is fed from
|
||||
`modules/radar.nim`'s new scan buffer. One definition is shared verbatim by the
|
||||
trainer and the gun.
|
||||
|
||||
### Training (`train_predict.nim`, 5 round-group folds, pooled held-out)
|
||||
|
||||
| model | RMSE | MAE | medAE | predMean |
|
||||
|---|---|---|---|---|
|
||||
| plain intercept (correction 0) | 0.31983 | 0.23227 | 0.15208 | 0 |
|
||||
| constant mean | 0.31978 | 0.23219 | 0.15173 | -0.00577 |
|
||||
| ELM H=256 λ=1e-2 | **0.23101** | **0.18174** | **0.14966** | **-0.00631** |
|
||||
| ELM H=128 λ=1e-2 | 0.23934 | 0.19052 | 0.15856 | -0.00630 |
|
||||
|
||||
The ELM beats the plain intercept by **-27.8% RMSE** and beats a constant by the
|
||||
same. Its output mean is **-0.0063 rad (-0.36°)** — the centring failure of the
|
||||
old model is gone. (H=64: RMSE 0.24895.)
|
||||
|
||||
### Confidence gate
|
||||
|
||||
The correction is applied only when `|correction| > GATE_TAU`, else the gun
|
||||
returns the exact geometric aim. `GATE_TAU = 0.10 rad (5.7°)`, the largest
|
||||
0.05-grid threshold whose gated RMSE stays within 2% of the un-gated model:
|
||||
**open-rate 61.4%**, gated RMSE 0.23348 (-27.0% vs the intercept). The offline
|
||||
optimum is `tau = 0` (the model helps at every magnitude), so the gate is a
|
||||
conservative filter for the small/noisy corrections, not a performance device.
|
||||
|
||||
### Real battles — 20 rounds each, RawRecord observer ground truth, all CONTIGUOUS
|
||||
|
||||
Damage per round is primary. `won/rd` = rounds where DevControlBot ranked first.
|
||||
|
||||
| opponent | gun | shots | hits | hit% | damage | dmg/rd | won/rd |
|
||||
|---|---|---|---|---|---|---|---|
|
||||
| DrussGT | geometric | 547 | 6 | 1.1% | 60 | 3.0 | 0/20 |
|
||||
| DrussGT | ELM-gated | 656 | 16 | 2.4% | 160 | **8.0** | 0/20 |
|
||||
| CassiusClay | geometric | 357 | 1 | 0.3% | 10 | 0.5 | 0/20 |
|
||||
| CassiusClay | ELM-gated | 427 | 14 | 3.3% | 140 | **7.0** | 0/20 |
|
||||
| Komarious | geometric | 356 | 29 | 8.1% | 290 | 14.5 | 0/20 |
|
||||
| Komarious | ELM-gated | 550 | 95 | 17.3% | 950 | **47.5** | **10/20** |
|
||||
| Phoenix | geometric | 350 | 3 | 0.9% | 30 | 1.5 | 0/20 |
|
||||
| Phoenix | ELM-gated | 447 | 8 | 1.8% | 80 | **4.0** | 0/20 |
|
||||
|
||||
Damage per *shot* also rises (DrussGT 0.11 -> 0.24, Komarious 0.81 -> 1.73):
|
||||
the gain is accuracy, not just volume.
|
||||
|
||||
Aim error at fire versus the **TRUE impact point** (deg; reference solved against
|
||||
the observer's true trajectory, so lead is not counted as error — this is not the
|
||||
constant-velocity reference printed by `score_battles.py`):
|
||||
|
||||
| opponent | gun | n | mean | median | sd |
|
||||
|---|---|---|---|---|---|
|
||||
| DrussGT | geometric | 542 | -2.56 | -12.13 | 33.96 |
|
||||
| DrussGT | ELM-gated | 647 | -4.27 | -9.24 | **23.39** |
|
||||
| CassiusClay | geometric | 341 | -1.70 | -3.56 | 40.64 |
|
||||
| CassiusClay | ELM-gated | 413 | -2.96 | -6.20 | **29.11** |
|
||||
| Komarious | geometric | 352 | 0.62 | -1.61 | 23.49 |
|
||||
| Komarious | ELM-gated | 529 | -2.66 | -2.05 | **12.96** |
|
||||
| Phoenix | geometric | 331 | 0.92 | 5.38 | 41.55 |
|
||||
| Phoenix | ELM-gated | 426 | -1.91 | 0.10 | **28.38** |
|
||||
|
||||
The ELM cuts the aim-error spread by 30-45% against every opponent.
|
||||
|
||||
### Bottom line
|
||||
|
||||
**Yes — with the movement label the ELM now beats the plain intercept in real
|
||||
battles**, on damage/round against all four opponents (2.7x..14x), with a higher
|
||||
hit rate, more damage per shot, one 10/20 round win vs Komarious, and a
|
||||
30-45% smaller true-impact aim-error spread. The old model's fatal constant bias
|
||||
is gone (predMean -0.36° vs +2.4°).
|
||||
|
||||
Honest caveats: these four opponents are *in* the pooled training corpus (same
|
||||
bots, different recording), so this is not a held-out-opponent generalisation
|
||||
claim; the absolute hit rate against DrussGT is still only 2.4%. The gain is
|
||||
real but the gun is still weak.
|
||||
|
||||
### Files and reproduction
|
||||
|
||||
| file | what |
|
||||
|---|---|
|
||||
| `DevControlBot/modules/ELM/elm_features.nim` | THE feature vector — one definition, used by both the trainer and the gun. 18 features, all derived from our own state + scan history. |
|
||||
| `DevControlBot/modules/ELM/elm_gun.nim` | the gun: weights loader, `heuristicAim` (the baseline), `predictAim` |
|
||||
| `DevControlBot/modules/ELM/elm_gun_weights.txt` | exported weights, plain text, format documented at the top of `elm_gun.nim` |
|
||||
| `DevControlBot/modules/radar.nim` | scan history added: previous scan, tick, and our own position at scan time (needed for velocity / age / displacement) |
|
||||
| `tools/elm_bench/train_elm_gun.nim` | trains on the pooled 9-opponent corpus, exports the weights |
|
||||
| `tools/elm_bench/score_battles.py` | scores recordings from the OBSERVER ground truth |
|
||||
|
||||
### Reproduce
|
||||
| `DevControlBot/modules/ELM/elm_features.nim` | the 32-D enemy-motion feature vector (shared trainer/gun) |
|
||||
| `DevControlBot/modules/ELM/elm_gun.nim` | geometric `heuristicAim`, ELM correction, gate, weights loader |
|
||||
| `DevControlBot/modules/radar.nim` | scan buffer (window of the last 8 scans) |
|
||||
| `DevControlBot/modules/ELM/elm_gun_weights.txt` | exported weights + `GATE_TAU` |
|
||||
| `tools/elm_bench/extract_predict.py` | relabel: true-impact correction corpus |
|
||||
| `tools/elm_bench/train_predict.nim` | 5-fold eval, baselines, gate calibration, export |
|
||||
|
||||
```sh
|
||||
# 1. train + export (~1.5 s)
|
||||
cd DevControlBot_garage/DevControlBot
|
||||
nim c -r --path:. ../../tools/elm_bench/train_elm_gun.nim
|
||||
|
||||
# 2. build the two A/B guns (the define switches the learned term off)
|
||||
nim c -d:release -o /tmp/DevElm DevControlBot.nim # ELM gun
|
||||
nim c -d:release -d:baselineGun -o /tmp/DevBase DevControlBot.nim # geometric only
|
||||
|
||||
# 3. battles (a bot dir needs <Name>.json + <Name>.sh + the binary, all
|
||||
# named after the DIRECTORY; RawRecord needs its --dir to exist)
|
||||
cd tools/raw_recorder
|
||||
JAR=.../robocode-tankroyale-runner.jar
|
||||
java -cp out:$JAR RawRecord --dir /tmp/rec_Elm_DrussGT --rounds 20 \
|
||||
--bot /tmp/bots/DevElm --bot /tmp/tr_bots/DrussGT
|
||||
|
||||
# 4. score from the recording's ground truth
|
||||
python3 tools/elm_bench/score_battles.py /tmp/rec_*
|
||||
python3 ../../tools/elm_bench/extract_predict.py /tmp/samples_predict.ndjson \
|
||||
../../data --stride=5
|
||||
nim c -r --hints:off --path:. ../../tools/elm_bench/train_predict.nim /tmp/samples_predict.ndjson
|
||||
nim c -d:release --out:/tmp/DevElm DevControlBot.nim # ELM-gated gun
|
||||
nim c -d:release -d:baselineGun --out:/tmp/DevBase DevControlBot.nim # intercept only
|
||||
# battles: java -cp tools/raw_recorder/out:$RUNNER_JAR RawRecord \
|
||||
# --dir /tmp/rec --rounds 20 --bot <DevElm dir> --bot <opponent dir>
|
||||
python3 tools/elm_bench/score_battles.py /tmp/rec_Elm_DrussGT /tmp/rec_Base_DrussGT
|
||||
```
|
||||
|
||||
### The caveat that was known before the first battle
|
||||
|
||||
The offline benchmark this model comes from beats a constant baseline by -62.6%
|
||||
RMSE, but **one single linear feature, `beliefPredErr`, gives -61.1% of it**: the
|
||||
whole ELM buys ~1.5 points over one fitted coefficient. The fit law is
|
||||
`label ~= 0.87 * beliefPredErr + 0.042 rad`, i.e. "the opponent applies ~13% of
|
||||
the lead it should". It also collapses under strictly-prior scans (97.3% of the
|
||||
corpus shots scan on the tick they fire). Expect a small effect, not a good gun.
|
||||
|
||||
### RESULTS — ELM gun vs geometric baseline, 20 real rounds each
|
||||
|
||||
Hit rate from `BulletFiredEvent` / `BulletHitBotEvent` in the RawRecord streams
|
||||
(observer ground truth, every recording verified CONTIGUOUS). A bullet still in
|
||||
flight at round end counts as a miss.
|
||||
|
||||
| opponent | gun | shots | hits | hit % | bullet damage |
|
||||
|---|---|---|---|---|---|
|
||||
| DrussGT | ELM | 324 | 8 | **2.5%** | 80 |
|
||||
| DrussGT | geometric | 345 | 6 | **1.7%** | 60 |
|
||||
| CassiusClay | ELM | 102 | 1 | **1.0%** | 10 |
|
||||
| CassiusClay | geometric | 49 | 4 | **8.2%** | 40 |
|
||||
| Komarious | ELM | 121 | 5 | **4.1%** | 50 |
|
||||
| Komarious | geometric | 147 | 12 | **8.2%** | 120 |
|
||||
| Phoenix | ELM | 143 | 73 | **51.0%** | 730 |
|
||||
| Phoenix | geometric | 185 | 184 | **99.5%** | 1840 |
|
||||
|
||||
### Verdict: no, it does not beat the heuristic. It loses, badly.
|
||||
|
||||
Three of four opponents get worse. The fourth (Phoenix, near-stationary) is the
|
||||
tell: against a target that barely moves, the geometric intercept is already
|
||||
almost perfect, and the model's fitted **+0.042 rad bias alone** pushes a
|
||||
near-miss over the edge — 99.5% -> 51.0%. That is the whole model doing exactly
|
||||
what it was fitted to do (the in-game median correction is 0.046 rad, matching
|
||||
the fitted intercept), and it is still the wrong thing to do: the corpus's
|
||||
"the opponent under-leads" habit belongs to legacy bots that lead imperfectly,
|
||||
and it is a *bias*, not an advantage. Against an opponent that leads correctly
|
||||
the correction is a pure error.
|
||||
|
||||
The one case where ELM wins (DrussGT, 2.5% vs 1.7%) is a single extra hit out of
|
||||
~330 shots: noise, n too small to claim.
|
||||
|
||||
### Two bugs found by measuring (both were invisible offline)
|
||||
|
||||
1. **Feature-domain shift.** `scanAge`, `logScan`, `disp` and `gunHeat` are
|
||||
structurally out of domain in game (the corpus is 97% same-tick shots by
|
||||
slowly-moving legacy bots with a warm gun; this bot fires when its gun is
|
||||
COOLED, 2-10 ticks after a scan, at up to 8 px/tick). With them the tanh
|
||||
model extrapolated and 95% of in-game shots returned the clamp, spraying
|
||||
bullets into walls. They were cut; the measured ranges are tabulated in
|
||||
`elm_features.nim`. This is a real cost, not a free win: the model is now
|
||||
blind to how stale its belief is.
|
||||
2. **Units.** The API speaks degrees and the model speaks radians. Mixing them
|
||||
made the gun fire at random; the conversions are now explicit in
|
||||
`DevControlBot.nim`.
|
||||
|
||||
Also worth knowing: `bulletSpeed` in the corpus is `power * 10`, NOT the physical
|
||||
`20 - 3*power` (an artifact of `extract_realistic.py`). The gun reproduces the
|
||||
corpus convention verbatim in the *feature*, and uses the physical speed in the
|
||||
geometric intercept.
|
||||
|
||||
### Calibration — not measured, and here is why
|
||||
|
||||
`elm_gun` exposes `lastCorrection` and the bot can log one line per shot
|
||||
(`-d:gunLog`, `ELM_GUN_LOG=file.csv`). The *truth* has to come from the
|
||||
recording, and the join failed: `bot.tick` counts loop iterations (~15.5k in a
|
||||
round the observer numbers to 2.8k), so a shot's tick cannot be mapped to an
|
||||
observer `turnNumber` without a per-round ratio that was not worth measuring.
|
||||
What IS known: the in-game correction distribution is healthy (mean 0.030 rad,
|
||||
median 0.046, 1% at the clamp), i.e. the model is operating in the regime it was
|
||||
fitted in and is not saturating — it is simply wrong for this opponent class.
|
||||
|
||||
### Gun plumbing diagnosis — the gun, isolated from the ELM
|
||||
|
||||
Run against a **stationary** opponent (`SittingDuck`, 10 rounds each, `RawRecord`,
|
||||
observer ground truth), geometric baseline (`-d:baselineGun`) vs the default ELM:
|
||||
|
||||
| build | shots | hits | hit% | bullet damage |
|
||||
|---|---|---|---|---|
|
||||
| geometric baseline | 112 | 110 | **98.2%** | 1100 |
|
||||
| default (ELM) | 197 | 103 | **52.3%** | 1030 |
|
||||
|
||||
Aim error at fire = observer `BulletFiredEvent.direction` minus the bearing to the
|
||||
target at fire time:
|
||||
|
||||
| gun | n | mean | median | sd | ≤1° | ≤5° | ≤10° |
|
||||
|---|---|---|---|---|---|---|---|
|
||||
| geometric | 112 | −0.00° | 0.00° | 0.04° | 100% | 100% | 100% |
|
||||
| ELM | 197 | +0.23° | +1.61° | 5.82° | 12% | 60% | 93% |
|
||||
|
||||
**The geometric plumbing is correct.** The bearing is absolute and in the same
|
||||
frame as `directionTo` (0°=east, positive=CCW);
|
||||
`setGunTurnRate(normalizeRelativeAngle(aim − getGunDirection()))` is the right
|
||||
idiom for `setAdjustGunForBodyTurn(true)` — the same one every working adversary
|
||||
uses. Against a stationary target the baseline lands every shot within 0.05° of
|
||||
the true bearing. **The default `UseElm` build is what misses**: its learned
|
||||
correction is a ~+0.03 rad bias plus noise (median +1.6°, sd 5.8°) applied on top
|
||||
of a shot that was already perfect. `-d:baselineGun` hits.
|
||||
|
||||
Two real plumbing bugs surfaced while measuring:
|
||||
|
||||
1. **Blocking fire.** `computeGun` called `fire()` (the blocking variant: it calls
|
||||
`go()` itself) and then the run loop called `go()` again — every shot double-
|
||||
stepped the server and desynced `bot.tick` from the observer's turn counter.
|
||||
Replaced with `discard setFire(...)` and the loop's single `go()`.
|
||||
2. **Degrees vs radians.** `abs(err) <= degToRad(FireSlopDeg)` compared a degrees
|
||||
value against 0.052°, not 3° — the gun withheld fire long after it was on
|
||||
target. Now compared in degrees. The 3° slop also fires mid-slew (up to 3°
|
||||
off, sd 1.65°); reduced to 1° (sd 0.53°, 110 hits, damage unchanged).
|
||||
|
||||
Feature-domain check (`-d:gunLog`): the four structurally out-of-domain features
|
||||
from job-42 (`scanAge`, `logScan`, `disp`, `gunHeat`) are **gone** from the
|
||||
18-vector. Against a stationary target many remaining features are constant by
|
||||
construction (`tgtSpeed`, `tgtLatVel`, `closing`, `losRate` = 0; `scanGap` = 1/30;
|
||||
`bulletSpd` = 0.7; `power` = 2/3; `predErr` = 0) — expected for a stationary
|
||||
target, not a bug. One thing to watch: `elmCorrection` rebuilds `bulletSpeed` as
|
||||
`power*10` for the feature (the corpus convention), while the geometric intercept
|
||||
uses the physical `20 − 3*power`; the corpus does the same, so the feature is
|
||||
consistent, but the two lead calculations are not the same number.
|
||||
|
||||
Reference in New Issue
Block a user