File size: 2,260 Bytes
b2ead12
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#!/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()