[ad_1]

How can we write a program that plays chess? How can we use machine learning to play chess?

Let’s begin by breaking down the process of playing chess:

- The first challenge is in identifying all possible legal moves from any board position. Legal moves are those moves allowed by the rules of chess.

For example, at the very start of the game, let’s examine all possible moves. Each pawn can move 1 or 2 squares forward, which means 16 possible moves for a total of 8 pawns. Then we have 2 knights, which can make 2 moves each. That makes a total of 20 possible moves for starting the game. Let’s call the set of possible moves allowed by rules of chess as legal moves. Note that as the game progresses, the bishops, queen, rooks, kings can all be more active and the possibilities increase.

2. The second step involves choosing the best move from the set of legal moves.

An experienced chess player would have an intuition for what’s a good move, based on experience. How do we use the experience of a chess player in developing our chess program? The answer is machine learning. My hypothesis is that if we can construct a dataset which contains several board positions and their scores, we can train a model to score a position based on how favorable it is for white/ black. Once we have this score prediction model, we can apply this at every turn to score resultant positions after legal moves and then select the best move to be played.

Where do we get such data? One of the earliest chess engines, DeepBlue was built mostly with inputs from Chess grandmasters. A chess engine has a component called ‘Evaluation function’ that looks at a particular position and estimates the strength of the position. It provides an evaluation score that indicates which player has an advantage at a position, and by how much.

My dataset was prepared from pgn files, which consist of sequences of moves played in a game. I took about 900 games to create a dataset of 100,000 positions.

Each position in a chess game is converted into a row of piece values in the data frame. White pieces are denoted with a positive sign and black with negative.

So, the starting board position, as seen here, is transformed into the numbers -5, -3, -3, -9, -4… denoting the black Rook, Knight, Bishop, Queen, King and so on respectively starting from square A8 all the way up to H1.

From this data frame, we can construct a regression model for the score, using the squares as features. However, we must first check if there is a linear association between the predictor variables & the outcome variable of evaluation score.

Regression stats for chess squares — p-values:

Regression stats for modified model:

[ad_2]

Source link