Skip to content

Commit

Permalink
Add experimental Wrapper (openai#276)
Browse files Browse the repository at this point in the history
  • Loading branch information
gdb authored Aug 11, 2016
1 parent 5a990b8 commit e305f95
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 6 deletions.
4 changes: 2 additions & 2 deletions gym/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ def sanity_check_dependencies():

sanity_check_dependencies()

from gym.core import Env, Space
from gym.core import Env, Space, Wrapper
from gym.envs import make, spec
from gym.scoreboard.api import upload

__all__ = ["Env", "Space", "make", "spec", "upload"]
__all__ = ["Env", "Space", "Wrapper", "make", "spec", "upload"]
76 changes: 72 additions & 4 deletions gym/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
logger = logging.getLogger(__name__)

import numpy as np
import weakref

from gym import error, monitoring
from gym.utils import closer, reraise
Expand Down Expand Up @@ -52,6 +53,7 @@ def __new__(cls, *args, **kwargs):
env._env_closer_id = env_closer.register(env)
env._closed = False
env._configured = False
env._unwrapped = None

# Will be automatically set when creating an environment via 'make'
env.spec = None
Expand Down Expand Up @@ -238,6 +240,42 @@ def configure(self, *args, **kwargs):
else:
raise

def build(self):
"""[EXPERIMENTAL: may be removed in a later version of Gym] Builds an
environment by applying any provided wrappers, with the
outmost wrapper supplied first. This method is automatically
invoked by 'gym.make', and should be manually invoked if
instantiating an environment by hand.
Notes:
The default implementation will wrap the environment in the
list of wrappers provided in self.metadata['wrappers'], in reverse
order. So for example, given:
class FooEnv(gym.Env):
metadata = {
'wrappers': [Wrapper1, Wrapper2]
}
Calling 'env.build' will return 'Wrapper1(Wrapper2(env))'.
Returns:
gym.Env: A potentially wrapped environment instance.
"""
wrapped = self
for wrapper in reversed(self.metadata.get('wrappers', [])):
wrapped = wrapper(wrapped)
return wrapped

@property
def unwrapped(self):
"""Avoid refcycles by making this into a property."""
if self._unwrapped is not None:
return self._unwrapped
else:
return self

def __del__(self):
self.close()

Expand All @@ -247,10 +285,9 @@ def __str__(self):
# Space-related abstractions

class Space(object):
"""
Provides a classification state spaces and action spaces,
so you can write generic code that applies to any Environment.
E.g. to choose a random action.
"""Defines the observation and action spaces, so you can write generic
code that applies to any Env. For example, you can choose a random
action.
"""

def sample(self, seed=0):
Expand All @@ -275,3 +312,34 @@ def from_jsonable(self, sample_n):
"""Convert a JSONable data type to a batch of samples from this space."""
# By default, assume identity is JSONable
return sample_n

class Wrapper(Env):
def __init__(self, env):
self.env = env
self.metadata = env.metadata
self.action_space = env.action_space
self.observation_space = env.observation_space
self.reward_range = env.reward_range
self.spec = env.spec
self._unwrapped = env.unwrapped

def _step(self, action):
return self.env.step(action)

def _reset(self):
return self.env.reset()

def _render(self, mode='human', close=False):
return self.env.render(mode, close)

def _close(self):
return self.env.close()

def _configure(self, *args, **kwargs):
return self.env.configure(*args, **kwargs)

def _seed(self, seed=None):
return self.env.seed(seed)

def __str__(self):
return '<{}{} instance>'.format(type(self).__name__, self.env)
2 changes: 2 additions & 0 deletions gym/envs/registration.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ def make(self):

# Make the enviroment aware of which spec it came from.
env.spec = self
env = env.build()

return env

def __repr__(self):
Expand Down

0 comments on commit e305f95

Please sign in to comment.