Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Re-allocate tree when tree is full #109

Open
wants to merge 8 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ TamaGo's command line options are as follows,
| Option | Description | Value | Example of value | Default value | Note |
| --- | --- | --- | --- | --- | --- |
| `--size` | Size of go board | Integer number more than 1 and less than or equal to BOARD_SIZE | 9 | BOARD_SIZE | BOARD_SIZE is defined in board/constant.py.
| `--superko` | Activation super-ko rule | true or false | true | true | It supports only positional super-ko. |
| `--superko` | Activation super-ko rule | true or false | true | false | It supports only positional super-ko. |
| `--model` | Path to a trained neural network parameters file | String of model file path | model/model.bin| None | It must be relative path from TamaGo's home directory |
| `--use-gpu` | Flag to use a GPU | true or false | true | false | |
| `--policy-move` | Flag to move according to Policy distribution | true or false | true | false | |
Expand Down
62 changes: 43 additions & 19 deletions doc/cn/README.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
# TamaGo

* 此翻譯對應 v0.6.5
* 此翻譯對應 v0.9.2

TamaGo 是以一個用 python 撰寫的圍棋程式。它有以下的功能
TamaGo 是以一個用純 python 撰寫的圍棋程式。它有以下的功能

- 通過監督學習(Supervised Learning)學習 SGF 格式的棋譜。
- 使用 Gumbel AlphaZero 實做強化學習(Reinforcement Learning)。
- 使用神經網路搭配蒙地卡羅樹搜索
- 使用 Gumbel AlphaZero 實做強化學習(Reinforcement Learning)系統
- 使用神經網路搭配蒙地卡羅樹搜尋

TamaGo 需要 python 3.6 或更高的版本
TamaGo 需要 python 3.6 或更高的版本,但以下命令都簡稱為 python

