chess ai
1.0.0
我玩國際象棋已經很長時間了,自從我開始玩CS以來,我一直想創建一個國際象棋機器人。我終於做到了?
這是一個視頻,其中我(白人)被我的機器人(黑人)壓垮了。
這是其 chess.com 簡介:https://www.chess.com/member/chessables_with_chat_gpt。
git clone https://github.com/samliu21/chess-ai
。使用cd chess-ai
導航到該目錄。python -m venv .
並使用source bin/activate
啟動它。python -m pip install -r requirements.txt
安裝必要的依賴項。cd gui
導航到正確的目錄並呼叫python main.py
來玩! 我使用了官方 Lichess 資料庫,其中包含標準 PGN 格式的遊戲。資料清洗過程如下:
pgn-extract
在每次移動後加入 FEN有關更多信息,請查看data_cleaning
資料夾。
最初,我嘗試創建一個板評估神經網路來與極小極大演算法配對。這種方法有兩個問題:
評估網路的表現沒有達到我的預期。它可以檢測材料不平衡,但無法檢測簡單的將死。
由於西洋棋中的動作空間很大,極小極大演算法非常慢,即使使用 alpha-beta 剪枝進行最佳化也是如此。
這些因素共同促使我放棄這個最初的想法並嘗試另一個想法。
GUI 是使用pygame
和python-chess
模組手工製作的。
這種架構很大程度上受到史丹佛大學這篇論文的啟發。
人工智慧使用兩種模型。它們都會接收棋盤位置作為輸入,並輸出一個8x8
的 softmax 機率矩陣。 「起始模型」預測要移出的方格,「目標模型」預測要移入的方格。
這種方法最好用一個例子來說明。考慮起始棋盤位置和移動: Nf3
。此移動的評估是 from 模型的g1
平方的值與 to 模型的f3
平方的值的乘積。
在所有合法的走法中,最大的乘積是選擇的走法。
神經網路由六個卷積層組成,後面是兩個仿射層和一個輸出層。更詳細的架構草圖如下:
Model: "model"
__________________________________________________________________________________________________
Layer (type) Output Shape Param # Connected to
==================================================================================================
input_1 (InputLayer) [(None, 8, 8, 12)] 0 []
conv2d (Conv2D) (None, 8, 8, 32) 3488 ['input_1[0][0]']
batch_normalization (BatchNorm (None, 8, 8, 32) 128 ['conv2d[0][0]']
alization)
activation (Activation) (None, 8, 8, 32) 0 ['batch_normalization[0][0]']
conv2d_1 (Conv2D) (None, 8, 8, 64) 18496 ['activation[0][0]']
batch_normalization_1 (BatchNo (None, 8, 8, 64) 256 ['conv2d_1[0][0]']
rmalization)
activation_1 (Activation) (None, 8, 8, 64) 0 ['batch_normalization_1[0][0]']
conv2d_2 (Conv2D) (None, 8, 8, 256) 147712 ['activation_1[0][0]']
batch_normalization_2 (BatchNo (None, 8, 8, 256) 1024 ['conv2d_2[0][0]']
rmalization)
activation_2 (Activation) (None, 8, 8, 256) 0 ['batch_normalization_2[0][0]']
concatenate (Concatenate) (None, 8, 8, 512) 0 ['activation_2[0][0]',
'activation_2[0][0]']
conv2d_3 (Conv2D) (None, 8, 8, 256) 1179904 ['concatenate[0][0]']
batch_normalization_3 (BatchNo (None, 8, 8, 256) 1024 ['conv2d_3[0][0]']
rmalization)
activation_3 (Activation) (None, 8, 8, 256) 0 ['batch_normalization_3[0][0]']
concatenate_1 (Concatenate) (None, 8, 8, 320) 0 ['activation_3[0][0]',
'activation_1[0][0]']
conv2d_4 (Conv2D) (None, 8, 8, 256) 737536 ['concatenate_1[0][0]']
batch_normalization_4 (BatchNo (None, 8, 8, 256) 1024 ['conv2d_4[0][0]']
rmalization)
activation_4 (Activation) (None, 8, 8, 256) 0 ['batch_normalization_4[0][0]']
concatenate_2 (Concatenate) (None, 8, 8, 288) 0 ['activation_4[0][0]',
'activation[0][0]']
conv2d_5 (Conv2D) (None, 8, 8, 256) 663808 ['concatenate_2[0][0]']
batch_normalization_5 (BatchNo (None, 8, 8, 256) 1024 ['conv2d_5[0][0]']
rmalization)
activation_5 (Activation) (None, 8, 8, 256) 0 ['batch_normalization_5[0][0]']
dense (Dense) (None, 8, 8, 256) 65792 ['activation_5[0][0]']
batch_normalization_6 (BatchNo (None, 8, 8, 256) 1024 ['dense[0][0]']
rmalization)
dense_1 (Dense) (None, 8, 8, 64) 16448 ['batch_normalization_6[0][0]']
batch_normalization_7 (BatchNo (None, 8, 8, 64) 256 ['dense_1[0][0]']
rmalization)
dense_2 (Dense) (None, 8, 8, 1) 65 ['batch_normalization_7[0][0]']
batch_normalization_8 (BatchNo (None, 8, 8, 1) 4 ['dense_2[0][0]']
rmalization)
softmax (Softmax) (None, 8, 8, 1) 0 ['batch_normalization_8[0][0]']
==================================================================================================
Total params: 2,839,013
Trainable params: 2,836,131
Non-trainable params: 2,882
__________________________________________________________________________________________________