STEM-RL-Tasks / tasks /ASTRO2 /verify.py
przemekch's picture
Publish STEM RL Tasks: 40 tasks across eight domains with verifiers and recorded attempts
6395f35 verified
Raw History Blame Contribute Delete
1.75 kB
#!/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()