Skip to content
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

Change XML saving for maze to fix infrequent crash #206

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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
24 changes: 18 additions & 6 deletions gymnasium_robotics/envs/maze/maze.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,11 +150,19 @@ def make_maze(
maze._unique_reset_locations += maze._combined_locations

# Save new xml with maze to a temporary file
with tempfile.TemporaryDirectory() as tmp_dir:
temp_xml_path = path.join(path.dirname(tmp_dir), "ant_maze.xml")
tree.write(temp_xml_path)

return maze, temp_xml_path
# Make temporary file object and make the string path to our new file
tmp_dir = tempfile.TemporaryDirectory()
temp_xml_path = path.join(tmp_dir.name, "ant_maze.xml")

# Write the new xml to the temporary file
with open(temp_xml_path, "wb") as xml_file:
tree.write(xml_file)

return (
maze,
temp_xml_path,
tmp_dir, # The tmp_dir object is returned to keep it alive
)


class MazeEnv(GoalEnv):
Expand All @@ -172,7 +180,7 @@ def __init__(

self.reward_type = reward_type
self.continuing_task = continuing_task
self.maze, self.tmp_xml_file_path = Maze.make_maze(
self.maze, self.tmp_xml_file_path, self.tmp_dir = Maze.make_maze(
agent_xml_path, maze_map, maze_size_scaling, maze_height
)

Expand Down Expand Up @@ -308,3 +316,7 @@ def compute_truncated(

def update_target_site_pos(self, pos):
raise NotImplementedError

def __del__(self):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this actually required? does it now cleanup in /tmp without it?

self.tmp_dir.cleanup()
super().__del__()
28 changes: 18 additions & 10 deletions gymnasium_robotics/envs/maze/maze_v4.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
"""
import math
import tempfile
import time
import xml.etree.ElementTree as ET
from os import path
from typing import Dict, List, Optional, Union
Expand Down Expand Up @@ -53,7 +52,6 @@ def __init__(
maze_size_scaling: float,
maze_height: float,
):

self._maze_map = maze_map
self._maze_size_scaling = maze_size_scaling
self._maze_height = maze_height
Expand Down Expand Up @@ -235,12 +233,19 @@ def make_maze(
maze._unique_reset_locations += maze._combined_locations

# Save new xml with maze to a temporary file
with tempfile.TemporaryDirectory() as tmp_dir:
temp_xml_name = f"ant_maze{str(time.time())}.xml"
temp_xml_path = path.join(path.dirname(tmp_dir), temp_xml_name)
tree.write(temp_xml_path)

return maze, temp_xml_path
# Make temporary file object and make the string path to our new file
tmp_dir = tempfile.TemporaryDirectory()
temp_xml_path = path.join(tmp_dir.name, "ant_maze.xml")

# Write the new xml to the temporary file
with open(temp_xml_path, "wb") as xml_file:
tree.write(xml_file)

return (
maze,
temp_xml_path,
tmp_dir, # The tmp_dir object is returned to keep it alive
)


class MazeEnv(GoalEnv):
Expand All @@ -256,11 +261,10 @@ def __init__(
position_noise_range: float = 0.25,
**kwargs,
):

self.reward_type = reward_type
self.continuing_task = continuing_task
self.reset_target = reset_target
self.maze, self.tmp_xml_file_path = Maze.make_maze(
self.maze, self.tmp_xml_file_path, self.tmp_dir = Maze.make_maze(
agent_xml_path, maze_map, maze_size_scaling, maze_height
)

Expand Down Expand Up @@ -419,3 +423,7 @@ def update_target_site_pos(self, pos):
"""Override this method to update the site qpos in the MuJoCo simulation
after a new goal is selected. This is mainly for visualization purposes."""
raise NotImplementedError

def __del__(self):
self.tmp_dir.cleanup()
super().__del__()
Loading