grandmaster level chess AI using python - Part 2 (the code)
In continuation of part 1, I will now building my chess AI in python. As mentioned before, my goal is to create a chess AI at the Grandmaster level (elo > 2500)
Here is the code for the chess AI.
https://github.com/luweizhang/chess_ai
Key components of the chess game / AI:
Explanation of the tree generator:
The tree generator component of the chess AI generates the possible future board positions based on the possible moves that the pieces can make. At any given position, there are on average 20 possible moves that can be made in chess.
The above illustrates a chess tree. Possible moves are generated from the current position to create hypothetical future states.
Explanation of the evaluation function
I would say that this is the intuitive component of the chess AI. Given an 8x8 chess position, the evaluation function with determine between a move is good or not by assigning it a numerical score. A negative score means that black is winning, a positive score means that white is winning.
This component of the AI is the most important because it is the one that can be optimized.
Explanation of the minimax algorithm:
The minimax algorithm essentially tries to find the optimal move from a tree of moves. Starting from the leaves of the tree, you want to backwards calculate back to the optimal move by alternating between min() and max() (hence the name, minimax).
When it is your turn, you want to optimize for your outcome, therefore you want to calculate the max() position score.
When it is your opponents turn, he wants to minimize your outcome, therefore it calculates the min() position score.
The guide below explains minimax very well:
http://web.cs.ucla.edu/~rosen/161/notes/minimax.html
Here is the code for the chess AI.
https://github.com/luweizhang/chess_ai
Key components of the chess game / AI:
- Python Classes:
- ChessGame
- instantiates the chessboard and pieces
- keeps track of whose turn it is
- RulesEnforcer
- Logic for how all the pieces move, castling
- Makes sure you cant make illegal moves
- Used in the chess AI to generation possible positions
- ChessAi
- Tree generator
- generates the tree of position moves
- Position Evaluator
- evaluates the quality of chess positions
- Minimax
- Determines the optimum move from the chess tree
- ChessDB
- Opening database
Explanation of the tree generator:
The tree generator component of the chess AI generates the possible future board positions based on the possible moves that the pieces can make. At any given position, there are on average 20 possible moves that can be made in chess.
The above illustrates a chess tree. Possible moves are generated from the current position to create hypothetical future states.
Explanation of the evaluation function
I would say that this is the intuitive component of the chess AI. Given an 8x8 chess position, the evaluation function with determine between a move is good or not by assigning it a numerical score. A negative score means that black is winning, a positive score means that white is winning.
This component of the AI is the most important because it is the one that can be optimized.
Explanation of the minimax algorithm:
The minimax algorithm essentially tries to find the optimal move from a tree of moves. Starting from the leaves of the tree, you want to backwards calculate back to the optimal move by alternating between min() and max() (hence the name, minimax).
When it is your turn, you want to optimize for your outcome, therefore you want to calculate the max() position score.
When it is your opponents turn, he wants to minimize your outcome, therefore it calculates the min() position score.
The guide below explains minimax very well:
http://web.cs.ucla.edu/~rosen/161/notes/minimax.html
Comments
Post a Comment