STEM-RL-Tasks / tasks /ENG2 /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
2.41 kB
#!/usr/bin/env python3
"""Deterministic verifier for ENG2 (standard library only, terminal engineering quantity)."""
import json, math, pathlib, sys
EXPECTED = {"kramers_kronig_dispersed_rms_width_fs":501.3093}
REL_TOL = 2e-3
ABS_TOL = 1e-5
def verify(path: pathlib.Path) -> tuple[bool, float, list[str]]:
if not path.exists():
return False, 0.0, [f"Missing candidate file: {path}"]
try:
data = json.loads(path.read_text(encoding="utf-8"))
except Exception as e:
return False, 0.0, [f"Invalid JSON: {e}"]
if not isinstance(data, dict):
return False, 0.0, ["Candidate root must be a JSON object"]
passed = 0
errors = []
for k, exp in EXPECTED.items():
if k not in data:
errors.append(f"Missing key: {k}")
continue
val = data[k]
if isinstance(val, bool) or not isinstance(val, (int, float)) or not math.isfinite(float(val)):
errors.append(f"Non-finite or non-numeric value for {k}: {val!r}")
continue
val = float(val)
tol = max(ABS_TOL, REL_TOL * abs(exp))
if abs(val - exp) <= tol:
passed += 1
else:
errors.append(f"Mismatch for {k}: got {val:.6g}, expected {exp:.6g} (tol={tol:.3g})")
reward = round(passed / max(1, len(EXPECTED)), 2)
return (passed == len(EXPECTED) and not errors), reward, errors
# Portable JSON command-line adapter; numerical verification above is unchanged.
def evaluate_file(candidate_path):
from pathlib import Path as _Path
try:
_path = _Path(candidate_path)
if not _path.is_file():
raise ValueError("missing_candidate_file")
if _path.stat().st_size > 1048576:
raise ValueError("candidate_exceeds_size_limit")
_passed, _reward, _errors = verify(_path)
return {"passed": bool(_passed), "reward": float(_reward), "errors": _errors}
except (OSError, ValueError, TypeError, OverflowError) as _exc:
return {"passed": False, "reward": 0.0, "errors": ["invalid_candidate:" + str(_exc)]}
if __name__ == "__main__":
from pathlib import Path as _Path
_answer = sys.argv[1] if len(sys.argv) > 1 else _Path(__file__).resolve().parent / "solution" / "solution.json"
_result = evaluate_file(_answer)
print(json.dumps(_result, ensure_ascii=False))
raise SystemExit(0 if _result["passed"] else 1)