Spaces:
Sleeping
Sleeping
File size: 2,758 Bytes
31226fd 9d98328 31226fd 9d98328 31226fd 9d98328 | 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 | """Deterministic environment used for the first Alpha.7 loop proof."""
from __future__ import annotations
from collections.abc import Mapping
from dataclasses import dataclass
from .environment import EnvironmentAdapter
from .models import ActionCommand, EnvironmentKind, EnvironmentObservation, JSONValue
@dataclass(slots=True)
class DeterministicTargetEnvironment(EnvironmentAdapter):
"""One-dimensional target task with deterministic seeded reset."""
target: int = 3
position: int = 0
tick: int = 0
@property
def environment_id(self) -> str:
return "deterministic-target-v1"
@property
def kind(self) -> EnvironmentKind:
return EnvironmentKind.SIMULATED
def reset(self, seed: int | None = None) -> EnvironmentObservation:
self.position = 0 if seed is None else seed % 2
self.tick = 0
return self._observation(0.0)
def step(self, action: ActionCommand) -> EnvironmentObservation:
self.tick += 1
if action.action == "right":
self.position += 1
elif action.action == "left":
self.position -= 1
reward = 1.0 if self.position == self.target else 0.0
return self._observation(reward)
def _observation(self, reward: float) -> EnvironmentObservation:
return EnvironmentObservation(
tick=self.tick,
state={"position": self.position, "target": self.target},
reward=reward,
terminated=self.position == self.target,
)
def snapshot_state(self) -> dict[str, JSONValue]:
"""Return the complete continuation state used by frozen-world replay."""
return {
"target": self.target,
"position": self.position,
"tick": self.tick,
}
def restore_state(self, state: Mapping[str, JSONValue]) -> None:
"""Restore a previously frozen deterministic world state."""
required = ("target", "position", "tick")
if set(state) != set(required):
raise ValueError(
"deterministic world state must contain target/position/tick"
)
values: list[int] = []
for field in required:
value = state[field]
if type(value) is not int:
raise ValueError(f"{field} must be an integer")
values.append(value)
target, position, tick = values
if tick < 0:
raise ValueError("tick must be >= 0")
self.target = target
self.position = position
self.tick = tick
def rng_fingerprint(self) -> str:
"""This environment has no runtime RNG after explicit state restore."""
return "deterministic-target:no-runtime-rng:v1"
|