File size: 1,748 Bytes
6395f35
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
import json
import math
import sys
from pathlib import Path

PROBLEM_ID = "ASTRO2"
# key -> (expected, rel_tol, abs_tol)
EXPECTED = {
    "initial_horizon_angular_velocity_rad_s": (5691.6727, 0.0015, 0.0056916727),
    "saturated_black_hole_spin_a_sat": (0.69461391, 0.0015, 6.946139099999999e-07),
    "saturated_cloud_boson_number": (4.5212278e+77, 0.0015, 4.521227799999999e+71),
    "observed_annihilation_gw_frequency_hz": (1063.9369, 0.0015, 0.0010639368999999998),
}


def fail(reason, reward=0.0):
    print(json.dumps({"passed": False, "reward": reward, "reason": reason}))
    sys.exit(1)


def is_num(x):
    return isinstance(x, (int, float)) and not isinstance(x, bool) and math.isfinite(x)


def main():
    if len(sys.argv) < 2:
        fail("usage: verify.py <candidate.json>")
    p = Path(sys.argv[1])
    if not p.is_file():
        fail("candidate.json not found")
    try:
        data = json.loads(p.read_text(encoding="utf-8"))
    except Exception as e:
        fail(f"invalid JSON: {e}")
    if not isinstance(data, dict):
        fail("root must be a JSON object")
    if data.get("problem_id") != PROBLEM_ID:
        fail("wrong problem_id")

    wrong = []
    for k, (exp, rtol, atol) in EXPECTED.items():
        if k not in data:
            wrong.append(f"missing {k}")
            continue
        v = data[k]
        if not is_num(v) or not math.isclose(float(v), exp, rel_tol=rtol, abs_tol=atol):
            wrong.append(k)

    graded = len(EXPECTED)
    reward = round((graded - len(wrong)) / graded, 4)
    if wrong:
        fail("wrong: " + ", ".join(wrong), reward=reward)
    print(json.dumps({"passed": True, "reward": 1.0, "reason": "ok"}))


if __name__ == "__main__":
    main()