Skip to content

Commit

Permalink
Updated gymnasium to be equivalent to gym v26.2 (#36)
Browse files Browse the repository at this point in the history
  • Loading branch information
pseudo-rnd-thoughts authored Oct 5, 2022
1 parent dc60cdc commit f6489c3
Show file tree
Hide file tree
Showing 21 changed files with 190 additions and 13 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
name: build
on: [pull_request, push]

permissions:
contents: read # to fetch code (actions/checkout)

jobs:
build:
runs-on: ubuntu-latest
Expand Down
4 changes: 4 additions & 0 deletions .github/workflows/pre-commit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ on:
pull_request:
push:
branches: [master]

permissions:
contents: read # to fetch code (actions/checkout)

jobs:
pre-commit:
runs-on: ubuntu-latest
Expand Down
8 changes: 8 additions & 0 deletions gymnasium/envs/box2d/bipedal_walker.py
Original file line number Diff line number Diff line change
Expand Up @@ -608,6 +608,14 @@ def step(self, action: np.ndarray):
return np.array(state, dtype=np.float32), reward, terminated, False, {}

def render(self):
if self.render_mode is None:
gym.logger.warn(
"You are calling render method without specifying any render mode. "
"You can specify the render_mode at initialization, "
f'e.g. gym("{self.spec.id}", render_mode="rgb_array")'
)
return

try:
import pygame
from pygame import gfxdraw
Expand Down
10 changes: 9 additions & 1 deletion gymnasium/envs/box2d/car_racing.py
Original file line number Diff line number Diff line change
Expand Up @@ -569,7 +569,15 @@ def step(self, action: Union[np.ndarray, int]):
return self.state, step_reward, terminated, truncated, {}

def render(self):
return self._render(self.render_mode)
if self.render_mode is None:
gym.logger.warn(
"You are calling render method without specifying any render mode. "
"You can specify the render_mode at initialization, "
f'e.g. gym("{self.spec.id}", render_mode="rgb_array")'
)
return
else:
return self._render(self.render_mode)

def _render(self, mode: str):
assert mode in self.metadata["render_modes"]
Expand Down
8 changes: 8 additions & 0 deletions gymnasium/envs/box2d/lunar_lander.py
Original file line number Diff line number Diff line change
Expand Up @@ -602,6 +602,14 @@ def step(self, action):
return np.array(state, dtype=np.float32), reward, terminated, False, {}

def render(self):
if self.render_mode is None:
gym.logger.warn(
"You are calling render method without specifying any render mode. "
"You can specify the render_mode at initialization, "
f'e.g. gym("{self.spec.id}", render_mode="rgb_array")'
)
return

try:
import pygame
from pygame import gfxdraw
Expand Down
11 changes: 10 additions & 1 deletion gymnasium/envs/classic_control/acrobot.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
import numpy as np
from numpy import cos, pi, sin

import gymnasium as gym
from gymnasium import Env, spaces
from gymnasium.envs.classic_control import utils
from gymnasium.error import DependencyNotInstalled

__copyright__ = "Copyright 2013, RLPy http://acl.mit.edu/RLPy"
Expand All @@ -20,7 +22,6 @@

# SOURCE:
# https://github.com/rlpy/rlpy/blob/master/rlpy/Domains/Acrobot.py
from gymnasium.envs.classic_control import utils


class AcrobotEnv(Env):
Expand Down Expand Up @@ -280,6 +281,14 @@ def _dsdt(self, s_augmented):
return dtheta1, dtheta2, ddtheta1, ddtheta2, 0.0

def render(self):
if self.render_mode is None:
gym.logger.warn(
"You are calling render method without specifying any render mode. "
"You can specify the render_mode at initialization, "
f'e.g. gym("{self.spec.id}", render_mode="rgb_array")'
)
return

try:
import pygame
from pygame import gfxdraw
Expand Down
8 changes: 8 additions & 0 deletions gymnasium/envs/classic_control/cartpole.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,14 @@ def reset(
return np.array(self.state, dtype=np.float32), {}

def render(self):
if self.render_mode is None:
gym.logger.warn(
"You are calling render method without specifying any render mode. "
"You can specify the render_mode at initialization, "
f'e.g. gym("{self.spec.id}", render_mode="rgb_array")'
)
return

try:
import pygame
from pygame import gfxdraw
Expand Down
8 changes: 8 additions & 0 deletions gymnasium/envs/classic_control/continuous_mountain_car.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,14 @@ def _height(self, xs):
return np.sin(3 * xs) * 0.45 + 0.55

def render(self):
if self.render_mode is None:
gym.logger.warn(
"You are calling render method without specifying any render mode. "
"You can specify the render_mode at initialization, "
f'e.g. gym("{self.spec.id}", render_mode="rgb_array")'
)
return

try:
import pygame
from pygame import gfxdraw
Expand Down
8 changes: 8 additions & 0 deletions gymnasium/envs/classic_control/mountain_car.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,14 @@ def _height(self, xs):
return np.sin(3 * xs) * 0.45 + 0.55

def render(self):
if self.render_mode is None:
gym.logger.warn(
"You are calling render method without specifying any render mode. "
"You can specify the render_mode at initialization, "
f'e.g. gym("{self.spec.id}", render_mode="rgb_array")'
)
return

try:
import pygame
from pygame import gfxdraw
Expand Down
8 changes: 8 additions & 0 deletions gymnasium/envs/classic_control/pendulum.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,14 @@ def _get_obs(self):
return np.array([np.cos(theta), np.sin(theta), thetadot], dtype=np.float32)

def render(self):
if self.render_mode is None:
gym.logger.warn(
"You are calling render method without specifying any render mode. "
"You can specify the render_mode at initialization, "
f'e.g. gym("{self.spec.id}", render_mode="rgb_array")'
)
return

try:
import pygame
from pygame import gfxdraw
Expand Down
8 changes: 8 additions & 0 deletions gymnasium/envs/toy_text/blackjack.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,14 @@ def reset(
return self._get_obs(), {}

def render(self):
if self.render_mode is None:
gym.logger.warn(
"You are calling render method without specifying any render mode. "
"You can specify the render_mode at initialization, "
f'e.g. gym("{self.spec.id}", render_mode="rgb_array")'
)
return

try:
import pygame
except ImportError:
Expand Down
9 changes: 9 additions & 0 deletions gymnasium/envs/toy_text/cliffwalking.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import numpy as np

import gymnasium as gym
from gymnasium import Env, spaces
from gymnasium.envs.toy_text.utils import categorical_sample
from gymnasium.error import DependencyNotInstalled
Expand Down Expand Up @@ -163,6 +164,14 @@ def reset(self, *, seed: Optional[int] = None, options: Optional[dict] = None):
return int(self.s), {"prob": 1}

def render(self):
if self.render_mode is None:
gym.logger.warn(
"You are calling render method without specifying any render mode. "
"You can specify the render_mode at initialization, "
f'e.g. gym("{self.spec.id}", render_mode="rgb_array")'
)
return

if self.render_mode == "ansi":
return self._render_text()
else:
Expand Down
9 changes: 9 additions & 0 deletions gymnasium/envs/toy_text/frozen_lake.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import numpy as np

import gymnasium as gym
from gymnasium import Env, spaces, utils
from gymnasium.envs.toy_text.utils import categorical_sample
from gymnasium.error import DependencyNotInstalled
Expand Down Expand Up @@ -268,6 +269,14 @@ def reset(
return int(self.s), {"prob": 1}

def render(self):
if self.render_mode is None:
gym.logger.warn(
"You are calling render method without specifying any render mode. "
"You can specify the render_mode at initialization, "
f'e.g. gym("{self.spec.id}", render_mode="rgb_array")'
)
return

if self.render_mode == "ansi":
return self._render_text()
else: # self.render_mode in {"human", "rgb_array"}:
Expand Down
10 changes: 9 additions & 1 deletion gymnasium/envs/toy_text/taxi.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import numpy as np

import gymnasium as gym
from gymnasium import Env, spaces, utils
from gymnasium.envs.toy_text.utils import categorical_sample
from gymnasium.error import DependencyNotInstalled
Expand Down Expand Up @@ -279,7 +280,14 @@ def reset(
return int(self.s), {"prob": 1.0, "action_mask": self.action_mask(self.s)}

def render(self):
if self.render_mode == "ansi":
if self.render_mode is None:
gym.logger.warn(
"You are calling render method without specifying any render mode. "
"You can specify the render_mode at initialization, "
f'e.g. gym("{self.spec.id}", render_mode="rgb_array")'
)
return
elif self.render_mode == "ansi":
return self._render_text()
else: # self.render_mode in {"human", "rgb_array"}:
return self._render_gui(self.render_mode)
Expand Down
4 changes: 2 additions & 2 deletions gymnasium/spaces/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ class GraphInstance(NamedTuple):
"""A Graph space instance.
* nodes (np.ndarray): an (n x ...) sized array representing the features for n nodes, (...) must adhere to the shape of the node space.
* edges (Optional[np.ndarray]): an (m x ...) sized array representing the features for m nodes, (...) must adhere to the shape of the edge space.
* edge_links (Optional[np.ndarray]): an (m x 2) sized array of ints representing the two nodes that each edge connects.
* edges (Optional[np.ndarray]): an (m x ...) sized array representing the features for m edges, (...) must adhere to the shape of the edge space.
* edge_links (Optional[np.ndarray]): an (m x 2) sized array of ints representing the indices of the two nodes that each edge connects.
"""

nodes: np.ndarray
Expand Down
7 changes: 4 additions & 3 deletions gymnasium/vector/async_vector_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -566,9 +566,10 @@ def _worker(index, env_fn, pipe, parent_pipe, shared_memory, error_queue):
info,
) = env.step(data)
if terminated or truncated:
old_observation = observation
old_observation, old_info = observation, info
observation, info = env.reset()
info["final_observation"] = old_observation
info["final_info"] = old_info
pipe.send(((observation, reward, terminated, truncated, info), True))
elif command == "seed":
env.seed(data)
Expand Down Expand Up @@ -636,10 +637,10 @@ def _worker_shared_memory(index, env_fn, pipe, parent_pipe, shared_memory, error
info,
) = env.step(data)
if terminated or truncated:
old_observation = observation
old_observation, old_info = observation, info
observation, info = env.reset()
info["final_observation"] = old_observation

info["final_info"] = old_info
write_to_shared_memory(
observation_space, index, observation, shared_memory
)
Expand Down
3 changes: 2 additions & 1 deletion gymnasium/vector/sync_vector_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,9 +150,10 @@ def step_wait(self):
) = env.step(action)

if self._terminateds[i] or self._truncateds[i]:
old_observation = observation
old_observation, old_info = observation, info
observation, info = env.reset()
info["final_observation"] = old_observation
info["final_info"] = old_info
observations.append(observation)
infos = self._add_info(infos, info, i)
self.observations = concatenate(
Expand Down
2 changes: 1 addition & 1 deletion gymnasium/version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
VERSION = "0.26.1"
VERSION = "0.26.2"
6 changes: 5 additions & 1 deletion gymnasium/wrappers/atari_preprocessing.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,6 @@ def __init__(
np.empty(env.observation_space.shape, dtype=np.uint8),
]

self.ale = env.unwrapped.ale
self.lives = 0
self.game_over = False

Expand All @@ -112,6 +111,11 @@ def __init__(
low=_low, high=_high, shape=_shape, dtype=_obs_dtype
)

@property
def ale(self):
"""Make ale as a class property to avoid serialization error."""
return self.env.unwrapped.ale

def step(self, action):
"""Applies the preprocessing for an :meth:`env.step`."""
total_reward, terminated, truncated, info = 0.0, False, False, {}
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"box2d": ["box2d-py==2.3.5", "pygame==2.1.0", "swig==4.*"],
"classic_control": ["pygame==2.1.0"],
"mujoco_py": ["mujoco_py<2.2,>=2.1"],
"mujoco": ["mujoco==2.2.0", "imageio>=2.14.1"],
"mujoco": ["mujoco==2.2", "imageio>=2.14.1"],
"toy_text": ["pygame==2.1.0"],
"other": ["lz4>=3.1.0", "opencv-python>=3.0", "matplotlib>=3.0", "moviepy>=1.0.0"],
}
Expand Down
Loading

0 comments on commit f6489c3

Please sign in to comment.