Skip to content

The first exam for code agents in the physical world: frozen robot tasks from many benchmark suites, each answered with a program and verified in a separate sandbox. Runs on Harbor.

3.5 KB
Raw Blame
"""Grader for the ikea_table scene: all four legs screwed down onto the tabletop's studs.

The four legs are identical sub-goals, so every rung is ONE stage whose value is the fraction
of legs (k/4) that have reached it. The scene has no grasp contract and exposes no threshold
for "lifted", so a grasped/lifted rung cannot be measured from state and is not one; per leg
the ladder is aligned -> seated, equal weights, all measured in the tabletop's own frame
(order-independent: any leg on any stud, as the scene's `seated()` is):
    aligned  legs standing on a stud — within `align_xy` of the nearest stud and screw axis
             within `align_axis_deg` of the stud axis: the scene's own two non-depth seat gates
    seated   mean over legs of the screw-down from the stud-top rest height (`leg_start_z`, the
             scene's on-stud spawn height) to the seat (`seat_z`), counted only while aligned —
             so a leg's value is 1.0 iff the scene calls it `seated()`
The rungs nest (seated => aligned), so progress reaches 1.0 exactly when every leg is seated.
The scene welds a seated leg to the table, so a welded leg stays `seated()` however the
assembly is later moved. Success is the scene's own `success()` (every leg `seated()`), read
when the delivery finishes.
"""

from __future__ import annotations

import math

from robobench.core import BaseGrader
from robobench.suites.assembly.scenes.ikea_table_assembly import IkeaTableAssemblyScene


class IkeaTableAssemblyGrader(BaseGrader):
    """All four legs seated on the studs — each threaded down to depth, on a stud axis, upright.

    Ladder (per leg, each rung 1/8): aligned 1/8 · seated 2/8;
    1 leg 0.25 · 2 legs 0.50 · 3 legs 0.75 · 4 legs 1.00 — two equal rungs for the two
    measurable steps of each leg (set it on its stud, screw it down); the second is a linear
    ramp of the leg's height from the stud-top rest height (`leg_start_z`) to the seat (`seat_z`).
    """

    SCENE = IkeaTableAssemblyScene
    # Equal weights: the two rungs of one leg's ladder; every stage is a k/4 fraction.
    RUBRIC = (("aligned", 1), ("seated", 1))
    scene: IkeaTableAssemblyScene

    def setup(self) -> None:
        pass  # every rung reads the live state against the scene's cfg; nothing to capture

    def check_success(self):
        return self.scene.success()  # (num_envs,) — every leg of that env seated

    def _on_stud(self):
        """(aligned, thread), each (num_envs, num_legs): whether the leg passes the scene's
        xy + tilt seat gates at its nearest stud, and its screw-down fraction from the
        stud-top rest height to the seat."""
        import torch

        c = self.scene.cfg
        off = self.scene._leg_offsets_in_table()  # (n, L, 3): leg position in the slab frame
        studs = torch.tensor(c.slots, device=off.device, dtype=off.dtype)  # (S, 2)
        near = (off[:, :, None, :2] - studs[None, None]).norm(dim=-1).amin(dim=-1)
        aligned = (near <= c.align_xy) & (
            self.scene._leg_axis_cos() >= math.cos(math.radians(c.align_axis_deg)))
        thread = ((c.leg_start_z - off[..., 2]) / (c.leg_start_z - c.seat_z)).clamp(0.0, 1.0)
        return aligned, thread

    # ---- rubric stages — each returns a (num_envs,) fraction over that env's legs -----------
    def aligned(self):
        aligned, _ = self._on_stud()
        return aligned.float().mean(dim=1)

    def seated(self):
        aligned, thread = self._on_stud()
        return (aligned * thread).mean(dim=1)