Download source/proxy_eval/tasks/math_tasks.py from rca-umb/five-desks-control: direct link, hf CLI and curl.
- Browser
- Download file 4.73 kB
-
https://huggingface.co/datasets/rca-umb/five-desks-control/resolve/main/source/proxy_eval/tasks/math_tasks.py
- Command line
-
hf download hf://datasets/rca-umb/five-desks-control/source/proxy_eval/tasks/math_tasks.py
-
curl -L -o math_tasks.py https://huggingface.co/datasets/rca-umb/five-desks-control/resolve/main/source/proxy_eval/tasks/math_tasks.py
4.73 kB
| """Math and formal reasoning: one-shot problems with an exact answer and a single-field action.""" | |
| import itertools | |
| import math | |
| from fractions import Fraction | |
| from pydantic import Field | |
| from ..core import Action, ProxyTask, Step | |
| class AnswerAction(Action): | |
| answer: str = Field(description="Your final answer, exactly in the requested format. One reply only.") | |
| def norm(text): | |
| return str(text).strip().lower().replace(" ", "").rstrip(".") | |
| class OneShotMath(ProxyTask): | |
| family = "math-oneshot" | |
| domain = "mathematics-or-formal-reasoning" | |
| Action = AnswerAction | |
| max_steps = 1 | |
| finish_action = {"answer": ""} | |
| def setup(self): | |
| r = self.rng | |
| v = self.seed % 5 | |
| if v == 0: # lattice paths avoiding a blocked point | |
| w, h = r.randint(4, 7), r.randint(4, 7) | |
| bx, by = r.randint(1, w - 1), r.randint(1, h - 1) | |
| total = math.comb(w + h, w) - math.comb(bx + by, bx) * math.comb(w - bx + h - by, w - bx) | |
| self.text = (f"How many shortest grid paths go from (0,0) to ({w},{h}) moving only right or up, " | |
| f"without passing through the point ({bx},{by})? Answer with an integer.") | |
| self.truth = str(total) | |
| elif v == 1: # probability as a reduced fraction | |
| red, blue = r.randint(3, 7), r.randint(3, 7) | |
| k = 3 | |
| p = Fraction(math.comb(red, 2) * blue, math.comb(red + blue, k)) | |
| self.text = (f"A bag has {red} red and {blue} blue marbles. Three are drawn without replacement. What " | |
| f"is the probability that exactly two are red? Answer as a reduced fraction a/b.") | |
| self.truth = f"{p.numerator}/{p.denominator}" | |
| elif v == 2: # bounded integer solutions | |
| n, k, cap = r.randint(10, 16), 3, r.randint(5, 7) | |
| count = sum(1 for xs in itertools.product(range(cap + 1), repeat=k) if sum(xs) == n) | |
| self.text = (f"How many ordered triples of integers (x, y, z) with 0 <= x, y, z <= {cap} satisfy " | |
| f"x + y + z = {n}? Answer with an integer.") | |
| self.truth = str(count) | |
| elif v == 3: # last digits of a power | |
| a, b = r.choice([3, 7, 13, 17]), r.randint(200, 999) | |
| self.text = f"What are the last two digits of {a}^{b}? Answer with two digits, e.g. 07." | |
| self.truth = f"{pow(a, b, 100):02d}" | |
| else: # knights and knaves | |
| while True: | |
| truth = [r.random() < 0.5 for _ in range(3)] | |
| names = ["A", "B", "C"] | |
| stmts = [] | |
| for i in range(3): | |
| j = r.choice([x for x in range(3) if x != i]) | |
| kind = r.choice(["is_knave", "same", "count"]) | |
| stmts.append((i, j, kind, r.randint(1, 2))) | |
| def holds(world, s): | |
| i, j, kind, c = s | |
| if kind == "is_knave": | |
| return not world[j] | |
| if kind == "same": | |
| return world[i] == world[j] | |
| return sum(world) == c | |
| worlds = [w for w in itertools.product([True, False], repeat=3) | |
| if all(holds(w, s) == w[s[0]] for s in stmts)] | |
| if len(worlds) == 1: | |
| break | |
| say = {"is_knave": lambda i, j, c: f"{names[j]} is a knave.", | |
| "same": lambda i, j, c: f"{names[j]} and I are the same type.", | |
| "count": lambda i, j, c: f"Exactly {c} of us {'is a knight' if c == 1 else 'are knights'}."} | |
| lines = " ".join(f"{names[i]} says: \"{say[kind](i, j, c)}\"" for i, j, kind, c in stmts) | |
| self.text = (f"On an island, knights always tell the truth and knaves always lie. {lines} Which of A, " | |
| f"B, C are knights? Answer with the letters in alphabetical order separated by commas, " | |
| f"or 'none'.") | |
| knights = [names[i] for i in range(3) if worlds[0][i]] | |
| self.truth = ",".join(knights) if knights else "none" | |
| def reset(self): | |
| return {"problem": self.text} | |
| def act(self, a): | |
| got = norm(a.answer) | |
| ok = got == norm(self.truth) | |
| if not ok and "/" in self.truth: | |
| try: | |
| ok = Fraction(got) == Fraction(self.truth) and "/" in got | |
| ok = ok and Fraction(got).denominator == int(got.split("/")[1]) # must be reduced | |
| except (ValueError, ZeroDivisionError): | |
| ok = False | |
| return Step({"problem": "", "result": "recorded"}, True, 1.0 if ok else 0.0) | |
| def oracle(self): | |
| return [{"answer": self.truth}] | |