Markus Krimmel 6c6c872523
Gymnasium Integration (#789)
Changes:
- Disclaimer in README
- Replaced all occurences of Gym with Gymnasium
- Removed code that is now dead since we no longer need to support the
old step API
- Updated type hints to only allow new step API
- Increased required version of envpool to support Gymnasium
- Increased required version of PettingZoo to support Gymnasium
- Updated `PettingZooEnv` to only use the new step API, removed hack to
also support old API
- I had to add some `# type: ignore` comments, due to new type hinting
in Gymnasium. I'm not that familiar with type hinting but I believe that
the issue is on the Gymnasium side and we are looking into it.
- Had to update `MyTestEnv` to support `options` kwarg
- Skip NNI tests because they still use OpenAI Gym
- Also allow `PettingZooEnv` in vector environment
- Updated doc page about ReplayBuffer to also talk about terminated and
truncated flags.

Still need to do: 
- Update the Jupyter notebooks in docs
- Check the entire code base for more dead code (from compatibility
stuff)
- Check the reset functions of all environments/wrappers in code base to
make sure they use the `options` kwarg
- Someone might want to check test_env_finite.py
- Is it okay to allow `PettingZooEnv` in vector environments? Might need
to update docs?
2023-02-03 11:57:27 -08:00

53 lines
1.6 KiB
Python

from typing import Any, Callable, List, Optional, Tuple
import gymnasium as gym
import numpy as np
from tianshou.env.worker import EnvWorker
class DummyEnvWorker(EnvWorker):
"""Dummy worker used in sequential vector environments."""
def __init__(self, env_fn: Callable[[], gym.Env]) -> None:
self.env = env_fn()
super().__init__(env_fn)
def get_env_attr(self, key: str) -> Any:
return getattr(self.env, key)
def set_env_attr(self, key: str, value: Any) -> None:
setattr(self.env.unwrapped, key, value)
def reset(self, **kwargs: Any) -> Tuple[np.ndarray, dict]:
if "seed" in kwargs:
super().seed(kwargs["seed"])
return self.env.reset(**kwargs)
@staticmethod
def wait( # type: ignore
workers: List["DummyEnvWorker"], wait_num: int, timeout: Optional[float] = None
) -> List["DummyEnvWorker"]:
# Sequential EnvWorker objects are always ready
return workers
def send(self, action: Optional[np.ndarray], **kwargs: Any) -> None:
if action is None:
self.result = self.env.reset(**kwargs)
else:
self.result = self.env.step(action) # type: ignore
def seed(self, seed: Optional[int] = None) -> Optional[List[int]]:
super().seed(seed)
try:
return self.env.seed(seed) # type: ignore
except (AttributeError, NotImplementedError):
self.env.reset(seed=seed)
return [seed] # type: ignore
def render(self, **kwargs: Any) -> Any:
return self.env.render(**kwargs)
def close_env(self) -> None:
self.env.close()