ChimeraBot_garage: M5.5 — unified metrics, cross-round posterior carry, probe set v2, G5' v2

This commit is contained in:
2026-10-07 02:27:11 +02:00
parent f9f3cd7576
commit 6dd0b1c3cb
12 changed files with 815 additions and 452 deletions
+13 -1
View File
@@ -35,6 +35,7 @@ import ChimeraBot/modules/ELM/elm_features
import ChimeraBot/chimera/observer
import ChimeraBot/chimera/dsl
import ChimeraBot/chimera/probes
import ChimeraBot/chimera/physics
const MaxScannedEnemies* = 16 ## movers see every alive enemy; the radar feeds
## only what it scans, so this is a cap, not a limit
@@ -288,9 +289,12 @@ method run*(bot: ChimeraBot) =
# removes this block entirely (G6 ablation OFF arm).
var cmdSpeed = speed
var cmdTurn = turnRate
var probeFired = false
when ProbesEnabled:
if bot.observer.hasCurrent:
let porigin = bot.observer.probeOrigin()
let gunBearing = bearingTo(porigin.ox, porigin.oy,
porigin.ex, porigin.ey)
let ctx = ProbeContext(
tick: bot.tick, ourEnergy: getEnergy(),
ownX: porigin.ox, ownY: porigin.oy, ownSpeed: porigin.ospeed,
@@ -299,6 +303,8 @@ method run*(bot: ChimeraBot) =
enemyGunHeat: bot.observer.enemyGunHeat(),
enemyAimedAtUs: aimedAtUs(porigin.ox, porigin.oy,
porigin.ex, porigin.ey, porigin.edir),
ourGunHeat: getGunHeat(),
ourGunAimed: abs(wrap180(gunBearing - getGunDirection())) <= 10.0,
roundLimit: RoundSoftLimit)
let decision = bot.probes.consider(porigin, ctx)
if decision.trigger:
@@ -309,9 +315,15 @@ method run*(bot: ChimeraBot) =
if decision.gate.allow:
cmdSpeed = decision.candidate.targetSpeed
cmdTurn = decision.candidate.turnRate
# GUN probe (M5.5): the probe IS the shot. Issued only when the
# camouflage gate kept it in the candidate set (cool + aligned).
if decision.candidate.fire > 0.0:
discard setFire(decision.candidate.fire)
probeFired = true
setTargetSpeed(cmdSpeed)
setTurnRate(cmdTurn)
bot.computeGun() # gun intent, also before go()
if not probeFired:
bot.computeGun() # gun intent, also before go()
go()
bot.observer.writeTick(bot.tick) # observation telemetry, after go()
inc bot.tick
@@ -58,6 +58,10 @@ type
f: File
fileOpen: bool
# Cross-round posterior carry-over (M5.5; default on, `-d:chimeraFreshRound`
# restores the per-round reset).
carryOver*: bool
## predictions: `predPos[(origin mod PredSlots)][h]` is the enemy position
## predicted `h` ticks after `origin`; `predOrigin` guards a slot's owner.
predPos: array[PredSlots, array[MaxHorizon + 1, tuple[x, y: float64]]]
@@ -79,12 +83,12 @@ proc initObserver*(kernels: seq[Kernel], telemetryDir = ""): Observer =
result.telemetryDir = telemetryDir
result.scanTick = -1
result.enemyHeat = 3.0
result.carryOver = not defined(chimeraFreshRound)
for i in 0 ..< PredSlots: result.predOrigin[i] = -1
proc resetRound(o: Observer) =
o.hedge = initHedge(kernelNames(o.kernels), o.cfg)
for i in 0 ..< o.kernels.len:
o.mems[i] = defaultMemory(o.kernels[i].defaultParams)
proc resetRoundStats(o: Observer) =
## Per-round observables and counters, with NO touch to the posterior or the
## kernel memories. Cleared on every round start (carry-over keeps the latter).
o.havePrev = false
o.haveCur = false
o.haveLastObs = false
@@ -98,6 +102,13 @@ proc resetRound(o: Observer) =
o.enemyHeat = 3.0 ## round-start heat (spec §5)
for i in 0 ..< PredSlots: o.predOrigin[i] = -1
proc resetRound(o: Observer) =
## Fresh-round ablation: uniform posterior + default kernel memories.
o.hedge = initHedge(kernelNames(o.kernels), o.cfg)
for i in 0 ..< o.kernels.len:
o.mems[i] = defaultMemory(o.kernels[i].defaultParams)
o.resetRoundStats()
proc sanitize*(s: string): string =
## Filename-safe opponent token: collapse runs of non-alphanumerics to `_`.
var prevSep = false
@@ -120,9 +131,18 @@ proc openTelemetry(o: Observer) =
o.fileOpen = true
proc startRound*(o: Observer, round: int) =
## Called on `RoundStartedEvent`. Re-initialises the posterior UNIFORM, as
## the spec requires per round (Corners re-samples its start corner each round).
o.resetRound()
## Called on `RoundStartedEvent`. With carry-over (default, M5.5) the posterior
## and kernel memories survive: the enemy is the same program across rounds, so
## identification compounds. Only RNG-derived memory is re-sampled (a per-round
## parameter such as Corners' start corner must not carry). With
## `-d:chimeraFreshRound` (`carryOver == false`) the posterior resets UNIFORM,
## which is the M3/M4 behaviour and the G6 carry-over ablation arm.
if o.carryOver:
o.resetRoundStats()
for i in 0 ..< o.kernels.len:
o.mems[i].rngState = o.kernels[i].defaultParams.seed
else:
o.resetRound()
o.round = round # `opponent` is retained: it is per-BATTLE, not per-round
# ── ledger folding (observer-local fire rule) ───────────────────────────────
@@ -43,12 +43,14 @@ const
type
ProbeKind* = enum
## Enum order is the deterministic tie-break (earlier wins).
pkSuddenStop, pkReversal, pkSpeedModulation, pkDirectionFlip
pkSuddenStop, pkReversal, pkSpeedModulation, pkDirectionFlip,
pkDistance, pkGunShot
ProbeCandidate* = object
kind*: ProbeKind
turnRate*: float64
targetSpeed*: float64
fire*: float64 ## gun probe: the probe IS a low-power shot (0 else)
GateState* = object
allow*: bool
@@ -63,6 +65,8 @@ type
enemyX*, enemyY*: float64
enemyGunHeat*: float64 ## from the ledger's fire channel (+ decay)
enemyAimedAtUs*: bool ## body-heading proxy; gun aim is unobservable
ourGunHeat*: float64 ## our gun heat (gun probes need it at 0)
ourGunAimed*: bool ## our gun points at the enemy (camouflage)
roundLimit*: int ## 0 disables the round-end gate
ProbeDecision* = object
@@ -109,14 +113,23 @@ proc survivalGate*(ctx: ProbeContext): GateState =
# ── candidates + camouflage ──────────────────────────────────────────────────
proc gunProbeAllowed*(ctx: ProbeContext): bool =
## Camouflage gate for a GUN probe (M5.5): a shot is only plausible — and only
## useful as a probe — when our gun is cool and already pointing at the enemy,
## so the shot reads as the ELM gun firing anyway. Combined with
## `survivalGate` (energy / enemy-hot-aimed / wall), never fire when exposed.
ctx.ourGunHeat <= 0.0 and ctx.ourGunAimed and
ctx.ourEnergy >= LowEnergy and not ctx.enemyAimedAtUs
proc candidates*(ctx: ProbeContext): seq[ProbeCandidate] =
## Four plausible body actions. Magnitudes are deliberately modest so a probe
## reads as ordinary movement noise, not as a tell.
## Six plausible body/gun actions. Magnitudes are deliberate so a probe reads
## as ordinary noise, not a tell. Four movement probes, one DISTANCE probe
## (approach/retreat burst), and — when the gun gate allows — one GUN probe.
let cap = maxTurnRate(ctx.ownSpeed)
var rev = -ctx.ownSpeed
if abs(rev) < 1.0: rev = -3.0
rev = clamp(rev, -4.0, 4.0)
@[
result = @[
ProbeCandidate(kind: pkSuddenStop, turnRate: 0.0, targetSpeed: 0.0),
ProbeCandidate(kind: pkReversal, turnRate: 0.0, targetSpeed: rev),
ProbeCandidate(kind: pkSpeedModulation, turnRate: 0.0,
@@ -124,7 +137,15 @@ proc candidates*(ctx: ProbeContext): seq[ProbeCandidate] =
MaxBackwardSpeed, MaxForwardSpeed)),
ProbeCandidate(kind: pkDirectionFlip, turnRate: min(4.0, cap),
targetSpeed: ctx.ownSpeed),
# DISTANCE probe: a strong range-changing burst (approach / retreat).
ProbeCandidate(kind: pkDistance, turnRate: 0.0,
targetSpeed: MaxForwardSpeed),
ProbeCandidate(kind: pkDistance, turnRate: 0.0,
targetSpeed: MaxBackwardSpeed),
]
if gunProbeAllowed(ctx):
result.add ProbeCandidate(kind: pkGunShot, turnRate: 0.0,
targetSpeed: ctx.ownSpeed, fire: MinFirepower)
proc camouflage*(c: ProbeCandidate, ctx: ProbeContext): ProbeCandidate =
## Clamp a candidate into the physics envelope: an unachievable command would
@@ -133,6 +154,7 @@ proc camouflage*(c: ProbeCandidate, ctx: ProbeContext): ProbeCandidate =
result.turnRate = clamp(c.turnRate, -maxTurnRate(ctx.ownSpeed),
maxTurnRate(ctx.ownSpeed))
result.targetSpeed = clamp(c.targetSpeed, MaxBackwardSpeed, MaxForwardSpeed)
result.fire = c.fire
proc plausible*(c: ProbeCandidate, ctx: ProbeContext): bool =
## Tests-only sanity predicate: camouflage output is always plausible.
@@ -172,7 +194,8 @@ proc reactionFeature(kernels: seq[Kernel], origin: ReplayTick, idx: int,
## flattened (x,y) per tick. This is the "reaction" the probe is choosing.
var plan = newSeq[PlanStep](horizon)
for h in 0 ..< horizon:
plan[h] = PlanStep(turnRate: cand.turnRate, targetSpeed: cand.targetSpeed)
plan[h] = PlanStep(turnRate: cand.turnRate, targetSpeed: cand.targetSpeed,
fire: cand.fire)
let r = rollout(kernels, onehot(origin, idx), horizon, plan = plan)
for p in r.points:
result.add p.x