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
__________________________________________________________________________________________________