seigot / tetris

A Tetris Game for programming education in Japanese
MIT License
24 stars 105 forks source link

強化学習要素の追加 #8

Open seigot opened 2 years ago

seigot commented 2 years ago

強化学習要素の追加をしたい

seigot commented 2 years ago

https://github.com/seigot/tetris/issues/4

seigot commented 2 years ago

neteru-sanの強化学習は以下で動いた。 ただし移植が必要である。

    ## prediction
    git clone http://github.com/neteru141/tetris_game -b v1.1.0
    cd tetris_game
    bash start.sh -l2

    ## train
    git clone http://github.com/neteru141/tetris_game -b dev4
    cd tetris_game
    bash start.sh -l2
    ### その後、しばらく20分くらい放置しておく
    ### train/tetris が学習モデル

    ## 以下のようにするとtrainしたモデルが動く
    git clone http://github.com/neteru141/tetris_game -b submit
    cd tetris_game
    bash start.sh -l2

pip3 install -r requirements.txtする。requirements.txtの中身は以下の通り。

pyqt5
numpy
pillow
matplotlib
torch
tensorboardX

パッチはこれをベースにするつもり https://github.com/seigot/tools/tree/master/tetris/neteru141

seigot commented 2 years ago

参考サイト https://github.com/uvipen/Tetris-deep-Q-learning-pytorch

以下のようにすると学習が動く python train.py python test.py

seigot commented 2 years ago

推論時にネットワークモデル(deep_q_network.py)がどこにあるかでエラーが出る事がある 学習時と推論時に、deep_q_network.pyのパスに違いがあると生じる事がある。

$ python start.py -l2 -mpredict_sample -d100 -t18 --ShapeListMax 6
Traceback (most recent call last):
  File "game_manager/game_manager.py", line 237, in timerEvent
    self.nextMove = BLOCK_CONTROLLER_TRAIN_SAMPLE.GetNextMove(nextMove, GameStatus)
  File "/Users/seigo/Desktop/python/tetris/game_manager/machine_learning/block_controller_train_sample.py", line 334, in GetNextMove
    self.model = torch.load("{}/tetris".format(self.saved_path), map_location=lambda storage, loc: storage)
  File "/anaconda3/lib/python3.7/site-packages/torch/serialization.py", line 607, in load
    return _load(opened_zipfile, map_location, pickle_module, **pickle_load_args)
  File "/anaconda3/lib/python3.7/site-packages/torch/serialization.py", line 882, in _load
    result = unpickler.load()
  File "/anaconda3/lib/python3.7/site-packages/torch/serialization.py", line 875, in find_class
    return super().find_class(mod_name, name)
ModuleNotFoundError: No module named 'deep_q_network'
error: subprocess failed.

例えば以下のようにシンボリックリンクを付けてやると統一が図れるかもしれない

cd game_manager
ln -s machine_learning/deep_q_network.py deep_q_network.py
seigot commented 2 years ago

neteru-sanの強化学習、移植版の使いかた

リポジトリを取得してパッチを当てる もし開発環境を分けたい場合はvenvを使ってもいいかも( https://qiita.com/seigot/items/256e04d326dc009313f2

以下は旧手順

# リポジトリを取得
git clone https://github.com/seigot/tetris
cd tetris

# pip install
curl -LJO https://raw.githubusercontent.com/seigot/tools/master/tetris/ai_tmp/requirements_machine_learning.txt
pip3 install -r requirements_machine_learning.txt

# 機械学習用のファイルをダウンロードして、game_managerの下におく
curl -LJO -H 'Accept: application/octet-stream' https://github.com/seigot/tools/releases/download/20220125_sample/machine_learning.zip
unzip machine_learning.zip
mv machine_learning game_manager/.

# 機械学習用のconfigを有効化
curl -LJO https://raw.githubusercontent.com/seigot/tools/master/tetris/20220125_enable_train_sample.patch
patch -p1 < 20220125_enable_train_sample.patch

以下が最新

# リポジトリを取得
git clone https://github.com/seigot/tetris
cd tetris

# pip install
curl -LJO https://raw.githubusercontent.com/seigot/tools/master/tetris/ai_tmp/requirements_machine_learning.txt
pip3 install -r requirements_machine_learning.txt

# 機械学習用のファイルをダウンロードして、game_managerの下におく
curl -LJO https://raw.githubusercontent.com/seigot/tools/master/tetris/ai_tmp/game_manager/machine_learning/block_controller_train_sample.py
mv block_controller_train_sample.py game_manager/machine_learning/.

curl -LJO https://raw.githubusercontent.com/seigot/tools/master/tetris/ai_tmp/game_manager/machine_learning/deep_q_network.py
mv deep_q_network.py game_manager/machine_learning/.

学習

$ python start.py -l2 -m train_sample -t -1 -d 1
## 3hほど放っておく

推論

$ python start.py -l2 -m predict_sample
or
$ python start.py -l2 -m predict_sample -d 100 -t 18

推論(学習済の学習モデルを利用する場合)

mkdir trained_models
curl -LJO https://github.com/seigot/tools/raw/master/tetris/ai_tmp/trained_models/tetris
cp tetris trained_models/.
python start.py -l2 -m predict_sample

中身の整理などはこれから

pip install hydra-core
pip install omegaconf