amphora commited on
Commit
f2d0018
·
verified ·
1 Parent(s): 1ca0407

Initial release: 3,577 construct/optimize math RL tasks with deterministic graders (part 2)

Browse files
Files changed (42) hide show
  1. graders/rlve_integer_linear_system/check.py +10 -0
  2. graders/rlve_largest_convex_polygon/__pycache__/check.cpython-314.pyc +0 -0
  3. graders/rlve_largest_convex_polygon/check.py +27 -0
  4. graders/rlve_largest_rectangle/__pycache__/check.cpython-314.pyc +0 -0
  5. graders/rlve_largest_rectangle/check.py +24 -0
  6. graders/rlve_matrix_permutation_equivalence/__pycache__/check.cpython-314.pyc +0 -0
  7. graders/rlve_matrix_permutation_equivalence/check.py +15 -0
  8. graders/rlve_max_achromatic_coloring/__pycache__/check.cpython-314.pyc +0 -0
  9. graders/rlve_max_achromatic_coloring/check.py +16 -0
  10. graders/rlve_maximum_clique/__pycache__/check.cpython-314.pyc +0 -0
  11. graders/rlve_maximum_clique/check.py +16 -0
  12. graders/rlve_min_chromatic_number/__pycache__/check.cpython-314.pyc +0 -0
  13. graders/rlve_min_chromatic_number/check.py +10 -0
  14. graders/rlve_min_harmonious_coloring/__pycache__/check.cpython-314.pyc +0 -0
  15. graders/rlve_min_harmonious_coloring/check.py +15 -0
  16. graders/rlve_pairwise_sums_recovery/__pycache__/check.cpython-314.pyc +0 -0
  17. graders/rlve_pairwise_sums_recovery/check.py +14 -0
  18. graders/rlve_pan_solar_panels/__pycache__/check.cpython-314.pyc +0 -0
  19. graders/rlve_pan_solar_panels/check.py +12 -0
  20. graders/rlve_polynomial_global_minimum/__pycache__/check.cpython-314.pyc +0 -0
  21. graders/rlve_polynomial_global_minimum/check.py +19 -0
  22. graders/rlve_polynomial_integer_roots/__pycache__/check.cpython-314.pyc +0 -0
  23. graders/rlve_polynomial_integer_roots/check.py +20 -0
  24. graders/rlve_polynomial_interpolation/__pycache__/check.cpython-314.pyc +0 -0
  25. graders/rlve_polynomial_interpolation/check.py +13 -0
  26. graders/rlve_prefix_product_permutation/__pycache__/check.cpython-314.pyc +0 -0
  27. graders/rlve_prefix_product_permutation/check.py +15 -0
  28. graders/rlve_prefix_sum_permutation/__pycache__/check.cpython-314.pyc +0 -0
  29. graders/rlve_prefix_sum_permutation/check.py +15 -0
  30. graders/rlve_prime_graph_coloring/__pycache__/check.cpython-314.pyc +0 -0
  31. graders/rlve_prime_graph_coloring/check.py +20 -0
  32. graders/rlve_recursive_sequence_sum/__pycache__/check.cpython-314.pyc +0 -0
  33. graders/rlve_recursive_sequence_sum/check.py +29 -0
  34. graders/rlve_round_robin_scores/__pycache__/check.cpython-314.pyc +0 -0
  35. graders/rlve_round_robin_scores/check.py +24 -0
  36. graders/rlve_shortest_path_count/__pycache__/check.cpython-314.pyc +0 -0
  37. graders/rlve_shortest_path_count/check.py +34 -0
  38. graders/rlve_smallest_enclosing_circle/__pycache__/check.cpython-314.pyc +0 -0
  39. graders/rlve_smallest_enclosing_circle/check.py +17 -0
  40. graders/rlve_subgraph_isomorphism/__pycache__/check.cpython-314.pyc +0 -0
  41. graders/rlve_subgraph_isomorphism/check.py +16 -0
  42. graders/run.py +127 -0
