Skip to content

Commit 56eb9a6

Browse files
authored
Fix list index error when goal_cell and reset_cell are passed as options in PointMaze env (#164)
* Fix list index error when goal_cell and reset_cell are passed as options * Pre-commit formatting * Tests for reset_cell and goal_cell
1 parent ddac411 commit 56eb9a6

File tree

3 files changed

+34
-7
lines changed

3 files changed

+34
-7
lines changed

gymnasium_robotics/envs/maze/maze.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -218,12 +218,11 @@ def reset(
218218
assert self.maze.map_length > options["goal_cell"][1]
219219
assert self.maze.map_width > options["goal_cell"][0]
220220
assert (
221-
self.maze.maze_map[options["goal_cell"][1], options["goal_cell"][0]]
221+
self.maze.maze_map[options["goal_cell"][1]][options["goal_cell"][0]]
222222
!= 1
223223
), f"Goal can't be placed in a wall cell, {options['goal_cell']}"
224224

225225
goal = self.maze.cell_rowcol_to_xy(options["goal_cell"])
226-
227226
else:
228227
goal = self.generate_target_goal()
229228

@@ -235,8 +234,8 @@ def reset(
235234
assert self.maze.map_length > options["reset_cell"][1]
236235
assert self.maze.map_width > options["reset_cell"][0]
237236
assert (
238-
self.maze.maze_map[
239-
options["reset_cell"][1], options["reset_cell"][0]
237+
self.maze.maze_map[options["reset_cell"][1]][
238+
options["reset_cell"][0]
240239
]
241240
!= 1
242241
), f"Reset can't be placed in a wall cell, {options['reset_cell']}"

gymnasium_robotics/envs/maze/maze_v4.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,7 @@ def reset(
295295
assert self.maze.map_length > options["goal_cell"][1]
296296
assert self.maze.map_width > options["goal_cell"][0]
297297
assert (
298-
self.maze.maze_map[options["goal_cell"][1], options["goal_cell"][0]]
298+
self.maze.maze_map[options["goal_cell"][1]][options["goal_cell"][0]]
299299
!= 1
300300
), f"Goal can't be placed in a wall cell, {options['goal_cell']}"
301301

@@ -312,8 +312,8 @@ def reset(
312312
assert self.maze.map_length > options["reset_cell"][1]
313313
assert self.maze.map_width > options["reset_cell"][0]
314314
assert (
315-
self.maze.maze_map[
316-
options["reset_cell"][1], options["reset_cell"][0]
315+
self.maze.maze_map[options["reset_cell"][1]][
316+
options["reset_cell"][0]
317317
]
318318
!= 1
319319
), f"Reset can't be placed in a wall cell, {options['reset_cell']}"

tests/envs/maze/test_point_maze.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,31 @@ def test_reset():
1111
assert not info["success"]
1212
dist = np.linalg.norm(obs["achieved_goal"] - obs["desired_goal"])
1313
assert dist > 0.45, f"dist={dist} < 0.45"
14+
15+
16+
def test_reset_cell():
17+
"""Check that passing the reset_cell location ensures that the agent resets in the right cell."""
18+
map = [
19+
[1, 1, 1, 1],
20+
[1, "r", "r", 1],
21+
[1, "r", "g", 1],
22+
[1, 1, 1, 1],
23+
]
24+
env = gym.make("PointMaze_UMaze-v3", maze_map=map)
25+
obs = env.reset(options={"reset_cell": [1, 2]}, seed=42)[0]
26+
desired_obs = np.array([0.67929896, 0.59868401, 0, 0])
27+
np.testing.assert_almost_equal(desired_obs, obs["observation"], decimal=4)
28+
29+
30+
def test_goal_cell():
31+
"""Check that passing the goal_cell location ensures that the goal spawns in the right cell."""
32+
map = [
33+
[1, 1, 1, 1],
34+
[1, "r", "g", 1],
35+
[1, "g", "g", 1],
36+
[1, 1, 1, 1],
37+
]
38+
env = gym.make("PointMaze_UMaze-v3", maze_map=map)
39+
obs = env.reset(options={"goal_cell": [2, 1]}, seed=42)[0]
40+
desired_goal = np.array([-0.36302198, -0.53056078])
41+
np.testing.assert_almost_equal(desired_goal, obs["desired_goal"], decimal=4)

0 commit comments

Comments
 (0)