Gym包的安装与使用(新旧版本问题,Atari游戏支持问题)

目录

  • 参考网页
  • 重要概念
  • gym
  • Atari
  • ALE
  • 安装
  • Hello world
  • 环境
  • gym的历史版本
  • 总结
  • 扩展知识:ALE相关
  • 说明文档
  • 单靠ALE开发强化学习代码
  • 与gym结合使用
  • 安装ROM
  • 录制视频
  • 参考网页

    Gym Documentation

  • 可以搜索函数用法
  • 可以查看不同环境输出的具体定义
  • 更像是真正的说明文档
  • 从github上指路而来
  • Gym: A toolkit for developing and comparing reinforcement learning algorithms

  • 一些简单的介绍
  • 更像是广告页
  • https://github.com/openai/gym

  • github页
  • 也有简单的说明
  • Gym安装Atari环境(Windows,Linux适用)_李子树_的博客-CSDN博客

    重要概念

    gym

  • The gym library is a collection of test problems — environments — that you can use to work out your reinforcement learning algorithms. These environments have a shared interface, allowing you to write general algorithms.
  • 可以理解为提供了调用的统一接口
  • 自带就有一些基本的小游戏(或者称之为环境),包括classical control,box2d,mujoco,toytext等类别的一些小游戏,具体游戏名可以去…\Lib\site-packages\gym\envs下相应的文件夹下查看,或者去…\Lib\site-packages\gym\envs\registration.py文件内查看
  • Atari

  • 游戏开发商雅达利(Atari),在这里是指相应的一系列游戏
  • ALE

  • Arcade Learning Environment
  • Arcade,是电玩街机。
  • The Arcade Learning Environment (ALE) is a simple framework that allows researchers and hobbyists to develop AI agents for Atari 2600 games. It is built on top of the Atari 2600 emulator Stella and separates the details of emulation from agent design.
  • Native support for OpenAI Gym.
  • 对应的安装包是ale-py
  • 取代的是atari-py
  • What’s the difference between the Atari environments in OpenAI Gym and the ALE?
  • The environments provided in Gym are built on the ALE. It just provides a different API to the ALE. As of ALE 0.7 OpenAI Gym now uses ale-py and so there’s no difference between using ale-py and Gym.
  • 安装

    pip install gym

  • 安装基础版的gym
  • Successfully installed cloudpickle-2.0.0 gym-0.23.1 gym-notices-0.0.6 numpy-1.22.3
  • 在envs文件夹下包含有classical control的5款游戏;box2d的4款游戏;mujoco的12+款游戏;toy_text的5款游戏
  • pip install gym[atari]

  • 除了安装gym外,还回安装ale-py
  • pip install gym[all]

  • 会安装许多其他的包,包括box2d-py,opencv-python,ale-py,mujoco-py,pygame。直接安装会报错,提示是mujoco-py必须单独安装,mujoco-py的安装有点麻烦,此处不再探索。
  • Hello world

    import gym
    
    env = gym.make('CartPole-v0')
    env.reset()
    for _ in range(1000):
        env.render()
        env.step(env.action_space.sample())  # take a random action
    env.close()
    

    环境

    gym.make(id)中id的命名方式是*[username/](env-name)-v(version)*

    from gym import envs
    for env in envs.registry.all():
        print(env.id)
    

    输出结果

    并不是所有的输出拿来都能用(主要是Atari相关的环境,有名字但是没有包的)

    As of Gym v0.20 and onwards all Atari environments are provided via ale-py. We do recommend using the new v5 environments in the ALE namespace:

    import gym
    
    env = gym.make('ALE/Breakout-v5')
    

    gym的历史版本

    从0.20开始,gym转而用ale-py了,这里测试019版本时期gym的效果

    pip install gym==0.19.0
    pip install atari_py==0.2.6
    

    0.19版本的gym和最新版的区别不是很大

    安装0.2.6版本的atari,相关目录下会有需要的ROM。

    但是测试时会报错

    Could not find module ‘D:\02 Python Envs\old_gym_test\lib\site-packages\atari_py\ale_interface\ale_c.dll’ (or one of its dependencies). Try using the full path with constructor syntax.

    网络上查看主要有三种解决思路:

    Could not find module \atari_py\ale_interface\ale_c.dll (or one of its dependencies)

  • 在windows上安装一些内容

    ‘module could not be found’ when running gym.make for atari environment. · Issue #1726 · openai/gym

  • 试过,没成功
  • 使用conda进行安装

  • conda install -c conda-forge atari_py
  • 没试
  • 下载缺失的文件到需要的文件加

  • 确实解决了
  • 只是显示的界面很小,速度很快
  • 总结

    总的来看,老版gym+atari-py的组合和新版gym+ale-py的区别主要在

    1. 新版组合想要用Atari的Rom时,需要自己下载

    2. 使用新版的gym时,调用atari游戏时不管是不是v5版本的,都要依照ale-py给出的渲染模式,即在程序创建环境时制定render_mode,后续程序中不再使用render函数

      # 新版
      import gym
      
      env = gym.make('Breakout-v0', render_mode='human')
      env.reset()
      for _ in range(10000):
          result = env.step(env.action_space.sample())  # take a random action
      env.close()
      
      # 老版
      import gym
      
      env = gym.make('Breakout-v0')
      env.reset()
      for _ in range(10000):
          env.render()
          result = env.step(env.action_space.sample())  # take a random action
      env.close()
      

    扩展知识:ALE相关

    说明文档

    Arcade-Learning-Environment/docs at master · mgbellemare/Arcade-Learning-Environment

    只能说非常不好找!这个包并没有一个很完善的官方网站。

    单靠ALE开发强化学习代码

    从上面的说明文档中找到的一段示例代码,说明ale-py本身该怎么用

    import sys
    from random import randrange
    from ale_py import ALEInterface
    
    def main(rom_file):
        ale = ALEInterface()
        ale.setInt('random_seed', 123)
        ale.loadROM(rom_file)
    
        # Get the list of legal actions
        legal_actions = ale.getLegalActionSet()
        num_actions = len(legal_actions)
    
        total_reward = 0
        while not ale.game_over():
          a = legal_actions[randrange(num_actions)]
          reward = ale.act(a)
          total_reward += reward
    
        print(f'Episode ended with score: {total_reward}')
    
    if __name__ == '__main__':
        if len(sys.argv) < 2:
          print(f"Usage: {sys.argv[0]} rom_file")
          sys.exit()
    
        rom_file = sys.argv[1]
        main(rom_file)
    

    与gym结合使用

  • The ALE now natively supports OpenAI Gym.

  • Although you could continue using the legacy environments as is we recommend using the new v5 environments

    import gym
    import ale_py
    
    env = gym.make('ALE/Breakout-v5')
    
  • 在创建环境时不推荐使用render,推荐使用以下做法

    import gym
    
    env = gym.make('Breakout-v0', render_mode='rgb_array')
    env.reset()
    _, _, _, metadata = env.step(0)
    assert 'rgb_array' in metadata
    
  • The render_mode argument supports either human | rgb_array. If rgb_array is specified we’ll return the full RGB observation in the metadata dictionary returned after an agent step.
  • 在给出环境ID时,传统的方法是使用后缀加版本的方式,这些方式也还保留着;新的版本不再使用后缀了,后缀所表达的含义推荐使用关键词给出。

  • The legacy game IDs, environment suffixes -NoFrameskip, -Deterministic, and versioning -v0, -v4 remain unchanged.

  • We do suggest that users transition to the -v5 versioning which is contained in the ALE
    namespace.

  • With the new -v5 versioning we don’t support any ID suffixes such as -NoFrameskip
    or -Deterministic, instead you should configure the environment through keyword arguments as such:

    import gym
    
    env = gym.make('ALE/Breakout-v5',
        obs_type='rgb',                   # ram | rgb | grayscale
        frameskip=5,                     # frame skip
        mode=0,                           # game mode, see Machado et al. 2018
        difficulty=0,                     # game difficulty, see Machado et al. 2018
        repeat_action_probability=0.25,   # Sticky action probability
        full_action_space=True,           # Use all actions
        render_mode=None                  # None | human | rgb_array
    )
    
  • 可以接受的命名包括

  • Pong-v0
  • PongNoFrameskip-v0
  • PongDeterministic-v0
  • Pong-v4
  • PongNoFrameskip-v0
  • PongDeterministic-v4
  • ALE/Pong-v5
  • 安装ROM

    ale-py支持的游戏在上面的说明文档有列出。

    安装ale-py自带的有一个游戏叫Tetris(俄罗斯方块)。使用如下代码,结合pygame,可以绘出图像。

    import gym
    
    env = gym.make('ALE/Tetris-v5', render_mode='human')
    env.reset()
    for _ in range(1000):
        result = env.step(env.action_space.sample())  # take a random action
    env.close()
    

    安装更多的ROMs有以下方法

  • 使用官方工具ale-import-roms

  • 安装ale-py时,会在pip同文件夹下安装ale-import-roms.exe文件,如果下载好ROM后,可以在命令行中执行ale-import-roms roms/命令来安装ROM(扩展名为bin的文件)(假设roms/是存ROM的文件路径)

  • 根据各种地方看到的资料,之所以使用这个工具而不是直接将bin文件拷贝过去,应该是存在文件版本校验的问题,比如平台是否支持,版本是否对应。一个不太官方的博客有说过版本的问题:If we want to get to a place in the community where we can feel confident comparing results on these types of benchmarks we have to take a tougher stance on standardizing these small details. This is why we now require ROMs using the new ROM management tools to match a known supported ROM.
  • 在我的计算机上跑(win11),一直报RuntimeError,还没有测试这个工具使用的具体情况
  • 有一个第三方网站存在着大量的ROM,有一些教程推荐前去下载

    Atari 2600 VCS ROM Collection

  • 使用第三方包autorom

  • pip install autorom

  • AutoROM --accept-license

  • 来自于以下问答

    Error in importing environment OpenAI Gym

  • 关于AutoROM的用法,github上的readme写的很清楚

    https://github.com/Farama-Foundation/AutoROM

  • 一个问题:隔了一段时间后,忽然又找不到了?

  • 手动把autorom的文件移到了ale-py rom下面,又能找到了

  • 卸载autorom,再安装并下载ROM到默认位置,系统可以识别

  • 录制视频

    Arcade-Learning-Environment/visualization.md at master · mgbellemare/Arcade-Learning-Environment

    来源:Fuxilab

    物联沃分享整理
    物联沃-IOTWORD物联网 » Gym包的安装与使用(新旧版本问题,Atari游戏支持问题)

    发表评论