Skip to content

Commit 41fcd8b

Browse files
committed
fix(python): accept PettingZoo framework reset options
2 parents d7c3b5e + 948338e commit 41fcd8b

5 files changed

Lines changed: 30 additions & 7 deletions

File tree

‎crates/apps/gfa-py/src/lib.rs‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,12 @@ impl NativeEnv {
112112
}
113113

114114
fn public_text(&self, py: Python<'_>) -> PyResult<String> {
115-
py.detach(|| self.inner.observe(Viewer::Spectator).map(|observation| observation.text))
116-
.map_err(error)
115+
py.detach(|| {
116+
self.inner
117+
.observe(Viewer::Spectator)
118+
.map(|observation| observation.text)
119+
})
120+
.map_err(error)
117121
}
118122

119123
fn clone(&self) -> Self {

‎crates/apps/gfa-server/src/briefing_tests.rs‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,8 @@ async fn every_game_briefing_contains_executable_examples_and_matching_views() -
151151
data(&compact, "observation_format")?["tensor_shape"],
152152
if game == "tictactoe" {
153153
json!([3, 3, 3])
154+
} else if game == "connect4" {
155+
json!([3, 6, 7])
154156
} else if game == "chess" {
155157
json!([20, 8, 8])
156158
} else if game == "sudoku" {

‎crates/games/gfa-game-connect4/src/lib.rs‎

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,10 @@ impl Game for Connect4 {
323323
rows,
324324
to_move: state.to_move
325325
}),
326-
tensor: Some(Tensor { shape: vec![3, 6, 7], values }),
326+
tensor: Some(Tensor {
327+
shape: vec![3, 6, 7],
328+
values,
329+
}),
327330
}
328331
}
329332

@@ -439,7 +442,9 @@ mod tensor_tests {
439442
assert_eq!(observation, Connect4::observe(&state, Viewer::Player(0)));
440443
assert_eq!(observation, Connect4::observe(&state, Viewer::Player(1)));
441444
let board: Board = serde_json::from_value(observation.json)?;
442-
let tensor = observation.tensor.ok_or_else(|| GameError::position("missing tensor"))?;
445+
let tensor = observation
446+
.tensor
447+
.ok_or_else(|| GameError::position("missing tensor"))?;
443448
assert_eq!(tensor.shape, vec![3, 6, 7]);
444449
assert_eq!(tensor.values.len(), 126);
445450
assert_eq!(tensor.values[4 * 7], 1.0);
@@ -450,7 +455,11 @@ mod tensor_tests {
450455
for plane in 0..2 {
451456
assert_eq!(
452457
tensor.values[plane * 42 + row * 7 + col],
453-
if *cell == Some(plane as PlayerId) { 1.0 } else { 0.0 }
458+
if *cell == Some(plane as PlayerId) {
459+
1.0
460+
} else {
461+
0.0
462+
}
454463
);
455464
}
456465
}

‎python/gamesforai/aec_env.py‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,7 @@ def _refresh_infos(self):
5656

5757
def reset(self, seed=None, options=None):
5858
options = options or {}
59-
if set(options) - {"position"}:
60-
raise ValueError("supported reset option: position")
59+
# PettingZoo callers may pass options intended for other wrappers.
6160
position = options.get("position")
6261
if position is not None and not isinstance(position, str):
6362
raise ValueError("position must be a notation string")

‎python/tests/test_aec.py‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,3 +68,12 @@ def test_seeded_self_play_and_cap_endings():
6868
assert not any(first.terminations.values())
6969
assert first.native.get_state() == second.native.get_state()
7070
assert first.render() == first.native.public_text()
71+
72+
73+
def test_reset_accepts_framework_options_and_validates_position():
74+
env = aec("tictactoe")
75+
env.reset(seed=4, options={"options": 1})
76+
before = env.native.get_state()
77+
with pytest.raises(ValueError, match="notation string"):
78+
env.reset(seed=4, options={"position": 12})
79+
assert env.native.get_state() == before

0 commit comments

Comments
 (0)