reindeer-sdk 0.3.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- reindeer_sdk-0.3.0/PKG-INFO +44 -0
- reindeer_sdk-0.3.0/README.md +24 -0
- reindeer_sdk-0.3.0/pyproject.toml +38 -0
- reindeer_sdk-0.3.0/reindeer_sdk/__init__.py +31 -0
- reindeer_sdk-0.3.0/reindeer_sdk/api.py +119 -0
- reindeer_sdk-0.3.0/reindeer_sdk/autopilot.py +49 -0
- reindeer_sdk-0.3.0/reindeer_sdk/bot.py +214 -0
- reindeer_sdk-0.3.0/reindeer_sdk/cli.py +253 -0
- reindeer_sdk-0.3.0/reindeer_sdk/config.py +52 -0
- reindeer_sdk-0.3.0/reindeer_sdk/data/golden/bots/bot-practice-1-relu.json +1 -0
- reindeer_sdk-0.3.0/reindeer_sdk/data/golden/bots/bot-practice-3-tanh.json +1 -0
- reindeer_sdk-0.3.0/reindeer_sdk/data/golden/bots/bot-twist-ice-hairpins-tanh.json +1 -0
- reindeer_sdk-0.3.0/reindeer_sdk/data/golden/corpus/practice-1-baseline.json +1 -0
- reindeer_sdk-0.3.0/reindeer_sdk/data/golden/corpus/practice-1-brake-reverse.json +1 -0
- reindeer_sdk-0.3.0/reindeer_sdk/data/golden/corpus/practice-1-random.json +1 -0
- reindeer_sdk-0.3.0/reindeer_sdk/data/golden/corpus/practice-1-tuned.json +1 -0
- reindeer_sdk-0.3.0/reindeer_sdk/data/golden/corpus/practice-1-wallhug.json +1 -0
- reindeer_sdk-0.3.0/reindeer_sdk/data/golden/corpus/practice-2-baseline.json +1 -0
- reindeer_sdk-0.3.0/reindeer_sdk/data/golden/corpus/practice-2-brake-reverse.json +1 -0
- reindeer_sdk-0.3.0/reindeer_sdk/data/golden/corpus/practice-2-random.json +1 -0
- reindeer_sdk-0.3.0/reindeer_sdk/data/golden/corpus/practice-2-tuned.json +1 -0
- reindeer_sdk-0.3.0/reindeer_sdk/data/golden/corpus/practice-2-wallhug.json +1 -0
- reindeer_sdk-0.3.0/reindeer_sdk/data/golden/corpus/practice-3-baseline.json +1 -0
- reindeer_sdk-0.3.0/reindeer_sdk/data/golden/corpus/practice-3-brake-reverse.json +1 -0
- reindeer_sdk-0.3.0/reindeer_sdk/data/golden/corpus/practice-3-random.json +1 -0
- reindeer_sdk-0.3.0/reindeer_sdk/data/golden/corpus/practice-3-tuned.json +1 -0
- reindeer_sdk-0.3.0/reindeer_sdk/data/golden/corpus/practice-3-wallhug.json +1 -0
- reindeer_sdk-0.3.0/reindeer_sdk/data/golden/corpus/twist-ice-hairpins-baseline.json +1 -0
- reindeer_sdk-0.3.0/reindeer_sdk/data/golden/corpus/twist-ice-hairpins-random.json +1 -0
- reindeer_sdk-0.3.0/reindeer_sdk/data/golden/corpus/twist-night-oval-baseline.json +1 -0
- reindeer_sdk-0.3.0/reindeer_sdk/data/golden/corpus/twist-night-oval-random.json +1 -0
- reindeer_sdk-0.3.0/reindeer_sdk/data/golden/corpus/twist-reverse-glacier-baseline.json +1 -0
- reindeer_sdk-0.3.0/reindeer_sdk/data/golden/corpus/twist-reverse-glacier-random.json +1 -0
- reindeer_sdk-0.3.0/reindeer_sdk/data/tracks/practice-1.json +1 -0
- reindeer_sdk-0.3.0/reindeer_sdk/data/tracks/practice-2.json +1 -0
- reindeer_sdk-0.3.0/reindeer_sdk/data/tracks/practice-3.json +1 -0
- reindeer_sdk-0.3.0/reindeer_sdk/data/twists/ice-hairpins.json +1 -0
- reindeer_sdk-0.3.0/reindeer_sdk/data/twists/night-oval.json +1 -0
- reindeer_sdk-0.3.0/reindeer_sdk/data/twists/reverse-glacier.json +1 -0
- reindeer_sdk-0.3.0/reindeer_sdk/data.py +60 -0
- reindeer_sdk-0.3.0/reindeer_sdk/env.py +82 -0
- reindeer_sdk-0.3.0/reindeer_sdk/export.py +68 -0
- reindeer_sdk-0.3.0/reindeer_sdk/fmath.py +108 -0
- reindeer_sdk-0.3.0/reindeer_sdk/hashing.py +32 -0
- reindeer_sdk-0.3.0/reindeer_sdk/py.typed +0 -0
- reindeer_sdk-0.3.0/reindeer_sdk/quant.py +39 -0
- reindeer_sdk-0.3.0/reindeer_sdk/render.py +76 -0
- reindeer_sdk-0.3.0/reindeer_sdk/replay.py +114 -0
- reindeer_sdk-0.3.0/reindeer_sdk/run.py +164 -0
- reindeer_sdk-0.3.0/reindeer_sdk/samples.py +46 -0
- reindeer_sdk-0.3.0/reindeer_sdk/sim.py +379 -0
- reindeer_sdk-0.3.0/reindeer_sdk/tracks.py +28 -0
- reindeer_sdk-0.3.0/reindeer_sdk/train.py +99 -0
- reindeer_sdk-0.3.0/reindeer_sdk/twist.py +117 -0
- reindeer_sdk-0.3.0/reindeer_sdk/vec_env.py +50 -0
- reindeer_sdk-0.3.0/reindeer_sdk.egg-info/PKG-INFO +44 -0
- reindeer_sdk-0.3.0/reindeer_sdk.egg-info/SOURCES.txt +62 -0
- reindeer_sdk-0.3.0/reindeer_sdk.egg-info/dependency_links.txt +1 -0
- reindeer_sdk-0.3.0/reindeer_sdk.egg-info/entry_points.txt +2 -0
- reindeer_sdk-0.3.0/reindeer_sdk.egg-info/requires.txt +10 -0
- reindeer_sdk-0.3.0/reindeer_sdk.egg-info/top_level.txt +1 -0
- reindeer_sdk-0.3.0/setup.cfg +4 -0
- reindeer_sdk-0.3.0/tests/test_engine.py +67 -0
- reindeer_sdk-0.3.0/tests/test_tools.py +177 -0
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: reindeer-sdk
|
|
3
|
+
Version: 0.3.0
|
|
4
|
+
Summary: Reindeer Grand Prix: the official sleigh-racing engine in Python, plus tools to train, evaluate and submit bots.
|
|
5
|
+
License-Expression: MIT
|
|
6
|
+
Project-URL: Homepage, https://github.com/dcaisoc/reindeer-gp
|
|
7
|
+
Project-URL: Documentation, https://dcaisoc.github.io/reindeer-gp/
|
|
8
|
+
Classifier: Programming Language :: Python :: 3
|
|
9
|
+
Classifier: Intended Audience :: Education
|
|
10
|
+
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
|
|
11
|
+
Requires-Python: >=3.10
|
|
12
|
+
Description-Content-Type: text/markdown
|
|
13
|
+
Requires-Dist: numpy>=1.23
|
|
14
|
+
Provides-Extra: torch
|
|
15
|
+
Requires-Dist: torch>=2.1; extra == "torch"
|
|
16
|
+
Provides-Extra: gym
|
|
17
|
+
Requires-Dist: gymnasium>=0.29; extra == "gym"
|
|
18
|
+
Provides-Extra: dev
|
|
19
|
+
Requires-Dist: pytest>=7; extra == "dev"
|
|
20
|
+
|
|
21
|
+
# reindeer-sdk
|
|
22
|
+
|
|
23
|
+
The official [Reindeer Grand Prix](https://github.com/dcaisoc/reindeer-gp) engine in Python, plus tools to train, evaluate and submit bots.
|
|
24
|
+
|
|
25
|
+
```sh
|
|
26
|
+
pip install reindeer-sdk # or, before the first PyPI release:
|
|
27
|
+
pip install "git+https://github.com/dcaisoc/reindeer-gp#subdirectory=sdk"
|
|
28
|
+
reindeer selftest # proves your machine scores exactly like the leaderboard
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
```python
|
|
32
|
+
from reindeer_sdk import load_laps, laps_to_arrays, evaluate_bot, load_track
|
|
33
|
+
from reindeer_sdk.train import train_mlp
|
|
34
|
+
|
|
35
|
+
laps = load_laps("my-laps/") # .rgpr files downloaded from Driving data
|
|
36
|
+
inputs = ["ray0", "ray1", "ray2", "ray3", "ray4", "ray5", "ray6", "speed"]
|
|
37
|
+
X, Y = laps_to_arrays(laps, inputs) # sensors -> what you did
|
|
38
|
+
bot, history = train_mlp(X, Y, inputs, name="My bot") # or PyTorch + reindeer_sdk.from_torch
|
|
39
|
+
print(evaluate_bot(load_track("practice-1"), bot).result)
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
Command line: `reindeer tracks`, `reindeer validate bot.json`, `reindeer eval bot.json`, `reindeer login`, `reindeer submit bot.json`.
|
|
43
|
+
|
|
44
|
+
Notebooks and full documentation: <https://dcaisoc.github.io/reindeer-gp/>.
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
# reindeer-sdk
|
|
2
|
+
|
|
3
|
+
The official [Reindeer Grand Prix](https://github.com/dcaisoc/reindeer-gp) engine in Python, plus tools to train, evaluate and submit bots.
|
|
4
|
+
|
|
5
|
+
```sh
|
|
6
|
+
pip install reindeer-sdk # or, before the first PyPI release:
|
|
7
|
+
pip install "git+https://github.com/dcaisoc/reindeer-gp#subdirectory=sdk"
|
|
8
|
+
reindeer selftest # proves your machine scores exactly like the leaderboard
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
```python
|
|
12
|
+
from reindeer_sdk import load_laps, laps_to_arrays, evaluate_bot, load_track
|
|
13
|
+
from reindeer_sdk.train import train_mlp
|
|
14
|
+
|
|
15
|
+
laps = load_laps("my-laps/") # .rgpr files downloaded from Driving data
|
|
16
|
+
inputs = ["ray0", "ray1", "ray2", "ray3", "ray4", "ray5", "ray6", "speed"]
|
|
17
|
+
X, Y = laps_to_arrays(laps, inputs) # sensors -> what you did
|
|
18
|
+
bot, history = train_mlp(X, Y, inputs, name="My bot") # or PyTorch + reindeer_sdk.from_torch
|
|
19
|
+
print(evaluate_bot(load_track("practice-1"), bot).result)
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
Command line: `reindeer tracks`, `reindeer validate bot.json`, `reindeer eval bot.json`, `reindeer login`, `reindeer submit bot.json`.
|
|
23
|
+
|
|
24
|
+
Notebooks and full documentation: <https://dcaisoc.github.io/reindeer-gp/>.
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=68"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "reindeer-sdk"
|
|
7
|
+
version = "0.3.0"
|
|
8
|
+
description = "Reindeer Grand Prix: the official sleigh-racing engine in Python, plus tools to train, evaluate and submit bots."
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
license = "MIT"
|
|
11
|
+
requires-python = ">=3.10"
|
|
12
|
+
dependencies = ["numpy>=1.23"]
|
|
13
|
+
classifiers = [
|
|
14
|
+
"Programming Language :: Python :: 3",
|
|
15
|
+
"Intended Audience :: Education",
|
|
16
|
+
"Topic :: Scientific/Engineering :: Artificial Intelligence",
|
|
17
|
+
]
|
|
18
|
+
|
|
19
|
+
[project.optional-dependencies]
|
|
20
|
+
torch = ["torch>=2.1"]
|
|
21
|
+
gym = ["gymnasium>=0.29"]
|
|
22
|
+
dev = ["pytest>=7"]
|
|
23
|
+
|
|
24
|
+
[project.scripts]
|
|
25
|
+
reindeer = "reindeer_sdk.cli:main"
|
|
26
|
+
|
|
27
|
+
[project.urls]
|
|
28
|
+
Homepage = "https://github.com/dcaisoc/reindeer-gp"
|
|
29
|
+
Documentation = "https://dcaisoc.github.io/reindeer-gp/"
|
|
30
|
+
|
|
31
|
+
[tool.setuptools.packages.find]
|
|
32
|
+
include = ["reindeer_sdk*"]
|
|
33
|
+
|
|
34
|
+
[tool.setuptools.package-data]
|
|
35
|
+
reindeer_sdk = ["data/tracks/*.json", "data/twists/*.json", "data/golden/corpus/*.json", "data/golden/bots/*.json", "py.typed"]
|
|
36
|
+
|
|
37
|
+
[tool.pytest.ini_options]
|
|
38
|
+
testpaths = ["tests"]
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
"""Reindeer Grand Prix SDK: the official engine in Python, plus tools to train,
|
|
2
|
+
evaluate and submit bots.
|
|
3
|
+
|
|
4
|
+
from reindeer_sdk import load_track, load_laps, laps_to_arrays, evaluate_bot
|
|
5
|
+
|
|
6
|
+
Scores computed here are identical, to the tick, to the leaderboard's:
|
|
7
|
+
run `reindeer selftest` to check your installation.
|
|
8
|
+
"""
|
|
9
|
+
|
|
10
|
+
from .autopilot import baseline_policy, tuned_policy
|
|
11
|
+
from .bot import CompiledBot, InvalidBot, count_params, evaluate_bot, is_trainer_architecture, load_bot, validate_bot
|
|
12
|
+
from .config import DEFAULT_CONFIG, ENGINE_VERSION, input_names, make_config
|
|
13
|
+
from .data import can_mirror, laps_to_arrays, load_laps
|
|
14
|
+
from .export import bot_from_layers, from_torch, save_bot
|
|
15
|
+
from .replay import Replay, decode_replay, encode_replay, load_replay
|
|
16
|
+
from .run import Recorder, extract_training_pairs, run_policy, simulate_replay
|
|
17
|
+
from .sim import Observation, ReindeerGame, TrackGeometry
|
|
18
|
+
from .render import render_replay
|
|
19
|
+
from .tracks import TRACK_IDS, list_tracks, load_track
|
|
20
|
+
from .twist import Course, bundled_twist_ids, course, load_twist, twist_config
|
|
21
|
+
from .vec_env import VecSleighEnv
|
|
22
|
+
|
|
23
|
+
__version__ = "0.3.0"
|
|
24
|
+
|
|
25
|
+
__all__ = [
|
|
26
|
+
"CompiledBot", "Course", "DEFAULT_CONFIG", "ENGINE_VERSION", "InvalidBot", "Observation", "Recorder", "ReindeerGame", "Replay",
|
|
27
|
+
"TRACK_IDS", "TrackGeometry", "VecSleighEnv", "baseline_policy", "bot_from_layers", "bundled_twist_ids", "can_mirror", "course", "count_params", "decode_replay",
|
|
28
|
+
"encode_replay", "evaluate_bot", "extract_training_pairs", "from_torch", "input_names", "is_trainer_architecture",
|
|
29
|
+
"laps_to_arrays", "list_tracks", "load_bot", "load_laps", "load_replay", "load_track", "load_twist", "make_config", "render_replay", "run_policy",
|
|
30
|
+
"save_bot", "simulate_replay", "tuned_policy", "twist_config", "validate_bot", "__version__",
|
|
31
|
+
]
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
"""Talk to the Reindeer Grand Prix server with a personal API token.
|
|
2
|
+
|
|
3
|
+
Create a token on your Team page (web app), then either run `reindeer login`
|
|
4
|
+
or set REINDEER_TOKEN. Uses only the standard library.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
from __future__ import annotations
|
|
8
|
+
|
|
9
|
+
import json
|
|
10
|
+
import os
|
|
11
|
+
import stat
|
|
12
|
+
import time
|
|
13
|
+
import urllib.error
|
|
14
|
+
import urllib.request
|
|
15
|
+
from pathlib import Path
|
|
16
|
+
from typing import Any, Callable
|
|
17
|
+
|
|
18
|
+
DEFAULT_URL = "https://reindeer-gp.dcaisoc.workers.dev"
|
|
19
|
+
CONFIG_PATH = Path(os.environ.get("REINDEER_CONFIG", Path.home() / ".config" / "reindeer-gp" / "config.json"))
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
class ApiError(RuntimeError):
|
|
23
|
+
def __init__(self, status: int, code: str, message: str, details: dict[str, Any] | None = None) -> None:
|
|
24
|
+
super().__init__(message)
|
|
25
|
+
self.status = status
|
|
26
|
+
self.code = code
|
|
27
|
+
self.details = details or {}
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
def load_config() -> dict[str, str]:
|
|
31
|
+
try:
|
|
32
|
+
return json.loads(CONFIG_PATH.read_text("utf-8"))
|
|
33
|
+
except (OSError, ValueError):
|
|
34
|
+
return {}
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
def save_config(url: str, token: str) -> Path:
|
|
38
|
+
CONFIG_PATH.parent.mkdir(parents=True, exist_ok=True)
|
|
39
|
+
CONFIG_PATH.write_text(json.dumps({"url": url, "token": token}), "utf-8")
|
|
40
|
+
try:
|
|
41
|
+
CONFIG_PATH.chmod(stat.S_IRUSR | stat.S_IWUSR) # only you can read your token
|
|
42
|
+
except OSError:
|
|
43
|
+
pass
|
|
44
|
+
return CONFIG_PATH
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
class Client:
|
|
48
|
+
def __init__(self, url: str | None = None, token: str | None = None) -> None:
|
|
49
|
+
cfg = load_config()
|
|
50
|
+
self.url = (url or os.environ.get("REINDEER_URL") or cfg.get("url") or DEFAULT_URL).rstrip("/")
|
|
51
|
+
self.token = token or os.environ.get("REINDEER_TOKEN") or cfg.get("token")
|
|
52
|
+
|
|
53
|
+
def _request(self, method: str, path: str, body: Any = None) -> Any:
|
|
54
|
+
headers = {"accept": "application/json", "user-agent": "reindeer-sdk"}
|
|
55
|
+
data = None
|
|
56
|
+
if body is not None:
|
|
57
|
+
data = json.dumps(body).encode("utf-8")
|
|
58
|
+
headers["content-type"] = "application/json"
|
|
59
|
+
if self.token:
|
|
60
|
+
headers["authorization"] = f"Bearer {self.token}"
|
|
61
|
+
req = urllib.request.Request(f"{self.url}/api/v1{path}", data=data, method=method, headers=headers)
|
|
62
|
+
try:
|
|
63
|
+
with urllib.request.urlopen(req, timeout=60) as res:
|
|
64
|
+
raw = res.read()
|
|
65
|
+
return json.loads(raw) if raw else None
|
|
66
|
+
except urllib.error.HTTPError as e:
|
|
67
|
+
try:
|
|
68
|
+
err = json.loads(e.read()).get("error", {})
|
|
69
|
+
except ValueError:
|
|
70
|
+
err = {}
|
|
71
|
+
msg = err.get("message", f"HTTP {e.code}")
|
|
72
|
+
if e.code == 401 and self.token:
|
|
73
|
+
msg = "Your API token was not accepted (it may have been revoked). Create a new one on your Team page, then run `reindeer login`."
|
|
74
|
+
raise ApiError(e.code, err.get("code", "error"), msg, err.get("details")) from None
|
|
75
|
+
except urllib.error.URLError as e:
|
|
76
|
+
raise ApiError(0, "offline", f"Can't reach {self.url} ({e.reason}).") from None
|
|
77
|
+
except (TimeoutError, OSError) as e:
|
|
78
|
+
raise ApiError(0, "offline", f"No answer from {self.url} ({e}). Check your connection and try again.") from None
|
|
79
|
+
|
|
80
|
+
def me(self) -> dict[str, Any] | None:
|
|
81
|
+
return self._request("GET", "/me").get("user")
|
|
82
|
+
|
|
83
|
+
def validate(self, bot: dict[str, Any]) -> dict[str, Any]:
|
|
84
|
+
return self._request("POST", "/bots/validate", bot)
|
|
85
|
+
|
|
86
|
+
def submit(self, bot: dict[str, Any], tracks: list[str] | None = None) -> list[dict[str, Any]]:
|
|
87
|
+
"""Submit a bot. Returns one queued submission per track."""
|
|
88
|
+
if not self.token:
|
|
89
|
+
raise ApiError(401, "unauthenticated", "No API token. Create one on your Team page, then run `reindeer login`.")
|
|
90
|
+
body: dict[str, Any] = {"bot": bot}
|
|
91
|
+
if tracks:
|
|
92
|
+
body["tracks"] = tracks
|
|
93
|
+
return self._request("POST", "/submissions", body)["submissions"]
|
|
94
|
+
|
|
95
|
+
def submission(self, sub_id: str) -> dict[str, Any]:
|
|
96
|
+
return self._request("GET", f"/submissions/{sub_id}")["submission"]
|
|
97
|
+
|
|
98
|
+
def wait(self, sub_id: str, on_update: Callable[[dict[str, Any]], None] | None = None, timeout: float = 180) -> dict[str, Any]:
|
|
99
|
+
start = time.time()
|
|
100
|
+
delay = 0.5
|
|
101
|
+
while True:
|
|
102
|
+
s = self.submission(sub_id)
|
|
103
|
+
if on_update:
|
|
104
|
+
on_update(s)
|
|
105
|
+
if s["status"] in ("done", "rejected") or time.time() - start > timeout:
|
|
106
|
+
return s
|
|
107
|
+
time.sleep(delay)
|
|
108
|
+
delay = min(3.0, delay * 1.5)
|
|
109
|
+
|
|
110
|
+
def twists(self) -> list[dict[str, Any]]:
|
|
111
|
+
"""Twists that are live now or have ended (each with its schedule), without track data."""
|
|
112
|
+
return self._request("GET", "/api/v1/twists")["twists"]
|
|
113
|
+
|
|
114
|
+
def twist(self, twist_id: str) -> dict[str, Any]:
|
|
115
|
+
"""The full twist file (track and config) of a live or past twist."""
|
|
116
|
+
return self._request("GET", f"/api/v1/twists/{twist_id}")["twist"]
|
|
117
|
+
|
|
118
|
+
def board(self, track: str, kind: str = "bots") -> dict[str, Any]:
|
|
119
|
+
return self._request("GET", f"/boards/s1/{track}/base/{kind}")
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
"""Rule-based drivers. Port of engine/reindeer/autopilot.ts."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import math
|
|
6
|
+
|
|
7
|
+
from .sim import Observation
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
def baseline_policy(o: Observation) -> tuple[float, float]:
|
|
11
|
+
"""The baseline: finishes, but is deliberately not fast."""
|
|
12
|
+
r = o.rays
|
|
13
|
+
last = len(r) - 1
|
|
14
|
+
mid = (len(r) - 1) / 2
|
|
15
|
+
left = 0.0
|
|
16
|
+
right = 0.0
|
|
17
|
+
i = 0
|
|
18
|
+
while i < mid:
|
|
19
|
+
left += r[i] * (1 + i)
|
|
20
|
+
i += 1
|
|
21
|
+
i = last
|
|
22
|
+
while i > mid:
|
|
23
|
+
right += r[i] * (1 + last - i)
|
|
24
|
+
i -= 1
|
|
25
|
+
steer = (right - left) * 1.2
|
|
26
|
+
if steer > 1:
|
|
27
|
+
steer = 1.0
|
|
28
|
+
if steer < -1:
|
|
29
|
+
steer = -1.0
|
|
30
|
+
front = r[math.floor(mid)]
|
|
31
|
+
target = 0.2 + 0.55 * front
|
|
32
|
+
throttle = 1.0 if o.speed < target else -0.5
|
|
33
|
+
return steer, throttle
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
def tuned_policy(o: Observation) -> tuple[float, float]:
|
|
37
|
+
"""A stronger hand-written driver, to show there is headroom."""
|
|
38
|
+
r = o.rays
|
|
39
|
+
steer = (r[4] - r[2]) * 2.2 + (r[5] - r[1]) * 1.2 + (r[6] - r[0]) * 0.5
|
|
40
|
+
if steer > 1:
|
|
41
|
+
steer = 1.0
|
|
42
|
+
if steer < -1:
|
|
43
|
+
steer = -1.0
|
|
44
|
+
look = (r[2] + r[4]) * 0.75
|
|
45
|
+
if r[3] < look:
|
|
46
|
+
look = r[3]
|
|
47
|
+
target = 0.3 + 0.75 * look
|
|
48
|
+
throttle = 1.0 if o.speed < target else -1.0 if o.speed > target + 0.08 else 0.2
|
|
49
|
+
return steer, throttle
|
|
@@ -0,0 +1,214 @@
|
|
|
1
|
+
"""Bot files ("rgp-bot" v1): validation and deterministic inference.
|
|
2
|
+
|
|
3
|
+
Port of engine/core/bot.ts and engine/reindeer/bot-policy.ts. The server scores
|
|
4
|
+
bots with the TypeScript version; this one gives bit-identical results, so
|
|
5
|
+
`reindeer eval` shows exactly the score the leaderboard will show.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from __future__ import annotations
|
|
9
|
+
|
|
10
|
+
import json
|
|
11
|
+
import math
|
|
12
|
+
from pathlib import Path
|
|
13
|
+
from typing import Any, Sequence
|
|
14
|
+
|
|
15
|
+
from .config import DEFAULT_CONFIG, input_names
|
|
16
|
+
from .fmath import fsigmoid, ftanh
|
|
17
|
+
from .quant import mulberry32
|
|
18
|
+
from .replay import Replay
|
|
19
|
+
from .run import Policy, run_policy
|
|
20
|
+
from .sim import Observation, TrackGeometry
|
|
21
|
+
|
|
22
|
+
BOT_FORMAT = "rgp-bot"
|
|
23
|
+
BOT_VERSION = 1
|
|
24
|
+
ACTIVATIONS = ["relu", "tanh", "sigmoid", "linear"]
|
|
25
|
+
PARAM_CAP = 5000
|
|
26
|
+
MAX_INPUTS = 16
|
|
27
|
+
MAX_LAYERS = 6
|
|
28
|
+
MAX_WEIGHT = 1000
|
|
29
|
+
BOT_TICKS_PER_ACTION = 3 # 20 Hz decisions
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
class InvalidBot(ValueError):
|
|
33
|
+
def __init__(self, errors: list[str]) -> None:
|
|
34
|
+
super().__init__("The bot file has problems:\n - " + "\n - ".join(errors))
|
|
35
|
+
self.errors = errors
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
def _is_int(x: Any) -> bool:
|
|
39
|
+
return isinstance(x, int) and not isinstance(x, bool)
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
def _is_num(x: Any) -> bool:
|
|
43
|
+
return isinstance(x, (int, float)) and not isinstance(x, bool) and math.isfinite(x)
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
def count_params(layers: Sequence[dict[str, Any]]) -> int:
|
|
47
|
+
return sum(l["in"] * l["out"] + l["out"] for l in layers)
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
def validate_bot(bot: Any, *, param_cap: int = PARAM_CAP, allowed_inputs: Sequence[str] | None = None) -> tuple[list[str], int]:
|
|
51
|
+
"""Check a bot file. Returns (errors, params); no errors means the server will accept it."""
|
|
52
|
+
allowed = list(allowed_inputs or input_names(DEFAULT_CONFIG))
|
|
53
|
+
e: list[str] = []
|
|
54
|
+
if not isinstance(bot, dict):
|
|
55
|
+
return ["The bot file is not a JSON object."], 0
|
|
56
|
+
b = bot
|
|
57
|
+
if b.get("format") != BOT_FORMAT:
|
|
58
|
+
e.append(f'"format" must be "{BOT_FORMAT}".')
|
|
59
|
+
if b.get("version") != BOT_VERSION:
|
|
60
|
+
e.append(f'"version" must be {BOT_VERSION} (this server reads version {BOT_VERSION} bots).')
|
|
61
|
+
if b.get("game") != "reindeer-gp":
|
|
62
|
+
e.append('"game" must be "reindeer-gp".')
|
|
63
|
+
meta = b.get("meta")
|
|
64
|
+
if not isinstance(meta, dict) or not isinstance(meta.get("name"), str) or not meta["name"].strip():
|
|
65
|
+
e.append("Give your bot a name in meta.name.")
|
|
66
|
+
elif len(meta["name"]) > 60:
|
|
67
|
+
e.append("meta.name must be at most 60 characters.")
|
|
68
|
+
|
|
69
|
+
inputs = b.get("inputs") if isinstance(b.get("inputs"), list) else None
|
|
70
|
+
if not inputs:
|
|
71
|
+
e.append('"inputs" must list at least one sensor name, e.g. ["ray0", ..., "speed"].')
|
|
72
|
+
else:
|
|
73
|
+
if len(inputs) > MAX_INPUTS:
|
|
74
|
+
e.append(f"Too many inputs: {len(inputs)} (the limit is {MAX_INPUTS}).")
|
|
75
|
+
seen: set[str] = set()
|
|
76
|
+
for n in inputs:
|
|
77
|
+
if not isinstance(n, str) or n not in allowed:
|
|
78
|
+
e.append(f'Unknown input "{n}". Available here: {", ".join(allowed)}.')
|
|
79
|
+
elif n in seen:
|
|
80
|
+
e.append(f'Input "{n}" is listed twice.')
|
|
81
|
+
if isinstance(n, str):
|
|
82
|
+
seen.add(n)
|
|
83
|
+
outs = b.get("outputs")
|
|
84
|
+
if not isinstance(outs, list) or outs != ["steer", "throttle"]:
|
|
85
|
+
e.append('"outputs" must be exactly ["steer", "throttle"].')
|
|
86
|
+
|
|
87
|
+
layers = b.get("layers") if isinstance(b.get("layers"), list) else None
|
|
88
|
+
params = 0
|
|
89
|
+
if not layers:
|
|
90
|
+
e.append("The network needs at least one layer.")
|
|
91
|
+
else:
|
|
92
|
+
if len(layers) > MAX_LAYERS:
|
|
93
|
+
e.append(f"Too many layers: {len(layers)} (the limit is {MAX_LAYERS}).")
|
|
94
|
+
prev_out = len(inputs) if inputs else -1
|
|
95
|
+
for i, l in enumerate(layers):
|
|
96
|
+
n = i + 1
|
|
97
|
+
if not isinstance(l, dict) or l.get("type") != "dense":
|
|
98
|
+
e.append(f'Layer {n}: only "dense" layers are supported in version 1.')
|
|
99
|
+
continue
|
|
100
|
+
li, lo = l.get("in"), l.get("out")
|
|
101
|
+
if not _is_int(li) or not _is_int(lo) or li < 1 or lo < 1 or li > 512 or lo > 512:
|
|
102
|
+
e.append(f'Layer {n}: "in" and "out" must be whole numbers between 1 and 512.')
|
|
103
|
+
continue
|
|
104
|
+
if prev_out >= 0 and li != prev_out:
|
|
105
|
+
e.append(f"Layer 1 expects {li} inputs but the bot lists {prev_out} inputs." if i == 0
|
|
106
|
+
else f"Layer {n} expects {li} inputs but layer {i} outputs {prev_out}.")
|
|
107
|
+
prev_out = lo
|
|
108
|
+
if l.get("activation") not in ACTIVATIONS:
|
|
109
|
+
e.append(f"Layer {n}: activation must be one of {', '.join(ACTIVATIONS)}.")
|
|
110
|
+
params += li * lo + lo
|
|
111
|
+
w = l.get("weights")
|
|
112
|
+
if not isinstance(w, list) or len(w) != lo or any(not isinstance(r, list) or len(r) != li for r in w):
|
|
113
|
+
e.append(f'Layer {n}: "weights" must be {lo} rows of {li} numbers.')
|
|
114
|
+
elif any(not _is_num(x) for r in w for x in r):
|
|
115
|
+
e.append(f"Layer {n}: weights must all be finite numbers (no NaN or Infinity).")
|
|
116
|
+
elif any(abs(x) > MAX_WEIGHT for r in w for x in r):
|
|
117
|
+
e.append(f"Layer {n}: a weight is larger than {MAX_WEIGHT} in size.")
|
|
118
|
+
bias = l.get("bias")
|
|
119
|
+
if not isinstance(bias, list) or len(bias) != lo:
|
|
120
|
+
e.append(f'Layer {n}: "bias" must have {lo} numbers.')
|
|
121
|
+
elif any(not _is_num(x) for x in bias):
|
|
122
|
+
e.append(f"Layer {n}: biases must all be finite numbers.")
|
|
123
|
+
elif any(abs(x) > MAX_WEIGHT for x in bias):
|
|
124
|
+
e.append(f"Layer {n}: a bias is larger than {MAX_WEIGHT} in size.")
|
|
125
|
+
if prev_out != 2:
|
|
126
|
+
e.append(f"The last layer must output 2 numbers (steer, throttle), not {prev_out}.")
|
|
127
|
+
if params > param_cap:
|
|
128
|
+
e.append(f"The network has {params} parameters; the limit is {param_cap}. Use fewer or smaller layers.")
|
|
129
|
+
|
|
130
|
+
if not e:
|
|
131
|
+
cb = CompiledBot(b)
|
|
132
|
+
rand = mulberry32(20261201)
|
|
133
|
+
for _ in range(256):
|
|
134
|
+
x = [rand() for _ in cb.inputs]
|
|
135
|
+
y = cb.forward(x)
|
|
136
|
+
if not _is_num(y[0]) or not _is_num(y[1]):
|
|
137
|
+
e.append("The network produces NaN or Infinity for some sensor readings. Check for exploding weights.")
|
|
138
|
+
break
|
|
139
|
+
return e, params
|
|
140
|
+
|
|
141
|
+
|
|
142
|
+
class CompiledBot:
|
|
143
|
+
"""A bot ready to run. forward() is the deterministic inference the server uses."""
|
|
144
|
+
|
|
145
|
+
def __init__(self, bot: dict[str, Any]) -> None:
|
|
146
|
+
self.inputs: list[str] = list(bot["inputs"])
|
|
147
|
+
self.layers = [(l["in"], l["out"], l["activation"], [[float(v) for v in row] for row in l["weights"]], [float(v) for v in l["bias"]])
|
|
148
|
+
for l in bot["layers"]]
|
|
149
|
+
self.params = count_params(bot["layers"])
|
|
150
|
+
self.meta = bot.get("meta", {})
|
|
151
|
+
|
|
152
|
+
def forward(self, x: Sequence[float]) -> list[float]:
|
|
153
|
+
cur = list(x)
|
|
154
|
+
for n_in, n_out, act, w, b in self.layers:
|
|
155
|
+
nxt = []
|
|
156
|
+
for o in range(n_out):
|
|
157
|
+
s = 0.0
|
|
158
|
+
row = w[o]
|
|
159
|
+
for i in range(n_in):
|
|
160
|
+
s = s + row[i] * cur[i]
|
|
161
|
+
s = s + b[o]
|
|
162
|
+
if act == "relu":
|
|
163
|
+
s = s if s > 0 else 0.0
|
|
164
|
+
elif act == "tanh":
|
|
165
|
+
s = ftanh(s)
|
|
166
|
+
elif act == "sigmoid":
|
|
167
|
+
s = fsigmoid(s)
|
|
168
|
+
nxt.append(s)
|
|
169
|
+
cur = nxt
|
|
170
|
+
return cur
|
|
171
|
+
|
|
172
|
+
def policy(self) -> Policy:
|
|
173
|
+
names = self.inputs
|
|
174
|
+
|
|
175
|
+
def act(o: Observation) -> tuple[float, float]:
|
|
176
|
+
y = self.forward(o.to_inputs(names))
|
|
177
|
+
return y[0], y[1]
|
|
178
|
+
|
|
179
|
+
return act
|
|
180
|
+
|
|
181
|
+
|
|
182
|
+
def load_bot(path: str | Path) -> dict[str, Any]:
|
|
183
|
+
return json.loads(Path(path).read_text("utf-8"))
|
|
184
|
+
|
|
185
|
+
|
|
186
|
+
def is_trainer_architecture(bot: dict[str, Any]) -> bool:
|
|
187
|
+
"""Could the in-browser trainer have made this? (Beginner bracket rule.)"""
|
|
188
|
+
L = bot["layers"]
|
|
189
|
+
if not 1 <= len(L) <= 3:
|
|
190
|
+
return False
|
|
191
|
+
for l in L[:-1]:
|
|
192
|
+
if l["out"] > 64 or l["activation"] not in ("relu", "tanh"):
|
|
193
|
+
return False
|
|
194
|
+
return L[-1]["activation"] == "tanh"
|
|
195
|
+
|
|
196
|
+
|
|
197
|
+
def evaluate_bot(track: Any, bot: dict[str, Any], *, check: bool = True,
|
|
198
|
+
cfg: dict[str, Any] | None = None, geo: TrackGeometry | None = None) -> Replay:
|
|
199
|
+
"""Race a bot for one lap, exactly as the server does. Returns the replay (result in .result).
|
|
200
|
+
|
|
201
|
+
`track` is a track id, a twist id, or a loaded track or twist (twists bring their own config).
|
|
202
|
+
"""
|
|
203
|
+
from .twist import course
|
|
204
|
+
|
|
205
|
+
c = course(track)
|
|
206
|
+
cfg = cfg or c.cfg
|
|
207
|
+
if check:
|
|
208
|
+
errors, _ = validate_bot(bot, allowed_inputs=input_names(cfg))
|
|
209
|
+
if errors:
|
|
210
|
+
raise InvalidBot(errors)
|
|
211
|
+
cb = CompiledBot(bot)
|
|
212
|
+
name = (bot.get("meta") or {}).get("name", "bot")
|
|
213
|
+
return run_policy(c.track, cb.policy(), {"kind": "bot", "name": name}, cfg=cfg, ticks_per_action=BOT_TICKS_PER_ACTION,
|
|
214
|
+
geo=geo, twist=c.twist)
|