0cc682152d
Wave queues (guess_factor, decay_gf, knn_gun): predict() stored ONE wave per tick while onResult() popped one per resolved bullet (~4/tick), so the queue drained to empty within a few dozen ticks, ~3 of every 4 resolutions returned without learning, and the survivor paired with a same-tick wave (bearingDelta ~= 0) pinning the histogram at centre. PROOF: GF.vHits == HeadOn.vHits and DecayGF.vHits == HeadOn.vHits byte-for-byte in every one of 50 rounds — the guns had degenerated to HeadOn. Now each gun keeps a per-bin FIFO with an O(1) head cursor. At most one push per (tick, bin) so the fire site's 5th predict() call is a no-op, and onResult pops the oldest wave of its OWN bin via e.bulletPower. Aiming math untouched (it was already correct: 0 deg = East, CCW+). maxBullets 2048 -> 8192: the rack spawns 52 bullets/tick so the ring wrapped every ~39 ticks while a long power-3 shot needs ~90, silently discarding unresolved bullets and biasing every measured hit rate by range. Added a droppedBullets counter so a future overflow is measurable, and wavePushes/ waveStarved counters on the three guns. After the fix: vDropped = 0 and vStarved = 0 across all 48 recorded rounds. fitnessFor is now exported, deterministic (enemies iterated in ascending id order) and shared by the selector and the stats dump, replacing a hand-rolled merge in ModularBot that never advanced its window head. Round lines gain additive keys: vDropped, vStarved.
136 lines
4.5 KiB
Nim
136 lines
4.5 KiB
Nim
## Recency-weighted GF gun: exponential decay on histogram bins.
|
|
## decay=0.998/tick gives ~350-tick half-life — adapts to mid-battle strategy shifts.
|
|
## Everything else identical to guess_factor.nim, including per-power-bin wave queues.
|
|
|
|
import std/math
|
|
import gun_harness/gun_interface
|
|
import gun_harness/virtual_bullets as vb # PowerBins
|
|
|
|
const
|
|
GFBins = 31
|
|
GFPrior = 0.1
|
|
DecayRate = 0.998 # ponytail: single global decay, tune if adaptation too slow/fast
|
|
DecayWaveCompactAt = 64
|
|
|
|
type
|
|
DWave = object
|
|
fireX, fireY: float
|
|
fireBearing: float
|
|
|
|
DecayGFGun* = object
|
|
bins: array[GFBins, float]
|
|
# One wave queue per power bin; a resolved bullet only learns from a wave
|
|
# queued for its own bin (matched on bulletSpeed / bulletPower).
|
|
waves: array[len(vb.PowerBins), seq[DWave]]
|
|
waveHead: array[len(vb.PowerBins), int] # O(1) pop cursor
|
|
waveStoredTick: array[len(vb.PowerBins), int] # last tick a wave was queued for this bin
|
|
cachedTick: int # last tick bins were decayed
|
|
wavePushes*: int
|
|
waveStarved*: int
|
|
debugGraphics*: bool
|
|
|
|
proc initDecayGFGun*(): DecayGFGun =
|
|
result.cachedTick = -1
|
|
result.debugGraphics = false
|
|
for b in 0..<len(vb.PowerBins):
|
|
result.waveStoredTick[b] = -1
|
|
let center = (GFBins - 1) div 2
|
|
for i in 0..<GFBins:
|
|
let d = abs(i - center)
|
|
result.bins[i] = GFPrior + 0.5 / float(1 + d)
|
|
|
|
proc gfToIndex(gf: float): int {.inline.} =
|
|
clamp(int(round((gf + 1.0) * 0.5 * float(GFBins - 1))), 0, GFBins - 1)
|
|
|
|
proc indexToGF(idx: int): float {.inline.} =
|
|
float(idx) / float(GFBins - 1) * 2.0 - 1.0
|
|
|
|
proc peakBin(g: DecayGFGun): int =
|
|
var best = 0
|
|
for i in 1..<GFBins:
|
|
if g.bins[i] > g.bins[best]:
|
|
best = i
|
|
best
|
|
|
|
proc binForSpeed(spd: float): int {.inline.} =
|
|
for i in 0..<len(vb.PowerBins):
|
|
if abs(spd - bulletSpeed(vb.PowerBins[i])) < 1e-6:
|
|
return i
|
|
-1
|
|
|
|
proc binForPower(power: float): int {.inline.} =
|
|
for i in 0..<len(vb.PowerBins):
|
|
if abs(power - vb.PowerBins[i]) < 1e-6:
|
|
return i
|
|
-1
|
|
|
|
proc takeOldestWave(g: var DecayGFGun, binIdx: int): (bool, DWave) =
|
|
if binIdx < 0 or g.waveHead[binIdx] >= g.waves[binIdx].len:
|
|
return (false, DWave())
|
|
result = (true, g.waves[binIdx][g.waveHead[binIdx]])
|
|
inc g.waveHead[binIdx]
|
|
if g.waveHead[binIdx] >= DecayWaveCompactAt 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 predict*(g: var DecayGFGun, state: WorldState, bulletSpeed: float): GunPrediction =
|
|
if bulletSpeed <= 0.0:
|
|
return GunPrediction(x: state.enemyX, y: state.enemyY)
|
|
|
|
let dx = state.enemyX - state.selfX
|
|
let dy = state.enemyY - state.selfY
|
|
let dist = sqrt(dx*dx + dy*dy)
|
|
let bearing = arctan2(dy, dx)
|
|
let mea = arcsin(clamp(8.0 / bulletSpeed, -1.0, 1.0))
|
|
|
|
if state.tick != g.cachedTick:
|
|
# Decay all bins once per tick
|
|
for i in 0..<GFBins:
|
|
g.bins[i] *= DecayRate
|
|
g.cachedTick = state.tick
|
|
|
|
# Queue at most one wave per (tick, power bin); the fire site's extra predict()
|
|
# 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 DWave(fireX: state.selfX, fireY: state.selfY, fireBearing: bearing)
|
|
g.waveStoredTick[binIdx] = state.tick
|
|
inc g.wavePushes
|
|
|
|
let peak = g.peakBin()
|
|
let peakGF = indexToGF(peak)
|
|
let gfAngle = bearing + peakGF * mea
|
|
let px = state.selfX + cos(gfAngle) * dist
|
|
let py = state.selfY + sin(gfAngle) * dist
|
|
|
|
GunPrediction(
|
|
x: clamp(px, BotRadius, state.arenaWidth - BotRadius),
|
|
y: clamp(py, BotRadius, state.arenaHeight - BotRadius),
|
|
)
|
|
|
|
proc onResult*(g: var DecayGFGun, e: FeedbackEvent) =
|
|
let binIdx = binForPower(e.bulletPower)
|
|
if binIdx < 0: return
|
|
|
|
let (found, w) = g.takeOldestWave(binIdx)
|
|
if not found:
|
|
inc g.waveStarved
|
|
return
|
|
|
|
let speed = bulletSpeed(e.bulletPower)
|
|
let mea = arcsin(clamp(8.0 / speed, -1.0, 1.0))
|
|
let actualDx = e.actualX - w.fireX
|
|
let actualDy = e.actualY - w.fireY
|
|
let actualBearing = arctan2(actualDy, actualDx)
|
|
var bearingDelta = actualBearing - w.fireBearing
|
|
while bearingDelta > PI: bearingDelta -= 2.0*PI
|
|
while bearingDelta < -PI: bearingDelta += 2.0*PI
|
|
|
|
let gf = if mea > 1e-10: clamp(bearingDelta / mea, -1.0, 1.0) else: 0.0
|
|
let centerIdx = gfToIndex(gf)
|
|
|
|
for i in 0..<GFBins:
|
|
let d = abs(i - centerIdx)
|
|
g.bins[i] += 1.0 / float(1 + d)
|