File size: 4,667 Bytes
6aa6c12 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 | """Dependency-free diagnostic world, never a participant capability benchmark."""
from __future__ import annotations
import random
from typing import Mapping, Sequence
from benchkit.contracts import (
ActionChunk,
ActionSpec,
BenchmarkDescriptor,
EpisodeContext,
EpisodeSpec,
JsonValue,
Observation,
ObservationSpec,
PolicyDecision,
PolicyDescriptor,
Transition,
)
from benchkit.registry import register_benchmark, register_policy
class ReachEpisode:
"""Tiny absolute-position world with native success and reward signals."""
def __init__(self, spec: EpisodeSpec) -> None:
self.spec = spec
self.position = 0.0
self.target = 0.0
self.done = False
self._reward = 0.0
def reset(self) -> Observation:
randomizer = random.Random(self.spec.seed)
self.target = randomizer.uniform(-0.8, 0.8)
self.position = -0.8 if self.target >= 0 else 0.8
self.done = False
return self._observation(0)
def _observation(self, step: int) -> Observation:
return Observation(
step=step,
instruction=self.spec.instruction,
state=(self.position,),
metadata={"target": self.target},
)
def step(self, action: tuple[float, ...]) -> Transition:
if self.done:
raise RuntimeError("cannot step a completed episode")
self.position = action[0]
distance = abs(self.target - self.position)
self.done = distance <= 0.05
self._reward = max(0.0, 1.0 - distance)
return Transition(
observation=self._observation(1),
reward=self._reward,
terminated=self.done,
info={"distance": distance},
)
def metrics(self) -> Mapping[str, JsonValue]:
return {"success": self.done, "final_distance": abs(self.target - self.position)}
def artifacts(self) -> Mapping[str, str]:
return {}
def close(self) -> None:
return None
class ReachBenchmark:
"""Benchmark adapter for the tiny reach world."""
descriptor = BenchmarkDescriptor(
name="smoke-reach",
version="1",
protocol_version="starvla-rsi.smoke/v1",
source_revision="builtin",
)
observation_spec = ObservationSpec(state_names=("position",), modalities=("language", "state"))
action_spec = ActionSpec(
mode="joint_position",
size=1,
names=("position",),
low=(-1.0,),
high=(1.0,),
control_hz=10.0,
)
def episodes(self, seed: int, limit: int | None = None) -> Sequence[EpisodeSpec]:
count = 3 if limit is None else limit
return [
EpisodeSpec(
id=f"reach-{index:03d}",
task="reach",
seed=seed + index,
instruction="Move the joint to the target position.",
)
for index in range(count)
]
def open_episode(self, episode: EpisodeSpec) -> ReachEpisode:
return ReachEpisode(episode)
class ScriptedReachPolicy:
"""Reference policy proving that runner wiring, not task skill, is functional."""
descriptor = PolicyDescriptor(name="smoke-scripted", kind="scripted", version="1")
def compatibility_errors(
self, observation_spec: ObservationSpec, action_spec: ActionSpec
) -> Sequence[str]:
errors: list[str] = []
if action_spec.mode != "joint_position" or action_spec.size != 1:
errors.append("smoke-scripted requires a 1-D joint_position action")
if not observation_spec.state_names:
errors.append("smoke-scripted requires proprioceptive state")
return errors
def start_episode(self, context: EpisodeContext) -> None:
return None
def act(self, observation: Observation) -> PolicyDecision:
target = float(observation.metadata["target"])
return PolicyDecision(
action=ActionChunk.from_steps(((target,),)),
note="Move directly to the target in the mock world.",
)
def end_episode(self) -> None:
return None
@register_benchmark("smoke")
def make_smoke_benchmark(parameters: Mapping[str, JsonValue]) -> ReachBenchmark:
if parameters:
raise ValueError(f"smoke benchmark has no parameters: {sorted(parameters)}")
return ReachBenchmark()
@register_policy("scripted")
def make_scripted_policy(parameters: Mapping[str, JsonValue]) -> ScriptedReachPolicy:
if parameters:
raise ValueError(f"scripted policy has no parameters: {sorted(parameters)}")
return ScriptedReachPolicy()
|