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

adding reverse openai gym wrapper and tests #8

Closed

Conversation

alexminnaar
Copy link
Contributor

This PR is related to #6 where I suggested a wrapper that converts Openai gym environments to bsuite environments. This PR contains the wrapper and a test case that converts the Openai CartPole-v0 environment to a bsuite environment.

Copy link
Member

@aslanides aslanides left a comment

Choose a reason for hiding this comment

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

Thanks for the PR! A few suggestions (mostly style nits). Many of these can be resolved automatically if you run pyformat over the code -- see https://pypi.org/project/pyformat/ for instructions.



class ReverseGymWrapper(dm_env.Environment):
"""A wrapper that converts an OpenAI gym environment to a dm_env.Environment"""
Copy link
Member

Choose a reason for hiding this comment

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

Looks like you're using 4 spaces here. Are you able to switch to 2 to be consistent with the rest of the codebase?

bsuite/utils/gym_wrapper.py Show resolved Hide resolved
class ReverseGymWrapper(dm_env.Environment):
"""A wrapper that converts an OpenAI gym environment to a dm_env.Environment"""

def __init__(self, gym_env:gym.Env):
Copy link
Member

Choose a reason for hiding this comment

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

Type annotations need a space after the ':' (ditto elsewhere)

def step(self, action):
"""convert gym step result (observations, reward, done, info) to dm_env TimeStep"""
gym_step_res = self.gym_env.step(action)
_obs = gym_step_res[0]
Copy link
Member

Choose a reason for hiding this comment

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

Why do these have _ prefixes?

Copy link
Member

Choose a reason for hiding this comment

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

Also it's more idiomatic to do something like

obs, reward, done, _ = self.gym_env.step(action)

self.gym_env.reset()

def step(self, action):
"""convert gym step result (observations, reward, done, info) to dm_env TimeStep"""
Copy link
Member

Choose a reason for hiding this comment

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

Can you make your docstrings proper sentences, i.e. 'Convert .... TimeStep.' (ditto elsewhere)


import dm_env
from dm_env import specs
from dm_env import test_utils
import mock
import numpy as np

import gym
Copy link
Member

Choose a reason for hiding this comment

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

Imports should be alphabetical, so this should be after dm_env and before mock.

bsuite/utils/wrappers_test.py Show resolved Hide resolved

# test converted observation spec
self.assertEqual(type(self.gym_test_env.observation_spec()), specs.BoundedArray)
self.assertEqual(self.gym_test_env.observation_spec().shape,(4,))
Copy link
Member

Choose a reason for hiding this comment

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

Can you add a single space after commas?

@@ -59,6 +59,7 @@ def render(self, mode: Text = 'rgb_array') -> Union[np.ndarray, bool]:
if mode == 'human':
if self.viewer is None:
from gym.envs.classic_control import rendering # pylint: disable=g-import-not-at-top

Copy link
Member

Choose a reason for hiding this comment

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

Nit, revert unrelated change.

def action_spec(self):
return self._action_spec

def space2spec(self, space:gym.Space, name:str=None):
Copy link
Member

Choose a reason for hiding this comment

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

Does this need to be a public method?

Copy link
Collaborator

Choose a reason for hiding this comment

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

It should also be a function, rather than a method.

class ReverseGymWrapperTest(absltest.TestCase):

def test_gym_cartpole(self):
self.gym_test_env = gym_wrapper.ReverseGymWrapper(gym.make('CartPole-v0'))
Copy link
Collaborator

Choose a reason for hiding this comment

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

There's no need to assign anything to self here. Can we just have a local gym_env variable?

@alexminnaar
Copy link
Contributor Author

Thanks for the comments @aslanides and @yotam , I should be able to make the corrections in the next day or so.

@alexminnaar
Copy link
Contributor Author

@yotam @aslanides I made some changes based on your suggestions

@yotam
Copy link
Collaborator

yotam commented Sep 5, 2019

Thanks for the changes Alex. I'm going to merge the change internally then push it and close here. Will make a few further edits and formatting changes on the way.

@yotam
Copy link
Collaborator

yotam commented Sep 5, 2019

Merged in af5b3fe

@yotam yotam closed this Sep 5, 2019
@alexminnaar
Copy link
Contributor Author

Thanks @yotam

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants