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

[ActionWrapper] support low/high bound is different in different dimensions #673

Open
wants to merge 3 commits into
base: develop
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
8 changes: 5 additions & 3 deletions parl/env/continuous_wrappers.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,17 @@ def __init__(self, env):
[low_bound, high_bound].
"""
gym.Wrapper.__init__(self, env)

assert hasattr(
self.env.action_space,
'low'), 'action space should be instance of gym.spaces.Box'
assert hasattr(
self.env.action_space,
'high'), 'action space should be instance of gym.spaces.Box'
self.low_bound = self.env.action_space.low[0]
self.high_bound = self.env.action_space.high[0]
assert self.high_bound > self.low_bound
self.low_bound = self.env.action_space.low
self.high_bound = self.env.action_space.high
assert np.all(self.high_bound >= self.low_bound)

if hasattr(env, '_max_episode_steps'):
self._max_episode_steps = int(self.env._max_episode_steps)

Expand Down
21 changes: 17 additions & 4 deletions parl/env/tests/continuous_wrappers_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@

class MockEnv(gym.Env):
def __init__(self, low, high):
self.action_space = gym.spaces.Box(low=low, high=high, shape=(3, ))
self.action_space = gym.spaces.Box(
low=np.array(low), high=np.array(high))
self._max_episode_steps = 1000

def step(self, action):
Expand All @@ -34,21 +35,33 @@ class TestActionMappingWrapper(unittest.TestCase):
def test_action_mapping(self):
origin_act = np.array([-1.0, 0.0, 1.0])

env = MockEnv(0.0, 1.0)
env = MockEnv([0.0] * 3, [1.0] * 3)
wrapper_env = ActionMappingWrapper(env)
wrapper_env.step(origin_act)
self.assertListEqual(list(env.action), [0.0, 0.5, 1.0])

env = MockEnv(-2.0, 2.0)
env = MockEnv([-2.0] * 3, [2.0] * 3)
wrapper_env = ActionMappingWrapper(env)
wrapper_env.step(origin_act)
self.assertListEqual(list(env.action), [-2.0, 0.0, 2.0])

env = MockEnv(-5.0, 10.0)
env = MockEnv([-5.0] * 3, [10.0] * 3)
wrapper_env = ActionMappingWrapper(env)
wrapper_env.step(origin_act)
self.assertListEqual(list(env.action), [-5.0, 2.5, 10.0])

# test low bound or high bound is different in different dimensions.
env = MockEnv([0.0, -2.0, -5.0], [1.0, 2.0, 10.0])
wrapper_env = ActionMappingWrapper(env)
wrapper_env.step(origin_act)
self.assertListEqual(list(env.action), [0.0, 0.0, 10.0])

origin_act = np.array([0.0, 0.0, 0.0])
env = MockEnv([0.0, -2.0, -5.0], [1.0, 2.0, 10.0])
wrapper_env = ActionMappingWrapper(env)
wrapper_env.step(origin_act)
self.assertListEqual(list(env.action), [0.5, 0.0, 2.5])


if __name__ == '__main__':
unittest.main()