-
Notifications
You must be signed in to change notification settings - Fork 822
Remote gym env wrapper #522
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
Open
ShuaibinLi
wants to merge
16
commits into
PaddlePaddle:develop
Choose a base branch
from
ShuaibinLi:remote_gym_env
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 7 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
b5a674f
remote gym env wrapper
72bde1e
yapf .py
5898208
modify test
0bf0f07
modify env_utils
c3cdb84
modify env_utils.py
4262f8c
modify test
e2f30e9
parl.connect, del file
7f163ab
try: env._elapsed_steps
5018ec5
try except _elspaed_steps
b859687
obs_space.high
ac93617
float equal
c88dc39
change location of env_test
21bb3c8
port = get_free_tcp_port()
176e077
repush for unit test
830e571
try: import gym
332395f
del logger.error & move test file
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
# Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
import gym | ||
from parl.utils import logger | ||
from parl.remote.remote_decorator import remote_class | ||
from gym.spaces import Box, Discrete | ||
|
||
__all__ = ['RemoteGymEnv'] | ||
|
||
|
||
@remote_class | ||
class RemoteGymEnv(object): | ||
""" | ||
|
||
Example: | ||
.. code-block:: python | ||
import parl | ||
from parl.utils import RemoteEnv | ||
|
||
parl.connect('localhost') | ||
env = RemoteGymEnv(env_name='HalfCheetah-v1') | ||
|
||
Attributes: | ||
env_name: Mujoco gym environment name | ||
|
||
Public Functions: the same as gym (mainly action_space, observation_space, reset, step, seed ...) | ||
|
||
Note: | ||
``RemoteGymEnv`` defines a remote environment wrapper for machines that do not support mujoco, | ||
rical730 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
support both Continuous action space and Discrete action space environments | ||
|
||
""" | ||
|
||
def __init__(self, env_name=None): | ||
assert isinstance(env_name, str) | ||
|
||
class ActionSpace(object): | ||
def __init__(self, | ||
action_space=None, | ||
low=None, | ||
high=None, | ||
shape=None, | ||
n=None): | ||
self.action_space = action_space | ||
self.low = low | ||
self.high = high | ||
self.shape = shape | ||
self.n = n | ||
|
||
def sample(self): | ||
return self.action_space.sample() | ||
|
||
class ObservationSpace(object): | ||
def __init__(self, observation_space, low, high, shape=None): | ||
self.observation_space = observation_space | ||
self.low = low | ||
self.high = high | ||
self.shape = shape | ||
|
||
self.env = gym.make(env_name) | ||
self._max_episode_steps = int(self.env._max_episode_steps) | ||
self._elapsed_steps = int(self.env._elapsed_steps) | ||
|
||
self.observation_space = ObservationSpace( | ||
self.env.observation_space, self.env.observation_space.low, | ||
self.env.observation_space.high, self.env.observation_space.shape) | ||
if isinstance(self.env.action_space, Discrete): | ||
self.action_space = ActionSpace(n=self.env.action_space.n) | ||
elif isinstance(self.env.action_space, Box): | ||
self.action_space = ActionSpace( | ||
self.env.action_space, self.env.action_space.low, | ||
self.env.action_space.high, self.env.action_space.shape) | ||
|
||
def reset(self): | ||
return self.env.reset() | ||
|
||
def step(self, action): | ||
return self.env.step(action) | ||
|
||
def seed(self, seed): | ||
return self.env.seed(seed) | ||
|
||
def render(self): | ||
return logger.warning( | ||
'Can not render in remote environment, render() have been skipped.' | ||
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
# Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
import unittest | ||
import threading | ||
import time | ||
import parl | ||
import numpy as np | ||
from parl.remote.master import Master | ||
from parl.remote.worker import Worker | ||
from parl.remote.client import disconnect | ||
from parl.utils import logger | ||
from env_utils import RemoteGymEnv | ||
import gym | ||
from gym.spaces import Box, Discrete | ||
|
||
|
||
# Test RemoteGymEnv | ||
# for both discrete and continuous action space | ||
class TestRemoteEnv(unittest.TestCase): | ||
def tearDown(self): | ||
disconnect() | ||
|
||
def test_discrete_env_wrapper(self): | ||
logger.info("Running: test discrete_env_wrapper") | ||
master = Master(port=8267) | ||
th = threading.Thread(target=master.run) | ||
th.start() | ||
time.sleep(3) | ||
woker1 = Worker('localhost:8267', 1) | ||
|
||
parl.connect('localhost:8267') | ||
logger.info("Running: test discrete_env_wrapper: 1") | ||
|
||
env = RemoteGymEnv(env_name='MountainCar-v0') | ||
env.seed(1) | ||
env.render() | ||
|
||
obs, done = env.reset(), False | ||
observation_space = env.observation_space | ||
obs_space_high = observation_space.high | ||
obs_space_low = observation_space.low | ||
self.assertEqual(obs_space_high[0], 0.6) | ||
self.assertEqual(obs_space_low[0], -1.2) | ||
|
||
action_space = env.action_space | ||
act_dim = action_space.n | ||
self.assertEqual(act_dim, 3) | ||
|
||
# Run an episode with a random policy | ||
total_steps, episode_reward = 0, 0 | ||
while not done: | ||
action = np.random.choice(act_dim) | ||
next_obs, reward, done, _ = env.step(action) | ||
episode_reward += reward | ||
logger.info('Episode done, total_steps {}, episode_reward {}'.format( | ||
total_steps, episode_reward)) | ||
|
||
master.exit() | ||
woker1.exit() | ||
|
||
def test_continuous_env_wrapper(self): | ||
logger.info("Running: test continuous_env_wrapper") | ||
master = Master(port=8268) | ||
th = threading.Thread(target=master.run) | ||
th.start() | ||
time.sleep(3) | ||
woker1 = Worker('localhost:8268', 1) | ||
|
||
parl.connect('localhost:8268') | ||
logger.info("Running: test continuous_env_wrapper: 1") | ||
|
||
env = RemoteGymEnv(env_name='Pendulum-v0') | ||
env.seed(0) | ||
env.render() | ||
|
||
obs, done = env.reset(), False | ||
observation_space = env.observation_space | ||
obs_space_high = observation_space.high | ||
obs_space_low = observation_space.low | ||
self.assertEqual(obs_space_high[1], 1.) | ||
self.assertEqual(obs_space_low[1], -1.) | ||
|
||
action_space = env.action_space | ||
action_space_high = action_space.high | ||
action_space_low = action_space.low | ||
self.assertEqual(action_space_high, [2.]) | ||
self.assertEqual(action_space_low, [-2.]) | ||
|
||
# Run an episode with a random policy | ||
total_steps, episode_reward = 0, 0 | ||
while not done: | ||
total_steps += 1 | ||
action = env.action_space.sample() | ||
next_obs, reward, done, info = env.step(action) | ||
episode_reward += reward | ||
logger.info('Episode done, total_steps {}, episode_reward {}'.format( | ||
total_steps, episode_reward)) | ||
|
||
master.exit() | ||
woker1.exit() | ||
|
||
|
||
if __name__ == '__main__': | ||
unittest.main() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.