diff --git a/SNNBot_garage/src/SNNBot.nim b/SNNBot_garage/src/SNNBot.nim index feb65dd..9d32085 100644 --- a/SNNBot_garage/src/SNNBot.nim +++ b/SNNBot_garage/src/SNNBot.nim @@ -71,14 +71,11 @@ const BETA = 1.0 # surrogate steepness (unitless; potentials are unitless) NOISE_AMP = 0.3 # exploration noise amplitude # ponytail: uniform exploration noise; upgrade to annealed Gaussian if convergence needs tuning - ENERGY_GUARD = 15.0 # keep buffer; Walls hits us while we shoot - P3_SPEED = 11.0 # bullet speed for P3 (20 - 3*3); used for exemplar learning - P1_SPEED = 17.0 # bullet speed for P1 (20 - 3*1) - P2_SPEED = 14.0 # bullet speed for P2 (20 - 3*2) - GRID_BULLET_SPEED = 14.0 # reference speed the grid learns at (power 2) + ENERGY_GUARD = 5.0 # keep buffer; Walls hits us while we shoot + FIRE_POWER = 3.0 # fixed fire power + BULLET_SPEED = 11.0 # 20 - 3 * FIRE_POWER COLD_K = 3 # exemplars needed before leaving cold start - PATIENCE_TICKS = 5 # P1 is forgiving, less patience needed - VPERP_TRANSITION = 2.0 # suppress fire when |vPerp change| > this (enemy changing direction) + PATIENCE_TICKS = 5 # ticks before first fire allowed # ── SNN types ───────────────────────────────────────────────────────────────── @@ -248,17 +245,11 @@ type velSpeed: float64 # speed (units/tick) from last scan delta lastDecideGunDir: float # gun heading captured at DECIDE time for EVALUATE lastVPerp: float # DECIDE-time vPerp, reused in EVALUATE - prevTickVPerp: float # vPerp from previous tick (transition detection) 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 bulletsHit: int # count of bullets that hit this round hitRateEMA: float # exponential moving average of hit rate - currentFirePower: float # dynamic fire power (1.0 / 2.0 / 3.0) - bulletSpeed: float # 20 - 3 * currentFirePower - lastBulletSpeed: float # bullet speed at last fire (for EVALUATE learning) - pendingPower: float # candidate new power level - powerChangeCounter: int # ticks the new power has been suggested # ── aimTo helper ────────────────────────────────────────────────────────────── @@ -318,18 +309,13 @@ method onRoundStarted*(bot: SNNBot, e: RoundStartedEvent) = bot.phase = DECIDE bot.tick = 0 bot.roundTick = 0 - # Reset per-round stats; fire power fixed at 2.0 (P2) + # Reset per-round stats bot.bulletsFired = 0 bot.bulletsHit = 0 bot.hitRateEMA = 0.5 # optimistic start - bot.powerChangeCounter = 0 - bot.currentFirePower = 2.0 - bot.bulletSpeed = P2_SPEED - bot.lastBulletSpeed = P2_SPEED bot.lastEnemyX = 0.0 bot.lastEnemyY = 0.0 bot.lastAbsBearing = 0.0 - bot.prevTickVPerp = 0.0 bot.lastDecideGunDir = 0.0 bot.targetAngle = 0.0 setTargetSpeed(0.0) @@ -392,18 +378,13 @@ method run*(bot: SNNBot) = let rate = if bot.bulletsFired > 0: float(bot.bulletsHit) / float(bot.bulletsFired) * 100.0 else: 0.0 if gridOffset <= -999.0: 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) & " cells=" & $bot.res.totalCount + echo "RES tick=" & $bot.tick & " 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 diagLog("DECIDE tick=" & $bot.tick & " offset=cold-start cells=" & $bot.res.totalCount) else: - # Scale grid offset (learned at GRID_BULLET_SPEED) to chosen fire power speed. - # sin(leadAngle) ∝ 1/bulletSpeed, so scale sin of the offset. - let chosenSpeed = 20.0 - 3.0 * bot.currentFirePower - let sinGrid = sin(degToRad(gridOffset)) - let sinScaled = clamp(sinGrid * GRID_BULLET_SPEED / chosenSpeed, -1.0, 1.0) - let aimOffset = radToDeg(arcsin(sinScaled)) - 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) & " cells=" & $bot.res.totalCount - diagLog("DECIDE tick=" & $bot.tick & " offset=" & formatFloat(aimOffset, ffDecimal, 2) & "° gridOffset=" & formatFloat(gridOffset, ffDecimal, 2) & "° cells=" & $bot.res.totalCount) + # Grid learned at BULLET_SPEED — use offset directly (no scaling needed) + bot.targetAngle = (absBearing + gridOffset + 360.0) mod 360.0 + echo "RES tick=" & $bot.tick & " hitEMA=" & formatFloat(bot.hitRateEMA * 100.0, ffDecimal, 0) & "% offset=" & formatFloat(gridOffset, 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 + diagLog("DECIDE tick=" & $bot.tick & " offset=" & formatFloat(gridOffset, ffDecimal, 2) & "° 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 @@ -436,33 +417,14 @@ method run*(bot: SNNBot) = aimTo(bot.targetAngle, gunDir) let err = abs(normalizeRelativeAngle(bot.targetAngle - gunDir)) - # Velocity transition detection: recompute current vPerp - var curVPerp = bot.lastVPerp - if bot.hasLastPos: - var relVelDir2 = bot.velDirDeg - bot.enemyBearing - while relVelDir2 >= 180.0: relVelDir2 -= 360.0 - while relVelDir2 < -180.0: relVelDir2 += 360.0 - curVPerp = bot.velSpeed * sin(degToRad(relVelDir2)) - let vPerpTransition = abs(curVPerp - bot.prevTickVPerp) > VPERP_TRANSITION - bot.prevTickVPerp = curVPerp - - # Fire gate: gun aimed, enough energy, not in cold-start, enemy not changing direction + # Fire gate: gun aimed, enough energy, not in cold-start let energy = getEnergy() let leadOffset = normalizeRelativeAngle(bot.targetAngle - bot.lastAbsBearing) - appendLog(aimDebugLog, fmt"TICK t={bot.roundTick} err={err:.2f} offset={leadOffset:.2f} vPerp={bot.lastVPerp:.2f} dist={bot.enemyDist:.1f} transition={vPerpTransition}") + appendLog(aimDebugLog, fmt"TICK t={bot.roundTick} err={err:.2f} offset={leadOffset:.2f} vPerp={bot.lastVPerp:.2f} dist={bot.enemyDist:.1f}") - # Adaptive fire power: high confidence = P3 (damage), low = P1 (cheap miss) - let chosenPower = - if err < 0.5: 3.0 - elif err < 2.0: 1.0 - else: 0.0 - bot.currentFirePower = if chosenPower > 0.0: chosenPower else: bot.currentFirePower - - let readyToFire = chosenPower > 0.0 and energy >= ENERGY_GUARD and - bot.roundTick >= PATIENCE_TICKS and not vPerpTransition + let readyToFire = err < 2.0 and energy >= ENERGY_GUARD and bot.roundTick >= PATIENCE_TICKS if readyToFire: - bot.lastBulletSpeed = 20.0 - 3.0 * chosenPower - discard setFire(chosenPower) + discard setFire(FIRE_POWER) bot.phase = EVALUATE else: discard setFire(0.0) @@ -470,19 +432,20 @@ method run*(bot: SNNBot) = of EVALUATE: # Predictive error signal: extrapolate enemy position at bullet impact time. if bot.hasLastPos: - # Use actual bullet speed for travel time (P1 = 17.0) - let travelTime = bot.enemyDist / bot.lastBulletSpeed + # correctOffset: correct lead angle at BULLET_SPEED + let travelTime = bot.enemyDist / BULLET_SPEED let velRad = degToRad(bot.velDirDeg) let futureX = bot.lastEnemyX + cos(velRad) * bot.velSpeed * travelTime let futureY = bot.lastEnemyY + sin(velRad) * bot.velSpeed * travelTime # correctAngle: absolute world-frame bearing [0,360) to predicted enemy position let correctAngle = directionTo(myX, myY, futureX, futureY) - # Adaptive learning dead zone: scale by enemy velocity (same as fire gate) + # Adaptive learning dead zone: scale by enemy velocity let t = (bot.velSpeed / 8.0).clamp(0.0, 1.0) let adaptiveDeadZone = 0.5 + (0.3 - 0.5) * t # lerp(0.5, 0.3, t) when USE_RESERVOIR: + # correctOffset: angle offset from bare bearing that a bullet at BULLET_SPEED needs let correctOffset = normalizeRelativeAngle(correctAngle - bot.lastAbsBearing) let producedOffset = bot.res.forward(bot.lastVPerp, bot.enemyDist) @@ -496,7 +459,6 @@ method run*(bot: SNNBot) = 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(rate, ffDecimal, 1) else: "0.0") & "%" & @@ -527,7 +489,7 @@ method run*(bot: SNNBot) = meanWout += abs(w) meanWout /= float(2 * N_HID) 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) + echo "tick=" & $bot.tick & " 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) bot.phase = DECIDE