graders/rlve_integer_linear_system/check.py ADDED
@@ -0,0 +1,10 @@
 
 
 
 
 
 
 
 
 
 
 
1
+ from run import Invalid, int_list
2
+
3
+
4
+ def check(inst, ans):
5
+ n = len(inst["coeffs"][0])
6
+ x = int_list(ans.get("x"), n, name="x")
7
+ for k, (e, r) in enumerate(zip(inst["coeffs"], inst["rhs"])):
8
+ if sum(c * v for c, v in zip(e, x)) != r:
9
+ raise Invalid(f"equation {k} is violated")
10
+ return None, {}
graders/rlve_largest_convex_polygon/__pycache__/check.cpython-314.pyc ADDED
Binary file (2.47 kB). View file
 
graders/rlve_largest_convex_polygon/check.py ADDED
@@ -0,0 +1,27 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from run import Invalid, int_list
2
+
3
+
4
+ def _cross(o, a, b):
5
+ return (a[0] - o[0]) * (b[1] - o[1]) - (a[1] - o[1]) * (b[0] - o[0])
6
+
7
+
8
+ def check(inst, ans):
9
+ P, k = inst["points"], inst["k"]
10
+ idx = int_list(ans.get("indices"), None, 0, len(P) - 1, "indices")
11
+ if len(set(idx)) != len(idx):
12
+ raise Invalid("indices must be distinct")
13
+ if len(idx) < k:
14
+ raise Invalid(f"{len(idx)} points given, need {k}")
15
+ pts = sorted(tuple(P[i]) for i in idx)
16
+ lower, upper = [], []
17
+ for p in pts: # monotone chain, strict (collinear points dropped)
18
+ while len(lower) >= 2 and _cross(lower[-2], lower[-1], p) <= 0:
19
+ lower.pop()
20
+ lower.append(p)
21
+ for p in reversed(pts):
22
+ while len(upper) >= 2 and _cross(upper[-2], upper[-1], p) <= 0:
23
+ upper.pop()
24
+ upper.append(p)
25
+ if len(lower) + len(upper) - 2 != len(pts):
26
+ raise Invalid("the chosen points are not in convex position")
27
+ return None, {}
graders/rlve_largest_rectangle/__pycache__/check.cpython-314.pyc ADDED
Binary file (2.52 kB). View file
 
graders/rlve_largest_rectangle/check.py ADDED
@@ -0,0 +1,24 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from run import Invalid, int_list
2
+
3
+
4
+ def check(inst, ans):
5
+ P = inst["points"]
6
+ idx = int_list(ans.get("indices"), 4, 0, len(P) - 1, "indices")
7
+ if len(set(idx)) != 4:
8
+ raise Invalid("indices must be distinct")
9
+ A, *rest = [tuple(P[i]) for i in idx]
10
+ if len(set([A] + rest)) != 4:
11
+ raise Invalid("points must be distinct")
12
+ # the vertex farthest from A is opposite; the other two are A's neighbours
13
+ rest.sort(key=lambda X: (X[0] - A[0]) ** 2 + (X[1] - A[1]) ** 2)
14
+ B, D, C = rest
15
+ u = (B[0] - A[0], B[1] - A[1])
16
+ v = (D[0] - A[0], D[1] - A[1])
17
+ if u[0] * v[0] + u[1] * v[1] != 0:
18
+ raise Invalid("not a rectangle (no right angle at the first vertex)")
19
+ if (B[0] + D[0] - A[0], B[1] + D[1] - A[1]) != C:
20
+ raise Invalid("not a rectangle")
21
+ area = abs(u[0] * v[1] - u[1] * v[0])
22
+ if area < inst["area"]:
23
+ raise Invalid(f"area {area} < {inst['area']}")
24
+ return None, {"area": area}
graders/rlve_matrix_permutation_equivalence/__pycache__/check.cpython-314.pyc ADDED
Binary file (1.37 kB). View file
 
