以前に、WSLでOpenAI Gymを動かす記事を書いた。
その際は、Windows側にX Window Serverの「VcXsrv」をインストールして行ったが、Windows 11では、WSLgがX Window Serverの役割を果たすので、Windows側のセットアップなしにLinuxのGUIを扱えるようになった。
はじめ、WSLにDockerをインストールして、Dockerコンテナ上で動かそうとしたが、GUIアプリの起動はできるがウィンドウが表示されないため、あきらめて、WSLのUbuntu上に直接、環境構築した。
「wsl --export」でインストール直後のUbuntuの環境を保存しておくと、WSLに同じディストリビューションで複数環境を構築できるため、Dockerがなくてもそれほど困らないだろう。
OpenAI Gymの環境構築手順
Anacondaインストール
$ wget https://repo.anaconda.com/archive/Anaconda3-2021.11-Linux-x86_64.sh $ bash Anaconda3-2021.11-Linux-x86_64.sh
で、Anacondaをインストールする。
OpenAI Gymインストール
$ pip install gym
実行できるかipythonで、以下のコードを入力して確認する。
import gym env = gym.make('CartPole-v0') env.reset() env.render()
以下のエラーが表示される。
ModuleNotFoundError Traceback (most recent call last) ~/anaconda3/lib/python3.9/site-packages/gym/envs/classic_control/rendering.py in <module> 14 try: ---> 15 import pyglet 16 except ImportError as e: ModuleNotFoundError: No module named 'pyglet' During handling of the above exception, another exception occurred: ImportError Traceback (most recent call last) <ipython-input-4-0fb217d1bc14> in <module> 1 for _ in range(1000): ----> 2 env.render() 3 env.step(env.action_space.sample()) # take a random action 4 env.close() ~/anaconda3/lib/python3.9/site-packages/gym/core.py in render(self, mode, **kwargs) 293 294 def render(self, mode="human", **kwargs): --> 295 return self.env.render(mode, **kwargs) 296 297 def close(self): ~/anaconda3/lib/python3.9/site-packages/gym/envs/classic_control/cartpole.py in render(self, mode) 177 178 if self.viewer is None: --> 179 from gym.envs.classic_control import rendering 180 181 self.viewer = rendering.Viewer(screen_width, screen_height) ~/anaconda3/lib/python3.9/site-packages/gym/envs/classic_control/rendering.py in <module> 15 import pyglet 16 except ImportError as e: ---> 17 raise ImportError( 18 """ 19 Cannot import pyglet. ImportError: Cannot import pyglet. HINT: you can install pyglet directly via 'pip install pyglet'. But if you really just want to install all Gym dependencies and not have to think about it, 'pip install -e .[all]' or 'pip install gym[all]' will do it.
pip install gym[all]をインストールしろということなので、実行すると、MuJoCoをインストールしろと言われる。
Running setup.py clean for mujoco-py Failed to build box2d-py mujoco-py Installing collected packages: lockfile, importlib-resources, glfw, pyglet, opencv-python, mujoco-py, lz4, box2d-py, ale-py Running setup.py install for mujoco-py ... error ERROR: Command errored out with exit status 1: command: /home/xxxx/anaconda3/bin/python -u -c 'import io, os, sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-p63nh23w/mujoco-py_a2ff8ae5e2664451813a1a75ca3ccbfd/setup.py'"'"'; __file__='"'"'/tmp/pip-install-p63nh23w/mujoco-py_a2ff8ae5e2664451813a1a75ca3ccbfd/setup.py'"'"';f = getattr(tokenize, '"'"'open'"'"', open)(__file__) if os.path.exists(__file__) else io.StringIO('"'"'from setuptools import setup; setup()'"'"');code = f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' install --record /tmp/pip-record-s0ldhh8m/install-record.txt --single-version-externally-managed --compile --install-headers /home/xxxx/anaconda3/include/python3.9/mujoco-py cwd: /tmp/pip-install-p63nh23w/mujoco-py_a2ff8ae5e2664451813a1a75ca3ccbfd/ Complete output (56 lines): running install running build You appear to be missing MuJoCo. We expected to find the file here: /home/xxxx/.mujoco/mjpro150 This package only provides python bindings, the library must be installed separately. Please follow the instructions on the README to install MuJoCo https://github.com/openai/mujoco-py#install-mujoco Which can be downloaded from the website https://www.roboti.us/index.html Traceback (most recent call last): File "<string>", line 1, in <module> File "/tmp/pip-install-p63nh23w/mujoco-py_a2ff8ae5e2664451813a1a75ca3ccbfd/setup.py", line 32, in <module> setup( File "/home/xxxx/anaconda3/lib/python3.9/site-packages/setuptools/__init__.py", line 153, in setup return distutils.core.setup(**attrs) File "/home/xxxx/anaconda3/lib/python3.9/distutils/core.py", line 148, in setup dist.run_commands() File "/home/xxxx/anaconda3/lib/python3.9/distutils/dist.py", line 966, in run_commands self.run_command(cmd) File "/home/xxxx/anaconda3/lib/python3.9/distutils/dist.py", line 985, in run_command cmd_obj.run() File "/home/xxxx/anaconda3/lib/python3.9/site-packages/setuptools/command/install.py", line 61, in run return orig.install.run(self) File "/home/xxxx/anaconda3/lib/python3.9/distutils/command/install.py", line 546, in run self.run_command('build') File "/home/xxxx/anaconda3/lib/python3.9/distutils/cmd.py", line 313, in run_command self.distribution.run_command(command) File "/home/xxxx/anaconda3/lib/python3.9/distutils/dist.py", line 985, in run_command cmd_obj.run() File "/tmp/pip-install-p63nh23w/mujoco-py_a2ff8ae5e2664451813a1a75ca3ccbfd/setup.py", line 28, in run import mujoco_py # noqa: force build File "/tmp/pip-install-p63nh23w/mujoco-py_a2ff8ae5e2664451813a1a75ca3ccbfd/mujoco_py/__init__.py", line 3, in <module> from mujoco_py.builder import cymj, ignore_mujoco_warnings, functions, MujocoException File "/tmp/pip-install-p63nh23w/mujoco-py_a2ff8ae5e2664451813a1a75ca3ccbfd/mujoco_py/builder.py", line 502, in <module> mjpro_path, key_path = discover_mujoco() File "/tmp/pip-install-p63nh23w/mujoco-py_a2ff8ae5e2664451813a1a75ca3ccbfd/mujoco_py/utils.py", line 93, in discover_mujoco raise Exception(message) Exception: You appear to be missing MuJoCo. We expected to find the file here: /home/xxxx/.mujoco/mjpro150 This package only provides python bindings, the library must be installed separately. Please follow the instructions on the README to install MuJoCo https://github.com/openai/mujoco-py#install-mujoco Which can be downloaded from the website https://www.roboti.us/index.html ---------------------------------------- ERROR: Command errored out with exit status 1: /home/xxxx/anaconda3/bin/python -u -c 'import io, os, sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-p63nh23w/mujoco-py_a2ff8ae5e2664451813a1a75ca3ccbfd/setup.py'"'"'; __file__='"'"'/tmp/pip-install-p63nh23w/mujoco-py_a2ff8ae5e2664451813a1a75ca3ccbfd/setup.py'"'"';f = getattr(tokenize, '"'"'open'"'"', open)(__file__) if os.path.exists(__file__) else io.StringIO('"'"'from setuptools import setup; setup()'"'"');code = f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' install --record /tmp/pip-record-s0ldhh8m/install-record.txt --single-version-externally-managed --compile --install-headers /home/xxxx/anaconda3/include/python3.9/mujoco-py Check the logs for full command output.
MuJoCoは、最近オープンソースになったが、バージョン1.5はライセンスが必要なため、gym[all]ではなくgym[atari]をインストールすることにした。
$ pip install gym[atari]
再び、動作を確認したところ、今度は、python-openglをインストールしろと言われた。
ImportError: Library "GLU" not found. During handling of the above exception, another exception occurred: ImportError Traceback (most recent call last) <ipython-input-4-d9761596d5d9> in <module> ----> 1 env.render() ~/anaconda3/lib/python3.9/site-packages/gym/core.py in render(self, mode, **kwargs) 293 294 def render(self, mode="human", **kwargs): --> 295 return self.env.render(mode, **kwargs) 296 297 def close(self): ~/anaconda3/lib/python3.9/site-packages/gym/envs/classic_control/cartpole.py in render(self, mode) 177 178 if self.viewer is None: --> 179 from gym.envs.classic_control import rendering 180 181 self.viewer = rendering.Viewer(screen_width, screen_height) ~/anaconda3/lib/python3.9/site-packages/gym/envs/classic_control/rendering.py in <module> 27 from pyglet.gl import * 28 except ImportError as e: ---> 29 raise ImportError( 30 """ 31 Error occurred while running `from pyglet.gl import *` ImportError: Error occurred while running `from pyglet.gl import *` HINT: make sure you have OpenGL installed. On Ubuntu, you can run 'apt-get install python-opengl'. If you're running on a server, you may need a virtual frame buffer; something like this should work: 'xvfb-run -s "-screen 0 1400x900x24" python <your_script.py>'
$ pip install python-opengl
再度、動作を確認したことろ、無事CartPoleの画面が表示できた。
初回は数十秒くらい時間がかかった。