forked from facebookresearch/habitat-lab
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[WIP] use gym Env in VectorEnv (facebookresearch#892)
* [WIP] use gym Env in VectorEnv * make it working for cartpole * making cartpole work * fixing pikle5 issue * fixing one of the tests * fixing filename typo * changing a test * removing the FIX INFO DICT option * removing debug print statement I forgot to remove * removing dead code * using a generator instead of list comprehension * removing redundant .cpu() call * flatten the dict * changing action space to space.Discrete * adding some training tests * adding GYM.CLASS_NAME to more configs * deleting ENV_NAME * fixing tests * using Rearrange in Test * using the dataset better * deleting last_obs * removing a test to check cuda error * run the baseline training tests separately * separating the tests more explicitely * removing the fuse keys that correspond to sensors * better data_profile path Co-authored-by: vincentpierre <[email protected]>
- Loading branch information
1 parent
5b74829
commit 710beab
Showing
63 changed files
with
564 additions
and
773 deletions.
There are no files selected for viewing
This file contains 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 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 |
---|---|---|
@@ -1,3 +1,5 @@ | ||
GYM: | ||
CLASS_NAME: NavRLEnv | ||
ENVIRONMENT: | ||
MAX_EPISODE_STEPS: 1000 | ||
|
||
|
This file contains 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 |
---|---|---|
@@ -1,3 +1,5 @@ | ||
GYM: | ||
CLASS_NAME: NavRLEnv | ||
ENVIRONMENT: | ||
MAX_EPISODE_STEPS: 1000 | ||
ITERATOR_OPTIONS: | ||
|
This file contains 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 |
---|---|---|
@@ -1,3 +1,5 @@ | ||
GYM: | ||
CLASS_NAME: NavRLEnv | ||
ENVIRONMENT: | ||
MAX_EPISODE_STEPS: 500 | ||
|
||
|
This file contains 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 |
---|---|---|
@@ -1,3 +1,5 @@ | ||
GYM: | ||
CLASS_NAME: NavRLEnv | ||
ENVIRONMENT: | ||
MAX_EPISODE_STEPS: 500 | ||
|
||
|
This file contains 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 |
---|---|---|
@@ -1,3 +1,5 @@ | ||
GYM: | ||
CLASS_NAME: NavRLEnv | ||
ENVIRONMENT: | ||
MAX_EPISODE_STEPS: 500 | ||
SIMULATOR: | ||
|
This file contains 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 |
---|---|---|
@@ -1,3 +1,5 @@ | ||
GYM: | ||
CLASS_NAME: NavRLEnv | ||
ENVIRONMENT: | ||
MAX_EPISODE_STEPS: 500 | ||
SIMULATOR: | ||
|
This file contains 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 |
---|---|---|
@@ -1,3 +1,5 @@ | ||
GYM: | ||
CLASS_NAME: NavRLEnv | ||
ENVIRONMENT: | ||
MAX_EPISODE_STEPS: 500 | ||
SIMULATOR: | ||
|
This file contains 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 |
---|---|---|
@@ -1,3 +1,5 @@ | ||
GYM: | ||
CLASS_NAME: NavRLEnv | ||
ENVIRONMENT: | ||
MAX_EPISODE_STEPS: 500 | ||
SIMULATOR: | ||
|
This file contains 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 |
---|---|---|
@@ -1,3 +1,5 @@ | ||
GYM: | ||
CLASS_NAME: NavRLEnv | ||
ENVIRONMENT: | ||
MAX_EPISODE_STEPS: 500 | ||
SIMULATOR: | ||
|
This file contains 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 |
---|---|---|
@@ -1,3 +1,5 @@ | ||
GYM: | ||
CLASS_NAME: RearrangeRLEnv | ||
ENVIRONMENT: | ||
MAX_EPISODE_STEPS: 10 | ||
ITERATOR_OPTIONS: | ||
|
This file contains 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 |
---|---|---|
@@ -1,3 +1,5 @@ | ||
GYM: | ||
CLASS_NAME: NavRLEnv | ||
ENVIRONMENT: | ||
MAX_EPISODE_STEPS: 500 | ||
SIMULATOR: | ||
|
This file contains 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 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,50 @@ | ||
from typing import Tuple, Union | ||
|
||
from gym import Env, Wrapper, spaces | ||
from gym.core import ActType, ObsType | ||
|
||
from habitat.core.dataset import Episode | ||
|
||
|
||
class EnvCountEpisodeWrapper(Wrapper): | ||
OBSERVATION_KEY = "obs" | ||
observation_space: spaces.Space | ||
|
||
def __init__(self, env: Env): | ||
""" | ||
A helper wrapper to count the number of episodes available | ||
""" | ||
super().__init__(env) | ||
self._has_number_episode = hasattr(env, "number_of_episodes") | ||
self._current_episode = 0 | ||
|
||
@property | ||
def number_of_episodes(self): | ||
if self._has_number_episode: | ||
return self.env.number_of_episodes | ||
else: | ||
return -1 | ||
|
||
@property | ||
def current_episode(self) -> Episode: | ||
if self._has_number_episode: | ||
return self.env.current_episode | ||
else: | ||
return Episode( | ||
episode_id=str(self._current_episode), | ||
scene_id="default", | ||
start_position=[], | ||
start_rotation=[], | ||
) | ||
|
||
def step(self, action: ActType) -> Tuple[ObsType, float, bool, dict]: | ||
"""Steps through the environment with action.""" | ||
o, r, done, i = self.env.step(action) | ||
if done: | ||
self._current_episode += 1 | ||
return o, r, done, i | ||
|
||
def reset(self, **kwargs) -> Union[ObsType, Tuple[ObsType, dict]]: | ||
"""Resets the environment with kwargs.""" | ||
self._current_episode += 1 | ||
return self.env.reset(**kwargs) |
This file contains 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,39 @@ | ||
from typing import Tuple, Union | ||
|
||
from gym import Env, Wrapper, spaces | ||
from gym.core import ActType, ObsType | ||
|
||
|
||
class EnvObsDictWrapper(Wrapper): | ||
OBSERVATION_KEY = "obs" | ||
observation_space: spaces.Space | ||
|
||
def __init__(self, env: Env): | ||
""" | ||
Wraps a VectorEnv environment and makes sure its obervation space is a | ||
Dictionary (If it is a Box, it will be wrapped into a dictionary) | ||
""" | ||
super().__init__(env) | ||
self._requires_dict = False | ||
if isinstance(self.observation_space, spaces.Box): | ||
self._requires_dict = True | ||
self.observation_space = spaces.Dict( | ||
{self.OBSERVATION_KEY: self.observation_space} | ||
) | ||
|
||
def step(self, action: ActType) -> Tuple[ObsType, float, bool, dict]: | ||
obs, reward, done, info = self.env.step(action) | ||
if self._requires_dict: | ||
obs = {self.OBSERVATION_KEY: obs} | ||
return obs, reward, done, info | ||
|
||
def reset(self, **kwargs) -> Union[ObsType, Tuple[ObsType, dict]]: | ||
if not self._requires_dict: | ||
return self.env.reset(**kwargs) | ||
reset_output = self.env.reset(**kwargs) | ||
if isinstance(reset_output, tuple): | ||
obs, info = self.env.reset(**kwargs) | ||
return {self.OBSERVATION_KEY: obs}, info | ||
else: | ||
obs = self.env.reset(**kwargs) | ||
return {self.OBSERVATION_KEY: obs} |
This file contains 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
Oops, something went wrong.