Skip to content

Commit

Permalink
Tuple Observations (#2)
Browse files Browse the repository at this point in the history
* typo

* change version to 0.1.0.dev0

* general requirements

* version 0.1.1

* tuple obs wrapper

* optimize tests with no resets

* test tuple obs

* must reset with wrappers

* wrong gym id

* format
  • Loading branch information
juliusfrost authored Jul 26, 2021
1 parent 6b96b2e commit 5ae7102
Show file tree
Hide file tree
Showing 7 changed files with 120 additions and 83 deletions.
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# minerl-wrappers

`minerl-wrapper` compiles common wrappers and standardizes code for reproducibility in the [MineRL environment](https://minerl.readthedocs.io/en/latest/index.html)!
`minerl-wrappers` compiles common wrappers and standardizes code for reproducibility in the [MineRL environment](https://minerl.readthedocs.io/en/latest/index.html)!

# Currently Supported Environments
- MineRL Basic Environments
Expand Down Expand Up @@ -62,10 +62,17 @@ DEFAULT_CONFIG = {
"randomize_action": False,
"eval_epsilon": 0.001,
"action_choices": None,
"include_vec_obs": False,
"tuple_obs_space": False,
},
}
```

# Requirements
- Java JDK 8
- Python 3.7+
- `minerl==0.4.0`

# Install

Make sure you have Java JDK 8 installed as the only Java version for MineRL.
Expand Down
2 changes: 2 additions & 0 deletions minerl_wrappers/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
"randomize_action": False,
"eval_epsilon": 0.001,
"action_choices": None,
"include_vec_obs": False,
"tuple_obs_space": False,
},
}

Expand Down
18 changes: 17 additions & 1 deletion minerl_wrappers/pfrl_2020_wrappers.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ def wrap_env(
randomize_action,
eval_epsilon,
action_choices,
include_vec_obs=False,
tuple_obs_space=False,
):
# wrap env: time limit...
# Don't use `ContinuingTimeLimit` for testing, in order to avoid unexpected behavior on submissions.
Expand All @@ -59,7 +61,10 @@ def wrap_env(
env = FrameSkip(env, skip=frame_skip)
if gray_scale:
env = GrayScaleWrapper(env, dict_space_key="pov")
env = ObtainPoVWrapper(env)
if not include_vec_obs:
env = ObtainPoVWrapper(env)
elif tuple_obs_space:
env = TupleObsWrapper(env)
env = MoveAxisWrapper(
env, source=-1, destination=0
) # convert hwc -> chw as Pytorch requires.
Expand Down Expand Up @@ -165,6 +170,17 @@ def observation(self, observation):
return observation["pov"]


class TupleObsWrapper(gym.ObservationWrapper):
def __init__(self, env):
super().__init__(env)
self.observation_space = gym.spaces.Tuple(
(self.observation_space["pov"], self.observation_space["vector"])
)

def observation(self, observation):
return observation["pov"], observation["vector"]


class UnifiedObservationWrapper(gym.ObservationWrapper):
"""Take 'pov', 'compassAngle', 'inventory' and concatenate with scaling.
Each element of 'inventory' is converted to a square whose side length is region_size.
Expand Down
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[tool.poetry]
name = "minerl-wrappers"
version = "0.1.0"
description = "minerl-wrapper compiles common wrappers and standardizes code for reproducibility in the MineRL environment!"
version = "0.1.1"
description = "minerl-wrappers compiles common wrappers and standardizes code for reproducibility in the MineRL environment!"
authors = ["Julius Frost"]

[tool.poetry.dependencies]
Expand Down
14 changes: 14 additions & 0 deletions tests/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,17 @@ def build_and_run_step(gym_id, config_file=None, **kwargs):
reset_and_sample_episode(env, 1)
logging.debug("Closing MineRL environment...")
env.close()


def build_and_run_list_config(gym_id, list_config: list, max_steps=1):
env = gym.make(gym_id)
for config in list_config:
logging.debug(f"testing config: {config}")
wrapped_env = wrap(env, **config)
wrapped_env.reset()
for step in range(max_steps):
action = wrapped_env.action_space.sample()
_, _, done, _ = wrapped_env.step(action)
if done:
break
env.close()
61 changes: 27 additions & 34 deletions tests/test_pfrl_2019_wrappers.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,51 +5,43 @@
import gym

from minerl_wrappers import wrap
from tests.common import reset_and_sample_episode
from tests.common import build_and_run_list_config

logging.basicConfig(level=logging.DEBUG)


def test_pfrl_2019_wrappers():
env = gym.make("MineRLObtainDiamondDense-v0")
config_validation(env)
logging.debug("Testing default wrapper")
wrapped_env = wrap(env)
reset_and_sample_episode(wrapped_env, 4)
logging.debug("Testing frame_skip and frame_stack")
config = {
"pfrl_2019": True,
"pfrl_2019_config": {
"frame_skip": 4,
"frame_stack": 4,
gym_id = "MineRLObtainDiamondDense-v0"
config_validation(gym_id)
config_list = [
{},
{
"pfrl_2019": True,
"pfrl_2019_config": {
"frame_skip": 4,
"frame_stack": 4,
},
},
}
wrapped_env = wrap(env, **config)
reset_and_sample_episode(wrapped_env, 4)
logging.debug("Testing gray_scale")
config = {
"pfrl_2019": True,
"pfrl_2019_config": {
"gray_scale": True,
{
"pfrl_2019": True,
"pfrl_2019_config": {
"gray_scale": True,
},
},
}
wrapped_env = wrap(env, **config)
reset_and_sample_episode(wrapped_env, 4)
logging.debug("Testing random_action")
config = {
"pfrl_2019": True,
"pfrl_2019_config": {
"random_action": True,
"eval_epsilon": 1,
{
"pfrl_2019": True,
"pfrl_2019_config": {
"random_action": True,
"eval_epsilon": 1,
},
},
}
wrapped_env = wrap(env, **config)
reset_and_sample_episode(wrapped_env, 4)
env.close()
]
build_and_run_list_config(gym_id, config_list, 4)


def config_validation(env):
def config_validation(gym_id):
logging.debug("Config validation tests...")
env = gym.make(gym_id)
config = {}
wrap(env, **config)
config = {
Expand All @@ -62,3 +54,4 @@ def config_validation(env):
Path(__file__).absolute().parent.joinpath("./configs/pfrl_2019_basic.yaml")
)
wrap(env, config_file=config_file)
env.close()
95 changes: 50 additions & 45 deletions tests/test_pfrl_2020_wrappers.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,64 +6,68 @@

from minerl_wrappers import wrap
from minerl_wrappers.utils import load_means
from tests.common import reset_and_sample_episode
from tests.common import build_and_run_list_config

logging.basicConfig(level=logging.DEBUG)


def test_pfrl_2020_wrappers():
env = gym.make("MineRLObtainDiamondDenseVectorObf-v0")
config_validation(env)
gym_id = "MineRLObtainDiamondDenseVectorObf-v0"
config_validation(gym_id)
means = load_means()
logging.debug("Testing default wrapper")
wrapped_env = wrap(env)
reset_and_sample_episode(wrapped_env, 4)
logging.debug("Testing preset action choices")
config = {
"pfrl_2020": True,
"pfrl_2020_config": {
"action_choices": means,
config_list = [
{},
{
"pfrl_2020": True,
"pfrl_2020_config": {
"action_choices": means,
},
},
}
wrapped_env = wrap(env, **config)
reset_and_sample_episode(wrapped_env, 4)
logging.debug("Testing frame_skip and frame_stack")
config = {
"pfrl_2020": True,
"pfrl_2020_config": {
"action_choices": means,
"frame_skip": 4,
"frame_stack": 4,
{
"pfrl_2020": True,
"pfrl_2020_config": {
"action_choices": means,
"frame_skip": 4,
"frame_stack": 4,
},
},
}
wrapped_env = wrap(env, **config)
reset_and_sample_episode(wrapped_env, 4)
logging.debug("Testing gray_scale")
config = {
"pfrl_2020": True,
"pfrl_2020_config": {
"action_choices": means,
"gray_scale": True,
{
"pfrl_2020": True,
"pfrl_2020_config": {
"action_choices": means,
"gray_scale": True,
},
},
}
wrapped_env = wrap(env, **config)
reset_and_sample_episode(wrapped_env, 4)
logging.debug("Testing random_action")
config = {
"pfrl_2020": True,
"pfrl_2020_config": {
"action_choices": means,
"random_action": True,
"eval_epsilon": 1,
{
"pfrl_2020": True,
"pfrl_2020_config": {
"action_choices": means,
"random_action": True,
"eval_epsilon": 1,
},
},
}
wrapped_env = wrap(env, **config)
reset_and_sample_episode(wrapped_env, 4)
env.close()
{
"pfrl_2020": True,
"pfrl_2020_config": {
"action_choices": means,
"include_vec_obs": True,
},
},
{
"pfrl_2020": True,
"pfrl_2020_config": {
"action_choices": means,
"include_vec_obs": True,
"tuple_obs_space": True,
},
},
]
build_and_run_list_config(gym_id, config_list, 4)


def config_validation(env):
def config_validation(gym_id):
logging.debug("Config validation tests...")
env = gym.make(gym_id)
config = {}
wrap(env, **config)
config = {
Expand All @@ -85,3 +89,4 @@ def config_validation(env):
Path(__file__).absolute().parent.joinpath("./configs/pfrl_2020_basic.yaml")
)
wrap(env, config_file=config_file)
env.close()

0 comments on commit 5ae7102

Please sign in to comment.