windows系统下安装最新版gym的安装方法(此时最新版的gym为0.24.0,gym==0.24.0)

当前gym的最新版本为0.24.0,本篇介绍对gym[atari]==0.24.0进行安装。

 

 

使用pip安装:

pip install gym[atari]

 

 

 

 

 

 

可以看到此时安装的是ale_py而不是atari_py库。

 

 

 

 

运行测试代码:

 

import gym
env=gym.make("Pong-v0")
print(env)
env.reset()

 

结果报错:

 

 

 

 

f'We\'re Unable to find the game "{self._game}". Note: Gym no longer distributes ROMs. '
gym.error.Error: We're Unable to find the game "Pong". Note: Gym no longer distributes ROMs. If you own a license to use the necessary ROMs for research purposes you can download them via `pip install gym[accept-rom-license]`. Otherwise, you should try importing "Pong" via the command `ale-import-roms`. If you believe this is a mistake perhaps your copy of "Pong" is unsupported. To check if this is the case try providing the environment variable `PYTHONWARNINGS=default::ImportWarning:ale_py.roms`. For more information see: https://github.com/mgbellemare/Arcade-Learning-Environment#rom-management

 

 

根据报错的信息我们知道是因为没有安装atari游戏对应的ROMs的bin文件,我们可以选择手动下载后添加到ale_py库中,但是根据报错信息的提醒我们知道可以使用命令:pip install gym[accept-rom-license] 来进行安装。

执行:

pip install gym[accept-rom-license]

 

此时再运行,结果:

 

 

 证明问题已解决,gym[atari]已经成功安装。

 

 

 

 

 

测试代码:

 

import time
import gym

env = gym.make('BreakoutNoFrameskip-v4')
print("Observation Space: ", env.observation_space)
print("Action Space       ", env.action_space)

obs = env.reset()
for i in range(1000):
    env.render()
    action = env.action_space.sample()
    obs, reward, done, info = env.step(action)
    time.sleep(0.01)
env.close()

 

发现报错:

 

 

 

 

查看报错的代码地址:lib\site-packages\gym\core.py

    def render(self, mode="human"):
        """Renders the environment.

        A set of supported modes varies per environment. (And some
        third-party environments may not support rendering at all.)
        By convention, if mode is:

        - human: render to the current display or terminal and
          return nothing. Usually for human consumption.
        - rgb_array: Return a numpy.ndarray with shape (x, y, 3),
          representing RGB values for an x-by-y pixel image, suitable
          for turning into a video.
        - ansi: Return a string (str) or StringIO.StringIO containing a
          terminal-style text representation. The text can include newlines
          and ANSI escape sequences (e.g. for colors).

        Note:
            Make sure that your class's metadata 'render_modes' key includes
            the list of supported modes. It's recommended to call super()
            in implementations to use the functionality of this method.

        Example:
            >>> import numpy as np
            >>> class MyEnv(Env):
            ...    metadata = {'render_modes': ['human', 'rgb_array']}
            ...
            ...    def render(self, mode='human'):
            ...        if mode == 'rgb_array':
            ...            return np.array(...) # return RGB frame suitable for video
            ...        elif mode == 'human':
            ...            ... # pop up a window and render
            ...        else:
            ...            super().render(mode=mode) # just raise an exception

        Args:
            mode: the mode to render with, valid modes are `env.metadata["render_modes"]`
        """
        raise NotImplementedError
View Code

可以看到由于gym的版本升级曾经多个版本的gym的测试代码已经不能继续使用了,于是我们进行修改:

 

 

 再次报错:

 

 

 报错信息的全部:

"render(mode='human') is deprecated. Please supply `render_mode` when "
gym.error.Error: render(mode='human') is deprecated. Please supply `render_mode` when constructing your environment, e.g., gym.make(ID, render_mode='human'). The new `render_mode` keyword argument supports DPI scaling, audio, and native framerates.

 

从上面的报错信息可以知道gym设置了一个绘图显示模式`render_mode,如果想使用绘图显示的功能就需要在创建环境的时候进行设置,形式如gym.make(ID, render_mode='human')。

修改后的代码:

import time
import gym

env = gym.make('BreakoutNoFrameskip-v4', render_mode='human')
print("Observation Space: ", env.observation_space)
print("Action Space       ", env.action_space)

obs = env.reset()
for i in range(1000):
    action = env.action_space.sample()
    obs, reward, done, info = env.step(action)
    time.sleep(0.01)
env.close()

 

 

可以正常运行并绘制显示图像:

 

 

通过对上面的代码测试,可以知道在设置绘图显示模式`render_mode时进行绘图的步骤为 env.reset() 和 env.step(action),在执行这两步操作时会自动进行绘图显示。

 

 

查看位于lib\site-packages\gym\envs\atari\environment.py中的代码:

def render(self, mode: str) -> Any:
"""
Render is not supported by ALE. We use a paradigm similar to
Gym3 which allows you to specify `render_mode` during construction.

For example,
gym.make("ale-py:Pong-v0", render_mode="human")
will display the ALE and maintain the proper interval to match the
FPS target set by the ROM.
"""
if mode == "rgb_array":
return self.ale.getScreenRGB()
elif mode == "human":
raise error.Error(
(
"render(mode='human') is deprecated. Please supply `render_mode` when "
"constructing your environment, e.g., gym.make(ID, render_mode='human'). "
"The new `render_mode` keyword argument supports DPI scaling, "
"audio, and native framerates."
)
)
else:
raise error.Error(
f"Invalid render mode `{mode}`. Supported modes: `rgb_array`."
)

可以知道在新版本的gym中一共存在两个环境模式,一个是'human'模式,一个是`rgb_array`模式。如果不在创建环境时通过gym.make(ID, render_mode='human')设置绘图显示模式的话默认就是`rgb_array`模式。

 

虽然新版的gym的改动不大但是对于习惯使用旧版本的gym的人来说还是有些不方便的,不过说一个敲黑板的事情,那就是如果你要发布你的依赖gym环境的强化学习代码一定要把gym的版本号和ale_py版本号或atari_py版本号给标注出来,否则不一定gym未来版本一升级你以前的代码就会运行报错,挨个试验旧版本的gym是个很困难的工作,所以发布代码时标注好你所使用的gym版本号是很有必要的。

 

 

 

=================================================

 

posted on 2022-05-30 22:18  Angry_Panda  阅读(3653)  评论(0编辑  收藏  举报

导航