Chess
Chess
INTRODUCTION TO ARTIFICIAL
INTELLIGENCE
Topic: Chess with AI
Students & IDs:
1
1. Introduction ........................................................................................... 1
1.1. Problem Statement ......................................................................................... 1
3. Evaluation ............................................................................................ 19
3.1. Environment................................................................................................. 19
3.4. Testing.......................................................................................................... 20
4. Conclusion ............................................................................................ 21
References ................................................................................................ 23
1. Introduction
1.1. Problem Statement
Chess is a timeless and universally recognized board game that combines
strategy, foresight, and creativity. With its origins tracing back over a
CAPSTONE PROJECT REPORT – Chess with AI
millennium, the game has evolved into one of the most studied and competitive
activities worldwide. The enduring appeal of chess lies in its blend of simple
rules and complex decision-making, offering an unparalleled platform for
intellectual challenge.
From an artificial intelligence (AI) perspective, chess has long been considered
the ultimate "thinking" game, making it a benchmark for evaluating and
advancing AI technologies. The game's complexity arises from its vast search
space; there are approximately 10120 possible positions in chess, far exceeding
the number of atoms in the observable universe. This combinatorial explosion
makes it impractical to solve chess using brute force alone, requiring
sophisticated algorithms and heuristics to guide decision-making.
Historically, chess has played a pivotal role in the development of AI. A
landmark moment was the 1997 match between IBM's Deep Blue and world
chess champion Garry Kasparov, where Deep Blue became the first computer
system to defeat a reigning champion in a full match under standard time
controls. This achievement demonstrated the power of computational
advancements and ushered in a new era of AI research.
Today, chess remains a fertile ground for innovation in AI. Modern chess
engines like Stockfish and AlphaZero utilize advanced techniques such as
neural networks and reinforcement learning, setting new standards for AI
capabilities. Beyond competitive play, chess AI serves as a valuable tool for
education, analysis, and entertainment, showcasing its multifaceted importance
in both technological and societal contexts.
1.2. Rule of chess
2
CAPSTONE PROJECT REPORT – Chess with AI
Each player has 16 pieces to control with 6-piece types: King, Queen, Rook,
Bishop, Knight and Pawn.
Each game begins with the white side making the first move, after which
each side takes turn to move their pieces. The game ends once the king on one
side is checkmated, a state where the King is attacked by the opponent’s
piece(s) and has no escape move. A game can be proposed to be a draw if there
are no more possible series of moves for a side to claim victory, or if both
players make 50 consecutive moves without capturing any pieces or moving any
pawns.
Each piece (or piece type) on the chess board has different moving patterns:
3
CAPSTONE PROJECT REPORT – Chess with AI
- The Pawn only moves one square step forward and can move up to
two squares in its first move.
There are also some major rules such as castling (moving the King to a
safer position and the Rook to an available-for-attacking position), en passant (a
special move of the Pawn to take another piece), etc.
1.3. Description
Our group aims to create a chess engine that integrates Monte Carlo Tree
Search (MCTS) with a deep neural network inspired by AlphaZero. The engine
is capable of evaluating board states, predicting optimal moves, and improving
its performance by learning from self-play. Unlike traditional rule-based
engines, this approach combines strategic search with data-driven insights,
leveraging reinforcement learning for continuous improvement. As a result, we
hope the engine will be able to play against humans and other engines at a
certain competitive level.
4
CAPSTONE PROJECT REPORT – Chess with AI
The chess environment can be classified as:
• Fully observable: All pieces and their positions are visible at all times.
• Deterministic: There is no randomness in move outcomes; the next state is
entirely predictable.
• Sequential: Each move builds on the previous state.
• Semi-dynamic: The environment changes only with player actions (no time
progression without moves).
• Discrete: The game of chess has only a finite number of moves. The number
of moves might vary with every game, but still, it’s finite.
• Competitive multi-agent: Two opposing agents interact, aiming to achieve
mutually exclusive goals (winning or losing).
5
CAPSTONE PROJECT REPORT – Chess with AI
Figure 3: Our chessboard
6
CAPSTONE PROJECT REPORT – Chess with AI
7
CAPSTONE PROJECT REPORT – Chess with AI
• 64–72: Pawn underpromotion moves (excluding promotion to queen).
To simplify processing, the policy is transformed into a 1D array of 4672
elements, where each index corresponds to a unique action on the board.
Details of V (Value or Reward)
The value represents the expected outcome of the game from the perspective of the
current player:
• +1: Indicates a win.
• 0: Indicates a draw.
• -1: Indicates a loss.
2.2. CNN Architecture
Purpose of using CNN: Using generated game data which contains board
states, move probabilities - policy , game results – value (-1, 0, 1), create a new
CNN model that can predict move probabilities and game results from board states.
2.1.1. Layers of the architecture
Convolutional layer: Essential for learning local features from a chessboard
(grid-like data). By applying filters of size 3x3, the model can extract local features
that are essential for understanding the position and making decisions about
potential moves.
Fully connected layer: Used in the final stages of the network to make
predictions based on the learned features.
The fully connected layer consists of neurons, where each neuron is connected to
every neuron in the previous layer. This means that the output of each neuron in
8
CAPSTONE PROJECT REPORT – Chess with AI
the fully connected layer is influenced by every feature learned by the previous
layer.
With this structure, the fully connected layers can take the learned local
features from previous layers and combine them to form a global representation of
the input, which is needed to make the final prediction.
9
CAPSTONE PROJECT REPORT – Chess with AI
For a layer of the network with d-dimensional input, ,
each dimension of its input is then normalized (i.e. re-centered and re-scaled)
separately,
• ReLU (Rectified Linear Unit): Used after convolutional layers and residual
blocks as it is computationally efficient and help reduce the risk of vanishing
gradient compared to other activation functions.
10
CAPSTONE PROJECT REPORT – Chess with AI
• Tanh: An activation function used in value head that squashes input values
to a range between -1 and 1.
11
CAPSTONE PROJECT REPORT – Chess with AI
Residual connection: A feature of deep neural networks where the output
of a layer (or set of layers) is added directly to the input of those layers.
Residual connection helps the model efficiently learn patterns in the chessboard
state without losing critical information as the network gets deeper.
12
CAPSTONE PROJECT REPORT – Chess with AI
The convolutional layer applies 256 filters to the input tensor, transforming
it into a higher-dimensional feature map of size (batch_size, 256, 8, 8), enables the
network to learn and represent a broader range of features.The Batch
Normalization layer after that normalizes the output from the convolutional layer.
The ReLU activation layer introduces non-linearity into the model.
2.3. Decision-making
We want our chess agent to be able to decide the next move of the current
input game board. To improve performance and accuracy of chess agent, we use
MCTS algorithms combined with the UCB evaluation function to search for the
best move.
We consider each node of the tree with these attributes: game_state,
node_prior, node_value, node_visited. A node is considered expanded if its child
nodes haven’t been visited yet.
A tree starts with a root node, which is currently state, and continuously explores
its child nodes to expand the tree. Tree expansion process can be divided into 4
stages:
• Selection: Start at the root node and successively select a child node until
reaching a node that is unexpanded. When selecting child node, we apply
UCB evaluation function to ensure that there is balance between exploring
the tree and delving into potential branches. Child node is selected which
maximizes this evaluation function:
14
CAPSTONE PROJECT REPORT – Chess with AI
• Simulation: From unexpanded node above, we ask our CNN model to return
policy and estimated value of the current board state. This is slightly
different from the original MCTS but keeps the same idea.
• Expansion: If the current node is not one move away terminate, we expand
this node by updating node_prior base on the returned policy to all its valid
child node. Especially, if expansion node is root node, we apply Dirichlet
noise to the policy before updating its valid child nodes’s node_prior, which
is an effort to ensure all moves may be tried, but the search may still
overrule bad moves.
Here is how we apply Dirichlet noise to children of root node:
15
CAPSTONE PROJECT REPORT – Chess with AI
.
All game states, along with their policies and termination values, will be
stored in a file to create a dataset for training purposes. This dataset will be used to
train and improve the performance of our CNN model.
2.5. Training
a) Some basic idea
Convolutional Neural Networks (CNNs) are structured such that, at each
layer, the input data is analyzed and decomposed into various features through
filters. These filters facilitate the extraction of significant characteristics from the
input, enabling the model to effectively learn and recognize patterns. These filter’s
weights and some other weights serve as hyperparameters that require tuning to
enhance the model's performance. The learning process involves the model
adjusting its weights to better fit the training dataset, thereby improving its
accuracy and effectiveness.
We divide our training data into many batches, each containing 30 data. We
rearrange training set again for each epoch. This is to ensure that the model not
only focuses on learning from a specific data but also performs well when faced
16
CAPSTONE PROJECT REPORT – Chess with AI
with new and unseen data.
Loss function
For tuning the model’s hyperparameters, we utilize the Adam optimizer
model to optimize the gradient descent technique. This process involves
backpropagation, where we compute the partial derivatives of the loss function
with respect to each hyperparameter. The Adam method is then employed to
determine the step size for each hyperparameter adjustment.
Backpropagation algorithm to find partial derivatives of the loss function for each
weight:
17
CAPSTONE PROJECT REPORT – Chess with AI
b) Workflow
For each epoch, we rearrange training data to the data batches. After each
18
CAPSTONE PROJECT REPORT – Chess with AI
100 epochs were run, the learning rate in Adam optimizer will decrease 5 times.
For each data batch, we let our model run all datasets in batch. We then
calculate the loss function and perform backpropagation to determine the partial
derivatives of the loss function with respect to each hyperparameter. We reset
Adam optimizer model and let it tunning our model’s hyperparameter. After
processing all batches, we proceed to the next epoch.
After finishing enough epochs, we save all important attributes of the current
model, which can be retrieved using the model.state_dict() and store it in a file for
reusing.
3. Evaluation
3.1. Environment
The file evaluator.py primarily serves as an evaluation environment for a
chess bot to compare the trained bot to find the best net or export the result of
matches.
It creates an "arena" where two versions of the chess bot (the current and
best) compete in simulated games. Outcomes of these games are recorded,
including board states, moves, and game results (e.g., wins, losses, or draws).
After each evaluation, we trained the model again with the evaluator data +
the already available data to optimize it.
3.4. Testing
To test the performance of our chess-playing agent, we have carried out
games for the agent against human players, mostly us, and against some well-
known chess bots, namely Leela Chess Zero based on AlphaGo Zero and Stockfish
on lichess.org.
20
CAPSTONE PROJECT REPORT – Chess with AI
Result against Leela Chess Zero AlphaGo Zero
Wins 0 0
Loses 15 15
Draws 0 0
c, Playing against Human
After playing 15 matches with us and 15 online matches on chess.com at an
Elo rating of 1200, we obtained the following results:
Therefore, we can estimate that our chess engine stands in the range of 900
to 1200 rating on Lichess.org, which is equivalent to the level of a decent chess
player, not too good to compete professionally but not an amateur.
4. Conclusion
It is certain that our chess program is not flawless and would require more
improvements to become a good chess engine. Throughout the making of the
chess-playing agent, we have conducted some observations and made comments
on some difficulties and how we can surmount them to further increase the
performance of our program:
• Chess is played with more than just simple calculations of a board
state; yet, of course, the “game sense” of a chess player is something
that cannot be depicted by just a function or a searching algorithm, so
the best we can do to make our agent act more human-like is through
improving evaluating methods and training with larger datasets.
21
CAPSTONE PROJECT REPORT – Chess with AI
• Although the basic rules of chess should be universal knowledge, it
takes a long road to become a good chess player and, therefore, our
view on how to evaluate a chessboard can be limited. Our model is
still primal and there is still room for improvement. .
• Despite the difficulties, this project allowed us to have hands-on
experiences on how to create an intelligent agent using knowledge
from the course. These experiences will be valuable to us as we
proceed to following courses in the future, allowing us to approach
these courses more easily and have deeper knowledge in the field of
Artificial Intelligence.
22
CAPSTONE PROJECT REPORT – Chess with AI
References
23