## Tier A tests for the CHIMERA probe scheduler (chimera/probes.nim, spec ยง9). ## Pure Nim, no Java. Run: from ChimeraBot_garage/src, `nimble test`. import std/math import chimera/dsl import chimera/twinsim import chimera/probes var failed = 0 proc check(name: string, cond: bool, detail: string = "") = if cond: echo " ok ", name else: echo " FAIL ", name, " [", detail, "]"; inc failed let kernels = allKernels() proc origin(): ReplayTick = var r = ReplayTick(tick: 60, ex: 200.0, ey: 300.0, edir: 0.0, espeed: 6.0, eenergy: 80.0, ox: 600.0, oy: 300.0, odir: 180.0, ospeed: 4.0, lastTurn: 0.0, lastSpeed: 6.0) r.weights = newSeq[float64](kernels.len) for i in 0 ..< kernels.len: r.weights[i] = 1.0 / float64(kernels.len) r.mems = newSeq[KernelMem](kernels.len) for i in 0 ..< kernels.len: r.mems[i] = defaultMemory(kernels[i].defaultParams) r proc baseCtx(): ProbeContext = ## All-clear context: mid-arena, healthy, enemy far and cold, not aimed. ProbeContext(tick: 60, ourEnergy: 100.0, ownX: 400.0, ownY: 300.0, ownSpeed: 4.0, arenaW: 800.0, arenaH: 600.0, enemyX: 750.0, enemyY: 300.0, enemyGunHeat: 3.0, enemyAimedAtUs: false, roundLimit: 2000) block: echo "probes 1: survival-gate truth table" var c = baseCtx() check("all clear allows", survivalGate(c).allow) check("all-clear reason", survivalGate(c).reason == "ok") c = baseCtx(); c.ourEnergy = 10.0 check("low energy denies", not survivalGate(c).allow) check("low energy reason", survivalGate(c).reason == "energy") c = baseCtx(); c.enemyAimedAtUs = true; c.enemyGunHeat = 0.0 check("hot+aimed denies", not survivalGate(c).allow) check("hot+aimed reason", survivalGate(c).reason == "enemy-hot-aimed") c.enemyGunHeat = 2.0 check("aimed but cold allows", survivalGate(c).allow) c = baseCtx(); c.ownX = 5.0 check("wall-pinned denies", not survivalGate(c).allow) check("wall-pinned reason", survivalGate(c).reason == "wall-pinned") c = baseCtx(); c.tick = 1980 check("round-end denies", not survivalGate(c).allow) check("round-end reason", survivalGate(c).reason == "round-end") # priority: energy checked before wall/heat c = baseCtx(); c.ourEnergy = 5.0; c.ownX = 5.0; c.enemyAimedAtUs = true c.enemyGunHeat = 0.0 check("energy has gate priority", survivalGate(c).reason == "energy") block: echo "probes 2: budget accounting" var s = initProbeScheduler(kernels) check("interval nominal 25", s.interval == 25) var c = baseCtx() var triggers = 0 for t in 1 .. 25: c.tick = t if s.consider(origin(), c).trigger: inc triggers check("first trigger exactly at interval", triggers == 1, $triggers) check("counter reset after executed probe", s.ticksSinceProbe == 0) check("one executed probe", s.probes == 1) # next trigger must wait a full interval again var before = false for t in 26 .. 49: c.tick = t if s.consider(origin(), c).trigger: before = true check("no trigger before next interval", not before) c.tick = 50 check("second trigger at 2*interval", s.consider(origin(), c).trigger) check("two executed probes", s.probes == 2) # gate deny: trigger fires past the interval but the budget never resets var d = initProbeScheduler(kernels) var dc = baseCtx(); dc.ourEnergy = 5.0 for t in 1 .. 25: dc.tick = t discard d.consider(origin(), dc) check("gated tick still triggers", d.ticksSinceProbe == 25) discard d.consider(origin(), dc) check("gated: no executed probe", d.probes == 0) check("gated: counter keeps growing", d.ticksSinceProbe == 26) check("interval clamped low", initProbeScheduler(kernels, 1).interval == 20) check("interval clamped high", initProbeScheduler(kernels, 99).interval == 30) block: echo "probes 3: entropy-selection determinism" let o = origin() let c = baseCtx() let d1 = chooseProbe(kernels, o, c) let d2 = chooseProbe(kernels, o, c) check("same candidate kind", d1.candidate.kind == d2.candidate.kind) check("same turn rate", d1.candidate.turnRate == d2.candidate.turnRate) check("same target speed", d1.candidate.targetSpeed == d2.candidate.targetSpeed) check("same entropyBefore", d1.entropyBefore == d2.entropyBefore) check("same entropyAfter", d1.entropyAfter == d2.entropyAfter) check("entropy does not increase", d1.entropyAfter <= d1.entropyBefore + 1e-9, $d1.entropyBefore & " -> " & $d1.entropyAfter) # selection is information-driven: at least one candidate must separate the # kernels, so a mixed posterior admits a strictly positive expected drop. check("selection finds information", d1.entropyBefore - d1.entropyAfter > 0.0, $(d1.entropyBefore - d1.entropyAfter)) block: echo "probes 4: camouflage plausibility" var c = baseCtx(); c.ownSpeed = 7.9 for cand in candidates(c): let cam = camouflage(cand, c) check("in envelope " & $cam.kind, plausible(cam, c), "turn=" & $cam.turnRate & " speed=" & $cam.targetSpeed) c = baseCtx(); c.ownSpeed = 0.0 var anySmall = false for cand in candidates(c): let cam = camouflage(cand, c) check("in envelope at rest " & $cam.kind, plausible(cam, c)) if abs(cam.targetSpeed) <= 8.0 and abs(cam.turnRate) <= 10.0: anySmall = true check("probe magnitudes stay plausible", anySmall) block: echo "probes 6: probe set v2 (distance + gun, M5.5)" var c = baseCtx() check("gunProbeAllowed false when not aimed", not gunProbeAllowed(c)) var kinds: set[ProbeKind] = {} var gunFire = 0.0 for cand in candidates(c): kinds.incl cand.kind if cand.kind == pkGunShot: gunFire = cand.fire check("distance candidate present", pkDistance in kinds) check("gun candidate absent when not aimed", pkGunShot notin kinds) c.ourGunHeat = 0.0; c.ourGunAimed = true check("gunProbeAllowed true when cool+aimed", gunProbeAllowed(c)) kinds = {}; gunFire = 0.0 for cand in candidates(c): kinds.incl cand.kind if cand.kind == pkGunShot: gunFire = cand.fire check("gun candidate present when cool+aimed", pkGunShot in kinds) check("gun probe IS a shot (low power)", gunFire > 0.0, $gunFire) check("all six probe types selectable", kinds.len == 6, $kinds) var d = baseCtx(); d.ourGunHeat = 0.0; d.ourGunAimed = true; d.enemyAimedAtUs = true check("gunProbeAllowed false when enemy aimed at us", not gunProbeAllowed(d)) var e = baseCtx(); e.ourGunHeat = 1.0; e.ourGunAimed = true check("gunProbeAllowed false when our gun hot", not gunProbeAllowed(e)) let dec = chooseProbe(kernels, origin(), c) check("chooseProbe deterministic over v2 set", dec.candidate.kind == chooseProbe(kernels, origin(), c).candidate.kind) check("entropy selection covers v2 set (non-increasing entropy)", dec.entropyAfter <= dec.entropyBefore + 1e-9) block: echo "probes 5: entropy closed form" check("uniform n=4 entropy = ln4", abs(entropyOf([0.25, 0.25, 0.25, 0.25]) - ln(4.0)) < 1e-12) check("onehot entropy = 0", abs(entropyOf([1.0, 0.0, 0.0])) < 1e-12) check("normalization-invariant", abs(entropyOf([2.0, 2.0, 2.0, 2.0]) - ln(4.0)) < 1e-12) echo "" if failed == 0: echo "all probe tests passed" else: echo failed, " probe test(s) FAILED"; quit(1)