File size: 2,686 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 | #!/usr/bin/env python3
import json, math, sys
from pathlib import Path
RULES = {"jarzynski_second_cumulant_dG_kBT":{"target":42.5,"kind":"real","rel_tol":0.002,"abs_tol":0.001},"mean_forward_dissipated_work_kBT":{"target":9.8,"kind":"pos","rel_tol":0.002,"abs_tol":0.001},"density_crossing_work_W_star_kBT":{"target":41.227372,"kind":"real","rel_tol":0.002,"abs_tol":0.001},"bhattacharyya_overlap_integral":{"target":0.012901,"kind":"prob","rel_tol":0.002,"abs_tol":0.0001},"transient_violation_probability":{"target":0.000844,"kind":"prob","rel_tol":0.002,"abs_tol":0.0001}}
def _check_val(v, r):
if isinstance(v, bool) or not isinstance(v, (int, float)):
return False
fv = float(v)
if not math.isfinite(fv):
return False
kind = r["kind"]
if kind == "int_nonneg":
return fv >= 0.0 and abs(fv - round(fv)) < 1e-9 and int(round(fv)) == int(r["target"])
if kind == "prob" and (fv < -1e-6 or fv > 1.0 + 1e-6):
return False
if kind == "pos" and fv <= 0.0:
return False
if kind == "nonneg" and fv < -1e-6:
return False
return math.isclose(fv, float(r["target"]), rel_tol=float(r["rel_tol"]), abs_tol=float(r["abs_tol"]))
def verify(candidate):
if isinstance(candidate, (str, Path)):
candidate = json.loads(Path(candidate).read_text(encoding="utf-8"))
if not isinstance(candidate, dict):
return False, list(RULES.keys())
errors = []
for k, r in RULES.items():
if k not in candidate or not _check_val(candidate[k], r):
errors.append(k)
return len(errors) == 0, 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, _errors = verify(_path)
_reward = round((len(RULES) - len(_errors)) / float(len(RULES)), 4)
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)
|