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.

13.2 KB
Raw Blame
"""A foosball player policy on a ball estimate: the scripted opponent (in the sim, from the true ball position a
reaction delay ago) and the camera-only reference player (solution/, from its own camera tracking) both use it.

Everything is in the seat's local frame (u towards the opponent's goal, v to the left) and uses only the public
geometry of the task document. update() returns a command for the rods {rod: {slide, angle, omega, speed}}.

Styles: 'team' coordinates the rods: build-up play (the goalie rods pass to the 5-rod, the 5-rod to the 3-rod, each
pass aimed through the opponent's figures at a figure of the receiving rod, which waits where it stands; the 3-rod
shoots), and the own rods between a kick and its target are lifted out of its way. 'solo' lets every rod play on its
own: it shoots at the goal from wherever it gets the ball and never passes (lifting=True: it still lifts the own rods
in front of its shots).
"""
import math

ROD_X = {'goalie': -.525, 'defence': -.375, 'midfield': -.075, 'attack': .225}
MEN = {'goalie': (-.20, 0., .20), 'defence': (-.12, .12), 'midfield': (-.24, -.12, 0., .12, .24), 'attack': (-.20, 0., .20)}
TRAVEL = {'goalie': .11, 'defence': .19, 'midfield': .06, 'attack': .11}
RODS = ('goalie', 'defence', 'midfield', 'attack')
NEXT = {'midfield': 'attack'}                       # who a rod passes to (team style)
X_REACH, BALL_R, HALF_W, HALF_L, GOAL_W = .077, .0175, .34, .60, .20
LIFT, COCK, BACK, FOLLOW = 1.5, -1.0, -1.6, 1.1      # rod angles (rad): lifted out of the way, cocked, raised behind, strike end


OPP_X = {'goalie': .525, 'defence': .375, 'midfield': .075, 'attack': -.225}       # the opponent's rods, local u
FIG_R, CONTACT = .012, .0295                  # a foot's radius; foot + ball radius


def clip(x, lo, hi): return lo if x < lo else hi if x > hi else x


def fold(y):
    """A straight path's y with the side walls folded in (reflections)."""
    r = HALF_W-BALL_R; y = (y+r) % (4*r)
    return (y if y < 2*r else 4*r-y)-r


