Download template/environment/maze/engine.py from HuggingFaceSwarms/s3-maze: direct link, hf CLI and curl.
- Browser
- Download file 3.62 kB
-
https://huggingface.co/datasets/HuggingFaceSwarms/s3-maze/resolve/main/template/environment/maze/engine.py
- Command line
-
hf download hf://datasets/HuggingFaceSwarms/s3-maze/template/environment/maze/engine.py
-
curl -L -o engine.py https://huggingface.co/datasets/HuggingFaceSwarms/s3-maze/resolve/main/template/environment/maze/engine.py
3.62 kB
| """The maze: one agent's walk through its own copy of a tree of rooms (s3-maze). | |
| The service (service.py) holds one Walk per Trial; budget.py simulates searches on this same | |
| code. Python 3.10+, standard library only. | |
| A tree is `{"start", "exit", "budget", "doors": {room: [neighbour, ...]}}`: each room lists its | |
| doors in the order fixed when the tree was generated, the same for every agent and every visit. | |
| """ | |
| import json | |
| DEAD_END = "Dead end: its only door leads back the way you came." | |
| class Maze: | |
| def __init__(self, tree: dict): | |
| self.start = tree["start"] | |
| self.exit = tree["exit"] | |
| self.budget = int(tree["budget"]) | |
| self.doors = {room: list(doors) for room, doors in tree["doors"].items()} | |
| def load(cls, path: str) -> "Maze": | |
| with open(path, encoding="utf-8") as f: | |
| return cls(json.load(f)) | |
| class Walk: | |
| """One agent's position and its accepted moves. Looking is free; a refused move costs | |
| nothing; at most `budget` moves are accepted, and none once the exit is reached.""" | |
| def __init__(self, maze: Maze, room: str | None = None, arrived_from: str | None = None, | |
| moves: int = 0, refused: int = 0): | |
| self.maze = maze | |
| self.room = room or maze.start | |
| self.arrived_from = arrived_from | |
| self.moves = moves | |
| self.refused = refused | |
| def reached(self) -> bool: | |
| return self.room == self.maze.exit | |
| def look(self) -> dict: | |
| """The paper's observation (s3, Appendix H), plus the moves left.""" | |
| doors = self.maze.doors[self.room] | |
| out = {"room": self.room, "doors": list(doors), "exit": self.reached, | |
| "arrived_from": self.arrived_from, "moves_left": self.maze.budget - self.moves} | |
| if len(doors) == 1 and not self.reached: | |
| out["notice"] = DEAD_END | |
| return out | |
| def refusal(self, room) -> str | None: | |
| """Why a move into `room` is refused (a short code), or None.""" | |
| if self.reached: | |
| return "at_exit" | |
| if self.moves >= self.maze.budget: | |
| return "no_moves_left" | |
| if not isinstance(room, str) or room not in self.maze.doors[self.room]: | |
| return "not_a_door" | |
| return None | |
| def move(self, room) -> tuple[dict, str | None]: | |
| """(the observation after moving into `room`, None), or (an error and the current | |
| observation, the refusal's code).""" | |
| why = self.refusal(room) | |
| if why is not None: | |
| self.refused += 1 | |
| return {"error": MESSAGES[why].format(room=_short(room), here=self.room, | |
| budget=self.maze.budget), **self.look()}, why | |
| self.arrived_from, self.room = self.room, room | |
| self.moves += 1 | |
| return self.look(), None | |
| def state(self) -> dict: | |
| return {"room": self.room, "arrived_from": self.arrived_from, "moves": self.moves, | |
| "refused": self.refused, "reached": self.reached} | |
| def from_state(cls, maze: Maze, state: dict) -> "Walk": | |
| return cls(maze, state["room"], state["arrived_from"], state["moves"], state["refused"]) | |
| MESSAGES = { | |
| "at_exit": "You are at the exit: the task is solved.", | |
| "no_moves_left": "No moves left: your budget of {budget} moves is used up.", | |
| "not_a_door": "{room} is not a door of {here}: move into one of the doors listed for it.", | |
| } | |
| def _short(value) -> str: | |
| text = value if isinstance(value, str) else json.dumps(value) | |
| return text if len(text) <= 40 else text[:40] + "..." | |