README: honest online-learning-gun results

This commit is contained in:
2026-10-06 19:37:38 +02:00
parent 1c7083ab75
commit 6bec6a41cd
+144
View File
@@ -557,3 +557,147 @@ result this clean says the current ELM should not be shipped as an improvement;
the next step is to make the gate/correction honest on non-moving targets before
re-measuring held-out opponents. 10 rounds per bot means the ~±4 dmg/rd rows are
noisy — but no amount of extra rounds turns a median of 0 into a win.
## Online-learning gun
The previous section is the null result that motivates this one: the
**pretrained** cross-opponent model does not generalise (4/13 held-out wins,
aggregate 1.8% worse). This section is the paradigm change it points to —
**learn the opponent in front of you during the battle**, and reset only when
the opponent changes.
### Reset event: `GameStartedEventForBot`, never `RoundStartedEvent`
In Tank Royale a **battle is many rounds against the same opponent**, and that
opponent's movement code does not change between rounds. The bot API delivers
both events distinctly (`onGameStarted` / `onRoundStarted`,
`schemas.nim:202/212`), and the API starts `run()` on the first
`RoundStartedEvent` while firing `onGameStarted` once per battle before it. So
`DevControlBot.onGameStarted` calls `resetOnline()` and `onRoundStarted` does
**nothing**: the model, the pending queue and the gate persist across every
round of a battle and are wiped exactly once, at the start of a new opponent.
(The mean online sample count grows 36 → 642 across the round buckets below,
which is the runtime proof the reset is per-battle, not per-round.)
### Online learning with delayed labels
The label is the same validated one (`wrap(trueImpactBearing -
geometricInterceptBearing)`, `extract_predict.py`). At fire time the true impact
position does not exist yet — it is only knowable once the bullet's flight time
has elapsed. So at every shot the bot enqueues a `PendingShot(feat, base,
selfX, selfY, dueTick = fireTick + round(range / bulletSpeed))`. Each tick
`drainPending(nowTick)` resolves every shot with `dueTick <= nowTick`: it reads
the enemy's **actual** position from a rolling per-tick enemy trace (kept in
`elm_gun.noteEnemyAt`, the nearest trace sample within 3 ticks), computes
`label = wrap(bearing(selfFire -> truePos) - base)`, and feeds one sample to the
ELM (the engine's `decay=0.999`, `refitEvery=25`, the tuning the earlier online
work validated). The queue is keyed by `dueTick`; nothing is trained before the
bullet would have arrived, so there is no future leakage.
### Neutral vs weak prior
Both start from the **same frozen hidden layer** (the pretrained `W`/`hs`); they
differ only in the output layer at `GameStartedEventForBot`:
* **neutral** (`priorScale = 0`, the default): zero `beta`, learns purely online.
* **weak prior** (`-d:elmPrior`): the pooled pretrained `beta`, scaled by 0.1.
Over the 9 held-out sample bots they are **tied within noise**: neutral 88.3
dmg/rd, weak-prior 88.9 (base 70.1). The weak prior is a touch better on the
hard movers (MyFirstLeader 103.5 vs 95.5, MyFirstBot 83.0 vs 76.5) and a touch
worse on the near-stationary ones (VelociBot 83.5 vs 87.0, Walls 68.0 vs 74.0).
Neither is a clear winner; the default is **neutral**, because starting from a
prior that was shown not to generalise risks importing exactly the bias the gate
is meant to catch, and the neutral model reaches the same place within a round.
### Novelty gate: never worse than the intercept by construction
The correction is applied only if **all** of these hold, else the gun returns the
exact geometric aim:
1. `nOnline >= OnlineMinSamples (12)` — no signal at all until the model has seen
real samples for *this* opponent;
2. `modelErr < 0.95 * zeroErr` — the model's **held-out-style** error must beat
what always aiming at the intercept costs. `feedSample` scores the *current*
model against each new label **before** training on it (EMA α = 0.08), so
`modelErr` is a genuine online held-out error and `zeroErr` is the
zero-correction baseline;
3. `|correction| > GATE_TAU (0.10 rad)` — the existing magnitude filter.
`modelErr` only starts moving after the first closed-form refit (25 samples), so
the gate opens conservatively. Measured open-rate (shots where a nonzero
correction was applied), logged with `-d:gunLog`:
| arm | r1–2 | r3–4 | r5–10 | r11–15 | r16–20 |
|---|---:|---:|---:|---:|---:|
| Online open-rate | 36% | 60% | 69% | 68% | 72% |
| Online mean samples | 36 | 110 | 271 | 449 | 642 |
| Prior open-rate | 58% | 64% | 62% | 62% | 56% |
The gate **opens later in the battle** — that is the online learning becoming
genuine signal for the opponent in front of it.
### Damage per round — 20 rounds each, RawRecord ground truth, all CONTIGUOUS
36 recordings (12 opponents × 3 arms), each `query.py --verify` = CONTIGUOUS.
Arms: **Base** = `-d:baselineGun` (pure intercept), **Online** = default
(neutral, online), **Prior** = `-d:elmPrior`. `Δ` is Online − Base.
| opponent | kind | Base | Online | Prior | Δ Online |
|---|---|---:|---:|---:|---:|
| Corners | held-out | 94.0 | 96.5 | 96.0 | +2.5 |
| Crazy | held-out | 58.0 | 72.5 | 76.0 | **+14.5** |
| MyFirstBot | held-out | 49.0 | 76.5 | 83.0 | **+27.5** |
| MyFirstLeader | held-out | 25.0 | 95.5 | 103.5 | **+70.5** |
| SpinBot | held-out | 36.0 | 91.0 | 85.5 | **+55.0** |
| Target | held-out | 103.5 | 102.5 | 103.5 | −1.0 |
| TrackFire | held-out | 99.0 | 99.5 | 101.5 | +0.5 |
| VelociBot | held-out | 95.5 | 87.0 | 83.5 | −8.5 |
| Walls | held-out | 71.0 | 74.0 | 68.0 | +3.0 |
| Komarious | legacy | 21.0 | 33.0 | 27.0 | **+12.0** |
| Phoenix | legacy | 3.5 | 7.5 | 7.0 | +4.0 |
| DrussGT | legacy | 5.5 | 10.5 | 16.5 | +5.0 |
Pooled: held-out 9 bots **70.1 → 88.3 dmg/rd (+26%)**, hit rate 23.5% → 33.0%;
legacy 3 bots **10.0 → 17.0 (+70%)**, 4.6% → 6.6%. Online wins **7/9 held-out**,
loses 2 (Target −1.0, VelociBot −8.5 — both already near the saturation ceiling
at ~95–103 dmg/rd). Note this is a **single 20-round recording per pair**, so the
± few-dmg rows are noisy; the large rows are not.
### The round-index curve — does it improve WITHIN a battle?
Pooled damage/round by round index (clean, log-free builds):
| arm | r1–2 | r3–4 | r5–10 | r11–15 | r16–20 |
|---|---:|---:|---:|---:|---:|
| Base | 65.9 | 74.7 | 67.8 | 69.8 | 65.4 |
| Online | 71.7 | 84.8 | 82.3 | 75.5 | 75.3 |
| Δ Online | +5.8 | +10.0 | +14.6 | +5.7 | +9.9 |
| Δ Prior | +1.4 | +8.8 | +12.5 | +10.2 | +7.6 |
**Yes, the gain grows with rounds seen**, peaking at +14.6 dmg/rd in rounds 5–10
while the gate open-rate climbs 36% → 72% and the model accumulates 36 → 642
samples for the same opponent. The r11–20 rows are noisier (bots die faster once
the gun lands, shortening rounds), but the direction is exactly the online
paradigm working.
### Bottom line
**Yes — online learning beats the plain geometric intercept on opponents it has
never seen.** On 9 held-out Nim sample bots it is +26% aggregate damage/round
(88.3 vs 70.1), higher hit rate (33.0% vs 23.5%), and the advantage grows across
rounds of the same battle (+5.8 → +14.6 dmg/rd as the gate opens). This is the
opposite of the pretrained model's null/negative result above: the model that
does **not** generalise from a corpus does fine when it trains on the opponent
in front of it. Two honest caveats: (1) the two losing rows are opponents the
intercept already kills at ~100 dmg/rd, so there is little headroom and the
losses are within noise; (2) neutral and weak-prior starts are statistically
indistinguishable here, so the win is the **online adaptation**, not the prior.
Reproduce:
```sh
# build the three arms (Base / Online / Prior)
bash tools/elm_bench/run_online_battles.sh Corners Crazy ... Walls Komarious Phoenix DrussGT
python3 tools/elm_bench/analyse_online.py /tmp/gun_exp_clean
```