Datasets:
Download src/robotics/benchmarks/blocksworld.py from Nabidnur/RoboRACE-Bench: direct link, hf CLI and curl.
- Browser
- Download file 5.95 kB
-
https://huggingface.co/datasets/Nabidnur/RoboRACE-Bench/resolve/main/src/robotics/benchmarks/blocksworld.py
- Command line
-
hf download hf://datasets/Nabidnur/RoboRACE-Bench/src/robotics/benchmarks/blocksworld.py
-
curl -L -o blocksworld.py https://huggingface.co/datasets/Nabidnur/RoboRACE-Bench/resolve/main/src/robotics/benchmarks/blocksworld.py
5.95 kB
| """PlanBench Blocksworld (Valmeekam et al., NeurIPS 2023 D&B), task 1 = plan generation. | |
| Exact simulator + plan validator replacing VAL for this domain. Parses PlanBench's | |
| natural-language [STATEMENT]s, executes plans written in natural language | |
| ("unstack the red block from on top of the blue block") or PDDL ("(unstack red blue)"), | |
| and reports the first failing step with the violated precondition, which is the feedback | |
| signal for LLM-Modulo style repair (Kambhampati et al., ICML 2024). | |
| """ | |
| from __future__ import annotations | |
| import json | |
| import re | |
| from dataclasses import dataclass, field | |
| COLOR = r"([a-z]+)" | |
| class State: | |
| on: dict = field(default_factory=dict) # block -> block | "table" | "hand" | |
| holding: str | None = None | |
| def clear(self, b): | |
| return self.on.get(b) != "hand" and all(v != b for v in self.on.values()) | |
| def copy(self): | |
| return State(dict(self.on), self.holding) | |
| def parse_statement(text: str): | |
| """Initial state and goal facts of the LAST [STATEMENT] in a PlanBench query.""" | |
| stmt = text.split("[STATEMENT]")[-1] | |
| init_txt = stmt.split("My goal is to have that")[0] | |
| goal_txt = stmt.split("My goal is to have that")[1].split("My plan is as follows")[0] | |
| s = State() | |
| for a, b in re.findall(rf"the {COLOR} block is on top of the {COLOR} block", init_txt): | |
| s.on[a] = b | |
| for a in re.findall(rf"the {COLOR} block is on the table", init_txt): | |
| s.on[a] = "table" | |
| for a in re.findall(rf"the {COLOR} block is clear", init_txt): | |
| s.on.setdefault(a, None) | |
| goal = [(a, b) for a, b in re.findall(rf"the {COLOR} block is on top of the {COLOR} block", goal_txt)] | |
| goal += [(a, "table") for a in re.findall(rf"the {COLOR} block is on the table", goal_txt)] | |
| missing = [b for b, v in s.on.items() if v is None] | |
| if missing: | |
| raise ValueError(f"under-specified blocks {missing}") | |
| return s, goal | |
| _NL = [ | |
| (re.compile(rf"unstack (?:the )?{COLOR}(?: block)? from (?:on top of )?(?:the )?{COLOR}(?: block)?"), "unstack"), | |
| (re.compile(rf"stack (?:the )?{COLOR}(?: block)? on (?:top of )?(?:the )?{COLOR}(?: block)?"), "stack"), | |
| (re.compile(rf"pick up (?:the )?{COLOR}(?: block)?"), "pick-up"), | |
| (re.compile(rf"put down (?:the )?{COLOR}(?: block)?"), "put-down"), | |
| ] | |
| def parse_plan(text: str): | |
| """List of (action, args) from an LLM reply (NL or PDDL); stops at [PLAN END].""" | |
| text = re.sub(r"<think>.*?</think>", "", text, flags=re.S).split("[PLAN END]")[0].lower() | |
| steps = [] | |
| for line in text.splitlines(): | |
| line = line.strip().strip("*-•`").strip() | |
| line = re.sub(r"^\d+[\.\)]\s*", "", line) | |
| m = re.match(r"\((unstack|stack|pick-up|put-down)\s+([a-z]+)(?:\s+([a-z]+))?\)", line) | |
| if m: | |
| steps.append((m.group(1), tuple(x for x in m.groups()[1:] if x))) | |
| continue | |
| for rx, act in _NL: | |
| m = rx.match(line) | |
| if m: | |
| steps.append((act, m.groups())) | |
| break | |
| return steps | |
| def step(s: State, act, args): | |
| """Apply one action; returns error string or None.""" | |
| if act == "pick-up": | |
| (b,) = args[:1] | |
| if s.holding: | |
| return f"cannot pick up the {b} block: the hand is not empty (holding the {s.holding} block)" | |
| if s.on.get(b) != "table": | |
| return f"cannot pick up the {b} block: it is not on the table" | |
| if not s.clear(b): | |
| return f"cannot pick up the {b} block: it is not clear" | |
| s.on[b], s.holding = "hand", b | |
| elif act == "unstack": | |
| b, c = args[:2] | |
| if s.holding: | |
| return f"cannot unstack the {b} block: the hand is not empty (holding the {s.holding} block)" | |
| if s.on.get(b) != c: | |
| return f"cannot unstack the {b} block from the {c} block: it is not on top of the {c} block" | |
| if not s.clear(b): | |
| return f"cannot unstack the {b} block: it is not clear" | |
| s.on[b], s.holding = "hand", b | |
| elif act == "put-down": | |
| (b,) = args[:1] | |
| if s.holding != b: | |
| return f"cannot put down the {b} block: it is not being held" | |
| s.on[b], s.holding = "table", None | |
| elif act == "stack": | |
| b, c = args[:2] | |
| if s.holding != b: | |
| return f"cannot stack the {b} block: it is not being held" | |
| if not s.clear(c) or c == b: | |
| return f"cannot stack the {b} block on the {c} block: the {c} block is not clear" | |
| s.on[b], s.holding = c, None | |
| else: | |
| return f"unknown action {act}" | |
| return None | |
| def validate(query: str, plan_text: str): | |
| """-> dict(valid, reached, n_steps, error, unmet).""" | |
| s, goal = parse_statement(query) | |
| steps = parse_plan(plan_text) | |
| if not steps: | |
| return {"valid": False, "reached": False, "n_steps": 0, "error": "no parsable actions", "unmet": goal} | |
| for i, (act, args) in enumerate(steps, 1): | |
| if any(a not in s.on for a in args): | |
| return {"valid": False, "reached": False, "n_steps": len(steps), | |
| "error": f"step {i} ({act} {' '.join(args)}): unknown block", "unmet": goal} | |
| err = step(s, act, args) | |
| if err: | |
| return {"valid": False, "reached": False, "n_steps": len(steps), "error": f"step {i}: {err}", "unmet": goal} | |
| unmet = [(a, b) for a, b in goal if s.on.get(a) != b] | |
| return {"valid": True, "reached": not unmet, "n_steps": len(steps), "error": None, "unmet": unmet} | |
| def feedback(v) -> str: | |
| if v["error"]: | |
| return f"Your plan is not executable. {v['error'].capitalize()}." | |
| goals = ", ".join(f"the {a} block is {'on the table' if b == 'table' else f'on top of the {b} block'}" | |
| for a, b in v["unmet"]) | |
| return f"Your plan is executable but does not achieve the goal. Still unmet: {goals}." | |
| def load(path="data/planbench/task_1_plan_generation.json"): | |
| return json.load(open(path))["instances"] | |