Datasets:
Download src/robotics/planner/engineered.py from Nabidnur/RoboRACE-Bench: direct link, hf CLI and curl.
- Browser
- Download file 5.91 kB
-
https://huggingface.co/datasets/Nabidnur/RoboRACE-Bench/resolve/main/src/robotics/planner/engineered.py
- Command line
-
hf download hf://datasets/Nabidnur/RoboRACE-Bench/src/robotics/planner/engineered.py
-
curl -L -o engineered.py https://huggingface.co/datasets/Nabidnur/RoboRACE-Bench/resolve/main/src/robotics/planner/engineered.py
5.91 kB
| """Engineered planner: prompt spec + few-shot + scene-grounded validator + repair loop. | |
| No model changes — pure engineering around any chat LLM: | |
| 1. a precise skill specification and worked examples (removes systematic errors such as | |
| 'move the tower base to the table first') | |
| 2. a symbolic, scene-grounded validator that simulates the plan (object/location grounding, | |
| only-move-what-was-asked, drawer open/closed state) and returns precise error messages | |
| 3. a repair loop that feeds those errors back to the model (max `repairs` rounds) | |
| """ | |
| from __future__ import annotations | |
| import json | |
| import re | |
| from robotics.planner.canon import _loc, canon_str | |
| from robotics.planner.task_planner import SKILLS, PlanError, parse_plan | |
| SPEC = """You are the task planner for a single robot arm on a tabletop. | |
| Skills (use ONLY these; JSON fields in brackets): | |
| pick [object] grasp an object (optional: place/stack grasp automatically) | |
| place [object, location] put the object in/on a container or another object | |
| stack [object, location] put a cube on top of another cube | |
| open [object] open a drawer (required before putting anything in it) | |
| close [object] close a drawer | |
| push [object, location] push an object somewhere | |
| home [] return the arm to rest | |
| Rules: | |
| - Objects and locations must be EXACT names from the scene list. | |
| - Move ONLY objects the instruction requires. Never move anything to 'table', 'center', | |
| 'home' or any location not in the scene. The bottom cube of a tower stays where it is. | |
| - Respect the order the instruction gives ("first ... then ..."). | |
| - If the instruction needs an object that is not in the scene, or an ability outside these | |
| skills (cutting, painting, throwing, leaving the table...), return | |
| {"steps": [], "infeasible_reason": "<why>"}. | |
| Respond with JSON only: {"steps": [{"skill": ..., "object": ..., "location": ...}]}""" | |
| FEWSHOT = [ | |
| ({"scene": ["red cube", "green cube", "blue cube", "white box"], | |
| "instr": "make a stack: green on the bottom, then blue, then red on top"}, | |
| {"steps": [{"skill": "stack", "object": "blue cube", "location": "green cube"}, | |
| {"skill": "stack", "object": "red cube", "location": "blue cube"}]}), | |
| ({"scene": ["yellow cube", "red cube", "drawer"], "instr": "put the red cube away in the drawer and close it"}, | |
| {"steps": [{"skill": "open", "object": "drawer"}, | |
| {"skill": "place", "object": "red cube", "location": "drawer"}, | |
| {"skill": "close", "object": "drawer"}]}), | |
| ({"scene": ["red cube", "blue bowl"], "instr": "put the orange cube in the bowl"}, | |
| {"steps": [], "infeasible_reason": "there is no orange cube in the scene"}), | |
| ] | |
| def messages(scene, instr, extra_system=""): | |
| msgs = [{"role": "system", "content": SPEC + extra_system}] | |
| for q, a in FEWSHOT: | |
| msgs += [{"role": "user", "content": f"Scene objects: {q['scene']}\nInstruction: {q['instr']}"}, | |
| {"role": "assistant", "content": json.dumps(a)}] | |
| msgs.append({"role": "user", "content": f"Scene objects: {scene}\nInstruction: {instr}"}) | |
| return msgs | |
| def validate(plan, scene, instr) -> list[str]: | |
| """Symbolic, scene-grounded checks. Returns a list of error strings (empty = ok).""" | |
| errs = [] | |
| names = {s.lower() for s in scene} | |
| text = instr.lower() | |
| # quantified instructions ("every cube", "all the cubes", "both") legitimately move objects | |
| # that are never named, so the only-move-what-was-asked check must not apply to them | |
| quantified = re.search(r"\b(every|all|each|both|everything|remaining|rest)\b", text) is not None | |
| drawer_open = False | |
| for i, s in enumerate(plan.steps, 1): | |
| obj = (s.object or "").lower().strip() | |
| loc = _loc(s.location) | |
| if obj and obj not in names: | |
| errs.append(f"step {i}: object '{s.object}' is not in the scene") | |
| if s.skill in ("place", "stack", "push"): | |
| if not loc: | |
| errs.append(f"step {i}: '{s.skill}' needs a location") | |
| elif loc not in names: | |
| errs.append(f"step {i}: location '{s.location}' is not a scene object; use one of {sorted(names)}") | |
| elif loc == obj: | |
| errs.append(f"step {i}: cannot put '{obj}' on itself") | |
| if s.skill in ("pick", "place", "stack", "push") and obj and obj in names and not quantified: | |
| head = obj.split()[0] | |
| if obj not in text and not re.search(rf"\b{re.escape(head)}\b", text): | |
| errs.append(f"step {i}: the instruction never asks to move '{obj}'") | |
| if s.skill == "open" and obj == "drawer": | |
| drawer_open = True | |
| if s.skill == "close" and obj == "drawer": | |
| drawer_open = False | |
| if s.skill in ("place", "push") and loc == "drawer" and not drawer_open: | |
| errs.append(f"step {i}: the drawer must be opened before putting something in it") | |
| return errs | |
| def plan_engineered(chat_fn, scene, instr, repairs=2, extra_system=""): | |
| """Returns (canonical answer, n_llm_calls, raw_last).""" | |
| msgs = messages(scene, instr, extra_system) | |
| calls, raw = 0, "" | |
| for attempt in range(repairs + 1): | |
| raw, _ = chat_fn(msgs) | |
| calls += 1 | |
| try: | |
| p = parse_plan(raw, None) | |
| errs = validate(p, scene, instr) | |
| except PlanError as e: | |
| p, errs = None, [str(e)] | |
| if not errs or attempt == repairs: | |
| if p is None: | |
| return f"INVALID:{errs[0][:40]}", calls, raw | |
| return canon_str(p.steps), calls, raw | |
| msgs = msgs + [{"role": "assistant", "content": raw}, | |
| {"role": "user", "content": "Your plan has errors:\n- " + "\n- ".join(errs) | |
| + "\nReturn the corrected JSON plan only."}] | |
| return "INVALID", calls, raw | |