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

Optional gym wrapper #1007

Merged
merged 43 commits into from
Aug 7, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
9b97bf0
Add initial gym wrapper
awjuliani Jun 25, 2018
ab8779a
Default to visual observations when present
awjuliani Jun 26, 2018
22dd085
Rename class
awjuliani Jun 26, 2018
b657115
Rename to unity_gym
awjuliani Jun 26, 2018
48c505f
Restructure files
awjuliani Jun 28, 2018
ede6f66
Merge branch 'develop' into develop-gym
awjuliani Jul 24, 2018
b90d7f3
Merge remote-tracking branch 'origin/develop' into develop-gym
awjuliani Jul 24, 2018
bcd8d99
Fix line length
awjuliani Jul 24, 2018
ca60778
Rename and add documentation
awjuliani Jul 25, 2018
91bde1b
New notebook
awjuliani Jul 26, 2018
24635f7
Additional documentation improvements
awjuliani Jul 26, 2018
155b166
Update documentation
awjuliani Jul 26, 2018
7c05c1f
Remove unnecessary docstrings
awjuliani Jul 26, 2018
5072e36
Fix single agent bug
awjuliani Jul 26, 2018
814007b
Merge remote-tracking branch 'origin/develop' into develop-gym
awjuliani Jul 26, 2018
5a7d095
Re-organize folders
awjuliani Jul 26, 2018
3d72cc6
Merge remote-tracking branch 'origin/develop' into develop-gym
awjuliani Jul 26, 2018
d984c30
Clean notebook
awjuliani Jul 26, 2018
97fdb16
Update documentation
awjuliani Jul 26, 2018
e7337e4
Remove /
awjuliani Jul 26, 2018
167383f
Fix link reference
awjuliani Jul 26, 2018
da32896
Update documentation
awjuliani Jul 26, 2018
37ca201
Refer to it as a wrapper
awjuliani Jul 26, 2018
2a4b582
Only allow one agent in single agent env
awjuliani Jul 27, 2018
dea3048
Allow only one agent in single agent env
awjuliani Jul 27, 2018
97bcc0c
Remove notebook checkpoint
awjuliani Jul 27, 2018
54c3dbe
Use find_packages() in setup
awjuliani Jul 28, 2018
64cb610
Merge into single wrapper
awjuliani Jul 30, 2018
7133d54
Update getting started notebook
awjuliani Jul 30, 2018
0a61ef8
Ignore checkpoints
awjuliani Jul 30, 2018
1dbe32e
Code cleanup
awjuliani Jul 31, 2018
1a988c5
Add initial tests
awjuliani Jul 31, 2018
b1658a2
Ignore pytest cache
awjuliani Jul 31, 2018
47d49f8
Merge branch 'develop' into develop-gym
awjuliani Jul 31, 2018
0746c68
Add documentation on using baselines
awjuliani Jul 31, 2018
c28ced2
Typo and formatting
awjuliani Jul 31, 2018
61457b6
Replace references to Basics.ipynb
awjuliani Jul 31, 2018
e8e6190
Address comments
awjuliani Aug 6, 2018
c588198
Merge remote-tracking branch 'origin/develop' into develop-gym
awjuliani Aug 7, 2018
9994d70
Fix pip install name
awjuliani Aug 7, 2018
5446462
Fix for multi-discrete
awjuliani Aug 7, 2018
5aecf66
Fix for continuous control
awjuliani Aug 7, 2018
e0e3218
Change use_visual functionality
awjuliani Aug 7, 2018
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
21 changes: 13 additions & 8 deletions python/gym/README.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
# ML-Agents Gym Wrapper
# Unity ML-Agents Gym Wrapper

## `gym_wrapper.py`
First draft on a gym wrapper for ML-Agents. To launch an environmnent do :
A common way in which researchers interact with simulation environments is via wrapper provided by OpenAI called `gym`. Here we provide a gym wrapper, and instructions for using it with existing research projects which utilize gyms.

