Skip to content

Commit

Permalink
gymからgymnasiumに変更
Browse files Browse the repository at this point in the history
  • Loading branch information
tyfkda committed Jul 30, 2024
1 parent b6f4e26 commit 5f92019
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 22 deletions.
13 changes: 8 additions & 5 deletions nes_py/nes_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
import itertools
import os
import sys
import gym
from gym.spaces import Box
from gym.spaces import Discrete
import gymnasium as gym
from gymnasium.spaces import Box
from gymnasium.spaces import Discrete
import numpy as np
from ._rom import ROM
from ._image_viewer import ImageViewer
Expand Down Expand Up @@ -86,6 +86,8 @@ class NESEnv(gym.Env):
# relevant meta-data about the environment
metadata = {
'render.modes': ['rgb_array', 'human'],
'render_modes': ['rgb_array', 'human'],
'render_fps': 50,
'video.frames_per_second': 60
}

Expand Down Expand Up @@ -270,7 +272,7 @@ def reset(self, seed=None, options=None, return_info=None):
# set the done flag to false
self.done = False
# return the screen from the emulator
return self.screen
return self.screen, {}

def _did_reset(self):
"""Handle any RAM hacking after a reset occurs."""
Expand Down Expand Up @@ -312,7 +314,8 @@ def step(self, action):
elif reward > self.reward_range[1]:
reward = self.reward_range[1]
# return the screen from the emulator and other relevant data
return self.screen, reward, self.done, info
truncated = False
return self.screen, reward, self.done, truncated, info

def _get_reward(self):
"""Return the reward after a step occurs."""
Expand Down
4 changes: 2 additions & 2 deletions nes_py/tests/test_multiple_makes.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def play(steps):
if done:
_ = env.reset()
action = env.action_space.sample()
_, _, done, _ = env.step(action)
_, _, done, _, _ = env.step(action)
# close the environment
env.close()

Expand Down Expand Up @@ -84,4 +84,4 @@ def test(self):
if dones[idx]:
_ = envs[idx].reset()
action = envs[idx].action_space.sample()
_, _, dones[idx], _ = envs[idx].step(action)
_, _, dones[idx], _, _ = envs[idx].step(action)
16 changes: 8 additions & 8 deletions nes_py/tests/test_nes_env.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""Test cases for the NESEnv class."""
from unittest import TestCase
import gym
import gymnasium as gym
import numpy as np
from .rom_file_abs_path import rom_file_abs_path
from nes_py.nes_env import NESEnv
Expand Down Expand Up @@ -79,17 +79,17 @@ def test(self):
for _ in range(500):
if done:
# reset the environment and check the output value
state = env.reset()
state, _ = env.reset()
self.assertIsInstance(state, np.ndarray)
# sample a random action and check it
action = env.action_space.sample()
self.assertIsInstance(action, int)
self.assertIsInstance(action, np.int64)
# take a step and check the outputs
output = env.step(action)
self.assertIsInstance(output, tuple)
self.assertEqual(4, len(output))
self.assertEqual(5, len(output))
# check each output
state, reward, done, info = output
state, reward, done, _, info = output
self.assertIsInstance(state, np.ndarray)
self.assertIsInstance(reward, float)
self.assertIsInstance(done, bool)
Expand All @@ -108,9 +108,9 @@ def test(self):

for _ in range(250):
if done:
state = env.reset()
state, _ = env.reset()
done = False
state, _, done, _ = env.step(0)
state, _, done, _, _ = env.step(0)

backup = state.copy()

Expand All @@ -120,7 +120,7 @@ def test(self):
if done:
state = env.reset()
done = False
state, _, done, _ = env.step(0)
state, _, done, _, _ = env.step(0)

self.assertFalse(np.array_equal(backup, state))
env._restore()
Expand Down
10 changes: 5 additions & 5 deletions nes_py/wrappers/joypad_space.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""An environment wrapper to convert binary to discrete action space."""
import gym
from gym import Env
from gym import Wrapper
import gymnasium as gym
from gymnasium import Env
from gymnasium import Wrapper


class JoypadSpace(Wrapper):
Expand Down Expand Up @@ -73,9 +73,9 @@ def step(self, action):
# take the step and record the output
return self.env.step(self._action_map[action])

def reset(self):
def reset(self, **kwargs):
"""Reset the environment and return the initial observation."""
return self.env.reset()
return self.env.reset(**kwargs)

def get_keys_to_action(self):
"""Return the dictionary of keyboard keys to actions."""
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
gym>=0.17.2
gymnasium~=0.29.1
numpy>=1.18.5
pyglet<=1.5.21,>=1.4.0
tqdm>=4.48.2
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
ext_modules=[LIB_NES_ENV],
zip_safe=False,
install_requires=[
'gym>=0.17.2',
'gymnasium>=0.29.1,<0.30.0',
'numpy>=1.18.5',
'pyglet<=1.5.21,>=1.4.0',
'tqdm>=4.48.2',
Expand Down

0 comments on commit 5f92019

Please sign in to comment.