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

Add VectorizeMode for make_vec #765

Closed
Changes from 1 commit
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
97ac1eb
Remove deprecated features (#609)
pseudo-rnd-thoughts Jul 15, 2023
d4aa544
Merge experimental functional api into root (#610)
pseudo-rnd-thoughts Jul 16, 2023
68a6a25
Merge experimental vector into root (#613)
pseudo-rnd-thoughts Jul 17, 2023
e49cdc8
Merge experimental wrappers to root (#614)
pseudo-rnd-thoughts Jul 18, 2023
0d78c98
[WIP] Merge stateful observation tests to main tests suite (#623)
jjshoots Jul 22, 2023
d932fe9
Update vector docs (#624)
pseudo-rnd-thoughts Jul 24, 2023
b754b5d
Update all of the docs to v1.0.0 (#637)
pseudo-rnd-thoughts Jul 31, 2023
93f6448
ENH: Add NormalizeReward & NormalizeObservation vector wrappers (#632)
younik Aug 2, 2023
f159ab4
Remove `VectorWrapper.__getattr__` (#645)
pseudo-rnd-thoughts Aug 3, 2023
04a8548
Update docs 2 (#649)
pseudo-rnd-thoughts Aug 4, 2023
700ffd0
Automatically generate the list of wrappers using their first line in…
pseudo-rnd-thoughts Aug 5, 2023
a220735
Merge experimental wrappers tests into wrappers tests (#656)
pseudo-rnd-thoughts Aug 7, 2023
02b7d6d
Improve the normalize vector wrapper tests (#659)
pseudo-rnd-thoughts Aug 10, 2023
e98bbad
Reduce the number of CI warnings (#663)
pseudo-rnd-thoughts Aug 10, 2023
937177d
Update version to v1.0.0a1
pseudo-rnd-thoughts Aug 10, 2023
f686481
Add more examples to docs for wrappers (#657)
jjshoots Aug 21, 2023
24cc085
Rename `AsyncVectorEnv` or `SyncVectorEnv` attributes (#678)
pseudo-rnd-thoughts Aug 21, 2023
e3ec4b8
Remove wrapper version numbers (#665)
pseudo-rnd-thoughts Aug 21, 2023
0bc7f1e
Support `gym.make_vec(envs.spec)` (#679)
pseudo-rnd-thoughts Aug 21, 2023
dcd9ab9
Add `VectorEnv.render` function (#666)
pseudo-rnd-thoughts Aug 21, 2023
36a446b
Remove old testing code (#680)
pseudo-rnd-thoughts Aug 22, 2023
ff1128f
Add `Wrapper.set_wrapper_attr` and fix issue 357 (#681)
pseudo-rnd-thoughts Aug 22, 2023
d2efa7c
Change to release candidate from alpha
pseudo-rnd-thoughts Aug 22, 2023
76e8871
Improve the documentation (#688)
pseudo-rnd-thoughts Aug 26, 2023
c48cbd7
Fixed `VectorizeTransformAction` and `VectorizeTransformObservation` …
jjshoots Aug 26, 2023
65c62eb
Remove `Autoreset.spec`, add type hints to `TimeLimit`, correct type …
pseudo-rnd-thoughts Aug 28, 2023
80ef108
Add change logs to wrapper documentation (#696)
pseudo-rnd-thoughts Aug 29, 2023
f7f0363
Add dtype check to `Box.__eq__` and `MultiDiscrete.__eq__` (#707)
pseudo-rnd-thoughts Sep 5, 2023
0313eac
add examples for vector wrappers (#673)
jjshoots Sep 5, 2023
2af443f
Add `make(max_episode_steps=0)` to not apply a `TimeLimit` wrapper (#…
pseudo-rnd-thoughts Sep 8, 2023
ce01fd9
Update `make_vec(vectorization_mode)` to default to `None` and use `v…
pseudo-rnd-thoughts Sep 8, 2023
adcc405
Update `make_vec` to use `make` for `async` and `sync` (#711)
pseudo-rnd-thoughts Sep 8, 2023
16f3a4e
Option to save frames to disk in video recorder (#715)
rk1a Sep 20, 2023
6d492ea
Fix CI problems of #715 (#719)
rk1a Sep 20, 2023
00a62a0
Add VectorizeMode for `make_vec`
pseudo-rnd-thoughts Nov 7, 2023
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
Prev Previous commit
Next Next commit
Remove old testing code (#680)
  • Loading branch information
pseudo-rnd-thoughts authored Aug 22, 2023
commit 36a446bb3285adde6447042e0b257791440156a2
196 changes: 15 additions & 181 deletions tests/test_core.py
Original file line number Diff line number Diff line change
@@ -1,185 +1,19 @@
"""Checks that the core Gymnasium API is implemented as expected."""
from __future__ import annotations

import re
from typing import Any, Dict, Optional, SupportsFloat, Tuple
from typing import Any, SupportsFloat

import numpy as np
import pytest

from gymnasium import Env, ObservationWrapper, RewardWrapper, Wrapper, spaces
from gymnasium.core import (
ActionWrapper,
ActType,
ObsType,
WrapperActType,
WrapperObsType,
)
from gymnasium import ActionWrapper, Env, ObservationWrapper, RewardWrapper, Wrapper
from gymnasium.core import ActType, ObsType, WrapperActType, WrapperObsType
from gymnasium.spaces import Box
from gymnasium.utils import seeding
from gymnasium.wrappers import OrderEnforcing, TimeLimit
from tests.testing_env import GenericTestEnv


# ==== Old testing code


class ArgumentEnv(Env):
"""Testing environment that records the number of times the environment is created."""

observation_space = spaces.Box(low=0, high=1, shape=(1,))
action_space = spaces.Box(low=0, high=1, shape=(1,))
calls = 0

def __init__(self, arg: Any):
"""Constructor."""
self.calls += 1
self.arg = arg


class UnittestEnv(Env):
"""Example testing environment."""

observation_space = spaces.Box(low=0, high=255, shape=(64, 64, 3), dtype=np.uint8)
action_space = spaces.Discrete(3)

def reset(self, *, seed: Optional[int] = None, options: Optional[dict] = None):
"""Resets the environment."""
super().reset(seed=seed)
return self.observation_space.sample(), {"info": "dummy"}

def step(self, action):
"""Steps through the environment."""
observation = self.observation_space.sample() # Dummy observation
return observation, 0.0, False, {}


class UnknownSpacesEnv(Env):
"""This environment defines its observation & action spaces only after the first call to reset.

Although this pattern is sometimes necessary when implementing a new environment (e.g. if it depends
on external resources), it is not encouraged.
"""

def reset(self, *, seed: Optional[int] = None, options: Optional[dict] = None):
"""Resets the environment."""
super().reset(seed=seed)
self.observation_space = spaces.Box(
low=0, high=255, shape=(64, 64, 3), dtype=np.uint8
)
self.action_space = spaces.Discrete(3)
return self.observation_space.sample(), {} # Dummy observation with info

def step(self, action):
"""Steps through the environment."""
observation = self.observation_space.sample() # Dummy observation
return observation, 0.0, False, {}


class OldStyleEnv(Env):
"""This environment doesn't accept any arguments in reset, ideally we want to support this too (for now)."""

def reset(self):
"""Resets the environment."""
super().reset()
return 0

def step(self, action):
"""Steps through the environment."""
return 0, 0, False, {}


class NewPropertyWrapper(Wrapper):
"""Wrapper that tests setting a property."""

def __init__(
self,
env,
observation_space=None,
action_space=None,
reward_range=None,
metadata=None,
):
"""New property wrapper.

Args:
env: The environment to wrap
observation_space: The observation space
action_space: The action space
reward_range: The reward range
metadata: The environment metadata
"""
super().__init__(env)
if observation_space is not None:
# Only set the observation space if not None to test property forwarding
self.observation_space = observation_space
if action_space is not None:
self.action_space = action_space
if reward_range is not None:
self.reward_range = reward_range
if metadata is not None:
self.metadata = metadata


def test_env_instantiation():
"""Tests the environment instantiation using ArgumentEnv."""
# This looks like a pretty trivial, but given our usage of
# __new__, it's worth having.
env = ArgumentEnv("arg")
assert env.arg == "arg"
assert env.calls == 1


properties = [
{
"observation_space": spaces.Box(
low=0.0, high=1.0, shape=(64, 64, 3), dtype=np.float32
)
},
{"action_space": spaces.Discrete(2)},
{"reward_range": (-1.0, 1.0)},
{"metadata": {"render_modes": ["human", "rgb_array_list"]}},
{
"observation_space": spaces.Box(
low=0.0, high=1.0, shape=(64, 64, 3), dtype=np.float32
),
"action_space": spaces.Discrete(2),
},
]


@pytest.mark.parametrize("class_", [UnittestEnv, UnknownSpacesEnv])
@pytest.mark.parametrize("props", properties)
def test_wrapper_property_forwarding(class_, props):
"""Tests wrapper property forwarding."""
env = class_()
env = NewPropertyWrapper(env, **props)

# If UnknownSpacesEnv, then call reset to define the spaces
if isinstance(env.unwrapped, UnknownSpacesEnv):
_ = env.reset()

# Test the properties set by the wrapper
for key, value in props.items():
assert getattr(env, key) == value

# Otherwise, test if the properties are forwarded
all_properties = {"observation_space", "action_space", "metadata"}
for key in all_properties - props.keys():
assert getattr(env, key) == getattr(env.unwrapped, key)


@pytest.mark.skip(reason="Wrappers are actually using the reset kwargs")
def test_compatibility_with_old_style_env():
"""Test compatibility with old style environment."""
env = OldStyleEnv()
env = OrderEnforcing(env)
env = TimeLimit(env, 100)
obs = env.reset()
assert obs == 0


# ==== New testing code


class ExampleEnv(Env):
"""Example testing environment."""

@@ -190,21 +24,21 @@ def __init__(self):

def step(
self, action: ActType
) -> Tuple[ObsType, float, bool, bool, Dict[str, Any]]:
) -> tuple[ObsType, float, bool, bool, dict[str, Any]]:
"""Steps through the environment."""
return 0, 0, False, False, {}

def reset(
self,
*,
seed: Optional[int] = None,
options: Optional[dict] = None,
) -> Tuple[ObsType, dict]:
seed: int | None = None,
options: dict | None = None,
) -> tuple[ObsType, dict]:
"""Resets the environment."""
return 0, {}


def test_gymnasium_env():
def test_example_env():
"""Tests a gymnasium environment."""
env = ExampleEnv()

@@ -224,14 +58,14 @@ def __init__(self, env: Env[ObsType, ActType]):
self.new_reward = 3

def reset(
self, *, seed: Optional[int] = None, options: Optional[Dict[str, Any]] = None
) -> Tuple[WrapperObsType, Dict[str, Any]]:
self, *, seed: int | None = None, options: dict[str, Any] | None = None
) -> tuple[WrapperObsType, dict[str, Any]]:
"""Resets the environment ."""
return super().reset(seed=seed, options=options)

def step(
self, action: WrapperActType
) -> Tuple[WrapperObsType, float, bool, bool, Dict[str, Any]]:
) -> tuple[WrapperObsType, float, bool, bool, dict[str, Any]]:
"""Steps through the environment."""
obs, reward, termination, truncation, info = self.env.step(action)
return obs, self.new_reward, termination, truncation, info
@@ -241,7 +75,7 @@ def access_hidden_np_random(self):
return self._np_random


def test_gymnasium_wrapper():
def test_example_wrapper():
"""Tests the gymnasium wrapper works as expected."""
env = ExampleEnv()
wrapper_env = ExampleWrapper(env)
@@ -303,7 +137,7 @@ def action(self, action: ActType) -> ActType:
return np.array([1])


def test_wrapper_types():
def test_reward_observation_action_wrapper():
"""Tests the observation, action and reward wrapper examples."""
env = GenericTestEnv()