RoboRACE-Bench / scripts /tune_trackD.py
Nabidnur's picture
sync scripts: RACE-Net-M, Track D baselines, RACE-Net-V v3
b2ead12 verified
Raw History Blame Contribute Delete
2.26 kB
#!/usr/bin/env python3
"""Development-set tuning for Track D (mazes with seeds 0-39; the benchmark uses 1000-1019).
python scripts/tune_trackD.py # compares fusion rules and risk-planner settings on dev mazes
Writes results/trackD/dev_tuning.json. Nothing here touches the benchmark seeds.
"""
import itertools
import json
import sys
from multiprocessing import Pool
from pathlib import Path
import numpy as np
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "src"))
from robotics.bench import trackD # noqa: E402
def _job(args):
seed, attack, F, method, tau = args
if tau is not None:
trackD.NET_TAU = tau
return trackD.episode(seed, attack, F, method)["spl"]
def score(method, seeds, tau=None, pool=None):
jobs = [(s, a, F, method, tau) for a in trackD.ATTACKS_D for F in ((0,) if a == "none" else (1, 2, 3, 4)) for s in seeds]
spl = pool.map(_job, jobs, chunksize=8)
by = {}
for (s, a, F, *_), v in zip(jobs, spl):
by.setdefault(a, []).append(v)
return float(np.mean(spl)), {a: round(100 * float(np.mean(v)), 1) for a, v in by.items()}
def main():
n = int(sys.argv[1]) if len(sys.argv) > 1 else 12
only = sys.argv[2].split(",") if len(sys.argv) > 2 else None
seeds = list(range(n))
out = {"dev_seeds": [0, n - 1], "methods": {}, "grid": {}}
with Pool(4) as pool:
for m in trackD.available_methods():
if only is None or "methods" in only:
mean, by = score(m, seeds, pool=pool)
out["methods"][m] = {"mean": mean, **by}
print(f"{m:30s} {100 * mean:5.1f}", by, flush=True)
if only is None or "grid" in only:
for m, tau in itertools.product(("race_net_m", "race_net_m" + trackD.MV), (1.0, 2.0, 3.0)):
mean, by = score(m, seeds, tau, pool)
out["grid"][f"{m}|tau={tau}"] = {"mean": mean, **by}
print(f"{m} tau {tau}: {100 * mean:5.1f}", by, flush=True)
f = Path("results/trackD/dev_tuning.json")
prev = json.loads(f.read_text()) if f.exists() else {}
for k in ("methods", "grid"):
out[k] = {**prev.get(k, {}), **out[k]}
f.write_text(json.dumps(out, indent=1))
if __name__ == "__main__":
main()