class Policy:
    def __init__(self, style='team', omega=80., speed=2.5, pass_omega=60., shoot=True, aim=True, passing=None, lifting=None,
                 splitting=None):
        self.style, self.omega, self.speed, self.pass_omega, self.shoot, self.aim = style, omega, speed, pass_omega, shoot, aim
        team = style == 'team'
        self.passing = team if passing is None else passing
        self.lifting = team if lifting is None else lifting
        self.splitting = team if splitting is None else splitting
        self.opp = self.rods = self.target = None
        self.reset(None)

    def reset(self, serve):
        """serve: the own rod that restarts play ('midfield' or 'goalie'), or None when the opponent restarts."""
        self.phase = {r: 'idle' for r in RODS}; self.since = {r: -1. for r in RODS}; self.slide_goal = {}
        self.target = None; self.serve = serve; self.serve_t = None; self.serve_side = 1.; self.kind = {r: 'shot' for r in RODS}

    def figure(self, rod, v):
        """The figure (index) and slide that put a figure on v (the nearest reachable one)."""
        best = None
        for j, m in enumerate(MEN[rod]):
            s = clip(v-m, -TRAVEL[rod], TRAVEL[rod]); miss = abs(v-m-s)
            if best is None or miss < best[2]-1e-9 or (abs(miss-best[2]) < 1e-9 and abs(s) < abs(best[1])): best = (j, s, miss)
        return best

    def set(self, rod, phase, t): self.phase[rod] = phase; self.since[rod] = t

    def lane(self, u, v, rod, kind, opp):
        """The aim (a target point) for a kick from (u, v): the straight path that passes farthest from the
        opponent's figures in front (opp: {rod: [figure v]}); a shot aims inside the goal mouth, a pass at the own
        3-rod's line within its figures' reach. Returns (target u, target v, clearance)."""
        if kind == 'pass':                          # anywhere a figure of the receiving rod can be put
            to = NEXT[rod]; tu = ROD_X[to]
            cands = [x/100 for x in range(-30, 31, 2)]
        else: tu, cands = HALF_L, [x/100 for x in range(-7, 8, 1)]
        best = None
        for tv in cands:
            k = (tv-v)/max(1e-6, tu-u)
            if abs(k) > .7: continue
            clear = 1.
            for r, ox in OPP_X.items():
                if not u+.02 < ox < tu: continue
                y = fold(v+k*(ox-u))
                figs = (opp or {}).get(r)
                if figs: clear = min(clear, min(abs(y-f) for f in figs)-CONTACT)
                if abs(v+k*(ox-u)) > HALF_W-BALL_R: clear -= .01        # off the wall: less sure
            score = clear-.02*abs(k)
            if best is None or score > best[3]: best = (tu, tv, clear, score)
        return best[:3] if best else (tu, 0., 0.)

    def update(self, t, p, w, rods, opp=None, opening_done=True):
        """t game seconds; p = (u, v) ball estimate or None; w = (du/dt, dv/dt); rods = {rod: {'slide', 'angle'}};
        opp = the opponent's figures {rod: [v of each figure]} in the local frame, if known; opening_done = False
        while the own opening sequence is still incomplete (the referee says so in the replies)."""
        out = {}; self.opp = opp; self.rods = rods
        if p is None:
            return {r: dict(slide=rods[r]['slide'], angle=0., omega=self.omega, speed=self.speed) for r in RODS}
        u, v = p; wu, wv = w
        owner = min((r for r in RODS if abs(u-ROD_X[r]) < X_REACH-.004), key=lambda r: abs(u-ROD_X[r]), default=None)
        if self.serve and not opening_done and owner == self.serve: return self.opening(t, p, rods)
        shooter = owner if owner and self.phase[owner] in ('cock', 'align', 'strike', 'around') else None
        for rod in RODS:
            x = ROD_X[rod]; a = rods[rod]['angle']; cur = rods[rod]['slide']
            if rod == owner:
                out[rod] = self.possess(rod, t, u, v, wu, wv, a, cur, rods)
                continue
            if self.phase[rod] == 'strike' and t-self.since[rod] < .2:                         # finish a strike
                out[rod] = dict(slide=clip(rods[rod]['slide'], -TRAVEL[rod], TRAVEL[rod]), angle=FOLLOW, omega=self.omega,
                                speed=self.speed); continue
            if self.phase[rod] not in ('idle', 'receive'): self.set(rod, 'idle', t)
            # where the ball crosses this rod's line, if it is coming; else its current line
            y = v
            if abs(wu) > .05 and (x-u)*wu > 0: y = fold(v+wv*(x-u)/wu)
            if rod == 'goalie' and self.splitting and u > ROD_X['defence']+.02 and wu > -.5:
                y = self.cover(u, v, rods)        # team: the goalkeeper covers what the 2-rod's figure leaves open
            if rod == 'goalie': y = clip(y, -GOAL_W/2+.02, GOAL_W/2-.02)
            j, s, _ = self.figure(rod, y)
            angle = 0.
            passing = shooter and self.kind.get(shooter) == 'pass'
            if passing and rod == NEXT[shooter] and self.target is not None:
                j, s, _ = self.figure(rod, self.target)                          # the receiver goes to the pass target
            elif self.lifting and shooter and x > ROD_X[shooter] and (not passing or x < ROD_X[NEXT[shooter]]):
                angle = LIFT                                                      # clear the lane of the shot or pass
            out[rod] = dict(slide=s, angle=angle, omega=self.omega, speed=self.speed)
        return out

    def cover(self, u, v, rods):
        """The goalkeeper's line while the ball is in front of the 2-rod: the middle of the larger part of the goal
        mouth, as seen from the ball, that the 2-rod figure nearest to the ball's line does not block."""
        gx, dx = -HALF_L, ROD_X['defence']
        d = min((m+rods['defence']['slide'] for m in MEN['defence']), key=lambda f: abs(f-v))
        k = (gx-u)/(dx-u)                                  # the figure's blocked band, projected onto the goal line
        lo, hi = v+(d-CONTACT-v)*k, v+(d+CONTACT-v)*k
        e = GOAL_W/2-BALL_R
        if hi < -e or lo > e: return clip(v+(0.-v)*(ROD_X['goalie']-u)/(gx-u), -e, e)   # the 2-rod covers nothing
        left, right = max(0., lo+e), max(0., e-hi)
        g = (-e+min(lo, e))/2 if left >= right else (max(hi, -e)+e)/2
        return v+(g-v)*(ROD_X['goalie']-u)/(gx-u)

    def possess(self, rod, t, u, v, wu, wv, a, cur, rods):
        """The rod that has the ball in reach: line a figure up and kick it forward (a shot or, 5-rod in team style, a
        pass to the 3-rod), going round a ball that lies behind the figures first."""
        x = ROD_X[rod]; du = u-x; ph = self.phase[rod]
        lead = v+wv*.05
        if self.aim and self.kind.get(rod) in ('shot', 'pass') and ph in ('cock', 'align', 'strike'):
            if self.kind[rod] == 'pass' and ph in ('cock', 'align') or self.target is None or self.kind[rod] != 'pass':
                tu, tv, _ = self.lane(u, lead, rod, self.kind[rod], self.opp)
                if self.kind[rod] == 'pass': self.target = tv
            else: tu, tv = ROD_X[NEXT[rod]], self.target
            phi = math.atan2(tv-lead, tu-u)
            om = self.pass_omega if self.kind[rod] == 'pass' else self.omega
            gain = .82+.35*clip((om-20.)/20., 0., 1.)                 # measured: angle out / asin(offset / contact)
            lead = lead-CONTACT*math.sin(clip(phi/gain, -1.1, 1.1))    # an off-centre hit sends the ball at an angle
        j, s, miss = self.figure(rod, lead)
        if ph in ('idle', 'receive') and (wu > .6 and du < 0 or wu < -.6 and du > 0):
            y = v if abs(wu) < .05 else fold(v+wv*(x-u)/wu)         # a fast ball arriving: block (catch) it first
            j, s, _ = self.figure(rod, y)
            return dict(slide=s, angle=0., omega=self.omega, speed=self.speed)
        if ph in ('idle', 'receive'):
            self.kind[rod] = 'shot'
            if self.passing and rod in NEXT:                    # pass when its lane is clearly the more open one
                cp = self.lane(u, v, rod, 'pass', self.opp)[2]; cs = self.lane(u, v, rod, 'shot', self.opp)[2]
                if cp > .012 and cp > cs+.01: self.kind[rod] = 'pass'; self.target = None
            if not self.shoot and rod in ('midfield', 'attack'): self.kind[rod] = 'block'
            if self.kind[rod] == 'block':
                return dict(slide=s, angle=0., omega=self.omega, speed=self.speed)
            if a < -1.25: ph = 'align'
            elif du < .012: ph = 'around'
            else: ph = 'cock'
            self.set(rod, ph, t)
        omega = self.pass_omega if self.kind[rod] == 'pass' else self.omega
        if ph == 'around':                       # get the foot behind the ball without touching it: lift the figure
            side = 1. if (cur+MEN[rod][j]) <= v else -1.        # forward, step aside, swing it back over, step back
            off = clip(v-MEN[rod][j]-side*.065, -TRAVEL[rod], TRAVEL[rod])
            if abs(v-(off+MEN[rod][j])) < .05:   # no room on that side: the other one
                off = clip(v-MEN[rod][j]+side*.065, -TRAVEL[rod], TRAVEL[rod])
            if abs(cur-off) > .012 and a > BACK+.4:
                return dict(slide=off, angle=max(a, .9) if a > -.3 else a, omega=self.omega, speed=self.speed)
            if a < BACK+.25 or t-self.since[rod] > .6: self.set(rod, 'align', t)
            return dict(slide=off, angle=BACK, omega=self.omega, speed=self.speed)
        ready = True
        if self.kind[rod] == 'pass' and self.target is not None:
            to = NEXT[rod]; ready = min(abs(m+rods[to]['slide']-self.target) for m in MEN[to]) < .012
        if ph == 'align':
            if abs(cur-s) < .01 and miss < .01 and (du > -.06 or wu > .2) and ready or t-self.since[rod] > .6:
                self.set(rod, 'strike', t)
            return dict(slide=s, angle=BACK, omega=self.omega, speed=self.speed)
        if ph == 'cock':
            if du < .006 and a > -.6: self.set(rod, 'around', t)
            elif a < COCK+.25 and abs(cur-s) < .012 and ready or t-self.since[rod] > .6: self.set(rod, 'strike', t)
            return dict(slide=s, angle=COCK, omega=self.omega, speed=self.speed)
        if ph == 'strike':
            if a > FOLLOW-.2 or t-self.since[rod] > .25: self.set(rod, 'recover', t)
            return dict(slide=s, angle=FOLLOW, omega=omega, speed=self.speed)
        if ph == 'recover':
            if t-self.since[rod] > .12: self.set(rod, 'idle', t)
            return dict(slide=s, angle=0., omega=self.omega, speed=self.speed)
        self.set(rod, 'idle', t)
        return dict(slide=s, angle=0., omega=self.omega, speed=self.speed)

    def opening(self, t, p, rods):
        """SMR 5.2 opening sequence from the restart rod: push the ball sideways from one figure to the next and back
        (three contacts by alternating figures), repeated until the referee reports it complete, then play on."""
        rod = self.serve; u, v = p
        if self.serve_t is None: self.serve_t = t; self.serve_side = 1. if v-rods[rod]['slide'] >= 0 else -1.
        out = {r: dict(slide=rods[r]['slide'] if r == rod else 0., angle=0., omega=self.omega, speed=self.speed) for r in RODS}
        el = (t-self.serve_t) % .9
        if el < .45: out[rod] = dict(slide=clip(self.serve_side*.05, -TRAVEL[rod], TRAVEL[rod]), angle=0., omega=self.omega, speed=.5)
        else: out[rod] = dict(slide=clip(-self.serve_side*.02, -TRAVEL[rod], TRAVEL[rod]), angle=0., omega=self.omega, speed=.5)
        return out