-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathinterface.py
More file actions
212 lines (167 loc) · 6.66 KB
/
Copy pathinterface.py
File metadata and controls
212 lines (167 loc) · 6.66 KB
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
"""Planner interface protocol for the Social Navigation Benchmark.
Defines the standard interface that all baseline planners must implement.
Ensures a consistent API across different planning algorithms including
SocialForce, PPO, Random, and future baselines.
"""
from __future__ import annotations
from dataclasses import dataclass, field
from typing import TYPE_CHECKING, Any, Protocol, TypedDict, TypeGuard, cast
if TYPE_CHECKING:
from collections.abc import Mapping
class ObservationKwargs(TypedDict, total=False):
"""Keyword payload accepted by the planner-facing Observation constructor."""
dt: float
robot: dict[str, Any]
agents: list[dict[str, Any]]
obstacles: list[Any]
@dataclass
class Observation:
"""Canonical baseline planner observation container.
The classic baseline planners consume a small world-frame payload with
timestep, robot state, nearby agents, and optional obstacle geometry.
Learned-policy dict observations intentionally remain outside this type.
"""
dt: float
robot: dict[str, Any]
agents: list[dict[str, Any]]
obstacles: list[Any] = field(default_factory=list)
def observation_from_mapping(obs: Mapping[str, Any]) -> Observation:
"""Build an Observation from a dict-like benchmark payload.
Missing optional obstacles use the dataclass default, while missing required keys
or unexpected keys still raise TypeError.
Returns:
Observation: A planner-facing observation container.
"""
payload = obs if isinstance(obs, dict) else dict(obs)
return Observation(**cast("ObservationKwargs", payload))
def is_observation_mapping(obs: Observation | dict[str, Any]) -> TypeGuard[dict[str, Any]]:
"""Return whether an observation payload is a mutable mapping input.
Returns:
bool: True when the payload should be converted through ``observation_from_mapping``.
"""
return isinstance(obs, dict)
@dataclass(frozen=True)
class ObservationContract:
"""Planner-facing observation assumptions declared as lightweight metadata."""
mode: str
supported_modes: tuple[str, ...]
required_inputs: tuple[str, ...]
active_mode: str | None = None
observation_level: str | None = None
perception_assumption: str | None = None
frame: str = "world"
normalization: str = "raw"
pedestrian_ordering: str = "distance_ascending"
missing_value: str | None = None
notes: str = ""
def to_metadata(self) -> dict[str, Any]:
"""Return a JSON-serializable observation contract payload."""
return {
"mode": self.mode,
"active_mode": self.active_mode or self.mode,
"observation_level": self.observation_level,
"perception_assumption": self.perception_assumption,
"supported_modes": list(self.supported_modes),
"required_inputs": list(self.required_inputs),
"frame": self.frame,
"normalization": self.normalization,
"pedestrian_ordering": self.pedestrian_ordering,
"missing_value": self.missing_value,
"notes": self.notes,
}
@dataclass(frozen=True)
class ActionContract:
"""Planner action assumptions before any benchmark/environment conversion."""
command_space: str
output_keys: tuple[str, ...]
frame: str = "robot"
normalization: str = "raw"
units: str = "mps_radps"
scaling: str = "none"
compatible_robot_kinematics: tuple[str, ...] = (
"differential_drive",
"bicycle_drive",
"holonomic",
"mixed",
"unknown",
)
active_robot_kinematics: str | None = None
notes: str = ""
bounds: dict[str, tuple[float, float]] = field(default_factory=dict)
def to_metadata(self) -> dict[str, Any]:
"""Return a JSON-serializable action contract payload."""
return {
"command_space": self.command_space,
"output_keys": list(self.output_keys),
"frame": self.frame,
"normalization": self.normalization,
"units": self.units,
"scaling": self.scaling,
"compatible_robot_kinematics": list(self.compatible_robot_kinematics),
"active_robot_kinematics": self.active_robot_kinematics,
"bounds": {key: list(value) for key, value in self.bounds.items()},
"notes": self.notes,
}
@dataclass(frozen=True)
class PlannerMetadata:
"""First-class planner compatibility metadata for benchmark-facing workflows."""
planner_id: str
observation_contract: ObservationContract
action_contract: ActionContract
reset_contract: str = "seeded_reset"
scenario_requirements: tuple[str, ...] = ()
compatibility_scope: str = "metadata_only"
notes: str = ""
def to_metadata(self) -> dict[str, Any]:
"""Return a JSON-serializable planner metadata payload."""
return {
"planner_id": self.planner_id,
"observation_contract": self.observation_contract.to_metadata(),
"action_contract": self.action_contract.to_metadata(),
"reset_contract": self.reset_contract,
"scenario_requirements": list(self.scenario_requirements),
"compatibility_scope": self.compatibility_scope,
"notes": self.notes,
}
class PlannerProtocol(Protocol):
"""Protocol defining the standard interface for navigation planners.
Responsibilities:
- Initialization with configuration and seed
- Action generation from observations
- State reset with optional seed
- Configuration updates
- Resource cleanup
"""
def __init__(self, config: Any, *, seed: int | None = None) -> None:
"""Initialize the planner.
Args:
config: Planner-specific configuration object or dict.
seed: Optional random seed for deterministic behavior.
"""
def step(self, obs: dict[str, Any] | Any) -> dict[str, float]:
"""Generate an action from an observation.
Args:
obs: Observation from the environment.
Returns:
Action dictionary (e.g., {"vx": 1.0, "vy": 0.5} or
{"v": 1.0, "omega": 0.2}).
"""
def reset(self, *, seed: int | None = None) -> None:
"""Reset internal planner state.
Args:
seed: Optional random seed.
"""
def configure(self, config: Any) -> None:
"""Update the planner's configuration.
Args:
config: New configuration object or dict.
"""
def close(self) -> None:
"""Release resources (models, files, etc.)."""
__all__ = [
"ActionContract",
"Observation",
"ObservationContract",
"PlannerMetadata",
"PlannerProtocol",
]