Train the aim-error ELM on the pooled 9-opponent corpus and export it
train_elm_gun.nim trains H=128, lambda=1e-2, decay=1.0 over every opponent in tools/elm_bench/gen and writes modules/ELM/elm_gun_weights.txt (trainRmse 0.431 vs a constant baseline 0.933). Pooled is the point: the gun meets opponents it was not fitted on.
This commit is contained in:
File diff suppressed because one or more lines are too long
@@ -0,0 +1,99 @@
|
||||
#!/usr/bin/env nim c -r
|
||||
# Train the aim-error ELM on the POOLED multi-opponent corpus and export the
|
||||
# weights for the in-game gun.
|
||||
#
|
||||
# cd DevControlBot_garage/DevControlBot
|
||||
# nim c -r ../../tools/elm_bench/train_elm_gun.nim
|
||||
#
|
||||
# Corpus: tools/elm_bench/gen/*.ndjson (the `*_prev.ndjson` variants are the
|
||||
# strictly-prior-scan ablation and are NOT used — the live bot legitimately sees
|
||||
# the same tick's scan it fires on). Features are rebuilt from each record's
|
||||
# belief_* fields into a GunBelief and passed through the SAME buildFeatures the
|
||||
# gun uses, so training and inference cannot diverge.
|
||||
#
|
||||
# Output: modules/ELM/elm_gun_weights.txt (plain text, documented in elm_gun.nim)
|
||||
|
||||
import std/[algorithm, json, math, os, sequtils, strutils]
|
||||
import modules/ELM/elm
|
||||
import modules/ELM/elm_features
|
||||
|
||||
const
|
||||
HiddenDim = 128
|
||||
Lambda = 1e-2 # pooled training: the corpus is 9 opponents, so more
|
||||
# regularisation than the single-opponent 1e-3 run
|
||||
Decay = 1.0 # no forgetting: every shot of every opponent is a prior
|
||||
Seed = 12345
|
||||
CorpusDir = "../../tools/elm_bench/gen"
|
||||
OutFile = "modules/ELM/elm_gun_weights.txt"
|
||||
|
||||
proc reconstruct(j: JsonNode): GunBelief =
|
||||
## Corpus belief_* fields -> the GunBelief the gun builds at runtime.
|
||||
## (Everything is stored body-relative in the corpus, so undo ownHeading to
|
||||
## get the absolute line-of-sight angle back.)
|
||||
result.everScanned = j["beliefEverScanned"].getFloat > 0.5
|
||||
result.selfX = j["ownX"].getFloat
|
||||
result.selfY = j["ownY"].getFloat
|
||||
result.selfHeading = j["ownHeading"].getFloat
|
||||
result.selfSpeed = j["ownSpeed"].getFloat
|
||||
result.gunHeat = j["gunHeat"].getFloat
|
||||
result.power = j["power"].getFloat
|
||||
result.bulletSpeed = j["bulletSpeed"].getFloat # = power * 10 in this corpus
|
||||
result.scanAge = j["beliefScanAge"].getFloat
|
||||
result.scanGap = j["beliefScanGap"].getFloat
|
||||
result.ownDisp = j["beliefDisp"].getFloat
|
||||
let ang = j["beliefRelBearing"].getFloat + result.selfHeading
|
||||
let de = j["beliefDist"].getFloat
|
||||
result.ex = result.selfX + de * cos(ang)
|
||||
result.ey = result.selfY + de * sin(ang)
|
||||
let lat = j["beliefTgtLatVel"].getFloat
|
||||
let rad = -j["beliefClosing"].getFloat # closing is -rad
|
||||
result.vx = rad * cos(ang) - lat * sin(ang)
|
||||
result.vy = rad * sin(ang) + lat * cos(ang)
|
||||
|
||||
proc main() =
|
||||
var e = initElm(ElmConfig(inputDim: FeatureDim, hiddenDim: HiddenDim, outputs: 1,
|
||||
seed: Seed, lambda: Lambda, decay: Decay, refitEvery: 0))
|
||||
var n = 0
|
||||
var files = sorted(toSeq(walkFiles(CorpusDir / "*.ndjson")))
|
||||
var xs = newSeq[float64]()
|
||||
var ts = newSeq[float64]()
|
||||
for f in files:
|
||||
if f.endsWith("_prev.ndjson"): continue
|
||||
var k = 0
|
||||
for line in lines(f):
|
||||
if line.len == 0: continue
|
||||
let j = parseJson(line)
|
||||
var x: seq[float64] = @[]
|
||||
for v in buildFeatures(reconstruct(j)): x.add v
|
||||
e.train(x, @[j["label"].getFloat])
|
||||
xs.add j["label"].getFloat
|
||||
inc k
|
||||
echo " ", f.extractFilename(), ": ", k, " samples"
|
||||
n += k
|
||||
e.refit()
|
||||
let d = diagnostics(e)
|
||||
echo "samples=", n, " trainRmse=", d.trainRmse
|
||||
|
||||
var s2 = 0.0
|
||||
for i in 0 ..< xs.len: s2 += xs[i] * xs[i]
|
||||
echo "label RMS (constant baseline) = ", sqrt(s2 / float64(xs.len))
|
||||
|
||||
let w = exportWeights(e)
|
||||
var o = open(OutFile, fmWrite)
|
||||
o.write("# DevControlBot ELM gun — exported ELM weights. Plain text, one matrix per line.\n")
|
||||
o.write("# inputDim hiddenDim outputs lambda decay seed activation featureCount\n")
|
||||
o.write("# ")
|
||||
o.write(featureNames.join(" "))
|
||||
o.write("\n")
|
||||
o.write(FeatureDim, " ", HiddenDim, " 1 ", Lambda, " ", Decay, " ", Seed, " tanh\n")
|
||||
o.write("W ", w.W.len, "\n")
|
||||
o.write(w.W.join(" "), "\n")
|
||||
o.write("HS ", w.hs.len, "\n")
|
||||
o.write(w.hs.join(" "), "\n")
|
||||
o.write("BETA ", w.beta.len, "\n")
|
||||
o.write(w.beta.join(" "), "\n")
|
||||
o.write("TRAIN_RMSE ", d.trainRmse, "\n")
|
||||
o.close()
|
||||
echo "wrote ", OutFile
|
||||
|
||||
main()
|
||||
Reference in New Issue
Block a user