feat(ModularBot): decay-GF gun (recency-weighted), 13 guns total
- Decay-GF: exponential decay on GF histogram (0.998/tick, ~350-tick half-life) - Adapts faster to mid-battle strategy changes than standard GF - Battle-tested vs WaveSurfer, Crazy, RandomMover
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
## ModularBot — plugin gun architecture tracer bullet.
|
||||
## Guns: HeadOnGun (0), LinearGun (1), TsetlinGun (2), CircularGun (3), GFGun (4), PatternMatcherGun (5), AntiSurferGun (6), WallBounceGun (7), AccelGun (8), StopShotGun (9), DisplacementGun (10), AveragedLeadGun (11) via GunHarness.
|
||||
## Guns: HeadOnGun (0), LinearGun (1), TsetlinGun (2), CircularGun (3), GFGun (4), PatternMatcherGun (5), AntiSurferGun (6), WallBounceGun (7), AccelGun (8), StopShotGun (9), DisplacementGun (10), AveragedLeadGun (11), DecayGFGun (12) via GunHarness.
|
||||
## Radar: RadarLockModule via radar harness.
|
||||
## Movement: OscillatorModule (perpendicular strafing).
|
||||
|
||||
@@ -22,12 +22,13 @@ import guns/accel_predictor
|
||||
import guns/stop_shot
|
||||
import guns/displacement
|
||||
import guns/averaged_lead
|
||||
import guns/decay_gf
|
||||
import movements/phantom_meteor
|
||||
|
||||
const botJsonPath = currentSourcePath().parentDir / "ModularBot.json"
|
||||
const DebugVBullets = false
|
||||
const DebugCircular = false
|
||||
const GunNames = ["HeadOn", "Linear", "Tsetlin", "Circular", "GuessFactor", "Pattern", "AntiSurf", "WallBounce", "Accel", "StopShot", "Displace", "AvgLead"]
|
||||
const GunNames = ["HeadOn", "Linear", "Tsetlin", "Circular", "GuessFactor", "Pattern", "AntiSurf", "WallBounce", "Accel", "StopShot", "Displace", "AvgLead", "DecayGF"]
|
||||
|
||||
type
|
||||
ModularBot = ref object of Bot
|
||||
@@ -48,6 +49,7 @@ type
|
||||
stopShot: StopShotGun
|
||||
displace: DisplacementGun
|
||||
avgLead: AveragedLeadGun
|
||||
decayGF: DecayGFGun
|
||||
mover: PhantomMeteorModule
|
||||
virtualHits: int
|
||||
virtualMiss: int
|
||||
@@ -90,6 +92,7 @@ method onScannedBot*(bot: ModularBot, e: ScannedBotEvent) =
|
||||
var ssPreds: array[len(PowerBins), GunPrediction]
|
||||
var dsPreds: array[len(PowerBins), GunPrediction]
|
||||
var alPreds: array[len(PowerBins), GunPrediction]
|
||||
var dgPreds: array[len(PowerBins), GunPrediction]
|
||||
for i in 0..<len(PowerBins):
|
||||
headsUp[i] = bot.headOn.predict(bot.lastState, bulletSpeed(PowerBins[i]))
|
||||
linPreds[i] = bot.linear.predict(bot.lastState, bulletSpeed(PowerBins[i]))
|
||||
@@ -103,6 +106,7 @@ method onScannedBot*(bot: ModularBot, e: ScannedBotEvent) =
|
||||
ssPreds[i] = bot.stopShot.predict(bot.lastState, bulletSpeed(PowerBins[i]))
|
||||
dsPreds[i] = bot.displace.predict(bot.lastState, bulletSpeed(PowerBins[i]))
|
||||
alPreds[i] = bot.avgLead.predict(bot.lastState, bulletSpeed(PowerBins[i]))
|
||||
dgPreds[i] = bot.decayGF.predict(bot.lastState, bulletSpeed(PowerBins[i]))
|
||||
|
||||
bot.tracker.spawnBullets(0, headsUp, bot.lastState)
|
||||
bot.tracker.spawnBullets(1, linPreds, bot.lastState)
|
||||
@@ -118,6 +122,7 @@ method onScannedBot*(bot: ModularBot, e: ScannedBotEvent) =
|
||||
bot.tracker.spawnBullets(9, ssPreds, bot.lastState)
|
||||
bot.tracker.spawnBullets(10, dsPreds, bot.lastState)
|
||||
bot.tracker.spawnBullets(11, alPreds, bot.lastState)
|
||||
bot.tracker.spawnBullets(12, dgPreds, bot.lastState)
|
||||
|
||||
# Resolve bullets that have travelled far enough
|
||||
let st = bot.lastState
|
||||
@@ -140,6 +145,7 @@ method onScannedBot*(bot: ModularBot, e: ScannedBotEvent) =
|
||||
of 9: bot.stopShot.onResult(fe)
|
||||
of 10: bot.displace.onResult(fe)
|
||||
of 11: bot.avgLead.onResult(fe)
|
||||
of 12: bot.decayGF.onResult(fe)
|
||||
else: discard
|
||||
if fe.hit: inc bot.virtualHits else: inc bot.virtualMiss
|
||||
when DebugVBullets:
|
||||
@@ -168,6 +174,7 @@ method onScannedBot*(bot: ModularBot, e: ScannedBotEvent) =
|
||||
of 9: setTurretColor("#990000"); setBulletColor("#CC3333") # StopShot: dark red
|
||||
of 10: setTurretColor("#006600"); setBulletColor("#009900") # Displace: dark green
|
||||
of 11: setTurretColor("#996633"); setBulletColor("#CC9966") # AvgLead: brown
|
||||
of 12: setTurretColor("#008888"); setBulletColor("#00AAAA") # DecayGF: dark cyan
|
||||
else: discard
|
||||
|
||||
let pred = case selectedGun
|
||||
@@ -182,6 +189,7 @@ method onScannedBot*(bot: ModularBot, e: ScannedBotEvent) =
|
||||
of 9: bot.stopShot.predict(bot.lastState, bulletSpeed(power))
|
||||
of 10: bot.displace.predict(bot.lastState, bulletSpeed(power))
|
||||
of 11: bot.avgLead.predict(bot.lastState, bulletSpeed(power))
|
||||
of 12: bot.decayGF.predict(bot.lastState, bulletSpeed(power))
|
||||
else: bot.headOn.predict(bot.lastState, bulletSpeed(power))
|
||||
let target = aimAngle(getX(), getY(), pred.x, pred.y)
|
||||
|
||||
@@ -241,7 +249,7 @@ method run*(bot: ModularBot) =
|
||||
|
||||
when isMainModule:
|
||||
var bot = ModularBot(
|
||||
tracker: vb.initTracker(12), # 0: HeadOn, 1: Linear, 2: Tsetlin, 3: Circular, 4: GuessFactor, 5: Pattern, 6: AntiSurf, 7: WallBounce, 8: Accel, 9: StopShot, 10: Displace, 11: AvgLead
|
||||
tracker: vb.initTracker(13), # 0: HeadOn, 1: Linear, 2: Tsetlin, 3: Circular, 4: GuessFactor, 5: Pattern, 6: AntiSurf, 7: WallBounce, 8: Accel, 9: StopShot, 10: Displace, 11: AvgLead, 12: DecayGF
|
||||
headOn: HeadOnGun(),
|
||||
linear: LinearGun(),
|
||||
circular: CircularGun(),
|
||||
@@ -254,6 +262,7 @@ when isMainModule:
|
||||
stopShot: initStopShotGun(),
|
||||
displace: initDisplacementGun(),
|
||||
avgLead: initAveragedLeadGun(),
|
||||
decayGF: initDecayGFGun(),
|
||||
radar: RadarLockModule(),
|
||||
mover: initPhantomMeteor(),
|
||||
currentGun: -1,
|
||||
|
||||
@@ -0,0 +1,96 @@
|
||||
## 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.
|
||||
|
||||
import std/math
|
||||
import gun_harness/gun_interface
|
||||
|
||||
const
|
||||
GFBins = 31
|
||||
GFPrior = 0.1
|
||||
DecayRate = 0.998 # ponytail: single global decay, tune if adaptation too slow/fast
|
||||
|
||||
type
|
||||
DWave = object
|
||||
fireX, fireY: float
|
||||
fireBearing: float
|
||||
|
||||
DecayGFGun* = object
|
||||
bins: array[GFBins, float]
|
||||
waves: seq[DWave]
|
||||
cachedTick: int
|
||||
cachedWaveStored: bool
|
||||
|
||||
proc initDecayGFGun*(): DecayGFGun =
|
||||
result.cachedTick = -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 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
|
||||
g.cachedWaveStored = false
|
||||
|
||||
if not g.cachedWaveStored:
|
||||
g.waves.add DWave(fireX: state.selfX, fireY: state.selfY, fireBearing: bearing)
|
||||
g.cachedWaveStored = true
|
||||
|
||||
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) =
|
||||
if g.waves.len == 0: return
|
||||
|
||||
let w = g.waves[0]
|
||||
g.waves.delete(0)
|
||||
|
||||
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)
|
||||
@@ -0,0 +1,27 @@
|
||||
import std/[os, strformat]
|
||||
import common_libs/test_framework/test_framework
|
||||
|
||||
const
|
||||
modularBotDir = "/home/davide/Projects/SirRoboGarage/ModularBot_garage"
|
||||
samplesDir = "/home/davide/Downloads/robocode-tankroyale/sample-bots-nim-linux-1.0.7"
|
||||
crazy = samplesDir / "Crazy"
|
||||
waveSurfer = "/home/davide/Projects/SirRoboGarage/WaveSurfer_garage"
|
||||
randomMover = "/home/davide/Projects/SirRoboGarage/RandomMover_garage"
|
||||
|
||||
const serverJar = "/home/davide/Projects/tank-royale/server/build/libs/robocode-tankroyale-server-0.35.5-all.jar"
|
||||
const runnerJar = "/home/davide/Projects/tank-royale/runner/examples/lib/robocode-tankroyale-runner.jar"
|
||||
|
||||
if not fileExists(serverJar) or not fileExists(runnerJar):
|
||||
echo "Skipping: Tank Royale JARs not found"
|
||||
quit(0)
|
||||
|
||||
proc battle(name: string, dir: string) =
|
||||
echo fmt"--- ModularBot vs {name} (10 rounds) ---"
|
||||
let r = runBattle(@[modularBotDir, dir], rounds = 10, timeout = 300000, maxSpeed = true)
|
||||
for result in r.results:
|
||||
echo fmt" {result.name}: score={result.totalScore} rank={result.rank}"
|
||||
echo ""
|
||||
|
||||
battle("WaveSurfer", waveSurfer)
|
||||
battle("Crazy", crazy)
|
||||
battle("RandomMover", randomMover)
|
||||
Reference in New Issue
Block a user