graders/rlve_matrix_permutation_equivalence/check.py ADDED
@@ -0,0 +1,15 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from run import Invalid, int_list
2
+
3
+
4
+ def check(inst, ans):
5
+ A, B = inst["A"], inst["B"]
6
+ N, M = len(A), len(A[0])
7
+ a = int_list(ans.get("a"), N, 0, N - 1, "a")
8
+ b = int_list(ans.get("b"), M, 0, M - 1, "b")
9
+ if len(set(a)) != N or len(set(b)) != M:
10
+ raise Invalid("a and b must be permutations")
11
+ for i in range(N):
12
+ for j in range(M):
13
+ if A[a[i]][b[j]] != B[i][j]:
14
+ raise Invalid(f"mismatch at ({i}, {j})")
15
+ return None, {}
graders/rlve_max_achromatic_coloring/__pycache__/check.cpython-314.pyc ADDED
Binary file (1.45 kB). View file
 
graders/rlve_max_achromatic_coloring/check.py ADDED
@@ -0,0 +1,16 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from run import Invalid, int_list
2
+
3
+
4
+ def check(inst, ans):
5
+ n, k = inst["n"], inst["k"]
6
+ c = int_list(ans.get("colors"), n, 0, k - 1, "colors")
7
+ if len(set(c)) != k:
8
+ raise Invalid(f"uses {len(set(c))} colours, need all {k}")
9
+ pairs = set()
10
+ for u, v in inst["edges"]:
11
+ if c[u] == c[v]:
12
+ raise Invalid(f"edge ({u},{v}) is monochromatic")
13
+ pairs.add((min(c[u], c[v]), max(c[u], c[v])))
14
+ if len(pairs) != k * (k - 1) // 2:
15
+ raise Invalid("some pair of colours is not joined by an edge")
16
+ return None, {}
graders/rlve_maximum_clique/__pycache__/check.cpython-314.pyc ADDED
Binary file (1.57 kB). View file
 
graders/rlve_maximum_clique/check.py ADDED
@@ -0,0 +1,16 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from run import Invalid, int_list
2
+
3
+
4
+ def check(inst, ans):
5
+ n, k = inst["n"], inst["k"]
6
+ c = int_list(ans.get("clique"), None, 0, n - 1, "clique")
7
+ if len(set(c)) != len(c):
8
+ raise Invalid("vertices must be distinct")
9
+ if len(c) < k:
10
+ raise Invalid(f"clique has {len(c)} vertices, need {k}")
11
+ es = {(min(u, v), max(u, v)) for u, v in inst["edges"]}
12
+ for i in range(len(c)):
13
+ for j in range(i + 1, len(c)):
14
+ if (min(c[i], c[j]), max(c[i], c[j])) not in es:
15
+ raise Invalid(f"{c[i]} and {c[j]} are not adjacent")
16
+ return None, {}
graders/rlve_min_chromatic_number/__pycache__/check.cpython-314.pyc ADDED
Binary file (853 Bytes). View file
 
graders/rlve_min_chromatic_number/check.py ADDED
@@ -0,0 +1,10 @@
 
 
 
 
 
 
 
 
 
 
 
1
+ from run import Invalid, int_list
2
+
3
+
4
+ def check(inst, ans):
5
+ n, k = inst["n"], inst["k"]
6
+ c = int_list(ans.get("colors"), n, 0, k - 1, "colors")
7
+ for u, v in inst["edges"]:
8
+ if c[u] == c[v]:
9
+ raise Invalid(f"edge ({u},{v}) is monochromatic")
10
+ return None, {}
graders/rlve_min_harmonious_coloring/__pycache__/check.cpython-314.pyc ADDED
Binary file (1.19 kB). View file
 
graders/rlve_min_harmonious_coloring/check.py ADDED
@@ -0,0 +1,15 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from run import Invalid, int_list
2
+
3
+
4
+ def check(inst, ans):
5
+ n, k = inst["n"], inst["k"]
6
+ c = int_list(ans.get("colors"), n, 0, k - 1, "colors")
7
+ seen = set()
8
+ for u, v in inst["edges"]:
9
+ if c[u] == c[v]:
10
+ raise Invalid(f"edge ({u},{v}) is monochromatic")
11
+ pair = (min(c[u], c[v]), max(c[u], c[v]))
12
+ if pair in seen:
13
+ raise Invalid(f"colour pair {pair} appears on two edges")
14
+ seen.add(pair)
15
+ return None, {}
graders/rlve_pairwise_sums_recovery/__pycache__/check.cpython-314.pyc ADDED
Binary file (1.33 kB). View file
 
