Datasets:
Download code/robohome/src/engine/source/packages/runtime/stack_batch.py from RoboParty/RoboHome: direct link, hf CLI and curl.
- Browser
- Download file 3.35 kB
-
https://huggingface.co/datasets/RoboParty/RoboHome/resolve/86ffa4ecc1047bb404fdc283987bd22cbc259a96/code/robohome/src/engine/source/packages/runtime/stack_batch.py
- Command line
-
hf download hf://datasets/RoboParty/RoboHome@86ffa4ecc1047bb404fdc283987bd22cbc259a96/code/robohome/src/engine/source/packages/runtime/stack_batch.py
-
curl -L -o stack_batch.py https://huggingface.co/datasets/RoboParty/RoboHome/resolve/86ffa4ecc1047bb404fdc283987bd22cbc259a96/code/robohome/src/engine/source/packages/runtime/stack_batch.py
3.35 kB
| from __future__ import annotations | |
| import time | |
| from collections.abc import Callable | |
| from dataclasses import dataclass | |
| from typing import Generic, TypeVar | |
| T = TypeVar('T') | |
| class StackJob: | |
| stack_id: str | |
| surface_id: str | |
| bbox_min_xy: tuple[float, float] | |
| bbox_max_xy: tuple[float, float] | |
| class StackBatchResult(Generic[T]): | |
| values: dict[str, T] | |
| batches: list[dict[str, object]] | |
| def group_stack_jobs(jobs: list[StackJob], margin_m: float = 0.05) -> list[list[StackJob]]: | |
| """Group stacks that share a support and may interact in one physics world.""" | |
| remaining = {job.stack_id: job for job in jobs} | |
| groups: list[list[StackJob]] = [] | |
| while remaining: | |
| _, first = remaining.popitem() | |
| group = [first] | |
| frontier = [first] | |
| while frontier: | |
| current = frontier.pop() | |
| touching = [ | |
| candidate | |
| for candidate in remaining.values() | |
| if _overlaps(current, candidate, margin_m) | |
| ] | |
| for candidate in touching: | |
| remaining.pop(candidate.stack_id) | |
| group.append(candidate) | |
| frontier.append(candidate) | |
| groups.append(sorted(group, key=lambda item: item.stack_id)) | |
| return sorted(groups, key=lambda group: group[0].stack_id) | |
| def run_stack_batches( | |
| groups: list[list[StackJob]], | |
| run_batch: Callable[[list[StackJob]], dict[str, T]], | |
| ) -> StackBatchResult[T]: | |
| """Execute stack worlds with deterministic bisection down to single-stack fallback.""" | |
| values: dict[str, T] = {} | |
| records: list[dict[str, object]] = [] | |
| def run(group: list[StackJob]) -> None: | |
| started = time.perf_counter() | |
| try: | |
| batch_values = run_batch(group) | |
| expected = {item.stack_id for item in group} | |
| if set(batch_values) != expected: | |
| raise ValueError("stack simulator returned an incomplete batch") | |
| values.update(batch_values) | |
| records.append( | |
| { | |
| "stack_ids": sorted(expected), | |
| "status": "complete", | |
| "duration_s": round(time.perf_counter() - started, 6), | |
| } | |
| ) | |
| except Exception as error: | |
| records.append( | |
| { | |
| "stack_ids": [item.stack_id for item in group], | |
| "status": "fallback" if len(group) > 1 else "failed", | |
| "duration_s": round(time.perf_counter() - started, 6), | |
| "error": repr(error), | |
| } | |
| ) | |
| if len(group) == 1: | |
| raise | |
| middle = len(group) // 2 | |
| run(group[:middle]) | |
| run(group[middle:]) | |
| for group in groups: | |
| run(group) | |
| return StackBatchResult(values=values, batches=records) | |
| def _overlaps(left: StackJob, right: StackJob, margin_m: float) -> bool: | |
| if left.surface_id != right.surface_id: | |
| return False | |
| return not ( | |
| left.bbox_max_xy[0] + margin_m < right.bbox_min_xy[0] | |
| or right.bbox_max_xy[0] + margin_m < left.bbox_min_xy[0] | |
| or left.bbox_max_xy[1] + margin_m < right.bbox_min_xy[1] | |
| or right.bbox_max_xy[1] + margin_m < left.bbox_min_xy[1] | |
| ) | |