Example #1
0
    def __init__(self, maze_file=None, maze_size=None, has_loops=False):

        self.viewer = None

        if maze_file:
            self.maze_view = MazeView2D(maze_name="OpenAI Gym - Maze (%s)" % maze_file,
                                        maze_file_path=maze_file,
                                        screen_size=(640, 640))
        elif maze_size:
            self.maze_view = MazeView2D(maze_name="OpenAI Gym - Maze (%d x %d)" % maze_size,
                                        maze_size=maze_size, screen_size=(640, 640),
                                        has_loops=has_loops)
        else:
            raise AttributeError("One must supply either a maze_file path (str) or the maze_size (tuple of length 2)")

        self.maze_size = self.maze_view.maze_size

        # forward or backward in each dimension
        self.action_space = spaces.Discrete(2*len(self.maze_size))

        # observation is the x, y coordinate of the grid
        low = np.zeros(len(self.maze_size), dtype=int)
        high =  np.array(self.maze_size, dtype=int) - np.ones(len(self.maze_size), dtype=int)
        self.observation_space = spaces.Box(low, high)

        # initial condition
        self.observation = None
        self.steps_beyond_done = None

        # Simulation related variables.
        self._seed()
        self._reset()

        # Just need to initialize the relevant attributes
        self._configure()
Example #2
0
    def __init__(self,
                 maze_file=None,
                 maze_size=None,
                 mode=None,
                 enable_render=True,
                 zero_step_rewards=False):

        self.viewer = None
        self.enable_render = enable_render
        self.zero_step_rewards = zero_step_rewards

        if maze_file:
            self.maze_view = MazeView2D(maze_name="OpenAI Gym - Maze (%s)" %
                                        maze_file,
                                        maze_file_path=maze_file,
                                        screen_size=(640, 640),
                                        enable_render=enable_render)
        elif maze_size:
            if mode == "plus":
                has_loops = True
                num_portals = int(round(min(maze_size) / 3))
            else:
                has_loops = False
                num_portals = 0

            self.maze_view = MazeView2D(
                maze_name="OpenAI Gym - Maze (%d x %d)" % maze_size,
                maze_size=maze_size,
                screen_size=(640, 640),
                has_loops=has_loops,
                num_portals=num_portals,
                enable_render=enable_render)
        else:
            raise AttributeError(
                "One must supply either a maze_file path (str) or the maze_size (tuple of length 2)"
            )

        self.maze_size = self.maze_view.maze_size

        # forward or backward in each dimension
        self.action_space = spaces.Discrete(2 * len(self.maze_size))

        # observation is the x, y coordinate of the grid
        low = np.zeros(len(self.maze_size), dtype=int)
        high = np.array(self.maze_size, dtype=int) - np.ones(
            len(self.maze_size), dtype=int)
        self.observation_space = spaces.Box(low, high, dtype=np.int64)

        # initial condition
        self.state = None
        self.steps_beyond_done = None

        # Simulation related variables.
        self.seed()
        self.reset()

        # Just need to initialize the relevant attributes
        self.configure()
Example #3
0
    def __init__(self, maze_file=None, maze_size=None, mode=None):

        self.viewer = None

        if maze_file:
            self.maze_view = MazeView2D(maze_name="OpenAI Gym - Maze (%s)" %
                                        maze_file,
                                        maze_file_path=maze_file,
                                        screen_size=(640, 640))
        elif maze_size:
            if mode == "plus":
                has_loops = True
                num_portals = int(round(min(maze_size) / 3))
            else:
                has_loops = False
                num_portals = 0

            self.maze_view = MazeView2D(
                maze_name="OpenAI Gym - Maze (%d x %d)" % maze_size,
                maze_size=maze_size,
                screen_size=(640, 640),
                has_loops=has_loops,
                num_portals=num_portals)
        else:
            raise AttributeError(
                "One must supply either a maze_file path (str) or the maze_size (tuple of length 2)"
            )

        self.maze_size = self.maze_view.maze_size  #10x10

        # forward or backward in each dimension
        self.action_space = spaces.Discrete(
            2 * len(self.maze_size)
        )  #2*2 {0, 1, 2, 3} 链接:http://gym.openai.com/docs/#spaces

        # observation is the x, y coordinate of the grid
        low = np.zeros(len(self.maze_size), dtype=int)  #2 (0,0)
        high = np.array(self.maze_size, dtype=int) - np.ones(
            len(self.maze_size), dtype=int)  #[10,10]-[1,1]=[9,9]
        self.observation_space = spaces.Box(low, high)

        # initial condition
        self.state = None
        self.steps_beyond_done = None

        # Simulation related variables.
        self._seed()
        self.reset()

        # Just need to initialize the relevant attributes
        self._configure()
Example #4
0
    def __init__(self, screen_size: Tuple[int, int] = (500, 500),
                 maze_file_path: str = None, maze_size: Tuple[int, int] = (10, 10)):
        """Initializes maze environment.

        Args:
            screen_size (tuple): screen size
            maze_size (tuple): maze size
            maze_file_path (str): maze file path
        """
        self.maze_size: Tuple[int, int] = maze_size

        # Creates a new maze view for the environment
        self.maze_view: MazeView2D = MazeView2D(caption="OpenAI Gym - Maze (%d x %d)" % maze_size,
                                                screen_size=screen_size, maze_size=maze_size,
                                                maze_file_path=maze_file_path)

        # Defines action space
        # They must be gym.spaces objects
        self.action_space: spaces = spaces.Discrete(2 * len(self.maze_size))

        # Defines observation space
        # The observation will be the coordinate of the agent
        low: np.ndarray = np.zeros(len(self.maze_size), dtype=int)
        high: np.ndarray = np.array(self.maze_size, dtype=int) - np.ones(len(self.maze_size), dtype=int)
        self.observation_space: spaces.Box = spaces.Box(low, high, dtype=np.int64)
import os
from gym_maze.envs.maze_view_2d import Maze, MazeView2D
import numpy as np

if __name__ == "__main__":

    # check if the folder "maze_samples" exists in the current working directory
    dir_name = os.path.join(os.getcwd(), "maze_samples", "very_big_mazes")
    if not os.path.exists(dir_name):
        os.mkdir(dir_name)

    np.random.seed(10)
    for i in range(10):
        maze_name = "{}.npy".format(i)
        maze_path = os.path.join(dir_name, maze_name)

        mazeview = MazeView2D(maze_size=(40, 40),
                              screen_size=(600, 600),
                              has_loops=True,
                              num_portals=3)
        mazeview.maze.save_maze(maze_path)
        print("New maze generated and saved at %s." % maze_path)

    maze_path = os.path.join(dir_name, '8.npy')

    mazeview = MazeView2D(maze_file_path=maze_path)
    mazeview.update()
    import time
    time.sleep(20)
    print("New maze generated and saved at %s." % maze_path)
# note: might also need to change setup.py