Download example/scoring.py from Reedy-Creek-Robotics/2026-BioBuzz-Movement-Benchmark: direct link, hf CLI and curl.
- Browser
- Download file 12.2 kB
-
https://huggingface.co/datasets/Reedy-Creek-Robotics/2026-BioBuzz-Movement-Benchmark/resolve/main/example/scoring.py
- Command line
-
hf download hf://datasets/Reedy-Creek-Robotics/2026-BioBuzz-Movement-Benchmark/example/scoring.py
-
curl -L -o scoring.py https://huggingface.co/datasets/Reedy-Creek-Robotics/2026-BioBuzz-Movement-Benchmark/resolve/main/example/scoring.py
12.2 kB
| """The benchmark rules and scores: when a move ends, and what is measured along the way. | |
| `Scorer` follows a batch of moves tick by tick. The runner calls, every tick: | |
| scorer.next_points(est) before the controller: switch intermediate points | |
| scorer.judge_done(est, sim.vel, done) on the state the controller saw: done flags, the hold rule | |
| sim.step(commands) | |
| scorer.after_physics(sim, commands) contact, line, clearances, stall, timeout | |
| and finally scorer.results() -> one dict per move. | |
| """ | |
| import math | |
| import numpy as np | |
| from sim import footprint_corners | |
| # ----------------------------------------------------------------------------------- the rules | |
| POS_TOL = 0.5 # in: arrived when the pose estimate is this close to the goal ... | |
| HEAD_TOL = 0.035 # rad (2 degrees): ... facing within this ... | |
| SPEED_TOL = 2.0 # in/s: ... and the robot is slower than this ... | |
| YAW_TOL = 0.2 # rad/s: ... and turning slower than this. | |
| HOLD_TICKS = 13 # a controller without a done flag arrives after 13 ticks (0.26 s) in a row inside them | |
| DONE_THRESHOLD = 0.5 # a done output above this ends the move | |
| TIMEOUT_TICKS = 400 # 8 s per leg (a multi-step move restarts the clock at each intermediate point) | |
| HARD_HIT = 12.0 # in/s: contact that loses more speed than this is a hard hit | |
| NEAR = 1.0 # in: "near" a hazard or the centre line | |
| STALL_POWER, STALL_MOVE, STALL_TURN, STALL_TICKS = 0.25, 0.5, 0.05, 25 # pushing but not moving for 0.5 s | |
| DT = 0.02 | |
| def _wrap(a): | |
| return (a + np.pi) % (2 * np.pi) - np.pi | |
| class Scorer: | |
| def __init__(self, moves, field, robot, has_done_flag): | |
| self.n = len(moves) | |
| self.has_done = has_done_flag | |
| self.hl, self.hw = robot["footprint"]["length_in"] / 2, robot["footprint"]["width_in"] / 2 | |
| self.line_x = field["centre_line_x"] | |
| self.hazards = np.array([h["rect"] for h in field["hazards"] if h["rect"][0] < self.line_x], dtype=float) | |
| self.moves = moves | |
| # the points of each move, in order: intermediate points, then the goal (an intermediate point without a | |
| # heading of its own takes the goal's) | |
| self.points = [] | |
| for m in moves: | |
| pts = [] | |
| for k in range(1, int(m["n_via"]) + 1): | |
| h = m[f"via{k}_h"] | |
| pts.append((m[f"via{k}_x"], m[f"via{k}_y"], m["goal_h"] if np.isnan(h) else h, m[f"via{k}_r"])) | |
| pts.append((m["goal_x"], m["goal_y"], m["goal_h"], 0.0)) | |
| self.points.append(pts) | |
| z = lambda v=0.0: np.full(self.n, v, dtype=float) | |
| self.leg = np.zeros(self.n, int) | |
| self.finished = np.zeros(self.n, bool) | |
| self.outcome = np.array(["running"] * self.n, dtype=object) | |
| self.t_end, self.tick = z(np.nan), 0 | |
| self.leg_ticks, self.hold = np.zeros(self.n, int), np.zeros(self.n, int) | |
| self.stall_n, self.anchor = np.zeros(self.n, int), None | |
| self.path_len, self.top_speed = z(), z() | |
| self.wall_touch, self.hazard_touch = np.zeros(self.n, bool), np.zeros(self.n, bool) | |
| self.hard_hits, self.max_hit = np.zeros(self.n, int), z() | |
| self.crossed, self.depth, self.line_time = np.zeros(self.n, bool), z(), z() | |
| self.min_line_gap, self.min_hazard_gap, self.near_time = z(np.inf), z(np.inf), z() | |
| self.switch_speeds = [[] for _ in range(self.n)] | |
| self.final_pose = np.zeros((self.n, 3)) | |
| self.prev_pose = None | |
| def goals(self): | |
| """(N, 3) the point each robot is driving to now.""" | |
| return np.array([self.points[i][self.leg[i]][:3] for i in range(self.n)]) | |
| # --------------------------------------------------------------------------- before the controller | |
| def next_points(self, est, true_vel): | |
| """Intermediate points: once the estimate is within a point's radius, the next point becomes the target.""" | |
| for i in np.flatnonzero(~self.finished): | |
| while self.leg[i] < len(self.points[i]) - 1: | |
| x, y, _, r = self.points[i][self.leg[i]] | |
| if math.hypot(est[i, 0] - x, est[i, 1] - y) > r: | |
| break | |
| self.switch_speeds[i].append(float(np.hypot(true_vel[i, 0], true_vel[i, 1]))) | |
| self.leg[i] += 1 | |
| self.leg_ticks[i] = 0 # the clock and the stall watchdog restart | |
| self.stall_n[i] = 0 | |
| if self.anchor is not None: | |
| self.anchor[i] = est[i] | |
| # ------------------------------------------------------------- on the state the controller saw | |
| def judge_done(self, est, true_vel, done): | |
| g = self.goals() | |
| last = self.leg == np.array([len(p) - 1 for p in self.points]) | |
| inside = (np.hypot(*(g[:, :2] - est[:, :2]).T) < POS_TOL) & (np.abs(_wrap(g[:, 2] - est[:, 2])) < HEAD_TOL) & \ | |
| (np.hypot(true_vel[:, 0], true_vel[:, 1]) < SPEED_TOL) & (np.abs(true_vel[:, 2]) < YAW_TOL) & last | |
| t = self.tick * DT | |
| for i in np.flatnonzero(~self.finished): | |
| if self.has_done: | |
| if done[i] is not None and done[i] > DONE_THRESHOLD: | |
| self._finish(i, "arrived" if inside[i] else "wrong_done", t) | |
| else: | |
| self.hold[i] = self.hold[i] + 1 if inside[i] else 0 | |
| if self.hold[i] >= HOLD_TICKS: # arrived when the hold began | |
| self._finish(i, "arrived", t - (HOLD_TICKS - 1) * DT) | |
| # --------------------------------------------------------------------------------- after physics | |
| def after_physics(self, sim, cmds): | |
| run = ~self.finished | |
| pose, vel = sim.pose, sim.vel | |
| if self.prev_pose is None: | |
| self.prev_pose = pose.copy() | |
| if self.anchor is None: | |
| self.anchor = sim.est.copy() | |
| self.tick += 1 | |
| step = np.hypot(*(pose[:, :2] - self.prev_pose[:, :2]).T) | |
| self.prev_pose = pose.copy() | |
| speed = np.hypot(vel[:, 0], vel[:, 1]) | |
| self.path_len += np.where(run, step, 0.0) | |
| self.top_speed = np.where(run, np.maximum(self.top_speed, speed), self.top_speed) | |
| # contact and hard hits | |
| touch = sim.contact_wall | sim.contact_hazard | |
| self.wall_touch |= run & sim.contact_wall | |
| self.hazard_touch |= run & sim.contact_hazard | |
| hit = run & touch & (sim.impact > HARD_HIT) | |
| self.hard_hits += hit | |
| self.max_hit = np.where(run & touch, np.maximum(self.max_hit, sim.impact), self.max_hit) | |
| # the centre line | |
| c, s = np.abs(np.cos(pose[:, 2])), np.abs(np.sin(pose[:, 2])) | |
| max_x = pose[:, 0] + c * self.hl + s * self.hw | |
| over = run & (max_x > self.line_x) | |
| self.crossed |= over | |
| self.depth = np.where(over, np.maximum(self.depth, max_x - self.line_x), self.depth) | |
| self.line_time += np.where(over, DT, 0.0) | |
| self.min_line_gap = np.where(run, np.minimum(self.min_line_gap, self.line_x - max_x), self.min_line_gap) | |
| # clearance to hazards | |
| gap = np.where(sim.contact_hazard, 0.0, _gap_to_rects(pose, self.hazards, self.hl, self.hw)) | |
| self.min_hazard_gap = np.where(run, np.minimum(self.min_hazard_gap, gap), self.min_hazard_gap) | |
| near = (gap < NEAR) | (self.line_x - max_x < NEAR) | |
| self.near_time += np.where(run & near, DT, 0.0) | |
| # stall watchdog: pushing (a command of at least STALL_POWER) without the estimate moving | |
| est = sim.est | |
| moved = np.hypot(*(est[:, :2] - self.anchor[:, :2]).T) | |
| turned = np.abs(_wrap(est[:, 2] - self.anchor[:, 2])) | |
| reset = (np.abs(cmds).max(-1) < STALL_POWER) | (moved > STALL_MOVE) | (turned > STALL_TURN) | |
| self.anchor = np.where(reset[:, None], est, self.anchor) | |
| self.stall_n = np.where(reset, 0, self.stall_n + 1) | |
| self.leg_ticks += 1 | |
| t = self.tick * DT | |
| for i in np.flatnonzero(run): | |
| if self.stall_n[i] >= STALL_TICKS: | |
| self._finish(i, "stalled", t) | |
| elif self.leg_ticks[i] >= TIMEOUT_TICKS: | |
| self._finish(i, "timed_out", t) | |
| self.final_pose = np.where(self.finished[:, None], self.final_pose, pose) | |
| def _finish(self, i, outcome, t): | |
| self.finished[i], self.outcome[i], self.t_end[i] = True, outcome, t | |
| def all_finished(self): | |
| return bool(self.finished.all()) | |
| # ---------------------------------------------------------------------------------- results | |
| def results(self): | |
| out = [] | |
| for i, m in enumerate(self.moves): | |
| gx, gy, gh, _ = self.points[i][-1] | |
| x, y, h = self.final_pose[i] | |
| arrived = self.outcome[i] == "arrived" | |
| sw = self.switch_speeds[i] | |
| out.append({ | |
| "id": int(m["id"]), "category": m["category"], "outcome": self.outcome[i], | |
| "success": bool(arrived and not self.crossed[i]), | |
| "time_s": float(self.t_end[i]) if arrived else None, | |
| "final_error_in": float(math.hypot(gx - x, gy - y)) if arrived else None, | |
| "final_heading_error_deg": float(abs(math.degrees(math.remainder(gh - h, 2 * math.pi)))) if arrived else None, | |
| "path_ratio": float(self.path_len[i] / m["route_path_dist"]) if arrived and m["route_path_dist"] >= 12 else None, | |
| "top_speed_in_s": float(self.top_speed[i]), | |
| "touched_wall": bool(self.wall_touch[i]), "touched_hazard": bool(self.hazard_touch[i]), | |
| "hard_hits": int(self.hard_hits[i]), "max_hit_speed_in_s": float(self.max_hit[i]), | |
| "crossed_line": bool(self.crossed[i]), "line_depth_in": float(self.depth[i]), | |
| "time_over_line_s": float(self.line_time[i]), | |
| "min_line_gap_in": float(self.min_line_gap[i]), "min_hazard_gap_in": float(self.min_hazard_gap[i]), | |
| "time_near_s": float(self.near_time[i]), | |
| "points_reached": int(self.leg[i]) + int(arrived), "points_total": len(self.points[i]), | |
| "switch_speed_in_s": float(np.mean(sw)) if sw else None, | |
| }) | |
| return out | |
| def _gap_to_rects(pose, rects, hl, hw): | |
| """(N,) the smallest distance between each robot's footprint and any of the rectangles (0 if touching).""" | |
| if len(rects) == 0: | |
| return np.full(len(pose), np.inf) | |
| box = footprint_corners(pose, hl, hw) # (N, 4, 2) | |
| x0, x1, y0, y1 = rects[:, 0], rects[:, 1], rects[:, 2], rects[:, 3] | |
| rc = np.stack([np.stack([x0, y0], -1), np.stack([x1, y0], -1), | |
| np.stack([x1, y1], -1), np.stack([x0, y1], -1)], 1) # (R, 4, 2) | |
| # box corners to each rectangle (axis-aligned: clamp) | |
| dx = np.maximum(np.maximum(x0[None, :, None] - box[:, None, :, 0], 0.0), box[:, None, :, 0] - x1[None, :, None]) | |
| dy = np.maximum(np.maximum(y0[None, :, None] - box[:, None, :, 1], 0.0), box[:, None, :, 1] - y1[None, :, None]) | |
| d1 = np.hypot(dx, dy).min(-1) # (N, R) | |
| # rectangle corners to each box edge | |
| a, b = box, np.roll(box, -1, axis=1) # edges a -> b, (N, 4, 2) | |
| p = rc[None, :, :, None, :] # (1, R, 4, 1, 2) | |
| ab = (b - a)[:, None, None] # (N, 1, 1, 4, 2) | |
| t = np.clip(((p - a[:, None, None]) * ab).sum(-1) / (ab * ab).sum(-1), 0.0, 1.0) | |
| closest = a[:, None, None] + t[..., None] * ab | |
| d2 = np.linalg.norm(p - closest, axis=-1).min(-1).min(-1) # (N, R) | |
| # overlap: a rectangle corner inside the box, or a box corner inside a rectangle -> 0 | |
| c, s = np.cos(pose[:, 2]), np.sin(pose[:, 2]) | |
| rel = rc[None] - pose[:, None, None, :2] # (N, R, 4, 2) | |
| u = rel[..., 0] * c[:, None, None] + rel[..., 1] * s[:, None, None] | |
| v = -rel[..., 0] * s[:, None, None] + rel[..., 1] * c[:, None, None] | |
| inside = ((np.abs(u) <= hl) & (np.abs(v) <= hw)).any(-1) | (d1 == 0) | |
| gap = np.where(inside, 0.0, np.minimum(d1, d2)) | |
| return gap.min(-1) | |