graders/rlve_pairwise_sums_recovery/check.py ADDED
@@ -0,0 +1,14 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from collections import Counter
2
+
3
+ from run import Invalid, int_list
4
+
5
+
6
+ def check(inst, ans):
7
+ n = inst["n"]
8
+ x = int_list(ans.get("numbers"), n, 1, None, "numbers")
9
+ if len(set(x)) != n:
10
+ raise Invalid("numbers must be distinct")
11
+ got = Counter(x[i] + x[j] for i in range(n) for j in range(i + 1, n))
12
+ if got != Counter(inst["sums"]):
13
+ raise Invalid("pairwise sums do not match")
14
+ return None, {}
graders/rlve_pan_solar_panels/__pycache__/check.cpython-314.pyc ADDED
Binary file (972 Bytes). View file
 
graders/rlve_pan_solar_panels/check.py ADDED
@@ -0,0 +1,12 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import math
2
+
3
+ from run import Invalid, as_int
4
+
5
+
6
+ def check(inst, ans):
7
+ X = as_int(ans.get("X"), "X", lo=inst["A"], hi=inst["B"])
8
+ Y = as_int(ans.get("Y"), "Y", lo=inst["C"], hi=inst["D"])
9
+ g = math.gcd(X, Y)
10
+ if g < inst["target"]:
11
+ raise Invalid(f"gcd(X, Y) = {g} < {inst['target']}")
12
+ return None, {"gcd": g}
graders/rlve_polynomial_global_minimum/__pycache__/check.cpython-314.pyc ADDED
Binary file (1.42 kB). View file
 
graders/rlve_polynomial_global_minimum/check.py ADDED
@@ -0,0 +1,19 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fractions import Fraction
2
+
3
+ from run import Invalid, num
4
+
5
+
6
+ def check(inst, ans):
7
+ x0 = Fraction(num(ans.get("x0"), "x0"))
8
+ if abs(x0) > 10 ** 6:
9
+ raise Invalid("x0 out of range")
10
+ v = Fraction(0)
11
+ for a in reversed(inst["coeffs"]):
12
+ v = v * x0 + a
13
+ fmin = Fraction(inst["fmin"])
14
+ tol = Fraction(1, 10 ** 9) * max(Fraction(1), abs(fmin))
15
+ if v > fmin + tol:
16
+ raise Invalid(f"f(x0) = {float(v)} is above the global minimum {float(fmin)}")
17
+ if v < fmin - tol: # cannot happen for a correct f_min; guards against a bad instance
18
+ raise Invalid("internal: f(x0) below recorded minimum")
19
+ return None, {"f_x0": float(v)}
graders/rlve_polynomial_integer_roots/__pycache__/check.cpython-314.pyc ADDED
Binary file (1.78 kB). View file
 
graders/rlve_polynomial_integer_roots/check.py ADDED
@@ -0,0 +1,20 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from run import Invalid, int_list
2
+
3
+
4
+ def check(inst, ans):
5
+ c = inst["coeffs"] # c[i] = coefficient of x^i, monic of degree len(c) - 1
6
+ n = len(c) - 1
7
+ r = int_list(ans.get("roots"), n, name="roots")
8
+ bound = 1 + max(abs(v) for v in c) # Cauchy bound on roots
9
+ if any(abs(a) > bound for a in r): # any integer root divides the constant term / is bounded by max|c_i|
10
+ raise Invalid("root out of range")
11
+ prod = [1]
12
+ for a in r: # multiply by (x - a)
13
+ nxt = [0] * (len(prod) + 1)
14
+ for i, v in enumerate(prod):
15
+ nxt[i + 1] += v
16
+ nxt[i] -= a * v
17
+ prod = nxt
18
+ if prod != c:
19
+ raise Invalid("the product does not expand to P(x)")
20
+ return None, {}
graders/rlve_polynomial_interpolation/__pycache__/check.cpython-314.pyc ADDED
Binary file (949 Bytes). View file
 
