feat(SNNBot): grid accumulator replaces exemplar ring buffer

Ring buffer had amnesia — cycled out all data every 128 ticks,
preventing convergence. Grid accumulator permanently stores average
lead offsets indexed by (v_perp, distance). 136 cells, 1.5 KB.
Knowledge accumulates across rounds → convergence guaranteed for
stationary velocity patterns.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-09-16 23:22:00 +02:00
parent d7e499209c
commit 039b47b151
2 changed files with 62 additions and 113 deletions
+17 -73
View File
@@ -9,7 +9,7 @@
# WAITING → aimTo() each tick; when error < 2° → EVALUATE
# EVALUATE → measure error, compute SuperSpike/reservoir update, log, → DECIDE
import std/[math, random, os, strutils, bitops]
import std/[math, random, os, strutils]
import robocode_tankroyale_botapi
import radar_lock/radar_lock as radar_lock
import reservoir
@@ -201,7 +201,7 @@ type
SNNBot = ref object of Bot
snn: SNN
res: BinaryAimer
res: LeadGrid
phase: Phase
targetAngle: float # SNN output (absolute bearing)
enemyBearing: float # last known enemy bearing
@@ -217,8 +217,7 @@ type
velDirDeg: float64 # velocity direction (degrees) from last scan delta
velSpeed: float64 # speed (units/tick) from last scan delta
lastDecideGunDir: float # gun heading captured at DECIDE time for EVALUATE
lastBinInput: BitVec80 # DECIDE-time binary input, reused in EVALUATE
lastInput: BitVec80 # previous EVALUATE-time input (for change detection)
lastVPerp: float # DECIDE-time vPerp, reused in EVALUATE
lastAbsBearing: float # absolute bearing to enemy captured at DECIDE time
roundTick: int # ticks elapsed in current round (reset each round)
bulletsFired: int # count of bullets fired this round
@@ -301,13 +300,12 @@ method onRoundStarted*(bot: SNNBot, e: RoundStartedEvent) =
bot.lastAbsBearing = 0.0
bot.lastDecideGunDir = 0.0
bot.targetAngle = 0.0
bot.lastInput = [0'u64, 0'u64]
setTargetSpeed(0.0)
setTurnRate(0.0)
method onGameStarted*(bot: SNNBot, e: GameStartedEventForBot) =
initSNN(bot.snn)
bot.res = initBinaryAimer()
bot.res = initLeadGrid()
method onBulletFired*(bot: SNNBot, e: BulletFiredEvent) =
@@ -319,50 +317,6 @@ method onBulletHit*(bot: SNNBot, e: BulletHitBotEvent) =
inc bot.bulletsHit
bot.hitRateEMA = bot.hitRateEMA * 0.85 + 1.0 * 0.15
# ── Reservoir helpers ─────────────────────────────────────────────────────────
proc toBinaryInput(relVelDir: float, velSpeed: float, distance: float, hasVel: bool): BitVec80 =
## Physics-based encoding for lead generalization (issue #152).
## Bits 0-15: v_perp signed thermometer — velSpeed*sin(relVelDir) in [-8,+8].
## Positive (target moves right): bits 0-7; negative (left): bits 8-15.
## +3 sets bits 0,1,2; -3 sets bits 8,9,10. Zero overlap between signs.
## Bits 16-31: v_parallel signed thermometer — velSpeed*cos(relVelDir) in [-8,+8].
## Positive = approaching (less lead needed), negative = receding.
## Bits 32-39: distance thermometer, 8 bands of ~125px in [0,1000].
## Bits 40-47: total speed thermometer, 8 bands of 1 unit/tick.
## Bits 48-79: unused.
result = [0'u64, 0'u64]
template setThermo(baseIdx: int, value: float) =
## Signed thermometer: positive → bits baseIdx..baseIdx+7, negative → bits baseIdx+8..baseIdx+15.
let mag = min(int(abs(value)), 8)
if value >= 0.0:
for b in 0 ..< mag:
let idx = baseIdx + b
result[idx div 64] = result[idx div 64] or (1'u64 shl (idx mod 64))
else:
for b in 0 ..< mag:
let idx = baseIdx + 8 + b
result[idx div 64] = result[idx div 64] or (1'u64 shl (idx mod 64))
if hasVel:
let vPerp = velSpeed * sin(degToRad(relVelDir)) # lateral speed, range [-8,+8]
let vPar = velSpeed * cos(degToRad(relVelDir)) # approach speed, range [-8,+8]
setThermo(0, vPerp)
setThermo(16, vPar)
# Total speed thermometer: bits 40-47
let speedInt = clamp(int(velSpeed + 0.5), 0, 8)
for b in 0 ..< speedInt:
let idx = 40 + b
result[idx div 64] = result[idx div 64] or (1'u64 shl (idx mod 64))
# Distance thermometer: bits 32-39, 8 bands of ~125px over [0,1000]
let band = clamp(int(distance / 125.0), 0, 7)
for b in 0 .. band:
let idx = 32 + b
result[idx div 64] = result[idx div 64] or (1'u64 shl (idx mod 64))
# ── Main loop ─────────────────────────────────────────────────────────────────
method run*(bot: SNNBot) =
@@ -390,20 +344,18 @@ method run*(bot: SNNBot) =
var relVelDir = bot.velDirDeg - absBearing
while relVelDir >= 180.0: relVelDir -= 360.0
while relVelDir < -180.0: relVelDir += 360.0
let binInput = toBinaryInput(relVelDir, bot.velSpeed, bot.enemyDist, bot.hasLastPos)
let aimOffset = bot.res.forward(binInput) # lead correction offset or -999.0 sentinel
bot.lastBinInput = binInput
let vPerp = bot.velSpeed * sin(degToRad(relVelDir))
let aimOffset = bot.res.forward(vPerp, bot.enemyDist)
bot.lastVPerp = vPerp
bot.lastDecideGunDir = gunDir
bot.lastAbsBearing = absBearing
let rate = if bot.bulletsFired > 0: float(bot.bulletsHit) / float(bot.bulletsFired) * 100.0 else: 0.0
if aimOffset <= -999.0:
# cold start: aim directly at enemy (no lead)
bot.targetAngle = absBearing
echo "RES tick=" & $bot.tick & " pwr=" & formatFloat(bot.currentFirePower, ffDecimal, 1) & " hitEMA=" & formatFloat(bot.hitRateEMA * 100.0, ffDecimal, 0) & "% cold-start aim=" & formatFloat(absBearing, ffDecimal, 1) & " fired=" & $bot.bulletsFired & " hit=" & $bot.bulletsHit & " rate=" & formatFloat(rate, ffDecimal, 1) & "% energy=" & formatFloat(getEnergy(), ffDecimal, 0)
echo "RES tick=" & $bot.tick & " pwr=" & formatFloat(bot.currentFirePower, ffDecimal, 1) & " hitEMA=" & formatFloat(bot.hitRateEMA * 100.0, ffDecimal, 0) & "% cold-start aim=" & formatFloat(absBearing, ffDecimal, 1) & " fired=" & $bot.bulletsFired & " hit=" & $bot.bulletsHit & " rate=" & formatFloat(rate, ffDecimal, 1) & "% energy=" & formatFloat(getEnergy(), ffDecimal, 0) & " cells=" & $bot.res.totalCount
else:
# apply lead correction offset to current bearing
bot.targetAngle = (absBearing + aimOffset + 360.0) mod 360.0
echo "RES tick=" & $bot.tick & " pwr=" & formatFloat(bot.currentFirePower, ffDecimal, 1) & " hitEMA=" & formatFloat(bot.hitRateEMA * 100.0, ffDecimal, 0) & "% offset=" & formatFloat(aimOffset, ffDecimal, 1) & " aim=" & formatFloat(bot.targetAngle, ffDecimal, 1) & " fired=" & $bot.bulletsFired & " hit=" & $bot.bulletsHit & " rate=" & formatFloat(rate, ffDecimal, 1) & "% energy=" & formatFloat(getEnergy(), ffDecimal, 0)
echo "RES tick=" & $bot.tick & " pwr=" & formatFloat(bot.currentFirePower, ffDecimal, 1) & " hitEMA=" & formatFloat(bot.hitRateEMA * 100.0, ffDecimal, 0) & "% offset=" & formatFloat(aimOffset, ffDecimal, 1) & " aim=" & formatFloat(bot.targetAngle, ffDecimal, 1) & " fired=" & $bot.bulletsFired & " hit=" & $bot.bulletsHit & " rate=" & formatFloat(rate, ffDecimal, 1) & "% energy=" & formatFloat(getEnergy(), ffDecimal, 0) & " cells=" & $bot.res.totalCount
else:
let inputs = encodeInputFull(absBearing, bot.velDirDeg, bot.velSpeed, bot.hasLastPos)
# Multi-tick inference: accumulate sin/cos and spike counts over N_INFER ticks
@@ -462,35 +414,29 @@ method run*(bot: SNNBot) =
let adaptiveDeadZone = 0.5 + (0.3 - 0.5) * t # lerp(0.5, 0.3, t)
when USE_RESERVOIR:
# correctOffset: how much lead to apply from current bearing
let correctOffset = normalizeRelativeAngle(correctAngle - bot.lastAbsBearing)
let producedOffset = bot.res.forward(bot.lastBinInput)
# Detect significant input change via Hamming distance
let hammingDist = popcount(bot.lastBinInput[0] xor bot.lastInput[0]) + popcount(bot.lastBinInput[1] xor bot.lastInput[1])
let inputChanged = hammingDist > 2 # 3+ bits flipped = situation changed
let producedOffset = bot.res.forward(bot.lastVPerp, bot.enemyDist)
# Learn if: error > dead zone OR input pattern changed significantly
# Learn if: cold start OR error > dead zone
if producedOffset > -999.0:
let aimErr = abs(normalizeRelativeAngle(producedOffset - correctOffset))
if aimErr > adaptiveDeadZone or inputChanged:
bot.res.learn(bot.lastBinInput, correctOffset)
if aimErr > adaptiveDeadZone:
bot.res.learn(bot.lastVPerp, bot.enemyDist, correctOffset)
else:
# Cold start: always learn
bot.res.learn(bot.lastBinInput, correctOffset)
bot.res.learn(bot.lastVPerp, bot.enemyDist, correctOffset)
let aimErr = if producedOffset > -999.0: abs(normalizeRelativeAngle(producedOffset - correctOffset)) else: -1.0
let rate = if bot.bulletsFired > 0: (float(bot.bulletsHit) / float(bot.bulletsFired) * 100.0) else: 0.0
echo "RES tick=" & $bot.tick &
" pwr=" & formatFloat(bot.currentFirePower, ffDecimal, 1) &
" hitEMA=" & formatFloat(bot.hitRateEMA * 100.0, ffDecimal, 0) & "%" &
" fired=" & $bot.bulletsFired & " hit=" & $bot.bulletsHit &
" rate=" & (if bot.bulletsFired > 0: formatFloat(float(bot.bulletsHit) / float(bot.bulletsFired) * 100.0, ffDecimal, 1) else: "0.0") & "%" &
" rate=" & (if bot.bulletsFired > 0: formatFloat(rate, ffDecimal, 1) else: "0.0") & "%" &
" energy=" & formatFloat(getEnergy(), ffDecimal, 0) &
" offset=" & (if producedOffset > -999.0: formatFloat(producedOffset, ffDecimal, 1) else: "cold") &
" correctOffset=" & formatFloat(correctOffset, ffDecimal, 1) &
" err=" & (if producedOffset > -999.0: formatFloat(aimErr, ffDecimal, 1) else: "n/a") &
" hammingDist=" & $hammingDist &
" inputChanged=" & $inputChanged &
" exemplars=" & $bot.res.count
" vPerp=" & formatFloat(bot.lastVPerp, ffDecimal, 2) &
" cells=" & $bot.res.totalCount
else:
let targetRel = normalizeRelativeAngle(correctAngle - bot.lastDecideGunDir)
bot.snn.superSpikeUpdate(bot.lastSpikes, bot.lastVSnap, bot.snn.preTrace, targetRel)
@@ -514,8 +460,6 @@ method run*(bot: SNNBot) =
let rate = if bot.bulletsFired > 0: (float(bot.bulletsHit) / float(bot.bulletsFired) * 100.0) else: 0.0
echo "tick=" & $bot.tick & " pwr=" & formatFloat(bot.currentFirePower, ffDecimal, 1) & " hitEMA=" & formatFloat(bot.hitRateEMA * 100.0, ffDecimal, 0) & "% fired=" & $bot.bulletsFired & " hit=" & $bot.bulletsHit & " rate=" & formatFloat(rate, ffDecimal, 1) & "% energy=" & formatFloat(getEnergy(), ffDecimal, 0) & " err=" & formatFloat(err, ffDecimal, 1) & "° infer_spk=" & $spikeCount & "/" & $(N_INFER * N_HID) & " maxV=" & formatFloat(maxV, ffDecimal, 3) & " |wih|=" & formatFloat(meanWih, ffDecimal, 4) & " |wOut|=" & formatFloat(meanWout, ffDecimal, 4) & " sin=" & formatFloat(bot.snn.lastSinOut, ffDecimal, 3) & " cos=" & formatFloat(bot.snn.lastCosOut, ffDecimal, 3) & " snnAngle=" & formatFloat(bot.snn.lastSnnAngle, ffDecimal, 1)
# Update lastInput for next EVALUATE cycle's change detection
bot.lastInput = bot.lastBinInput
bot.phase = DECIDE
# Radar lock