* [需求](#需求)
* [安裝套件](#安裝套件)
Expand All @@ -20,21 +20,23 @@ TamaGo 需要 python 3.6 或更高的版本
# 需求
| 套件 | 用途 |
|---|---|
| click | 支援內建選項 |
| click | 支援內建選項功能 |
| numpy | 用於計算和其它 |
| pytorch | 構成神經網路和學習 |

# 安裝套件
可通過 Python 輸入下列指令直接安裝套件。

可通過 Python 輸入下列指令直接安裝依賴的套件。
```
pip install -r requirements.txt
```

你可以僅使用 CPU 執行 TamaGo,但推薦在有 GPU 機器上執行之。
TamaGo 可以僅使用 CPU 執行,但推薦在有 GPU 機器上執行之,執行神經網路運算會快數倍。


# 如何啟用 GTP 引擎

TamaGo 支援 GTP 協議的界面(GoGui, Sabaki, Lizzie 等),使用下列指令即可啟動之
TamaGo 支援 GTP 協議的界面軟體(GoGui, Sabaki, Lizzie 等),使用下列指令即可啟動之

```
python main.py
Expand All @@ -45,48 +47,55 @@ TamaGo 支援以下的指令
| 選項 | 描述 | 設定值 | 預設值 | 設定範例 | 備註 |
|---|---|---|---|---|---|
| `--size` | 棋盤大小 | 2 以上 BOARD_SIZE 以下 | 9 | BOARD_SIZE | BOARD_SIZE 定義在 board/constant.py|
| `--superko` | 是否使用禁全同規則 | true 或是 false | true | false | 只支援 positional superko |
| `--superko` | 是否使用禁全同規則 | true 或是 false | true | false | 僅支援 positional superko |
| `--model` | 使用的網路權重 | | model/model.bin | 無 | 指定的路徑必須要在 TamaGo 的路徑之下 |
| `--use-gpu` | 是否使用 GPU | true 或 false | true | false | |
| `--policy-move` | 是否只使用 Policy 網路下棋 | true 或 false | true | false | 主要是為了確認 Policy 網路的強度 |
| `--sequential-halving` | 數搜索時是否使用 Sequential Halving | true 或 false | true | false | 主要是為了 debug |
| `--sequential-halving` | 搜尋時是否使用 Sequential Halving | true 或 false | true | false | 主要是為了 debug |
| `--visits` | 每手棋的訪問數 | 1 以上的整數 | 1000 | 1000 | 當使用 --const-time 或 --time 參數時,此選項會被忽略 |
| `--const-time` | 每手棋的思考時間 (秒) | 0 以上的任意數 | 10.0 | | 當使用 --time 參數時,此選項會被忽略 |
| `--time` | 總思考時間 (秒) | 0 以上的任意數 | 600.0 | |
| `--batch-size` | MCTS 搜索使用的 batch 大小 | 大於零的整數 | 13 | NN_BATCH_SIZE | NN_BATCH_SIZE 定義在 mcts/constant.py |
| `--batch-size` | 搜尋使用的 batch 大小 | 大於零的整數 | 1 | NN_BATCH_SIZE | NN_BATCH_SIZE 定義在 mcts/constant.py |
| `--tree-size` | MCTS 的節點上限 | 大於 0 的整數 | 65536 | MCTS_TREE_SIZE | MCTS_TREE_SIZE 定義在 mcts/constant.py. |
| `--cgos-mode` | 嘗試提光所有死棋 | true or false | true | false | |

## 執行範例

1) 設定棋盤大小為 5,使用的權重為 model/model.bin ,禁止使用 GPU
1) 設定棋盤大小為 5,使用的權重為 model/model.bin ,禁止使用 GPU
```
python main.py --size 5 --model model/model.bin --use-gpu false
```

2) 啟用禁全同規則
2) 啟用禁全同規則
```
python main.py --superko true
```

3) 使用的權重為 model/model.bin,使用 policy 網路產生下一手
3) 使用的權重為 model/model.bin,使用 policy 網路產生下一手
```
python main.py --model model/sl-model.bin --policy-move true
```

4) 設定總思考時間為 10 分鐘
4) 設定總思考時間為 10 分鐘
```
python main.py --time 600
```

5) 設定每手的訪問數為 500
5) 設定每手的訪問數為 500
```
python main.py --visits 500
```

6) 設定每手的思考時間為 10 秒
6) 設定每手的思考時間為 10 秒
```
python main.py --const-time 10.0
```

7) 運行在 CGOS 上
```
python main.py --model model/sl-model.bin --use-gpu true --cgos-mode true --superko true --batch-size 13 --time 600 --komi 7 --tree-size 200000
```

## 使用預先訓練好的權重

你可以從[這裡](https://github.com/kobanium/TamaGo/releases)下載預先訓練好的權重,將權重改名成 ```model.bin``` 後放置在 ```model``` 路徑之下即可使用。請注意每不同版本的網路結構可能不一樣,不同版本的 TamaGo 可能需要對應對不同版本的權重。
Expand All @@ -101,7 +110,7 @@ python main.py --const-time 10.0
# 如何使用強化學習
強化學習教程請看[這裡](reinforcement_learning.md)。

# GoGui analyze commands
# GoGui 分析指令
[GoGui](https://sourceforge.net/projects/gogui/) 可以用顏色顯示 policy 網路輸出的分佈,或是用數值直接顯示。

![Display policy value](../../img/gogui_analyze_policy.png)
Expand All @@ -110,5 +119,20 @@ python main.py --const-time 10.0

![Coloring policy value](../../img/gogui_analyze_policy_color.png)

# Analyze 指令

自 0.7.0 版本後,TamaGo 支援 lz-analyze, lz-genmove_analyze 分析指令,可以在 Sabaki 或 Lizzie 上使用。

![lz-analyze-sample](../../img/lz_analyze_sample.png)

# CGOS 分析模式

自 0.7.0 版本後,TamaGo 支援 cgos-analyze, cgos-genmove_analyze 分析指令,可以在 [Computer Go Server (CGOS)](http://www.yss-aya.com/cgos/) 上顯示分析結果。由於 CGOS 使用的規則需要提光所有死棋才能正確計地,因此建議 --cgos-mode 選項。

![cgos-analyze](../../img/cgos-analyze.png)

![cgos-analyze-pv](../../img/cgos-analyze-pv.png)


# 授權條款
TamaGo 使用 [Apache License 2.0](LICENSE) 授權。
4 changes: 3 additions & 1 deletion doc/cn/reinforcement_learning.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ apt install gnugo
| [nn/network/head/policy_head.py](../../nn/network/head/policy_head.py) | 定義 Policy Head |
| [nn/network/head/value_head.py](../../nn/network/head/value_head.py) | 定義 Value Head |

如果你想改進網路結構,建議可以先簡單的提昇 filter 和 block 數目,它們定義在 dual_net.py 裡

# TamaGo 的強化學習步驟

請通過以下順序執行 TamaGo 的強化學習
Expand Down Expand Up @@ -68,7 +70,7 @@ apt install gnugo
| 選項 | 描述 | 設定範例 | 預設值 | 備註 |
| --- | --- | --- | --- | --- |
| `--kifu-dir` | 訓練時使用的棋譜的路徑 | /home/user/sgf_files | None | |
| `--size` | 棋盤大小 | 5 | 9 | |
| `--size` | 棋盤大小 | 9 | 9 | |
| `--use-gpu` | 是否使用 GPU | true | true | true 或是 false. |
| `--rl` | 是否為強化學習 | false | false | |
| `--window-size` | 學習時使用的資料數目(最新的優先讀取) | 500000 | 300000 | |
Expand Down
4 changes: 3 additions & 1 deletion doc/cn/supervised_learning.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ TamaGo 可以使用 SGF 格式的棋譜作為訓練資料,關於 SGF 的更多
| [nn/network/head/policy_head.py](../../nn/network/head/policy_head.py) | 定義 Policy Head |
| [nn/network/head/value_head.py](../../nn/network/head/value_head.py) | 定義 Value Head |

如果你想改進網路結構,建議可以先簡單的提昇 filter 和 block 數目,它們定義在 dual_net.py 裡

# TamaGo 的監督學習步驟

請通過以下順序執行 TamaGo 的監督學習
Expand Down Expand Up @@ -80,7 +82,7 @@ python train.py
| 選項 | 描述 | 設定範例 | 預設值 | 備註 |
| --- | --- | --- | --- | --- |
| `--kifu-dir` | 訓練時使用的棋譜的路徑 | /home/user/sgf_files | None | |
| `--size` | 棋盤大小 | 5 | 9 | |
| `--size` | 棋盤大小 | 9 | 9 | |
| `--use-gpu` | 是否使用 GPU | true | true | Value is true or false. |
| `--rl` | 是否為強化學習 | false | false | |
| `--window-size` | 學習時使用的資料數目(最新的優先讀取) | 500000 | 300000 | |
4 changes: 4 additions & 0 deletions mcts/tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,10 @@ def expand_node(self, board: GoBoard, color: Stone) -> NoReturn:
color (Stone): 現在の手番の色。
"""
node_index = self.num_nodes
tree_size = len(self.node)
if node_index >= tree_size:
self.node.extend([MCTSNode() for i in range(tree_size)])
sys.stderr.write(f"Tree is full. Allocate new space {tree_size} -> {len(self.node)}\n")

candidates = board.get_all_legal_pos(color)
candidates = [candidate for candidate in candidates \
Expand Down