graders/rlve_polynomial_interpolation/check.py ADDED
@@ -0,0 +1,13 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from run import Invalid, int_list
2
+
3
+
4
+ def check(inst, ans):
5
+ X, Y = inst["X"], inst["Y"]
6
+ c = int_list(ans.get("coeffs"), len(X), name="coeffs")
7
+ for x, y in zip(X, Y):
8
+ v = 0
9
+ for a in reversed(c):
10
+ v = v * x + a
11
+ if v != y:
12
+ raise Invalid(f"f({x}) = {v}, need {y}")
13
+ return None, {}
graders/rlve_prefix_product_permutation/__pycache__/check.cpython-314.pyc ADDED
Binary file (994 Bytes). View file
 
graders/rlve_prefix_product_permutation/check.py ADDED
@@ -0,0 +1,15 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from run import Invalid, int_list
2
+
3
+
4
+ def check(inst, ans):
5
+ n = inst["n"]
6
+ p = int_list(ans.get("perm"), n, 1, n, "perm")
7
+ if len(set(p)) != n:
8
+ raise Invalid("not a permutation")
9
+ seen, acc = set(), 1
10
+ for v in p:
11
+ acc = acc * v % n
12
+ if acc in seen:
13
+ raise Invalid(f"prefix product residue {acc} repeats")
14
+ seen.add(acc)
15
+ return None, {}
graders/rlve_prefix_sum_permutation/__pycache__/check.cpython-314.pyc ADDED
Binary file (986 Bytes). View file
 
graders/rlve_prefix_sum_permutation/check.py ADDED
@@ -0,0 +1,15 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from run import Invalid, int_list
2
+
3
+
4
+ def check(inst, ans):
5
+ n = inst["n"]
6
+ p = int_list(ans.get("perm"), n, 1, n, "perm")
7
+ if len(set(p)) != n:
8
+ raise Invalid("not a permutation")
9
+ seen, acc = set(), 0
10
+ for v in p:
11
+ acc = (acc + v) % n
12
+ if acc in seen:
13
+ raise Invalid(f"prefix sum residue {acc} repeats")
14
+ seen.add(acc)
15
+ return None, {}
graders/rlve_prime_graph_coloring/__pycache__/check.cpython-314.pyc ADDED
Binary file (1.46 kB). View file
 
graders/rlve_prime_graph_coloring/check.py ADDED
@@ -0,0 +1,20 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from run import Invalid, int_list
2
+
3
+
4
+ def check(inst, ans):
5
+ n, k = inst["n"], inst["k"]
6
+ c = int_list(ans.get("colors"), n, 0, k - 1, "colors")
7
+ sieve = [True] * (n + 1)
8
+ sieve[0] = False
9
+ if n >= 1:
10
+ sieve[1] = False
11
+ for i in range(2, int(n ** 0.5) + 1):
12
+ if sieve[i]:
13
+ for j in range(i * i, n + 1, i):
14
+ sieve[j] = False
15
+ for p in range(2, n):
16
+ if sieve[p]:
17
+ for u in range(n - p):
18
+ if c[u] == c[u + p]:
19
+ raise Invalid(f"vertices {u + 1} and {u + p + 1} (difference {p}) share a colour")
20
+ return None, {}
graders/rlve_recursive_sequence_sum/__pycache__/check.cpython-314.pyc ADDED
Binary file (1.71 kB). View file
 