## `unity_gym.py`
First draft on a gym wrapper for ML-Agents. To launch an environmnent use :
Copy link
Contributor

Choose a reason for hiding this comment

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

s/enviromnent/environment


```python
raw_env = UnityEnvironment(<env-name>)
env = GymWrapper(raw_env)
env = GymWrapper(environment_filename, worker_id, default_visual)
```

The environment `env` will behave like a gym.
* `environment_filename` refers to the path to the Unity environment.
* `worker_id` refers to the port to use for communication with the environment.
* `default_visual` refers to whether to use visual observations (True) or vector observations (False) as the default observation provided by the `reset` and `step` functions.

The resulting environment `env` will behave like a gym.

__Limitations :__

* Only works with environments containing one external brain
* Only works with environments containing one agent
* Only first agent in first external brain will be exposed via API.
* By default the first visual observation is provided as the `observation`, if present. Otherwise vector observations are provided.
32 changes: 21 additions & 11 deletions python/gym/gym_wrapper.py → python/gym/unity_gym.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,21 @@ def multi_agent_check(info):


class UnityEnv(gym.Env):
def __init__(self, unity_environment: UnityEnvironment):
def __init__(self, environment_filename: str, worker_id = 0, default_visual = True):
"""
Environment initialization
:param unity_environment: The UnityEnvironment to be wrapped for gym
"""
self.env = unity_environment
self.name = self.env.academy_name
self._env = UnityEnvironment(environment_filename, worker_id)
self.name = self._env.academy_name
self.visual_obs = None
if len(self.env.brains) != 1:
if len(self._env.brains) != 1:
raise UnityGymWrapperException(
"There can only be one brain in a UnityEnvironment "
"if it is wrapped in a gym.")
self.brain_name = self.env.external_brain_names[0]
brain = self.env.brains[self.brain_name]
self.use_visual = brain.number_visual_observations == 1
self.brain_name = self._env.external_brain_names[0]
brain = self._env.brains[self.brain_name]
self.use_visual = brain.number_visual_observations == 1 and default_visual
if brain.num_stacked_vector_observations != 1:
raise UnityGymWrapperException(
"There can only be one stacked vector observation in a UnityEnvironment "
Expand All @@ -44,7 +44,17 @@ def __init__(self, unity_environment: UnityEnvironment):
high = np.array([1] * brain.vector_action_space_size)
self._action_space = spaces.Box(-high, high, dtype=np.float32)
high = np.array([np.inf] * brain.vector_observation_space_size)
self._observation_space = spaces.Box(-high, high, dtype=np.float32)
if self.use_visual:
if brain.camera_resolutions[0]["blackAndWhite"]:
depth = 1
else:
depth = 3
self._observation_space = spaces.Box(0, 1, dtype=np.float32,
shape=(brain.camera_resolutions[0]["height"],
brain.camera_resolutions[0]["width"],
depth))
else:
self._observation_space = spaces.Box(-high, high, dtype=np.float32)

def step(self, action):
"""Run one timestep of the environment's dynamics. When end of
Expand All @@ -59,7 +69,7 @@ def step(self, action):
done (boolean): whether the episode has ended.
info (dict): contains auxiliary diagnostic information (helpful for debugging, and sometimes learning)
"""
info = self.env.step(action)[self.brain_name]
info = self._env.step(action)[self.brain_name]
multi_agent_check(info)
if self.use_visual:
self.visual_obs = info.visual_observations[0][0, :, :, :]
Expand All @@ -74,7 +84,7 @@ def reset(self):
Returns: observation (object): the initial observation of the
space.
"""
info = self.env.reset()[self.brain_name]
info = self._env.reset()[self.brain_name]
multi_agent_check(info)
if self.use_visual:
self.visual_obs = info.visual_observations[0][0, :, :, :]
Expand Down Expand Up @@ -120,7 +130,7 @@ def close(self):
Environments will automatically close() themselves when
garbage collected or when the program exits.
"""
self.env.close()
self._env.close()

def seed(self, seed=None):
"""Sets the seed for this env's random number generator(s).
Expand Down