fix(gun_harness): random tiebreak + drop AntiSurfer + raise MinObsBeforeCompete

- bestGun: replace first-index-wins argmax with random pick among guns
  within TieMargin (2%) of best rate. HeadOn at index 0 was silently
  winning every tie, starving Tsetlin/Linear/etc.
- MinObsBeforeCompete 15 -> 50 (Pattern entered competition on noise)
- add MinHitRateFloor 0.10: if no gun clears it, fall back to HeadOn
  instead of selecting the best of a bad field
- ModularBot: remove AntiSurfer gun (0% virtual hit rate everywhere),
  14 -> 13 guns, renumber ids and selection counters
This commit is contained in:
2026-09-20 22:07:49 +02:00
parent c034eb9d25
commit 343e631633
2 changed files with 55 additions and 48 deletions
+34 -43
View File
@@ -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), DecayGFGun (12) via GunHarness.
## Guns: HeadOnGun (0), LinearGun (1), TsetlinGun (2), CircularGun (3), GFGun (4), PatternMatcherGun (5), WallBounceGun (6), AccelGun (7), StopShotGun (8), DisplacementGun (9), AveragedLeadGun (10), DecayGFGun (11), KNNGun (12) via GunHarness.
## Radar: RadarLockModule (1v1) / MeleeScanModule (2+ enemies), auto-switched per tick.
## Movement: OscillatorModule (perpendicular strafing).
@@ -17,7 +17,6 @@ import guns/circular
import guns/tsetlin
import guns/guess_factor
import guns/pattern_matcher
import guns/anti_surfer
import guns/wall_bounce
import guns/accel_predictor
import guns/stop_shot
@@ -36,7 +35,7 @@ import targeting/target_selector
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", "DecayGF", "KNN"]
const GunNames = ["HeadOn", "Linear", "Tsetlin", "Circular", "GuessFactor", "Pattern", "WallBounce", "Accel", "StopShot", "Displace", "AvgLead", "DecayGF", "KNN"]
const
CLR_GUN = "\e[33m" # yellow
@@ -64,7 +63,6 @@ type
tsetlin: TsetlinGun
guessFactor: GFGun
patternMatcher: PatternMatcherGun
antiSurfer: AntiSurferGun
wallBounce: WallBounceGun
accelGun: AccelGun
stopShot: StopShotGun
@@ -92,7 +90,7 @@ type
roundNumber: int
realShotsFired: int
realHits: int
gunSelectionCount: array[14, int]
gunSelectionCount: array[13, int]
lastKnownTargetId: int ## persists through death, used for round-end stats
proc printConfig(bot: ModularBot, changed: string = "") =
@@ -184,7 +182,7 @@ method onRoundEnded*(bot: ModularBot, e: RoundEndedEventForBot) =
f
var gunsArr = newJArray()
for gid in 0..<14:
for gid in 0..<13:
var totalShots = 0
var totalHits = 0
for binIdx in 0..<len(vb.PowerBins):
@@ -221,7 +219,7 @@ method onRoundStarted*(bot: ModularBot, e: RoundStartedEvent) =
bot.realShotsFired = 0
bot.realHits = 0
bot.lastKnownTargetId = -1
for i in 0..<14: bot.gunSelectionCount[i] = 0
for i in 0..<13: bot.gunSelectionCount[i] = 0
setAdjustGunForBodyTurn(true)
setAdjustRadarForBodyTurn(true)
setAdjustRadarForGunTurn(true)
@@ -409,7 +407,6 @@ method run*(bot: ModularBot) =
var circPreds: array[len(PowerBins), GunPrediction]
var gfPreds: array[len(PowerBins), GunPrediction]
var pmPreds: array[len(PowerBins), GunPrediction]
var asPreds: array[len(PowerBins), GunPrediction]
var wbPreds: array[len(PowerBins), GunPrediction]
var acPreds: array[len(PowerBins), GunPrediction]
var ssPreds: array[len(PowerBins), GunPrediction]
@@ -424,7 +421,6 @@ method run*(bot: ModularBot) =
circPreds[i] = bot.circular.predict(bot.lastState, bulletSpeed(PowerBins[i]))
gfPreds[i] = bot.guessFactor.predict(bot.lastState, bulletSpeed(PowerBins[i]))
pmPreds[i] = bot.patternMatcher.predict(bot.lastState, bulletSpeed(PowerBins[i]))
asPreds[i] = bot.antiSurfer.predict(bot.lastState, bulletSpeed(PowerBins[i]))
wbPreds[i] = bot.wallBounce.predict(bot.lastState, bulletSpeed(PowerBins[i]))
acPreds[i] = bot.accelGun.predict(bot.lastState, bulletSpeed(PowerBins[i]))
ssPreds[i] = bot.stopShot.predict(bot.lastState, bulletSpeed(PowerBins[i]))
@@ -440,14 +436,13 @@ method run*(bot: ModularBot) =
bot.tracker.spawnBullets(3, circPreds, bot.lastState, tid)
bot.tracker.spawnBullets(4, gfPreds, bot.lastState, tid)
bot.tracker.spawnBullets(5, pmPreds, bot.lastState, tid)
bot.tracker.spawnBullets(6, asPreds, bot.lastState, tid)
bot.tracker.spawnBullets(7, wbPreds, bot.lastState, tid)
bot.tracker.spawnBullets(8, acPreds, bot.lastState, tid)
bot.tracker.spawnBullets(9, ssPreds, bot.lastState, tid)
bot.tracker.spawnBullets(10, dsPreds, bot.lastState, tid)
bot.tracker.spawnBullets(11, alPreds, bot.lastState, tid)
bot.tracker.spawnBullets(12, dgPreds, bot.lastState, tid)
bot.tracker.spawnBullets(13, knnPreds, bot.lastState, tid)
bot.tracker.spawnBullets(6, wbPreds, bot.lastState, tid)
bot.tracker.spawnBullets(7, acPreds, bot.lastState, tid)
bot.tracker.spawnBullets(8, ssPreds, bot.lastState, tid)
bot.tracker.spawnBullets(9, dsPreds, bot.lastState, tid)
bot.tracker.spawnBullets(10, alPreds, bot.lastState, tid)
bot.tracker.spawnBullets(11, dgPreds, bot.lastState, tid)
bot.tracker.spawnBullets(12, knnPreds, bot.lastState, tid)
# Build slim enemy table for tickBullets
var enemyPositions: Table[int, tuple[x, y: float, lastSeenTick: int, alive: bool]]
@@ -468,14 +463,13 @@ method run*(bot: ModularBot) =
echo fmt"[circ-vb] predicted=({fe.prediction.x:.0f},{fe.prediction.y:.0f}) actual=({ax:.0f},{ay:.0f}) miss={fe.missDistance:.1f}px hit={fe.hit}"
of 4: bot.guessFactor.onResult(fe)
of 5: bot.patternMatcher.onResult(fe)
of 6: bot.antiSurfer.onResult(fe)
of 7: bot.wallBounce.onResult(fe)
of 8: bot.accelGun.onResult(fe)
of 9: bot.stopShot.onResult(fe)
of 10: bot.displace.onResult(fe)
of 11: bot.avgLead.onResult(fe)
of 12: bot.decayGF.onResult(fe)
of 13: bot.knnGun.onResult(fe)
of 6: bot.wallBounce.onResult(fe)
of 7: bot.accelGun.onResult(fe)
of 8: bot.stopShot.onResult(fe)
of 9: bot.displace.onResult(fe)
of 10: bot.avgLead.onResult(fe)
of 11: bot.decayGF.onResult(fe)
of 12: bot.knnGun.onResult(fe)
else: discard
if fe.hit: inc bot.virtualHits else: inc bot.virtualMiss
when DebugVBullets:
@@ -496,14 +490,13 @@ method run*(bot: ModularBot) =
of 3: setTurretColor("#33CC33"); setBulletColor("#66FF66")
of 4: setTurretColor("#FFCC00"); setBulletColor("#FFE066")
of 5: setTurretColor("#FF6600"); setBulletColor("#FF9944")
of 6: setTurretColor("#00CCFF"); setBulletColor("#66DDFF")
of 7: setTurretColor("#CCCCCC"); setBulletColor("#EEEEEE")
of 8: setTurretColor("#FF00FF"); setBulletColor("#FF66FF")
of 9: setTurretColor("#990000"); setBulletColor("#CC3333")
of 10: setTurretColor("#006600"); setBulletColor("#009900")
of 11: setTurretColor("#996633"); setBulletColor("#CC9966")
of 12: setTurretColor("#008888"); setBulletColor("#00AAAA")
of 13: setTurretColor("#CC00CC"); setBulletColor("#FF44FF")
of 6: setTurretColor("#CCCCCC"); setBulletColor("#EEEEEE")
of 7: setTurretColor("#FF00FF"); setBulletColor("#FF66FF")
of 8: setTurretColor("#990000"); setBulletColor("#CC3333")
of 9: setTurretColor("#006600"); setBulletColor("#009900")
of 10: setTurretColor("#996633"); setBulletColor("#CC9966")
of 11: setTurretColor("#008888"); setBulletColor("#00AAAA")
of 12: setTurretColor("#CC00CC"); setBulletColor("#FF44FF")
else: discard
let pred = case selectedGun
@@ -512,14 +505,13 @@ method run*(bot: ModularBot) =
of 3: bot.circular.predict(bot.lastState, bulletSpeed(power))
of 4: bot.guessFactor.predict(bot.lastState, bulletSpeed(power))
of 5: bot.patternMatcher.predict(bot.lastState, bulletSpeed(power))
of 6: bot.antiSurfer.predict(bot.lastState, bulletSpeed(power))
of 7: bot.wallBounce.predict(bot.lastState, bulletSpeed(power))
of 8: bot.accelGun.predict(bot.lastState, bulletSpeed(power))
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))
of 13: bot.knnGun.predict(bot.lastState, bulletSpeed(power))
of 6: bot.wallBounce.predict(bot.lastState, bulletSpeed(power))
of 7: bot.accelGun.predict(bot.lastState, bulletSpeed(power))
of 8: bot.stopShot.predict(bot.lastState, bulletSpeed(power))
of 9: bot.displace.predict(bot.lastState, bulletSpeed(power))
of 10: bot.avgLead.predict(bot.lastState, bulletSpeed(power))
of 11: bot.decayGF.predict(bot.lastState, bulletSpeed(power))
of 12: bot.knnGun.predict(bot.lastState, bulletSpeed(power))
else: bot.headOn.predict(bot.lastState, bulletSpeed(power))
let aimTarget = aimAngle(getX(), getY(), pred.x, pred.y)
@@ -537,14 +529,13 @@ method run*(bot: ModularBot) =
when isMainModule:
var bot = ModularBot(
tracker: vb.initTracker(14), # 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, 13: KNN
tracker: vb.initTracker(13), # 0: HeadOn, 1: Linear, 2: Tsetlin, 3: Circular, 4: GuessFactor, 5: Pattern, 6: WallBounce, 7: Accel, 8: StopShot, 9: Displace, 10: AvgLead, 11: DecayGF, 12: KNN
headOn: HeadOnGun(),
linear: LinearGun(),
circular: CircularGun(),
tsetlin: initTsetlinGun(),
guessFactor: initGFGun(),
patternMatcher: PatternMatcherGun(),
antiSurfer: initAntiSurferGun(),
wallBounce: initWallBounceGun(),
accelGun: initAccelGun(),
stopShot: initStopShotGun(),
+21 -5
View File
@@ -5,6 +5,7 @@
import std/math
import std/tables
import std/random
import gun_interface
const
@@ -12,7 +13,9 @@ const
WindowSize* = 100 ## rolling window ticks for fitness
MaxBullets* = 2048 ## hard cap; ponytail: ring buffer, resize if more guns added
MinHitRate* = 0.40 ## 40% threshold for acceptable power selection
MinObsBeforeCompete* = 15 ## min observations before a gun×bin enters competition
MinObsBeforeCompete* = 50 ## min observations before a gun×bin enters competition
TieMargin* = 0.02 ## guns within this hit-rate margin of best are tied
MinHitRateFloor* = 0.10 ## if best gun < this, fall back to gun 0 (HeadOn)
type
GunId* = int ## index into the guns seq
@@ -168,11 +171,11 @@ proc bestGun*(t: VirtualTracker, targetId: int = -1): GunId =
## Guns with fewer than MinObsBeforeCompete observations are skipped
## unless every gun is below threshold (then fall back to best of all).
## Uses per-enemy fitness when targetId >= 0 and data exists; else aggregate.
## Ties (within TieMargin) are broken randomly to avoid index-0 bias.
## ponytail: O(n*bins), fine for small gun counts
let fit = t.fitnessFor(targetId)
proc bestAmong(fit: seq[GunFitness], requireMin: bool): GunId =
var bestRate = -1.0
result = 0
for gunId in 0..<fit.len:
var maxCount = 0
for binIdx in 0..<len(PowerBins):
@@ -180,9 +183,22 @@ proc bestGun*(t: VirtualTracker, targetId: int = -1): GunId =
if requireMin and maxCount < MinObsBeforeCompete: continue
for binIdx in 0..<len(PowerBins):
let r = fit[gunId].bins[binIdx].hitRate()
if r > bestRate:
bestRate = r
result = gunId
if r > bestRate: bestRate = r
# Floor check: if nothing hits well enough, HeadOn is the safe default
if bestRate < MinHitRateFloor: return 0
var tied: seq[GunId]
for gunId in 0..<fit.len:
var maxCount = 0
for binIdx in 0..<len(PowerBins):
maxCount = max(maxCount, fit[gunId].bins[binIdx].count)
if requireMin and maxCount < MinObsBeforeCompete: continue
for binIdx in 0..<len(PowerBins):
let r = fit[gunId].bins[binIdx].hitRate()
if r >= bestRate - TieMargin:
tied.add(gunId)
break
if tied.len == 0: return 0
return tied[rand(tied.len - 1)]
var anyQualifies = false
for gunId in 0..<fit.len: