この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。
English version is here.
はじめに
先月、Microsoftからテキストアドベンチャーゲームで強化学習を行うためのツールTextWorldが公開されました。
テキストアドベンチャーゲームとは、アドベンチャーゲームと呼ばれるジャンルのコンピュータゲームの最もシンプルな形式で、物語の描写やプレイヤーの行動の選択など、ゲームにおけるインタラクションがテキストのみで行われるアドベンチャーゲームのことです。そもそも「アドベンチャーゲーム」という呼称は、1976年に発表されたテキストアドベンチャーゲーム「コロッサル・ケーブ・アドベンチャー」が由来となっているそうです。
TextWorldは、テキストアドベンチャーゲームを学習環境として強化学習のエージェントをトレーニングするためのフレームワークで、エージェントにプレーさせるためのテキストアドベンチャーゲームを自動作成する機能と、ゲームとエージェントを接続するためのプログラミングインターフェースが提供されます。このインターフェースを介して、エージェントの代わりに人間がゲームを遊ぶこともできます。
この記事では、TextWorldをAmazon SageMaker上にインストールして、ゲームを自動作成して人間が遊んでみるための手順をご紹介します。
準備
TextWorldの動作にはPython3が必要で、現時点ではLinuxかMac上での動作がサポートされています。 今回はゲームで遊んでみるだけですが、強化学習エージェントの開発も見据えてAmazon SageMaker(以下SageMaker)上で動かしてます。
まずノートブックインスタンスを作成します。AWSマネージメントコンソールにログインし、SageMakerのダッシュボードを開きます。
ノートブックインスタンス名を入力し、「IAMロール」のドロップダウンリストから「新しいロールの作成」を選択します。
「指定するS3バケット」は「なし」をチェックして、「ロールの作成」をクリックします。
「ノートブックインスタンスの作成」をクリックします。
作成したノートブックインスタンスの「ステータス」が「InService」になったら、「オープン」をクリックしてJupyter Notebookを開きます。
Jupyter Notebookが開いたら、右側の「New」のドロップダウンリストから「Terminal」を選択します。以降の作業はJupyter Notebook上のターミナルで行います。
TextWorldのインストール
TextWorldのGitリポジトリをGitHubからクローンして、作成されたディレクトリに移動します。
$ git clone https://github.com/Microsoft/TextWorld.git
Cloning into 'TextWorld'...
(省略)
Resolving deltas: 100% (134/134), done.
$ cd TextWorld/
TextWorldのインストールに必要なパッケージをyum
でインストールします。
$ sudo yum install libuuid-devel libffi-devel -y
(省略)
Installed:
libffi-devel.x86_64 0:3.0.13-16.5.amzn1 libuuid-devel.x86_64 0:2.23.2-33.28.amzn1
Complete!
pip
経由でもインストールする必要があるパッケージがあります。ノートブックインスタンスにはデフォルトでpip
がインストールされていますが、バージョンが古いと警告されて気持ち悪いので先にアップグレードしておきます。
$ pip install --upgrade pip
(省略)
Successfully installed pip-18.0
$ pip install -r requirements.txt
TextWorld本体をインストールします。
$ pip install .
(省略)
Successfully built textworld
Installing collected packages: textworld
Successfully installed textworld-0.0.3
プレフィックスtw-
ではじまる4つのコマンドがインストールされます。ゲームの自動作成にはtw-make
、ゲームを遊ぶにはtw-play
コマンドを使用します。
tw-data tw-make tw-play tw-stats
早速ゲームを自動作成して遊びたいのですが、ゲームの自動作成を行うtw-make
コマンドを実行する時にpybars
モジュールが見つからずエラーになるので、pip
でpybars
をインストールしておきます。
$ pip install pybars3
(省略)
Installing collected packages: PyMeta3, pybars3
Successfully installed PyMeta3-0.5.1 pybars3-0.9.3
もう一点、tw-make
がPython 3の標準ライブラリargparse
モジュールに含まれるArgumentParser
クラスのadd_subparsers
メソッドを呼び出す際に与えているrequired
という引数によってエラーが発生するため、argparse
モジュールに以下の内容のパッチを適用します。このエラーはPython3.7以降であれば発生しません。ノートブックインスタンスのデフォルトのPythonのバージョンは執筆時点では3.6でした。
argparse.patch
--- /home/ec2-user/anaconda3/lib/python3.6/argparse.py 2018-04-29 16:18:42.000000000 +0000
+++ argparse.py 2018-08-13 02:30:36.317112960 +0000
@@ -1064,6 +1064,7 @@
prog,
parser_class,
dest=SUPPRESS,
+ required=False,
help=None,
metavar=None):
@@ -1077,6 +1078,7 @@
dest=dest,
nargs=PARSER,
choices=self._name_parser_map,
+ required=required,
help=help,
metavar=metavar)
$ patch /home/ec2-user/anaconda3/lib/python3.6/argparse.py argparse.patch
patching file /home/ec2-user/anaconda3/lib/python3.6/argparse.py
ゲームの作成
ゲームを自動作成してみます。tw-make
コマンドにサブコマンドcustom
とオプションを以下のように指定して実行します。
$ tw-make custom --world-size 5 --nb-objects 10 --quest-length 5 --output gen_games/
Global seed: 25817
Game generated: gen_games/game_25817.ulx
ゲームはGLulxというフォーマットのファイル(.ulxファイル)として作成されます。
オプションの--world-size
はゲームの舞台を構成する部屋の数、--nb-objects
はゲームに登場するオブジェクトの数、--quest-length
はゲームをクリアするために必要な最小のステップ数を表します。ゲームの内容は作成する度にランダムに変化しますが、ゲームを作成した時に表示されるシード値を--seed
というオプションで指定すると、同じ内容のゲームを再構成することができます。
ゲームの遊び方
自動作成されたゲームで遊ぶ場合
tw-play
コマンドに作成したゲームの場所を指定して実行すると
ゲームのゴールとスタート地点の部屋の説明が表示され、コマンド入力待ちになります。
プレイヤーはテキストでコマンドを入力して、部屋から部屋へと移動しながら、ゲーム内のオブジェクトを操作してゴールの達成を目指します。
$ tw-play gen_games/game_25817.ulx
Using GitGlulxMLEnvironment.
It's time to explore the amazing world of TextWorld! Here is your task for
today. First step, retrieve the passkey that's in the studio. Then, unlock the
door in the studio. Then, ensure that the door in the studio is open. After
that, make an attempt to go to the north. Then, retrieve the Advent Calendar
from the toolbox. Got that? Good!
-= Studio =-
You are in a studio. A standard kind of place.
You make out a stand. The stand is ordinary. But oh no! there's nothing on this
piece of garbage. What, you think everything in TextWorld should have stuff on
it?
There is a closed door leading north.
There is a passkey on the floor.
>
help
と入力すると受付可能なコマンドのリストを見ることができます。
> help
Available commands:
look: describe the current room
goal: print the goal of this game
inventory: print player's inventory
go <dir>: move the player north, east, south or
west
examine <something>: examine something more closely
eat <something>: eat something edible
open <something>: open a door or a container
close <something>: close a door or a container
drop <something>: drop an object on the floor
take <something>: take an object that is on the floor
put <something> on <something>: place an object on a supporter
take <something> from <something>: take an object from a container or a
supporter
insert <something> into <something>: place an object into a container
lock <something> with <something>: lock a door or a container with a key
unlock <something> with <something>: unlock a door or a container with a key
ゲームのゴールはgoal
というコマンドでいつでも確認できます。
今回作成されたゲームのゴールは、スタジオにあるパスキーを入手して、スタジオのドアを開けてから北へ移動し、移動先の部屋に置いてある道具箱に入っているアドベントカレンダーを手に入れることです。
> goal
It's time to explore the amazing world of TextWorld! Here is your task for
today. First step, retrieve the passkey that's in the studio. Then, unlock the
door in the studio. Then, ensure that the door in the studio is open. After
that, make an attempt to go to the north. Then, retrieve the Advent Calendar
from the toolbox. Got that? Good!
look
コマンドで現在地の部屋の様子をいつでも確認できます。
スタート地点の部屋はスタジオで、ゲームのゴールで指定されているパスキーが床に落ちています。
> look
-= Studio =-
You are in a studio. A standard kind of place.
You make out a stand. The stand is ordinary. But oh no! there's nothing on this
piece of garbage. What, you think everything in TextWorld should have stuff on
it?
There is a closed door leading north.
There is a passkey on the floor.
床に落ちているパスキーを拾います。
> take the passkey
Taken.
自分が手に入れた物はinventory
というコマンドで確認できます。
> inventory
You are carrying:
a passkey
a berry
a key
a book
a pen
ゲーム開始時点で、いくつか持っている物があったようです。ベリーを食べてみます。
> eat my berry
You eat the berry. Not bad.
食べた物は、ちゃんと持ち物から消えています。
> inventory
You are carrying:
a passkey
a key
a book
a pen
寄り道ばっかりせずに、ゴールを目指します。手に入れたパスキーで、スタジオのドアの鍵を開けます。
> unlock the door with the passkey
You unlock door.
鍵が開いたので、北へ向かいます。鍵を開けただけでなく、明示的にドアを「開く」必要があるようです。
> go north
You have to open the door first.
> open the door
You open door.
北へ移動すると、そこは仕事場のようです。
> go north
-= Workshop =-
Well I'll be, you are in a place we're calling a workshop. You decide to just
list off a complete list of everything you see in the room, because hey, why
not?
You can make out a toolbox. The toolbox contains an Advent Calendar. You can
make out a closed locker here.
There is a closed gateway leading east. There is an open door leading south. You
don't like doors? Why not try going west, that entranceway is unblocked.
仕事場に、ゴールで指定されているアドベントカレンダー入りの道具箱がありました。カレンダーを取ってみます。
> take the Advent Calendar from the toolbox
Taken.
*** The End ***
You scored 1 out of a possible 1, in 17 turn(s).
Would you like to RESTART, RESTORE a saved game, QUIT or UNDO the last command?
>
Done after 19 steps. Score 1/1.
$
無事、ゴールが達成されたので、ゲームはここで終了です。終了までに要した手数とスコアも表示されます。 強化学習を行う場合は、高いスコアを得られるようなエージェントを開発していくことになるのだと思います。
おまけ:既存のテキストアドベンチャーゲームで遊ぶ場合
tw-play
コマンドは、Z-machineというフォーマットのファイルにも対応しています。「はじめに」で紹介したコロッサル・ケーブ・アドベンチャーがこのZ-machineフォーマットでインターネット上に公開されていたので、最初の方を少しだけやってみます。
$ wget http://www.ifarchive.org/if-archive/games/zcode/Advent.z5
$ tw-play Advent.z5
Using FrotzEnvironment.
ADVENTURE
The Interactive Original
By Will Crowther (1976) and Don Woods (1977)
Reconstructed in three steps by:
Donald Ekman, David M. Baggett (1993) and Graham Nelson (1994)
[In memoriam Stephen Bishop (1820?-1857): GN]
Release 9 / Serial number 060321 / Inform v6.31 Library 6/11 S
At End Of Road
You are standing at the end of a road before a small brick building. Around
you is a forest. A small stream flows out of the building and down a gully.
>
> enter the building
Inside Building Score: 36 Moves: 1
.
Inside Building
You are inside a building, a well house for a large spring.
There are some keys on the ground here.
There is tasty food here.
There is a shiny brass lamp nearby.
There is an empty bottle here.
>
> eat the food
Inside Building Score: 36 Moves: 2
.
(first taking the tasty food)
Delicious!
>
>
おわりに
TextWorldをAmazon SageMaker上にインストールして、ゲームを自動作成して人間が遊んでみるための手順をご紹介しました。
テキストしか扱えなかった時代のコンピュータゲームが、最先端の機械学習のネタとして注目されているという点をとても興味深く感じました。 個人的には、インターフェースをうまくつなぎ合わせてAlexaなどの音声アシスタントと連携させてみたいです。
最後まで読んでいただきありがとうございました。参考になるところがありましたら、SNSでシェアしていただけると嬉しいです。コメントもお待ちしています!
参照
- TextWorld: A learning environment for training reinforcement learning agents, inspired by text-based games - Microsoft Research
- GitHub - Microsoft/TextWorld: TextWorld is a sandbox learning environment for the training and evaluation of reinforcement learning (RL) agents on text-based games.
- コロッサル・ケーブ・アドベンチャー
- 機械学習モデルとアルゴリズム | AWS での Amazon SageMaker