graders/rlve_recursive_sequence_sum/check.py ADDED
@@ -0,0 +1,29 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from run import Invalid, int_list
2
+
3
+
4
+ def F(inst, n):
5
+ F0, A, B = inst["F0"], inst["A"], inst["B"]
6
+ if A == 1:
7
+ return F0 + n * B
8
+ An = A ** n
9
+ return An * F0 + B * (An - 1) // (A - 1)
10
+
11
+
12
+ def check(inst, ans):
13
+ S = inst["S"]
14
+ idx = int_list(ans.get("indices"), None, 1, None, "indices")
15
+ if not idx:
16
+ raise Invalid("need at least one index")
17
+ if len(set(idx)) != len(idx):
18
+ raise Invalid("indices must be distinct")
19
+ total = 0
20
+ for n in idx:
21
+ # F is strictly increasing with F(n) >= n and, when A >= 2, F(n) >= 2^(n-1); bail out before big powers
22
+ if n > S or (inst["A"] >= 2 and n - 1 > S.bit_length()):
23
+ raise Invalid(f"F({n}) exceeds S")
24
+ total += F(inst, n)
25
+ if total > S:
26
+ raise Invalid("sum exceeds S")
27
+ if total != S:
28
+ raise Invalid(f"sum is {total}, need {S}")
29
+ return None, {}
graders/rlve_round_robin_scores/__pycache__/check.cpython-314.pyc ADDED
Binary file (1.91 kB). View file
 
graders/rlve_round_robin_scores/check.py ADDED
@@ -0,0 +1,24 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from run import Invalid
2
+
3
+
4
+ def check(inst, ans):
5
+ W = inst["W"]
6
+ n = len(W)
7
+ A = ans.get("A")
8
+ if not isinstance(A, list) or len(A) != n:
9
+ raise Invalid(f"A must be a list of {n} strings")
10
+ for r in A:
11
+ if not isinstance(r, str) or len(r) != n or set(r) - {"0", "1", "2"}:
12
+ raise Invalid(f"each row must be a string of {n} digits 0/1/2")
13
+ for i in range(n):
14
+ if A[i][i] != "0":
15
+ raise Invalid("diagonal must be 0")
16
+ w = 0
17
+ for j in range(n):
18
+ if i != j:
19
+ if int(A[i][j]) + int(A[j][i]) != 2:
20
+ raise Invalid(f"A[{i}][{j}] + A[{j}][{i}] != 2")
21
+ w += 3 * (A[i][j] == "2") + (A[i][j] == "1")
22
+ if w != W[i]:
23
+ raise Invalid(f"W[{i}] = {w}, need {W[i]}")
24
+ return None, {}
graders/rlve_shortest_path_count/__pycache__/check.cpython-314.pyc ADDED
Binary file (2.59 kB). View file
 
graders/rlve_shortest_path_count/check.py ADDED
@@ -0,0 +1,34 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from run import Invalid, as_int
2
+
3
+
4
+ def check(inst, ans):
5
+ n = as_int(ans.get("n"), "n", lo=2, hi=inst["n_max"])
6
+ adj = ans.get("adj")
7
+ if not isinstance(adj, list) or len(adj) != n:
8
+ raise Invalid(f"adj must be a list of {n} strings")
9
+ for r in adj:
10
+ if not isinstance(r, str) or len(r) != n or set(r) - {"Y", "N"}:
11
+ raise Invalid("each row must be a string of length n over {'Y','N'}")
12
+ for i in range(n):
13
+ if adj[i][i] != "N":
14
+ raise Invalid("self-loop")
15
+ for j in range(i + 1, n):
16
+ if adj[i][j] != adj[j][i]:
17
+ raise Invalid("matrix not symmetric")
18
+ nb = [[j for j in range(n) if adj[i][j] == "Y"] for i in range(n)]
19
+ dist = [-1] * n
20
+ cnt = [0] * n
21
+ dist[0], cnt[0] = 0, 1
22
+ q = [0]
23
+ for u in q: # BFS with exact (Python int) path counts
24
+ for v in nb[u]:
25
+ if dist[v] < 0:
26
+ dist[v] = dist[u] + 1
27
+ q.append(v)
28
+ if dist[v] == dist[u] + 1:
29
+ cnt[v] += cnt[u]
30
+ if dist[1] < 0:
31
+ raise Invalid("vertex 2 is unreachable from vertex 1")
32
+ if cnt[1] != inst["K"]:
33
+ raise Invalid(f"{cnt[1]} shortest paths, need {inst['K']}")
34
+ return None, {}
graders/rlve_smallest_enclosing_circle/__pycache__/check.cpython-314.pyc ADDED
Binary file (1.37 kB). View file
 
graders/rlve_smallest_enclosing_circle/check.py ADDED
@@ -0,0 +1,17 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import math
2
+
3
+ from run import Invalid, num
4
+
5
+ TOL = 1e-6
6
+
7
+
8
+ def check(inst, ans):
9
+ x, y, r = num(ans.get("x"), "x"), num(ans.get("y"), "y"), num(ans.get("r"), "r")
10
+ if r <= 0:
11
+ raise Invalid("r must be positive")
12
+ if r > inst["r_opt"] + TOL:
13
+ raise Invalid(f"r = {r} exceeds the minimum radius {inst['r_opt']} by more than {TOL}")
14
+ for px, py in inst["points"]:
15
+ if math.hypot(px - x, py - y) > r + TOL:
16
+ raise Invalid(f"point ({px}, {py}) is outside the circle")
17
+ return None, {"r": r}
graders/rlve_subgraph_isomorphism/__pycache__/check.cpython-314.pyc ADDED
Binary file (1.53 kB). View file
 
graders/rlve_subgraph_isomorphism/check.py ADDED
@@ -0,0 +1,16 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from run import Invalid, int_list
2
+
3
+
4
+ def check(inst, ans):
5
+ n1, n2 = inst["n1"], inst["n2"]
6
+ p = int_list(ans.get("p"), n1, 0, n2 - 1, "p")
7
+ if len(set(p)) != n1:
8
+ raise Invalid("p is not injective")
9
+ e1 = {(min(u, v), max(u, v)) for u, v in inst["edges1"]}
10
+ e2 = {(min(u, v), max(u, v)) for u, v in inst["edges2"]}
11
+ for u in range(n1):
12
+ for v in range(u + 1, n1):
13
+ a, b = min(p[u], p[v]), max(p[u], p[v])
14
+ if ((u, v) in e1) != ((a, b) in e2):
15
+ raise Invalid(f"pair ({u}, {v}) is not preserved")
16
+ return None, {}
graders/run.py ADDED
@@ -0,0 +1,127 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """rlmath runtime grader. Copied into every task as /tests/run.py next to /tests/check.py.
2
+
3
+ Deterministic; no LLM in the reward path. Runs with python3 + numpy/scipy/sympy/networkx only.
4
+
5
+ python3 /tests/run.py /tests/instance.json /workdir/answer.json
6
+
7
+ Prints the reward in [0, 1] and writes /logs/verifier/details.json when that directory exists.
8
+
9
+ check.py must define check(inst, ans) -> (value, extra_dict) and raise Invalid for a bad answer.
10
+ construct : value is ignored; a returned check means the witness is valid -> reward 1.0
11
+ optimize : value is the raw objective; reward = 0.1 + 0.9 * progress, where
12
+ progress = clip((s - baseline) / (best_known - baseline), 0, 1) (maximize)
13
+ clip((baseline - s) / (baseline - best_known), 0, 1) (minimize)
14
+ inst must carry "direction", "baseline", "best_known".
15
+ """
16
+ import json
17
+ import math
18
+ import os
19
+ import sys
20
+
21
+ VALID_FLOOR = 0.1
22
+
23
+
24
+ class Invalid(Exception):
25
+ pass
26
+
27
+
28
+ # ------------------------------------------------------------ input helpers
29
+
30
+ def int_list(v, n=None, lo=None, hi=None, name="list"):
31
+ if not isinstance(v, list):
32
+ raise Invalid(f"{name} must be a list")
33
+ if n is not None and len(v) != n:
34
+ raise Invalid(f"{name} must have length {n}, got {len(v)}")
35
+ for x in v:
36
+ if isinstance(x, bool) or not isinstance(x, int):
37
+ raise Invalid(f"{name} must contain integers only")
38
+ if (lo is not None and x < lo) or (hi is not None and x > hi):
39
+ raise Invalid(f"{name} entry {x} out of range [{lo}, {hi}]")
40
+ return v
41
+
42
+
43
+ def as_int(x, name="value", lo=None, hi=None):
44
+ return int_list([x], 1, lo, hi, name)[0]
45
+
46
+
47
+ def num(x, name="value"):
48
+ if isinstance(x, bool) or not isinstance(x, (int, float)):
49
+ raise Invalid(f"{name} must be a number")
50
+ if not math.isfinite(x):
51
+ raise Invalid(f"{name} is not finite")
52
+ return float(x)
53
+
54
+
55
+ def float_matrix(v, rows=None, cols=None, name="matrix"):
56
+ if not isinstance(v, list) or (rows is not None and len(v) != rows):
57
+ raise Invalid(f"{name} must be a list of {rows} rows")
58
+ out = []
59
+ for r in v:
60
+ if not isinstance(r, list) or (cols is not None and len(r) != cols):
61
+ raise Invalid(f"each row of {name} must have {cols} numbers")
62
+ out.append([num(x, name) for x in r])
63
+ return out
64
+
65
+
66
+ def float_list(v, n=None, name="list"):
67
+ if not isinstance(v, list) or (n is not None and len(v) != n):
68
+ raise Invalid(f"{name} must be a list of length {n}")
69
+ return [num(x, name) for x in v]
70
+
71
+
72
+ # ------------------------------------------------------------------ reward
73
+
74
+ def optimize_reward(inst, s):
75
+ b, best = inst["baseline"], inst["best_known"]
76
+ if inst.get("progress_scale") == "log":
77
+ # for non-negative losses spanning orders of magnitude (e.g. overlap loss -> 0);
78
+ # progress is measured in log10(value + floor)
79
+ f = inst.get("log_floor", 1e-3)
80
+ b, best, s = (math.log10(max(x, 0.0) + f) for x in (b, best, s))
81
+ if inst["direction"] == "maximize":
82
+ p = (s - b) / (best - b)
83
+ else:
84
+ p = (b - s) / (b - best)
85
+ return VALID_FLOOR + (1 - VALID_FLOOR) * max(0.0, min(1.0, p))
86
+
87
+
88
+ def grade(inst, ans, check):
89
+ try:
90
+ if not isinstance(ans, dict):
91
+ raise Invalid("answer must be a JSON object")
92
+ value, extra = check(inst, ans)
93
+ if inst.get("subset") == "optimize":
94
+ s = float(value)
95
+ if not math.isfinite(s):
96
+ raise Invalid("non-finite score")
97
+ return optimize_reward(inst, s), {"valid": True, "raw_score": s, **(extra or {})}
98
+ return 1.0, {"valid": True, **(extra or {})}
99
+ except Invalid as e:
100
+ return 0.0, {"valid": False, "error": str(e)}
101
+ except Exception as e: # any malformed answer
102
+ return 0.0, {"valid": False, "error": f"{type(e).__name__}: {e}"}
103
+
104
+
105
+ def main():
106
+ here = os.path.dirname(os.path.abspath(__file__))
107
+ sys.path.insert(0, here)
108
+ # import ourselves as `run` so check.py's `from run import Invalid` is the same class
109
+ import run as R # noqa: E402
110
+ import check as family # noqa: E402
111
+
112
+ inst = json.load(open(sys.argv[1]))
113
+ try:
114
+ ans = json.load(open(sys.argv[2]))
115
+ except Exception as e:
116
+ reward, det = 0.0, {"valid": False, "error": f"unreadable answer: {e}"}
117
+ else:
118
+ reward, det = R.grade(inst, ans, family.check)
119
+ det["reward"] = reward
120
+ if os.path.isdir("/logs/verifier"):
121
+ with open("/logs/verifier/details.json", "w") as f:
122
+ json.dump(det, f)
123
+ print(reward)
124
+
125
+
126
+ if __name__ == "__main__":
127
+ main()