Integrating an Existing Gym Environment

Maze supports a seamless integration of existing OpenAI Gym environments. To get full Maze feature support for Gym environments we first have to transform them into Maze environments. This page shows how this is easily accomplished via the GymMazeEnv.

../_images/gym_env_wrapper.png

A Gym environment is transformed into a GymMazeEnv by:

  • Wrapping the Gym environment into a GymCoreEnv.

  • This requires transforming the observation and action spaces into a dictionary spaces via the GymObservationConversion and GymActionConversion interfaces.

  • Finally, the GymCoreEnv is packed into a GymMazeEnv which is fully compatible with all other Maze components and modules.

To get a better understanding of the overall structure please refer to the Maze environment hierarchy.

Instantiating a Gym Environment as a Maze Environment

The config snippet below shows how to instantiate an existing, already registered Gym environment as a GymMazeEnv referenced by its environment name (here CartPole-v0).

# @package env
type: maze.core.wrappers.maze_gym_env_wrapper.make_gym_maze_env
name: "CartPole-v0"

To achieve the same result directly with plain Python you can start with the code snippet below.

from maze.core.wrappers.maze_gym_env_wrapper import GymMazeEnv
env = GymMazeEnv(env="CartPole-v0")

In case your environment is not yet registered with Gym you can also directly instantiate the Gym environment before passing it to the GymMazeEnv. This might be useful in case you already have your own custom Gym environments implemented.

import gym
from maze.core.wrappers.maze_gym_env_wrapper import GymMazeEnv
gym_env = gym.make("CartPole-v0")
env = GymMazeEnv(env=gym_env)

Where to Go Next