目前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', render_mode='human')
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()