fix(SNNBot): hold aim when error is within dead zone
Adds aim-lock mechanism to prevent re-running forward() pass during DECIDE when the error is already within 1.5° dead zone. Lock is set in EVALUATE when error < 1.5°, reusing the previous targetAngle in DECIDE. Lock releases on fresh enemy scan or when error exceeds threshold, eliminating the ±0.9° wobble from input pattern drift. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -215,6 +215,7 @@ type
|
|||||||
velSpeed: float64 # speed (units/tick) from last scan delta
|
velSpeed: float64 # speed (units/tick) from last scan delta
|
||||||
lastDecideGunDir: float # gun heading captured at DECIDE time for EVALUATE
|
lastDecideGunDir: float # gun heading captured at DECIDE time for EVALUATE
|
||||||
lastBinInput: BitVec80 # DECIDE-time binary input, reused in EVALUATE
|
lastBinInput: BitVec80 # DECIDE-time binary input, reused in EVALUATE
|
||||||
|
aimLocked: bool # when true, skip forward() and reuse targetAngle
|
||||||
|
|
||||||
# ── aimTo helper ──────────────────────────────────────────────────────────────
|
# ── aimTo helper ──────────────────────────────────────────────────────────────
|
||||||
|
|
||||||
@@ -254,6 +255,7 @@ method onScannedBot*(bot: SNNBot, e: ScannedBotEvent) =
|
|||||||
bot.enemyBearing = directionTo(bx, by, e.x, e.y)
|
bot.enemyBearing = directionTo(bx, by, e.x, e.y)
|
||||||
bot.enemyDist = distanceTo(bx, by, e.x, e.y)
|
bot.enemyDist = distanceTo(bx, by, e.x, e.y)
|
||||||
bot.hasContact = true
|
bot.hasContact = true
|
||||||
|
bot.aimLocked = false # release lock on fresh enemy scan
|
||||||
if bot.hasLastPos:
|
if bot.hasLastPos:
|
||||||
let dx = e.x - bot.lastEnemyX
|
let dx = e.x - bot.lastEnemyX
|
||||||
let dy = e.y - bot.lastEnemyY
|
let dy = e.y - bot.lastEnemyY
|
||||||
@@ -273,6 +275,7 @@ method onRoundStarted*(bot: SNNBot, e: RoundStartedEvent) =
|
|||||||
bot.velSpeed = 0.0
|
bot.velSpeed = 0.0
|
||||||
bot.phase = DECIDE
|
bot.phase = DECIDE
|
||||||
bot.tick = 0
|
bot.tick = 0
|
||||||
|
bot.aimLocked = false
|
||||||
setTargetSpeed(0.0)
|
setTargetSpeed(0.0)
|
||||||
setTurnRate(0.0)
|
setTurnRate(0.0)
|
||||||
|
|
||||||
@@ -335,48 +338,49 @@ method run*(bot: SNNBot) =
|
|||||||
|
|
||||||
case bot.phase
|
case bot.phase
|
||||||
of DECIDE:
|
of DECIDE:
|
||||||
let relBearing = normalizeRelativeAngle(bot.enemyBearing - gunDir)
|
if not bot.aimLocked:
|
||||||
bot.lastRelBearing = relBearing
|
let relBearing = normalizeRelativeAngle(bot.enemyBearing - gunDir)
|
||||||
when USE_RESERVOIR:
|
bot.lastRelBearing = relBearing
|
||||||
let binInput = toBinaryInput(relBearing, bot.velDirDeg, bot.velSpeed, bot.hasLastPos)
|
when USE_RESERVOIR:
|
||||||
let aimRaw = bot.res.forward(binInput) # [0,360) or -1.0 sentinel
|
let binInput = toBinaryInput(relBearing, bot.velDirDeg, bot.velSpeed, bot.hasLastPos)
|
||||||
bot.lastBinInput = binInput
|
let aimRaw = bot.res.forward(binInput) # [0,360) or -1.0 sentinel
|
||||||
bot.lastDecideGunDir = gunDir
|
bot.lastBinInput = binInput
|
||||||
if aimRaw < 0.0:
|
bot.lastDecideGunDir = gunDir
|
||||||
# cold start: use raw bearing directly
|
if aimRaw < 0.0:
|
||||||
bot.targetAngle = bot.enemyBearing
|
# cold start: use raw bearing directly
|
||||||
echo "RES tick=" & $bot.tick & " cold-start aim=" & formatFloat(bot.enemyBearing, ffDecimal, 1)
|
bot.targetAngle = bot.enemyBearing
|
||||||
|
echo "RES tick=" & $bot.tick & " cold-start aim=" & formatFloat(bot.enemyBearing, ffDecimal, 1)
|
||||||
|
else:
|
||||||
|
# aimRaw is absolute angle [0,360); convert to -180..180 relative then add gunDir
|
||||||
|
let aimRel = normalizeRelativeAngle(aimRaw - 180.0) # exemplars stored as 0-360, relative bearing is -180..180
|
||||||
|
bot.targetAngle = gunDir + aimRel
|
||||||
|
echo "RES tick=" & $bot.tick & " aim=" & formatFloat(aimRaw, ffDecimal, 1) & " rel=" & formatFloat(aimRel, ffDecimal, 1)
|
||||||
else:
|
else:
|
||||||
# aimRaw is absolute angle [0,360); convert to -180..180 relative then add gunDir
|
let inputs = encodeInputFull(relBearing, bot.velDirDeg, bot.velSpeed, bot.hasLastPos)
|
||||||
let aimRel = normalizeRelativeAngle(aimRaw - 180.0) # exemplars stored as 0-360, relative bearing is -180..180
|
# Multi-tick inference: accumulate sin/cos and spike counts over N_INFER ticks
|
||||||
bot.targetAngle = gunDir + aimRel
|
var totalSin = 0.0; var totalCos = 0.0
|
||||||
echo "RES tick=" & $bot.tick & " aim=" & formatFloat(aimRaw, ffDecimal, 1) & " rel=" & formatFloat(aimRel, ffDecimal, 1)
|
var tickSpikes: array[N_HID, float]
|
||||||
else:
|
var tickVSnap: array[N_HID, float]
|
||||||
let inputs = encodeInputFull(relBearing, bot.velDirDeg, bot.velSpeed, bot.hasLastPos)
|
for h in 0 ..< N_HID: bot.lastSpikes[h] = 0.0
|
||||||
# Multi-tick inference: accumulate sin/cos and spike counts over N_INFER ticks
|
for _ in 0 ..< N_INFER:
|
||||||
var totalSin = 0.0; var totalCos = 0.0
|
var sinT, cosT: float
|
||||||
var tickSpikes: array[N_HID, float]
|
bot.snn.forward(inputs, tickSpikes, tickVSnap, sinT, cosT)
|
||||||
var tickVSnap: array[N_HID, float]
|
totalSin += sinT; totalCos += cosT
|
||||||
for h in 0 ..< N_HID: bot.lastSpikes[h] = 0.0
|
for h in 0 ..< N_HID:
|
||||||
for _ in 0 ..< N_INFER:
|
bot.lastSpikes[h] += tickSpikes[h] # accumulate counts
|
||||||
var sinT, cosT: float
|
# Store last-tick voltages for learning
|
||||||
bot.snn.forward(inputs, tickSpikes, tickVSnap, sinT, cosT)
|
bot.lastVSnap = tickVSnap
|
||||||
totalSin += sinT; totalCos += cosT
|
bot.snn.lastSinOut = totalSin; bot.snn.lastCosOut = totalCos
|
||||||
|
bot.snn.lastSnnAngle = arctan2(totalSin, totalCos) * 180.0 / PI
|
||||||
|
bot.targetAngle = gunDir + bot.snn.lastSnnAngle
|
||||||
|
bot.lastDecideGunDir = gunDir
|
||||||
|
# Log total spike count across inference window
|
||||||
|
var spikeCount = 0
|
||||||
|
var maxV = 0.0
|
||||||
for h in 0 ..< N_HID:
|
for h in 0 ..< N_HID:
|
||||||
bot.lastSpikes[h] += tickSpikes[h] # accumulate counts
|
spikeCount += int(bot.lastSpikes[h])
|
||||||
# Store last-tick voltages for learning
|
maxV = max(maxV, bot.snn.vHid[h])
|
||||||
bot.lastVSnap = tickVSnap
|
echo "tick=" & $bot.tick & " infer_spk=" & $spikeCount & "/" & $(N_INFER * N_HID) & " maxV=" & formatFloat(maxV, ffDecimal, 3)
|
||||||
bot.snn.lastSinOut = totalSin; bot.snn.lastCosOut = totalCos
|
|
||||||
bot.snn.lastSnnAngle = arctan2(totalSin, totalCos) * 180.0 / PI
|
|
||||||
bot.targetAngle = gunDir + bot.snn.lastSnnAngle
|
|
||||||
bot.lastDecideGunDir = gunDir
|
|
||||||
# Log total spike count across inference window
|
|
||||||
var spikeCount = 0
|
|
||||||
var maxV = 0.0
|
|
||||||
for h in 0 ..< N_HID:
|
|
||||||
spikeCount += int(bot.lastSpikes[h])
|
|
||||||
maxV = max(maxV, bot.snn.vHid[h])
|
|
||||||
echo "tick=" & $bot.tick & " infer_spk=" & $spikeCount & "/" & $(N_INFER * N_HID) & " maxV=" & formatFloat(maxV, ffDecimal, 3)
|
|
||||||
bot.phase = WAITING
|
bot.phase = WAITING
|
||||||
|
|
||||||
of WAITING:
|
of WAITING:
|
||||||
@@ -404,18 +408,28 @@ method run*(bot: SNNBot) =
|
|||||||
let aimErr = abs(normalizeRelativeAngle(aimRaw - correctAngle))
|
let aimErr = abs(normalizeRelativeAngle(aimRaw - correctAngle))
|
||||||
if aimErr > 1.5:
|
if aimErr > 1.5:
|
||||||
bot.res.learn(bot.lastBinInput, correctAngle)
|
bot.res.learn(bot.lastBinInput, correctAngle)
|
||||||
|
bot.aimLocked = false # error still large, keep recomputing
|
||||||
|
else:
|
||||||
|
bot.aimLocked = true # error within dead zone, freeze aim
|
||||||
else:
|
else:
|
||||||
# Cold start: always learn
|
# Cold start: always learn
|
||||||
bot.res.learn(bot.lastBinInput, correctAngle)
|
bot.res.learn(bot.lastBinInput, correctAngle)
|
||||||
|
bot.aimLocked = false
|
||||||
let aimErr = if aimRaw >= 0.0: abs(normalizeRelativeAngle(aimRaw - correctAngle)) else: -1.0
|
let aimErr = if aimRaw >= 0.0: abs(normalizeRelativeAngle(aimRaw - correctAngle)) else: -1.0
|
||||||
echo "RES tick=" & $bot.tick &
|
echo "RES tick=" & $bot.tick &
|
||||||
" aim=" & (if aimRaw >= 0.0: formatFloat(aimRaw, ffDecimal, 1) else: "cold") &
|
" aim=" & (if aimRaw >= 0.0: formatFloat(aimRaw, ffDecimal, 1) else: "cold") &
|
||||||
" correct=" & formatFloat(correctAngle, ffDecimal, 1) &
|
" correct=" & formatFloat(correctAngle, ffDecimal, 1) &
|
||||||
" err=" & (if aimRaw >= 0.0: formatFloat(aimErr, ffDecimal, 1) else: "n/a") &
|
" err=" & (if aimRaw >= 0.0: formatFloat(aimErr, ffDecimal, 1) else: "n/a") &
|
||||||
" exemplars=" & $bot.res.count
|
" exemplars=" & $bot.res.count &
|
||||||
|
" lock=" & (if bot.aimLocked: "ON" else: "OFF")
|
||||||
else:
|
else:
|
||||||
let err = abs(normalizeRelativeAngle(gunDir - bot.enemyBearing))
|
let err = abs(normalizeRelativeAngle(gunDir - bot.enemyBearing))
|
||||||
bot.snn.superSpikeUpdate(bot.lastSpikes, bot.lastVSnap, bot.snn.preTrace, targetRel)
|
bot.snn.superSpikeUpdate(bot.lastSpikes, bot.lastVSnap, bot.snn.preTrace, targetRel)
|
||||||
|
# Aim lock: freeze when error within 1.5° threshold
|
||||||
|
if err < 1.5:
|
||||||
|
bot.aimLocked = true
|
||||||
|
else:
|
||||||
|
bot.aimLocked = false
|
||||||
# Compute verbose logging metrics
|
# Compute verbose logging metrics
|
||||||
var spikeCount = 0
|
var spikeCount = 0
|
||||||
var maxV = 0.0
|
var maxV = 0.0
|
||||||
@@ -432,7 +446,7 @@ method run*(bot: SNNBot) =
|
|||||||
for w in bot.snn.wCos:
|
for w in bot.snn.wCos:
|
||||||
meanWout += abs(w)
|
meanWout += abs(w)
|
||||||
meanWout /= float(2 * N_HID)
|
meanWout /= float(2 * N_HID)
|
||||||
echo "tick=" & $bot.tick & " 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)
|
echo "tick=" & $bot.tick & " 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) & " lock=" & (if bot.aimLocked: "ON" else: "OFF")
|
||||||
bot.phase = DECIDE
|
bot.phase = DECIDE
|
||||||
|
|
||||||
# Radar lock
|
# Radar lock
|
||||||
|
|||||||
Reference in New Issue
Block a user