Secrets of Stockfish: The Ultimate Open Source Chess AI

Robert Keith Rippetoe
5 min readJan 8, 2024

--

Stockfish is the most powerful chess engine to ever exist. Although computers have been able to beat the best Chess players on earth ever since the infamous duel between Garry Kasparov and IBM’s Deep Blue in 1996, the gap between computers and their human competitors has only grown since. Using new computational techniques and innovative strategies, Stockfish has shown how computers continue to improve even when the only competition they face is themselves.

Just How Good is Stockfish?

To visualize how chess players stack up against one another, mechanized or otherwise, Chess uses a system called Elo. Named after the Hungarian-American physics professor Arpad Elo, each player is given an Elo score that predicts the odds of winning against a player of another score. If a player wins, they will gain a number of points corresponding to the ranking of the score of their opponent. Correspondingly, losing a game will lower your Elo and vary in the same manner. Using an Elo calculator, you yourself can see the odds of one competitor beating another using just their Elo scores.

There is no such thing as an average Chess player, but a new player commonly ranks between 800–1200 Elo. A club Chess player may rank around 1500–1700 Elo, and a grandmaster can exceed over 2000. Magnus Carlsen, the current world champion of Chess, has an Elo of 2830. All of this builds up to the rating of Stockfish 16, the most recently released and powerful iteration yet, that boasts an Elo of over 3,500. Using a calculator, we can then estimate that Stockfish would beat the current world champion Magnus 98.12% of the time in a single match. These odds become even worse accounting for best-of-3 or best-of-5 formats. But what makes Stockfish so strong, even in comparison to other Chess engines?

How Magnus Would do Against Stockfish

The Evaluation Function

When Stockfish plays chess, it first checks if certain conditions are met that simplify the decisions it has to make in the future. These factors include things like the number of pieces remaining on the chessboard, or whether or not either player is in a state of check. Failing these conditions, Stockfish will then analyze the board and give rankings to different future positions on the board given the current state of the game. The rankings Stockfish gives to different positions is determined by going through multiple subsystems that change the overall value of the position depending on the type of subsystem being called. These subsystems then go into even smaller subsystems that calculate everything from the number of attacks a piece possesses to the file and rank of each passed pawn on the board. Interestingly, Stockfish possesses different subsystem evaluations for endgame and middle positions, and uses “phase” based on non-pawn material to craft a combined determination from both systems.

Minimax Algorithm and Alpha-Beta Pruning

The next step in the Stockfish game plan is to push forward and analyze future possible positions, just as it has done for the current game state. Although this is possible through simply applying the main evaluation function to future moves, what determines which positions Stockfish explores and how deep Stockfish explores one position before moving to the next?

First Stockfish determines which moves it and its opponent are most likely to take through the Minimax Algorithm, that in more simplified terms scores moves by the chance of the first player winning. It is assumed that the first player will always select the maximum value to maximize their chance of winning, and thus the second player will always choose the minimum to prevent the first player’s victory. Alpha-Beta Pruning is an improved kind of Minimax Algorithm that cuts off part of the “tree” of decisions based on two new values: Alpha and Beta. These values establish upper and lower bounds to the possible scores of future moves, and “cuts” all future possible nodes that have no chance of producing a more maximized or minimized value. Many other engines besides Stockfish use these same methods, but combining this together with the fearsome main evaluation function means that these scores are more accurately calculated than ever!

How I, at 1000 Elo, Would do Against Magnus

Chess is a Solved Game (Kind Of)

Once a Chess board is reduced to seven or less pieces, the game is unofficially over. Table bases, lists of moves from a starting position that contain the ending given the most optimal play possible, already solve every move imaginable. For this reason, Chess engines like Stockfish simply reference these tables when the game removes enough pieces instead of relying on its own calculations. Chess legends like Magnus Carlsen are made and broken over the power of their prowess in endgame positions, but they are simply inferior compared to what is essentially looking at a terabyte sized answer sheet to every test imaginable.

Sheer Processing Power

Stockfish calculates all these moves in real time, and is thus limited by how much processing power it can access in a traditional timed match. When Kasparov first challenged Deep Blue in 1996, it commanded a processing power inferior to that of even a standard desktop computer in the current age. Now with the advent of GPUs and even TPUs, the amount of moves that Stockfish can look into the future even without the previously discussed optimizations are immense. The truth is that even a relatively badly optimized Chess engine would still be able to take on the greatest player on Earth simply because of how many moves it is allowed to process in the same time span.

Games like Chess are where AI is truly allowed to shine because of its complex yet completely predictable nature. Now because of how powerful Chess engines have become they are trained against each other instead of actual players! Stockfish has outcompeted even other engines because of its intelligent design decisions built into its evaluation functions, which were largely made possible due to a robust open source community that allows different developers to collaborate together to make incremental improvements. We have yet to develop Artificial General Intelligence that can handle a broad variety of tasks like ourselves, but when it comes to very specific tasks like Chess, AI has already far exceeded us in many capabilities. If you have any questions or concerns, please feel free to reach out to me on Linkedin, or contact me at hello@rogueethic.com!

--

--

Robert Keith Rippetoe

Software Engineer with an emphasis on cloud infrastructure, devops, and site reliability.