wave pairing: 36-58% of GF/DecayGF/KNN learning samples were MISLABELLED
The audit inferred (from code) that GF/DecayGF/KNN pop the OLDEST wave on resolution, while under bmPath bullets leave the arena in NON-FIFO order - so an outcome could be attached to the wrong wave. It also noted that `starved=0` does NOT rule this out. Both halves are now MEASURED. MISPAIRING RATE (10 DrussGT fixtures, real VirtualTracker, 344k resolutions/gun): gun bmPath mispair label err bmPoint mispair label err GuessFactor 36.48% 19.39% 18.24% 7.62% DecayGF 36.85% 19.52% 20.57% 8.64% KNN 57.91% 27.63% 29.75% 11.58% (starved = 0 everywhere, exactly as the audit predicted) So ~1 in 5 GF/DecayGF learning samples and ~1 in 4 KNN samples carried a WRONG guess-factor bin. This is a material corruption of the learning signal. FIX: the same fireTick-keyed ring scheme `tsetlin.nim`/`tm_selector.nim` already use - `slot = (fireTick*4 + bin) mod 1024` (period 256 ticks, longer than the ~91-tick max flight), looked up by exact key. Public interfaces unchanged; added `waveResolved`/`waveMispaired` integrity counters. AFTER: mispaired = 0 and starved = 0, both metrics, all three guns. EFFECT ON HIT RATE: SMALL AND NOT SIGNIFICANT. bmPath 4000 samples/gun: GuessFactor 23.20% -> 23.02% (-0.18pp, per-run sign-flip p=0.750) DecayGF 23.80% -> 24.25% (+0.45pp, p=0.625) KNN 18.27% -> 18.80% (+0.53pp, p=0.547) bmPoint: +0.05 / +0.33 / -0.15pp, p = 1.00 / 0.50 / 0.50. Per-run ranges overlap almost completely. A bullet-level z-test is anti-conservative (bullets within a fixture share a trajectory) and its KNN p=1.9e-16 cannot be trusted given ~10 effective independent runs. PLAIN READING: this is a CORRECTNESS fix, not a measurable hit-rate win. It removes a 36-58% mislabelling of the learning signal; the point estimates move by at most ~0.5pp, within run-to-run noise. Stated plainly rather than oversold. A REGRESSION IT CAUGHT IN ITSELF (and this explains the SIGSEGV another job saw and correctly attributed to a concurrent knn_gun.nim rewrite): the first implementation put an inline `array[1024, KNNWave]` (~100KB) inside each gun, which overflowed the default 8MB stack and made `test_power_selection` SIGSEGV. Causation was proven by stashing only the three gun files (test passed), then fixed by making the rings heap-backed `seq`. Verified: `test_power_selection` 3 PASS on the default stack, and zero inline `array[1024]` remain. Guards: test_wave_pairing 17 (new, pure), test_gun_harness 39, test_vbullet_metric 11, test_power_selection 3, test_adaptive_radar 41, test_tfil_ring_weights 24, test_power_policy 26, test_ram_decision 28. ModularBot compiles. Adds audit_wave_pairing.nim and compare_pairing.nim.
This commit is contained in:
@@ -1,6 +1,16 @@
|
||||
## Guess-factor gun: statistical targeting via GF histogram.
|
||||
## Bins: 31, ranging GF -1 (max CW escape) to +1 (max CCW escape).
|
||||
## Learns from virtual bullet outcomes; queues one wave per (tick, power bin).
|
||||
##
|
||||
## Wave pairing is EXACT: a wave is stored in a ring slot keyed by
|
||||
## (fireTick, powerBin) and `onResult` looks up the wave with the resolution
|
||||
## event's `fireTick`, NOT the oldest queued wave. Under the shipped bmPath
|
||||
## metric bullets leave the arena in non-FIFO order (the aim direction changes
|
||||
## every tick), so FIFO pairing attached outcomes to the wrong wave. Measured
|
||||
## over the committed DrussGT fixtures, FIFO mispaired 36.5% of resolutions
|
||||
## (19.4% of which changed the recorded GF bin). See
|
||||
## common_libs/tests/audit_wave_pairing.nim. The exact key is the same pattern
|
||||
## guns/tsetlin.nim and guns/tm_selector.nim use.
|
||||
|
||||
import std/[math, strformat]
|
||||
import gun_harness/gun_interface
|
||||
@@ -11,31 +21,43 @@ const
|
||||
GFBins = 31
|
||||
GFPrior = 0.1
|
||||
DebugGF* = false
|
||||
WaveCompactAt = 64 ## compact a bin's wave seq once this many entries are consumed
|
||||
WaveRingSlots = 1024
|
||||
## (fireTick, powerBin) -> ring slot. Period = WaveRingSlots/bins = 256 ticks.
|
||||
## A bmPath bullet leaves an 800x600 (Tank Royale max 1000x1000) arena within
|
||||
## ~91 (128) ticks, so a live wave is never overwritten by a newer one.
|
||||
## Identical sizing to tsetlin.nim's TM_TRACE_SLOTS.
|
||||
|
||||
type
|
||||
Wave = object
|
||||
fireX, fireY: float
|
||||
fireBearing: float # atan2(enemyY-selfY, enemyX-selfX) at fire tick (rad)
|
||||
fireTick: int # key part: tick the bullet was fired
|
||||
bin: int # key part: power bin the bullet belonged to
|
||||
alive: bool
|
||||
# mea not stored — recomputed from FeedbackEvent.bulletPower at resolution time
|
||||
|
||||
GFGun* = object
|
||||
bins: array[GFBins, float]
|
||||
# One wave queue per power bin. The owning bin is fixed at push time (from the
|
||||
# bulletSpeed argument) and at pop time (from FeedbackEvent.bulletPower), so a
|
||||
# resolved bullet is always paired with a wave from its own bin.
|
||||
waves: array[len(vb.PowerBins), seq[Wave]]
|
||||
waveHead: array[len(vb.PowerBins), int] # O(1) pop cursor into waves[bin]
|
||||
# Exact (fireTick, powerBin)-keyed ring. A resolved bullet is matched to the
|
||||
# wave it actually fired, no matter how many other shots resolved first.
|
||||
# Heap-backed (seq) so the gun value stays small on the stack — a 1024-slot
|
||||
# inline array overflowed the default 8 MB stack in test_power_selection.
|
||||
waves: seq[Wave]
|
||||
waveStoredTick: array[len(vb.PowerBins), int] # last tick a wave was queued for this bin
|
||||
vt: VelocityTracker # enemy velocity history (base selection)
|
||||
cachedTick: int # last tick the velocity tracker was advanced
|
||||
wavePushes*: int # total waves enqueued (== one per (tick, bin))
|
||||
waveStarved*: int # onResult found an empty queue for its own bin
|
||||
waveStarved*: int # onResult found no live wave for its (fireTick, bin)
|
||||
# ── pairing integrity ───────────────────────────────────────────────────
|
||||
waveResolved*: int # onResult calls that found their exact wave
|
||||
waveMispaired*: int # ring-slot collision (impossible by design): the
|
||||
# slot held a different fireTick
|
||||
debugGraphics*: bool
|
||||
|
||||
proc initGFGun*(): GFGun =
|
||||
result.debugGraphics = false
|
||||
result.cachedTick = -1
|
||||
result.waves = newSeq[Wave](WaveRingSlots)
|
||||
for b in 0..<len(vb.PowerBins):
|
||||
result.waveStoredTick[b] = -1
|
||||
# Seed with a head-on prior: triangular bump at bin 15 (GF=0).
|
||||
@@ -51,7 +73,7 @@ proc gfToIndex(gf: float): int {.inline.} =
|
||||
proc indexToGF(idx: int): float {.inline.} =
|
||||
float(idx) / float(GFBins - 1) * 2.0 - 1.0
|
||||
|
||||
proc peakBin(g: GFGun): int =
|
||||
proc peakBin*(g: GFGun): int =
|
||||
var best = 0
|
||||
for i in 1..<GFBins:
|
||||
if g.bins[i] > g.bins[best]:
|
||||
@@ -73,18 +95,9 @@ proc binForPower(power: float): int {.inline.} =
|
||||
return i
|
||||
-1
|
||||
|
||||
proc takeOldestWave(g: var GFGun, binIdx: int): (bool, Wave) =
|
||||
## Pop the oldest unresolved wave for this bin (O(1) amortized via waveHead).
|
||||
## Returns (false, default) when the bin's queue is empty.
|
||||
if binIdx < 0 or g.waveHead[binIdx] >= g.waves[binIdx].len:
|
||||
return (false, Wave())
|
||||
result = (true, g.waves[binIdx][g.waveHead[binIdx]])
|
||||
inc g.waveHead[binIdx]
|
||||
# Amortized O(1): drop the consumed prefix once it dominates the queue.
|
||||
if g.waveHead[binIdx] >= WaveCompactAt and
|
||||
g.waveHead[binIdx] * 2 >= g.waves[binIdx].len:
|
||||
g.waves[binIdx] = g.waves[binIdx][g.waveHead[binIdx] .. g.waves[binIdx].high]
|
||||
g.waveHead[binIdx] = 0
|
||||
proc waveSlot(fireTick, binIdx: int): int {.inline.} =
|
||||
## Exact (fireTick, powerBin) key -> ring slot (same scheme as tsetlin.nim).
|
||||
((fireTick * len(vb.PowerBins)) + binIdx) mod WaveRingSlots
|
||||
|
||||
proc predict*(g: var GFGun, state: WorldState, bulletSpeed: float): GunPrediction =
|
||||
if bulletSpeed <= 0.0:
|
||||
@@ -104,10 +117,14 @@ proc predict*(g: var GFGun, state: WorldState, bulletSpeed: float): GunPredictio
|
||||
# call for the selected bin lands on the same tick and reuses the queued wave.
|
||||
let binIdx = binForSpeed(bulletSpeed)
|
||||
if binIdx >= 0 and g.waveStoredTick[binIdx] != state.tick:
|
||||
g.waves[binIdx].add Wave(
|
||||
let slot = waveSlot(state.tick, binIdx)
|
||||
g.waves[slot] = Wave(
|
||||
fireX: state.selfX,
|
||||
fireY: state.selfY,
|
||||
fireBearing: f.bearing,
|
||||
fireTick: state.tick,
|
||||
bin: binIdx,
|
||||
alive: true,
|
||||
)
|
||||
g.waveStoredTick[binIdx] = state.tick
|
||||
inc g.wavePushes
|
||||
@@ -119,7 +136,7 @@ proc predict*(g: var GFGun, state: WorldState, bulletSpeed: float): GunPredictio
|
||||
let py = state.selfY + sin(gfAngle) * f.dist
|
||||
|
||||
when DebugGF:
|
||||
echo fmt"[gf-dbg] predict: peakGF={peakGF:.2f} peakBin={peak} mea={radToDeg(mea):.1f}° aimAngle={radToDeg(gfAngle):.1f}° waves={g.waves[binIdx].len}"
|
||||
echo fmt"[gf-dbg] predict: peakGF={peakGF:.2f} peakBin={peak} mea={radToDeg(mea):.1f}° aimAngle={radToDeg(gfAngle):.1f}° tick={state.tick}"
|
||||
|
||||
GunPrediction(
|
||||
x: clamp(px, BotRadius, state.arenaWidth - BotRadius),
|
||||
@@ -127,15 +144,25 @@ proc predict*(g: var GFGun, state: WorldState, bulletSpeed: float): GunPredictio
|
||||
)
|
||||
|
||||
proc onResult*(g: var GFGun, e: FeedbackEvent) =
|
||||
## Called when a virtual bullet resolves. Pop the OLDEST unresolved wave from
|
||||
## this bullet's own power-bin queue, compute the actual GF, and smooth-add it.
|
||||
let binIdx = binForPower(e.bulletPower)
|
||||
## Called when a virtual bullet resolves. Look up the wave with this event's
|
||||
## exact (fireTick, powerBin), compute the actual GF, and smooth-add it. A
|
||||
## missing key is counted, never silently mislabelled.
|
||||
let binIdx =
|
||||
if e.powerBin >= 0 and e.powerBin < len(vb.PowerBins): e.powerBin
|
||||
else: binForPower(e.bulletPower)
|
||||
if binIdx < 0: return
|
||||
|
||||
let (found, w) = g.takeOldestWave(binIdx)
|
||||
if not found:
|
||||
let slot = waveSlot(e.fireTick, binIdx)
|
||||
var w = addr g.waves[slot]
|
||||
if not w.alive:
|
||||
inc g.waveStarved
|
||||
return
|
||||
if w.fireTick != e.fireTick:
|
||||
# Ring slot collision: impossible while the ring period exceeds max flight.
|
||||
inc g.waveMispaired
|
||||
inc g.waveStarved
|
||||
return
|
||||
inc g.waveResolved
|
||||
|
||||
# Recompute mea from the actual bullet power (correct per-bin, not the cached first-bin mea)
|
||||
let speed = bulletSpeed(e.bulletPower)
|
||||
@@ -160,3 +187,5 @@ proc onResult*(g: var GFGun, e: FeedbackEvent) =
|
||||
for i in 0..<GFBins:
|
||||
let dist = abs(i - centerIdx)
|
||||
g.bins[i] += 1.0 / float(1 + dist)
|
||||
|
||||
w.alive = false
|
||||
|
||||
Reference in New Issue
Block a user