Download graders/ea_edges_vs_triangles/check.py from amphora/MathConstructOptimize-Envs: direct link, hf CLI and curl.
- Browser
- Download file 2.44 kB
-
https://huggingface.co/datasets/amphora/MathConstructOptimize-Envs/resolve/main/graders/ea_edges_vs_triangles/check.py
- Command line
-
hf download hf://datasets/amphora/MathConstructOptimize-Envs/graders/ea_edges_vs_triangles/check.py
-
curl -L -o check.py https://huggingface.co/datasets/amphora/MathConstructOptimize-Envs/resolve/main/graders/ea_edges_vs_triangles/check.py
2.44 kB
| """Edges vs triangles (Razborov) upper-bound curve (EinsteinArena edges-vs-triangles, MIT). | |
| Each row is a probability vector over 20 parts (a complete 20-partite graphon); edge density | |
| 1 - sum w^2 ... (upstream uses S1^2 - S2 and S1^3 - 3 S1 S2 + 2 S3). Score = -(area under the slope-3 | |
| envelope + 10 * max gap) (maximize). Ported verbatim from the upstream verifier. Changes: at most | |
| inst["max_rows"] rows (the description says m <= 500 but upstream did not enforce it); NaN/inf rejected.""" | |
| import numpy as np | |
| from run import Invalid, float_matrix | |
| def _densities(A): | |
| S1 = A.sum(1) | |
| S2 = (A ** 2).sum(1) | |
| S3 = (A ** 3).sum(1) | |
| return S1 ** 2 - S2, S1 ** 3 - 3 * S1 * S2 + 2 * S3 | |
| def _curve(ex, ty): | |
| slope = 3.0 | |
| o = np.argsort(ex) | |
| fx = np.concatenate(([0.0], ex[o], [1.0])) | |
| fy = np.concatenate(([0.0], ty[o], [1.0])) | |
| ux, inv = np.unique(fx, return_inverse=True) | |
| my = np.full(len(ux), np.inf) | |
| np.minimum.at(my, inv, fy) | |
| fx, fy = ux, my | |
| if len(fx) < 2: | |
| return 5.0 / 6.0, 1.0 | |
| left = np.minimum.accumulate(fy - slope * fx) | |
| right = np.minimum.accumulate(fy[::-1])[::-1] | |
| area = 0.0 | |
| for i in range(len(fx) - 1): | |
| x0, x1 = fx[i], fx[i + 1] | |
| w = x1 - x0 | |
| if w <= 0: | |
| continue | |
| cap = min(float(right[i + 1]), 1.0) | |
| yl, yr = slope * x0 + left[i], slope * x1 + left[i] | |
| if yl >= cap: | |
| area += cap * w | |
| elif yr <= cap: | |
| area += (yl + yr) * w / 2.0 | |
| else: | |
| xh = (cap - left[i]) / slope | |
| area += (yl + cap) * (xh - x0) / 2.0 + cap * (x1 - xh) | |
| gaps = np.diff(fx) | |
| idx = np.where((fx[:-1] >= 0.0) & (fx[:-1] < 1.0))[0] | |
| return area, float(np.max(gaps[idx])) if idx.size else 0.0 | |
| def check(inst, ans): | |
| w = ans.get("weights") | |
| if not isinstance(w, list) or not 1 <= len(w) <= inst["max_rows"]: | |
| raise Invalid(f"weights must be a list of 1..{inst['max_rows']} rows") | |
| A = np.array(float_matrix(w, None, inst["parts"], "weights")) | |
| if np.any(A < 0): | |
| raise Invalid("weights must be non-negative") | |
| s = A.sum(1) | |
| if np.any(s < 1e-7): | |
| raise Invalid("a row sums to (nearly) zero") | |
| A = A / s[:, None] | |
| e, t = _densities(A) | |
| area, gap = _curve(e, t) | |
| val = -(area + 10 * gap) | |
| if not np.isfinite(val): | |
| raise Invalid("non-finite score") | |
| return float(val), {"area": float(area), "max_gap": gap} | |