Skip to content

Fix list index error when goal_cell and reset_cell are passed as options in PointMaze env #164

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions gymnasium_robotics/envs/maze/maze.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,12 +218,11 @@ def reset(
assert self.maze.map_length > options["goal_cell"][1]
assert self.maze.map_width > options["goal_cell"][0]
assert (
self.maze.maze_map[options["goal_cell"][1], options["goal_cell"][0]]
self.maze.maze_map[options["goal_cell"][1]][options["goal_cell"][0]]
!= 1
), f"Goal can't be placed in a wall cell, {options['goal_cell']}"

goal = self.maze.cell_rowcol_to_xy(options["goal_cell"])

else:
goal = self.generate_target_goal()

Expand All @@ -235,8 +234,8 @@ def reset(
assert self.maze.map_length > options["reset_cell"][1]
assert self.maze.map_width > options["reset_cell"][0]
assert (
self.maze.maze_map[
options["reset_cell"][1], options["reset_cell"][0]
self.maze.maze_map[options["reset_cell"][1]][
options["reset_cell"][0]
]
!= 1
), f"Reset can't be placed in a wall cell, {options['reset_cell']}"
Expand Down
6 changes: 3 additions & 3 deletions gymnasium_robotics/envs/maze/maze_v4.py
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ def reset(
assert self.maze.map_length > options["goal_cell"][1]
assert self.maze.map_width > options["goal_cell"][0]
assert (
self.maze.maze_map[options["goal_cell"][1], options["goal_cell"][0]]
self.maze.maze_map[options["goal_cell"][1]][options["goal_cell"][0]]
!= 1
), f"Goal can't be placed in a wall cell, {options['goal_cell']}"

Expand All @@ -312,8 +312,8 @@ def reset(
assert self.maze.map_length > options["reset_cell"][1]
assert self.maze.map_width > options["reset_cell"][0]
assert (
self.maze.maze_map[
options["reset_cell"][1], options["reset_cell"][0]
self.maze.maze_map[options["reset_cell"][1]][
options["reset_cell"][0]
]
!= 1
), f"Reset can't be placed in a wall cell, {options['reset_cell']}"
Expand Down
28 changes: 28 additions & 0 deletions tests/envs/maze/test_point_maze.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,31 @@ def test_reset():
assert not info["success"]
dist = np.linalg.norm(obs["achieved_goal"] - obs["desired_goal"])
assert dist > 0.45, f"dist={dist} < 0.45"


def test_reset_cell():
"""Check that passing the reset_cell location ensures that the agent resets in the right cell."""
map = [
[1, 1, 1, 1],
[1, "r", "r", 1],
[1, "r", "g", 1],
[1, 1, 1, 1],
]
env = gym.make("PointMaze_UMaze-v3", maze_map=map)
obs = env.reset(options={"reset_cell": [1, 2]}, seed=42)[0]
desired_obs = np.array([0.67929896, 0.59868401, 0, 0])
np.testing.assert_almost_equal(desired_obs, obs["observation"], decimal=4)


def test_goal_cell():
"""Check that passing the goal_cell location ensures that the goal spawns in the right cell."""
map = [
[1, 1, 1, 1],
[1, "r", "g", 1],
[1, "g", "g", 1],
[1, 1, 1, 1],
]
env = gym.make("PointMaze_UMaze-v3", maze_map=map)
obs = env.reset(options={"goal_cell": [2, 1]}, seed=42)[0]
desired_goal = np.array([-0.36302198, -0.53056078])
np.testing.assert_almost_equal(desired_goal, obs["desired_goal"], decimal=4)