MacでAlpha ZeroライクのLeela Chess Zero (lc0)というチェスエンジンを動かして、lichessで対戦してみた。初期だと弱っちだが、指し方がなかなか可愛くて初心者っぽかった。
前提
- macOS Mojaveで試しています
- Home Brewをインストールしておくこと
Leela Chessのビルド
チェスエンジンは Leela Chess の lc0 を利用。Macだとバイナリの配布がないので、自分でビルド。
$ git clone -b release --recurse-submodules \
https://github.com/LeelaChessZero/lc0.git
$ cd lc0
$ brew install python3 meson ninja
$ ./build.sh # ビルド
$ ./build/release/lc0 # 軽く動作確認
v0.19.0-rc4 built Nov 14 2018
Ctr^C で終了
http://lczero.org/networks から使いたいニューラルネットワークの学習済み重みデータをダウンロード解凍し、latest.txt
というファイル名にする。
Lichess Botの準備
これは、チェスエンジン(後述のLeelaやStockfish等)を対戦の場 Lichess のAPIに繋げてくれるエージェント。lichess-bot/READMEに従うが、要約すると
$ git clone https://github.com/careless25/lichess-bot.git
$ cd lichess-bot
$ pip3 install virtualenv
$ virtualenv .venv -p python3
$ source .venv/bin/activate
$ pip3 install -r requirements.txt
$ cp config.yml.default config.yml
先にビルドしたLeela Chessのバイナリ./build/release/lc0
及び重み
latest.txt
を lichess-botのengines
フォルダにコピー。その上 config.yml を適切に変更。
$ diff -b config.yml*
1c1
< token: "取得したトークン" # lichess OAuth2 Token
---
> token: "xxxxxxxxxxxxxxxx" # lichess OAuth2 Token
6,7c6
< name: "lczero" # binary name of the engine to use
---
> name: "engine_name" # binary name of the engine to use
11,12c10,11
< lczero: # lczero specific settings
< weights: "engines/latest.txt" # weights file path
---
> # lczero: # lczero specific settings
> # weights: "engines/latest.txt" # weights file path
46c45
< abort_time: 120 # time to abort a game in seconds when there is no activity
---
> abort_time: 20 # time to abort a game in seconds when there is no activity
54,55c53,54
実行して対戦してみる
$ source .venv/bin/activate
$ python3 lichess-bot.py
次のようなログが出力される
2018-11-14 16:59:08,208: . _/|
. // o\
. || ._) lichess-bot 1.1.0
. //__\
. )___( Play on Lichess with a bot
2018-11-14 16:59:09,844: Welcome aifun!
2018-11-14 16:59:09,844: You're now connected to https://lichess.org/ and awaiting challenges.
2018-11-14 16:59:11,138: --- Process Used. Total Queued: 0. Total Used: 1
2018-11-14 16:59:11,140: --- Process Used. Total Queued: 0. Total Used: 2
_
| _ | |
|_ |_ |_| v0.19.0-rc4 built Nov 14 2018
Found pb network file: ./engines/latest.txt
Creating backend [opencl]...
OpenCL, maximum batch size set to 16.
Initializing OpenCL.
Detected 1 OpenCL platforms.
Platform version: OpenCL 1.2 (Oct 11 2018 21:04:03)
Platform profile: FULL_PROFILE
Platform name: Apple
Platform vendor: Apple
Device ID: 0
Device name: Intel(R) Core(TM) i5-5257U CPU @ 2.70GHz
Device type: CPU
Device vendor: Intel
Device driver: 1.1
Device speed: 2700 MHZ
Device cores: 4 CU
Device score: 512
Device ID: 1
Device name: Intel(R) Iris(TM) Graphics 6100
Device type: GPU
Device vendor: Intel Inc.
Device driver: 1.2(Oct 11 2018 20:55:57)
Device speed: 1050 MHZ
Device cores: 48 CU
Device score: 612
Selected platform: Apple
Selected device: Intel(R) Iris(TM) Graphics 6100
with OpenCL 1.2 capability.
Loaded existing SGEMM tuning for batch size 16.
Wavefront/Warp size: 8
Max workgroup size: 256
Max workgroup dimensions: 256 256 256
これで、lichessのクライアントアプリ(iOS, Android or Web)で起動した lichess bot と対戦できるようになる。
初期の状態だと弱っちだが、育ってくることを期待する。
問題点
- Leelaが結構パワーを必要としているようで、対戦中にMacがフリーズしたり、再起動したりするので、重要なデータは保存しておくこと!
- ネットワーク通信状態が悪い時に、Leelaがゲームを放棄しないように、configの
abort_time
を調整
この後
- LeelaChessZero/lc0/wiki/Google-Cloud-guide-(lc0)を見て、GCPでlc0を動かしてみる
- GCPでlc0/lichess-botを動かして、lichessで対戦してみる
- LeelaZero (Go) もやってみるかな