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.1 KB
Raw Blame
"""goalkeeper-save reference solution (privileged): the H1-2 walks along the goal line (Unitree's walking policy, closed
loop on the privileged pelvis pose) so that its body is in each shot's line, stops there with its feet on the line
before the kick, and lets the ball hit its body (arms at rest); the ball comes off it into the field.

Where to stand: first the stated aim of each machine. A machine scatters (about 2 degrees: up to 0.4 m at the goal
line), but it is deterministic, so the privileged session's unlimited resets are used: whenever a shot is kicked, its
ball's crossing of the goal line is predicted from its flight (straight-line extrapolation of its last 1 m before the
line); if an attempt fails, the next attempt stands where the learned shots actually cross, and where a ball came off the
body into the goal from there, a little beside that line (each attempt is a full episode from the frozen start; the
rows of the last attempt are the trajectory)."""
import numpy as np

SHOTS = 3


def attempt(s, stand_y, gain=1.5, tol=0.02, leave_after_s=0.8):
    w = s.world
    T = w.task
    p = T.p
    rest = w.robot.rest[12:]
    seen = {}
    prev = [w.pos(f"ball{k}") for k in range(1, SHOTS + 1)]
    while not s.done:
        tr = T.trackers
        # the shot to get ready for: the first whose kick has not happened (or happened less than leave_after_s ago)
        k = next((i for i in range(SHOTS) if tr["kick_t"][i] is None or w.t < tr["kick_t"][i] + leave_after_s), SHOTS - 1)
        x, y, _, yaw = w.pelvis()
        ex, ey = 0.0 - x, stand_y[k] - y
        if tr["kick_t"][k] is not None or (abs(ey) < tol and abs(ex) < 0.05):
            cmd = (0.0, 0.0, float(np.clip(-1.5 * yaw, -0.5, 0.5)))
        else:
            c, sn = np.cos(yaw), np.sin(yaw)
            cmd = (float(np.clip(gain * (c * ex + sn * ey), -0.4, 0.4)), float(np.clip(gain * (-sn * ex + c * ey), -0.5, 0.5)),
                   float(np.clip(-1.5 * yaw, -0.5, 0.5)))
        s.step("walk", np.r_[cmd, rest, 0, 0])
        for i in range(SHOTS):                       # where does each kicked ball cross the line?
            b = w.pos(f"ball{i + 1}")
            if tr["kick_t"][i] is not None and i not in seen and prev[i][0] > 1.0 >= b[0]:
                v = w.vel(f"ball{i + 1}")
                seen[i] = float(b[1] + v[1] * (0.0 - b[0]) / v[0])
            prev[i] = b
    return s.status(), seen


OFFSETS = (0.0, 0.12, -0.12, 0.24, -0.24, 0.06, -0.06)


def solve(s, tries=10):
    T = s.world.task
    aim = [T.p[f"aim{k}_y_m"] for k in range(1, SHOTS + 1)]
    learned, off = {}, [0] * SHOTS
    for n in range(tries):
        if n:
            s.reset()
        stand_y = [learned.get(i, aim[i]) + OFFSETS[off[i] % len(OFFSETS)] for i in range(SHOTS)]
        st, seen = attempt(s, stand_y)
        if st["success"]:
            return True
        fail = next((e for e in reversed(st["events"]) if e["ev"] == "fail"), {})
        k = fail.get("shot")
        if k is not None and (k - 1) in learned:   # stood in the learned line and still conceded: try beside it
            off[k - 1] += 1
        for i, y in seen.items():
            learned.setdefault(i, y)
    return False