ChimeraBot_garage: M7.5 — virtual-gun tournament, Dodger-v2, discriminating bench
This commit is contained in:
@@ -1,15 +1,19 @@
|
||||
## G8 OFFLINE BENCH — flight-horizon aiming (twin gun) vs ELM vs head-on.
|
||||
## Pure Nim, no Java. Source of truth: docs/chimera-spec.md G8.
|
||||
## G8 OFFLINE BENCH (M7.5) — virtual-gun tournament vs the single guns.
|
||||
## Pure Nim, no Java. Source of truth: docs/chimera-spec.md G8 (M7 + M7.5).
|
||||
##
|
||||
## Replays recorded trajectories (the archived M6 telemetry under
|
||||
## `CHIMERA_G8_TELEMETRY`, default `/tmp/chimera_g4_run`) plus a deterministically
|
||||
## SYNTHESIZED Dodger trajectory (the archetype's documented spec function run
|
||||
## SYNTHESIZED Dodger-v2 trajectory (the archetype's documented spec function run
|
||||
## through the physics mirror), and, for every sampled origin tick, simulates the
|
||||
## bullet each of three aim policies would have fired:
|
||||
## bullet each aim policy would have fired:
|
||||
##
|
||||
## * HEAD-ON — aim at the enemy's position at fire time (no lead);
|
||||
## * ELM — the ELM gun's base: geometric intercept from observed velocity;
|
||||
## * TWIN — `chimera/aimer.nim`'s flight-horizon fused aim + spread power.
|
||||
## * TWIN — `chimera/aimer.nim`'s flight-horizon fused aim + spread power;
|
||||
## * VGUN — `chimera/virtual_guns.nim`'s tournament: every sampled tick the
|
||||
## TWIN/ELM/HEAD-ON candidates each launch a virtual bullet, the
|
||||
## realized outcomes score them (tau=200 EMA), and VGUN fires the
|
||||
## argmax under 0.1 hysteresis.
|
||||
##
|
||||
## Hit = the bullet passes within `HitRadius` of the enemy's realized position on
|
||||
## some flight tick before the wall. Aim slew and cadence are abstracted away: the
|
||||
@@ -17,12 +21,13 @@
|
||||
##
|
||||
## Run: `nim c -r --hints:off -d:release tests/bench_g8.nim`
|
||||
|
||||
import std/[math, os, strutils, tables, algorithm, times, sequtils]
|
||||
import std/[math, os, strutils, tables, algorithm, sequtils]
|
||||
import chimera/dsl
|
||||
import chimera/twinsim
|
||||
import chimera/residual
|
||||
import chimera/aimer
|
||||
import chimera/physics
|
||||
import chimera/virtual_guns
|
||||
import dodger_spec
|
||||
|
||||
const
|
||||
@@ -35,7 +40,7 @@ const
|
||||
|
||||
type
|
||||
Policy = enum
|
||||
pHeadOn, pElm, pTwin
|
||||
pHeadOn, pElm, pTwin, pVgun
|
||||
|
||||
Tally = object
|
||||
hits, shots: int
|
||||
@@ -77,7 +82,8 @@ proc simulateHit(sx, sy, bearing, power: float64, originTick: int,
|
||||
false
|
||||
|
||||
proc evalRecord(rec: Record, kernels: seq[Kernel],
|
||||
arenaW, arenaH: float64): Table[Policy, Tally] =
|
||||
arenaW, arenaH: float64,
|
||||
v: var VirtualGunSet): Table[Policy, Tally] =
|
||||
let emap = enemyMap(rec)
|
||||
let rps = replay(rec, kernels)
|
||||
# ONE aimer per record, trained online exactly like the live bot: at each
|
||||
@@ -85,8 +91,15 @@ proc evalRecord(rec: Record, kernels: seq[Kernel],
|
||||
# per-origin aimer would never learn the bucket weights and would misrepresent
|
||||
# the twin gun.
|
||||
var a = initAimer()
|
||||
# The tournament persists PER OPPONENT (across the opponent's replay files) —
|
||||
# that is what "per opponent with exponential forgetting" means. Only the
|
||||
# in-flight virtual bullets are cleared at a record boundary.
|
||||
v.bullets.setLen(0)
|
||||
var n = 0
|
||||
for rt in rps:
|
||||
# Advance/resolve the live virtual bullets every tick (that is how the
|
||||
# tournament scores realized outcomes), independent of the sampling stride.
|
||||
v.vgTick(rt.ex, rt.ey, HitRadius, arenaW, arenaH)
|
||||
if rt.tick mod Stride != 0: continue
|
||||
if n >= MaxOrigins: break
|
||||
inc n
|
||||
@@ -101,18 +114,29 @@ proc evalRecord(rec: Record, kernels: seq[Kernel],
|
||||
let elm = arctan2(rt.ey + vy * tof - rt.oy, rt.ex + vx * tof - rt.ox)
|
||||
let aim = a.computeAim(kernels, rt, ResidualPrediction(), rt.ox, rt.oy,
|
||||
arenaW, arenaH, ElmPower)
|
||||
let twinBearing = if aim.active: aim.aimBearing else: elm
|
||||
let twinPower = if aim.active: aim.power else: ElmPower
|
||||
# Launch the three candidates, then let the tournament pick.
|
||||
v.launchVirtual(vgTwin, rt.ox, rt.oy, twinBearing, twinPower)
|
||||
v.launchVirtual(vgElm, rt.ox, rt.oy, elm, ElmPower)
|
||||
v.launchVirtual(vgHeadOn, rt.ox, rt.oy, head, ElmPower)
|
||||
let chosen = v.selectGun()
|
||||
let (vb, vp) =
|
||||
case chosen
|
||||
of vgTwin: (twinBearing, twinPower)
|
||||
of vgElm: (elm, ElmPower)
|
||||
of vgHeadOn: (head, ElmPower)
|
||||
for (pol, bearing, power) in [(pHeadOn, head, ElmPower),
|
||||
(pElm, elm, ElmPower),
|
||||
(pTwin, (if aim.active: aim.aimBearing else: elm),
|
||||
(if aim.active: aim.power else: ElmPower))]:
|
||||
(pTwin, twinBearing, twinPower),
|
||||
(pVgun, vb, vp)]:
|
||||
result.mgetOrPut(pol, Tally()).shots.inc
|
||||
if simulateHit(rt.ox, rt.oy, bearing, power, rt.tick, emap, arenaW, arenaH):
|
||||
result[pol].hits.inc
|
||||
if aim.active: a.noteShot(aim, rt.tick)
|
||||
|
||||
proc synthesizeDodger(kernels: seq[Kernel]): Record =
|
||||
## A deterministic Dodger record: our bot sits at (600,300); a wave is fired
|
||||
## every 20 ticks; the enemy runs `dodgeDecision` through the physics mirror.
|
||||
## A deterministic Dodger-v2 record: our bot sits at (600,300); a wave is fired
|
||||
## every 12 ticks; the enemy runs `dodgeDecision` through the physics mirror.
|
||||
## Gives the bench a MOVING, DODGING opponent without Java.
|
||||
const W = 800.0
|
||||
const H = 600.0
|
||||
@@ -165,76 +189,87 @@ proc main() =
|
||||
|
||||
var rows: seq[string]
|
||||
var total: Table[Policy, Tally]
|
||||
var winOverBest = 0
|
||||
var winOverElm = 0
|
||||
var winOverHead = 0
|
||||
let movers = ["Crazy", "SpinBot", "VelociBot", "Walls", "Dodger"]
|
||||
var nOpp = 0
|
||||
echo "opponent twin% elm% head% n"
|
||||
echo "opponent twin% elm% head% vgun% n"
|
||||
for opp in opps:
|
||||
var t: Table[Policy, Tally]
|
||||
var v = initVirtualGuns() # one tournament per opponent
|
||||
if opp == "Dodger":
|
||||
t = evalRecord(synthesizeDodger(kernels), kernels, 800.0, 600.0)
|
||||
t = evalRecord(synthesizeDodger(kernels), kernels, 800.0, 600.0, v)
|
||||
else:
|
||||
let files = byOpp[opp]
|
||||
var used = 0
|
||||
for f in files:
|
||||
if used >= 5: break
|
||||
addInto(t, evalRecord(loadRecord(f), kernels, 800.0, 600.0))
|
||||
addInto(t, evalRecord(loadRecord(f), kernels, 800.0, 600.0, v))
|
||||
inc used
|
||||
let tw = t.getOrDefault(pTwin)
|
||||
let el = t.getOrDefault(pElm)
|
||||
let ho = t.getOrDefault(pHeadOn)
|
||||
let vg = t.getOrDefault(pVgun)
|
||||
addInto(total, t)
|
||||
inc nOpp
|
||||
if tw.hits >= el.hits: inc winOverElm
|
||||
if tw.hits >= ho.hits and opp in movers: inc winOverHead
|
||||
# vgun >= the best single gun on this opponent
|
||||
if vg.hits >= max(tw.hits, max(el.hits, ho.hits)): inc winOverBest
|
||||
if vg.hits >= el.hits and opp in movers: inc winOverElm
|
||||
echo alignLeft(opp, 12),
|
||||
align(formatFloat(tw.hitRate, ffDecimal, 1), 7),
|
||||
align(formatFloat(el.hitRate, ffDecimal, 1), 7),
|
||||
align(formatFloat(ho.hitRate, ffDecimal, 1), 7),
|
||||
align($tw.shots, 6)
|
||||
align(formatFloat(vg.hitRate, ffDecimal, 1), 7),
|
||||
align($vg.shots, 6)
|
||||
rows.add "| " & opp & " | " & formatFloat(tw.hitRate, ffDecimal, 1) & " | " &
|
||||
formatFloat(el.hitRate, ffDecimal, 1) & " | " &
|
||||
formatFloat(ho.hitRate, ffDecimal, 1) & " | " & $tw.shots & " |"
|
||||
formatFloat(ho.hitRate, ffDecimal, 1) & " | " &
|
||||
formatFloat(vg.hitRate, ffDecimal, 1) & " | " & $vg.shots & " |"
|
||||
|
||||
let passElm = winOverElm >= 6
|
||||
let passHead = winOverHead >= movers.len
|
||||
let verdict = if passElm and passHead: "PASS" else: "FAIL"
|
||||
echo "\nG8 offline: twin >= ELM on ", winOverElm, "/", nOpp,
|
||||
" (bar >= 6); twin >= head-on on ", winOverHead, "/", movers.len,
|
||||
let passBest = winOverBest >= 6
|
||||
let passElm = winOverElm == movers.len
|
||||
let verdict = if passBest and passElm: "PASS" else: "FAIL"
|
||||
echo "\nG8.5 offline: vgun >= best single gun on ", winOverBest, "/", nOpp,
|
||||
" (bar >= 6); vgun >= ELM-base on ", winOverElm, "/", movers.len,
|
||||
" movers; verdict ", verdict
|
||||
echo "SUITE twin ", formatFloat(total[pTwin].hitRate, ffDecimal, 1),
|
||||
"% elm ", formatFloat(total[pElm].hitRate, ffDecimal, 1),
|
||||
"% head ", formatFloat(total[pHeadOn].hitRate, ffDecimal, 1), "%"
|
||||
"% head ", formatFloat(total[pHeadOn].hitRate, ffDecimal, 1),
|
||||
"% vgun ", formatFloat(total[pVgun].hitRate, ffDecimal, 1), "%"
|
||||
|
||||
var md = "# G8 offline hit-rate bench (M7, twin gun)\n\n"
|
||||
md.add "Replayed archived trajectories (`" & dir & "`) + a synthesized Dodger; " &
|
||||
"hit = bullet passes within " & formatFloat(HitRadius, ffDecimal, 0) &
|
||||
" units of the realized enemy position. Aim slew/cadence abstracted. " &
|
||||
"Generated by `tests/bench_g8.nim`.\n\n"
|
||||
md.add "| opponent | twin % | ELM % | head-on % | shots |\n|---|---:|---:|---:|---:|\n"
|
||||
var md = "# G8.5 offline hit-rate bench (M7.5, virtual-gun tournament)\n\n"
|
||||
md.add "Replayed archived trajectories (`" & dir & "`) + a synthesized " &
|
||||
"Dodger-v2; hit = bullet passes within " &
|
||||
formatFloat(HitRadius, ffDecimal, 0) &
|
||||
" units of the realized enemy position on its flight. Aim slew/cadence " &
|
||||
"abstracted. Generated by `tests/bench_g8.nim`.\n\n"
|
||||
md.add "| opponent | twin % | ELM % | head-on % | vgun % | shots |\n" &
|
||||
"|---|---:|---:|---:|---:|---:|\n"
|
||||
for r in rows: md.add r & "\n"
|
||||
md.add "\n**G8 offline bar: twin >= ELM on >= 6/9 opponents AND twin >= head-on " &
|
||||
"on the movers.** Result: twin >= ELM on " & $winOverElm & "/" & $nOpp &
|
||||
"; twin >= head-on on " & $winOverHead & "/" & $movers.len &
|
||||
" movers. **Verdict: " & verdict & ".**\n"
|
||||
md.add "\n**G8.5 offline bar: vgun >= max(twin, ELM-base, head-on) on >= 6/9 " &
|
||||
"opponents AND vgun >= ELM-base on all movers (Crazy, SpinBot, " &
|
||||
"VelociBot, Walls, Dodger).** Result: vgun >= best single gun on " &
|
||||
$winOverBest & "/" & $nOpp & "; vgun >= ELM-base on " & $winOverElm &
|
||||
"/" & $movers.len & " movers. **Verdict: " & verdict & ".**\n"
|
||||
md.add "\n## Method / caveats\n\n"
|
||||
md.add "* `TWIN` = `chimera/aimer.nim`: iterated flight time \n" &
|
||||
" `h = dist/(20-3*power)`, horizon-bucketed predictor Hedge " &
|
||||
md.add "* `VGUN` = `chimera/virtual_guns.nim`: per-tick virtual bullets for " &
|
||||
"{TWIN, ELM, HEAD-ON}, a per-shot hit-rate EMA (tau=100) plus a " &
|
||||
"damage-weighted accumulator with tau=200 exponential forgetting, " &
|
||||
"argmax fired under 0.1 hysteresis. Classic Robocode Virtual Guns " &
|
||||
"pattern (https://robowiki.net/wiki/Virtual_Guns).\n"
|
||||
md.add "* `TWIN` = `chimera/aimer.nim`: iterated flight time " &
|
||||
"`h = dist/(20-3*power)`, horizon-bucketed predictor Hedge " &
|
||||
"(mixture/residual/persistence), trained ONLINE per record (one aimer, " &
|
||||
"score-then-aim each sampled tick), spread-selected power.\n"
|
||||
md.add "* `ELM` = the ELM gun's **base** geometric intercept from the " &
|
||||
"observed velocity (the learned correction is gated and small, and the " &
|
||||
"online ELM state cannot be replayed offline). This is a proxy for the " &
|
||||
"full ELM gun — a documented deviation.\n"
|
||||
md.add "* Power monotonicity: `selectPowerFromSpreads` returns the largest " &
|
||||
"power whose required spread fits the enemy-silhouette tolerance " &
|
||||
"`2*atan2(18, dist)`; the choice is non-increasing in spread " &
|
||||
"(`test_aimer.nim`, aimer 4).\n"
|
||||
md.add "* The synthesized Dodger fires a wave every 12 ticks; a real battle " &
|
||||
"would add shooter motion. Live results (`m7_g8_live.md`) are the " &
|
||||
"reality check.\n"
|
||||
let art = currentSourcePath().parentDir.parentDir / "benchmarks" / "m7_g8.md"
|
||||
md.add "* The synthesized Dodger is Dodger-v2 (`tests/archetypes/Dodger`): " &
|
||||
"wave-based perpendicular strafe, centre-pull, seeded reversals. It " &
|
||||
"fires a wave every 12 ticks; a real battle adds shooter motion. Live " &
|
||||
"results (`m7_5_g8_live.md`) are the reality check.\n"
|
||||
let art = currentSourcePath().parentDir.parentDir / "benchmarks" / "m7_5_g8.md"
|
||||
createDir(art.parentDir)
|
||||
writeFile(art, md)
|
||||
echo "Artifact: ", art
|
||||
|
||||
Reference in New Issue
Block a user