SlideShare a Scribd company logo
1 of 25
Chess
Engine
Programming
BY ARNO HUETTER
About the Author
Arno Huetter
I wrote my first lines of code on a Sinclair ZX80
in 1984. My daytime job is at Dynatrace, where
I work as Development Lead.
Being a history buff in general, I also enjoy
reading and writing about computer history.
A brief History
• Chess originated in 6th century India
• 1770: Wolfgang von Kempelen's Mechanical Turk
• 1928: John von Neumann describes MiniMax
• 1948-51: Alan Turing's Turochamp / Paper
Machine, later on Mark1 (2012: Kasparov vs.
Turochamp, Kasparov wins in 15 moves)
• 1950: Claude Shannon publishes "Programming a
Computer for Playing Chess", builds limited relay
chess machine
• 1957: Bernstein Chess on IBM 704
• 1978: Ken Thompson's Belle
• 1983: 1K ZX Chess
A brief History
• 1997: IBM's Deep Blue beats Gary Kasparov 3.5 : 2.5
• In 1996 Kasparov had won 4 : 2
• 32 node IBM RS/6000, 120 MHz P2SC + 8 VLSI chips per node for MiniMax / Alpha-Beta Pruning
• 32GB transposition table
• 200m moves / sec, Ply 8-12, ELO 2750
• 2002-2006: Hydra Project
• FPGA-based, Dr. Christian Donninger (AT / JKU), similar to Deep Blue, 150m moves / sec
• 2014: StockFish Computer Chess World Champion
• 70m moves / sec on off-the-shelf hardware, Ply 20-45 (much better ordering/pruning), ELO 3400
• 1TB transposition table
A brief History
• 2017: AlphaZero Chess wins against StockFish
• 28 wins, 72 draws, 0 losses
• Controversy: StockFish on limited hardware (8 CPUs), too many threads for the HW (64), wrong time
management settings and too small transposition table memory
• AlphaZero running on 5000 1st gen TPUs to generate self-played games + 64 2nd gen TPUs (45
TFLOPS each) to train neural network (only 4 TPUs used)
• Residual neural network, two outputs: board evaluation and move evaluation
• Blank state reinforcement learning, playing against itself (nothing supervised from human chess
history / knowledge)
• Monte Carlo Tree Search (instead of MiniMax): expanding by applying board/move evaluations
• 80k moves / sec, est. ELO 3750
Source: (8)
A brief History
Source: (4)
Chess Engine Fundamentals
• Board Representation
• BitBoards
• Evaluation Function
• Material / Position / Mobility
• MiniMax Algorithm
• Search tree backtracking algorithm, minimize possible loss (expect opponent to make best possible
move)
• Move Generator / Iterator
• Alpha-Beta Pruning
• Decrease number of node evaluations in search tree
• Opening Book
• Performance Tuning
BitBoards
• 64bit int / bitmask
• 1 bitboard for every piece-type and color => 12 bitboards for board state
• Compact representation, perfect for bitwise operations, CPU pipelining
• Used for lookup tables, bitmasks, move and attack tables, etc.
// A pawn is backward when it is behind all pawns of the same color
// on the adjacent files and cannot be safely advanced.
backward = !(ourPawns & PawnAttackSpan[Them][s + Up])
&& (stoppers & (leverPush | (s + Up)));
Source: (10)
Evaluation
Material: Centipawns
Source: (5)
Evaluation
Position: Piece Square Tables
Source: (1)
Source: (1)
Evaluation
Mobility
MobilityBonus[][32] = {
// snip
{ S(-75,-76), S(-56,-54), S( -9,-26), S( -2,-10), S( 6, 5), S( 15, 11), // Knights
S( 22, 26), S( 30, 28), S( 36, 29) },
// snip
{ S(-40,-35), S(-25,-12), S( 2, 7), S( 4, 19), S( 14, 37), S( 24, 55), // Queens
S( 25, 62), S( 40, 76), S( 43, 79), S( 47, 87), S( 54, 94), S( 56,102),
S( 60,111), S( 70,116), S( 72,118), S( 73,122), S( 75,128), S( 77,130),
S( 85,133), S( 94,136), S( 99,140), S(108,157), S(112,158), S(113,161),
S(118,174), S(119,177), S(123,191), S(128,199) }
};
Source: (10)
MiniMax
Source: (13)
MiniMax
Source: (11)
Move Generator
• The Move Generator creates a list of legal moves or pseudo-legal moves (check not
considered)
• MiniMax usually just invokes a PickMove() method within a loop, which encapsulates move
generation and iteration. After a move is applied, MiniMax calls itself again for the next ply.
• Moves are often created lazily during iteration - in anticipation that early cutoffs will likely
happen within the first few moves, and no unnecessary move generation work is done.
Horizon Effects / Quiescence Search
• Evaluation should always happen on "quiet positions, e.g. not immediately after a capture or a
check.
• This avoids erroneous move selection due to Horizon Effects, which are overly optimistic
evaluations due to the fact that negative consequences are looming beyond the maximum
search depth (or also pessimistic evaluations, in case of positive consequences).
• Horizon effects can also lead to suicidal moves, e.g. sacrificing more pieces for material that is
already lost for certain anyway, but is obscured as the certain loss is thus moved behind the
horizon by the sacrifice.
• Quiescence Search adds additional noisy moves (=capture moves, possibly also promotions
and check-replies) on unstable positions at the end of the search tree, until no more captures
are possible. This then prevents horizon effects.
Alpha-Beta Pruning
• Developed independently by several researchers in the
1950s. 1975: Donald Knuth: "An Analysis of Alpha-Beta
Pruning"
• Allows for ignoring paths not worth evaluating - just what
the human mind does intuitively (ignoring moves that
don't make sense)
• Alpha: min. score for maximizing player - lower bound
• Beta: max. score for minimizing player - upper bound
• Alpha and beta are passed up and down during search
tree traversal. Nodes outside those bounds don't need
to be traversed ("cutoff"), and we can safely return
prematurely.
Savings: Source: (12)
Alpha-Beta Pruning
Source: (1)
Alpha-Beta Pruning
Source: (12)
Move Ordering
What if we would have found board evaluation "6" earlier in the example,
instead of "3"?
For Alpha-Beta pruning to perform well, the (supposedly) best moves must be
searched first => early cutoff
1. Order according to previous depth-limited search results (see: Iterative
deepening)
2. Hash move (if cached via transposition table, previous best move on same
board or at least good enough to trigger cutoffs)
3. Winning captures (MVV-LVA: Most Valuable Victim - Least Valuable
Aggressor), including pawn promotions
4. Killer moves (which caused earlier cutoffs at the same ply)
5. Quiet moves (sorted by positional delta)
6. Losing captures / opponent captures
Transposition Tables
Store and re-use results of previous searches via large Hashtable
• Key: board representation, e.g. 64bit Zobrist hash
• Zobrist hash: based on 12 (piece type) x 64 (positions) = 768 64bit random numbers. XOR
hashes when pieces are moved => rapid incremental hash calculation
• Value: depth, evaluation (exact, lower bound, upper bound), best move, depth
Collision detection: check if stored move is pseudo-legal move
Applied for
• Re-using cached evaluations (check on each node before deeper search)
• Move ordering (hash move)
Iterative Deepening
• Run depth-first search repeatedly with ever-increasing
depths
• Originally for time management reasons (has searched
all moves, albeit not with the same depth, and can
provide a good-enough intermediate result when time
runs out)
• Evaluations and best moves from previous runs can
then be retrieved per transposition table
• Return cached exact evaluations straight away, or
adjust alpha/beta on cached bound evaluations
• Apply caches hash moves for quick cutoffs in follow-
up-run
Source: (1)
There is more…
• NegaMax (simplified MiniMax implementation)
• Incremental…
• … Calculations (board hash (Zobrist), attack tables, etc)
• … Move Generator
• … Evaluation
• Aspiration Windows
• Null-Moves (unless under "Zugzwang")
• Futility Pruning, Late Move Reduction
• X-Rays
• King Safety / Pawn Storms / Open Files
• Piece Square Table interpolation depending on game stage (midgame vs. endgame)
• Static Exchange Evaluation
Tools and Protocols
• Arena - Free Chess GUI (http://www.playwitharena.com/)
• Universal Chess Interface (UCI) enables chess engines to communicate with user interfaces
• Let two chess engines play against each other
• Lichess (https://lichess.org/)
• Great chess platform, online community, StockFish in browser, board editor/analysis
• Forsyth–Edwards Notation (FEN)
• Board position data
• 1rbqkb2/2p2p1p/p1p1pp2/8/3P4/P1NQpP2/1PP3rP/2KR2NR w - - 0 13
• Portable Game Notation (PGN)
• Chess game recording
• 1. e4 e6 2. d4 d5 3. Nc3 dxe4 4. a3 Nc6 5. Bb5 Nf6 6. Be3 a6 7. Bxc6+ bxc6
8. Bg5 Rb8 9. Bxf6 gxf6 10. f3 e3 11. Qd3 Rg8 12. O-O-O
Sources
(1) https://www.chessprogramming.org/
(2) https://en.chessbase.com/post/reconstructing-turing-s-paper-machine
(3) https://thebestschools.org/magazine/brief-history-of-computer-chess/
(4) https://www.eff.org/ai/metrics
(5) https://www.slideshare.net/carlosjustiniano2/how-computers-play-chess-26552933
(6) https://medium.freecodecamp.org/simple-chess-ai-step-by-step-1d55a9266977
(7) http://members.home.nl/matador/Inside%20Rebel.pdf
(8) https://www.chess.com/article/view/how-does-alphazero-play-chess
(9) https://www.chess.com/article/view/whats-inside-alphazeros-brain
(10) https://github.com/official-stockfish/Stockfish
(11) https://en.wikipedia.org/wiki/Minimax
(12) https://en.wikipedia.org/wiki/Alpha%E2%80%93beta_pruning
(13) http://www.dcc.fc.up.pt/~fds/aulas/PPD/1314/project3.html
Thank you!
Twitter: https://twitter.com/ArnoHu
Blog: http://arnosoftwaredev.blogspot.com
Scratch Chess: https://scratch.mit.edu/projects/148769358/

More Related Content

What's hot

AI based Tic Tac Toe game using Minimax Algorithm
AI based Tic Tac Toe game using Minimax AlgorithmAI based Tic Tac Toe game using Minimax Algorithm
AI based Tic Tac Toe game using Minimax AlgorithmKiran Shahi
 
Introduction to Alphago Zero
Introduction to Alphago ZeroIntroduction to Alphago Zero
Introduction to Alphago ZeroChia-Ching Lin
 
Informed and Uninformed search Strategies
Informed and Uninformed search StrategiesInformed and Uninformed search Strategies
Informed and Uninformed search StrategiesAmey Kerkar
 
Game Tree ( Oyun Ağaçları )
Game Tree ( Oyun Ağaçları )Game Tree ( Oyun Ağaçları )
Game Tree ( Oyun Ağaçları )Alp Çoker
 
AI_Session 7 Greedy Best first search algorithm.pptx
AI_Session 7 Greedy Best first search algorithm.pptxAI_Session 7 Greedy Best first search algorithm.pptx
AI_Session 7 Greedy Best first search algorithm.pptxAsst.prof M.Gokilavani
 
AI_Session 15 Alpha–Beta Pruning.pptx
AI_Session 15 Alpha–Beta Pruning.pptxAI_Session 15 Alpha–Beta Pruning.pptx
AI_Session 15 Alpha–Beta Pruning.pptxAsst.prof M.Gokilavani
 
Lecture 17 Iterative Deepening a star algorithm
Lecture 17 Iterative Deepening a star algorithmLecture 17 Iterative Deepening a star algorithm
Lecture 17 Iterative Deepening a star algorithmHema Kashyap
 
3 problem-solving-
3 problem-solving-3 problem-solving-
3 problem-solving-Mhd Sb
 
An introduction to deep reinforcement learning
An introduction to deep reinforcement learningAn introduction to deep reinforcement learning
An introduction to deep reinforcement learningBig Data Colombia
 
Adversarial search with Game Playing
Adversarial search with Game PlayingAdversarial search with Game Playing
Adversarial search with Game PlayingAman Patel
 
Lecture 23 alpha beta pruning
Lecture 23 alpha beta pruningLecture 23 alpha beta pruning
Lecture 23 alpha beta pruningHema Kashyap
 
Minmax Algorithm In Artificial Intelligence slides
Minmax Algorithm In Artificial Intelligence slidesMinmax Algorithm In Artificial Intelligence slides
Minmax Algorithm In Artificial Intelligence slidesSamiaAziz4
 
Artificial Intelligence- TicTacToe game
Artificial Intelligence- TicTacToe gameArtificial Intelligence- TicTacToe game
Artificial Intelligence- TicTacToe gamemanika kumari
 

What's hot (20)

AI based Tic Tac Toe game using Minimax Algorithm
AI based Tic Tac Toe game using Minimax AlgorithmAI based Tic Tac Toe game using Minimax Algorithm
AI based Tic Tac Toe game using Minimax Algorithm
 
Introduction to Alphago Zero
Introduction to Alphago ZeroIntroduction to Alphago Zero
Introduction to Alphago Zero
 
Informed and Uninformed search Strategies
Informed and Uninformed search StrategiesInformed and Uninformed search Strategies
Informed and Uninformed search Strategies
 
Adversarial search
Adversarial searchAdversarial search
Adversarial search
 
Game Tree ( Oyun Ağaçları )
Game Tree ( Oyun Ağaçları )Game Tree ( Oyun Ağaçları )
Game Tree ( Oyun Ağaçları )
 
5 csp
5 csp5 csp
5 csp
 
Adversarial search
Adversarial search Adversarial search
Adversarial search
 
AI_Session 7 Greedy Best first search algorithm.pptx
AI_Session 7 Greedy Best first search algorithm.pptxAI_Session 7 Greedy Best first search algorithm.pptx
AI_Session 7 Greedy Best first search algorithm.pptx
 
AI_Session 15 Alpha–Beta Pruning.pptx
AI_Session 15 Alpha–Beta Pruning.pptxAI_Session 15 Alpha–Beta Pruning.pptx
AI_Session 15 Alpha–Beta Pruning.pptx
 
Lecture 17 Iterative Deepening a star algorithm
Lecture 17 Iterative Deepening a star algorithmLecture 17 Iterative Deepening a star algorithm
Lecture 17 Iterative Deepening a star algorithm
 
AlphaGo and AlphaGo Zero
AlphaGo and AlphaGo ZeroAlphaGo and AlphaGo Zero
AlphaGo and AlphaGo Zero
 
3 problem-solving-
3 problem-solving-3 problem-solving-
3 problem-solving-
 
An introduction to deep reinforcement learning
An introduction to deep reinforcement learningAn introduction to deep reinforcement learning
An introduction to deep reinforcement learning
 
Adversarial search with Game Playing
Adversarial search with Game PlayingAdversarial search with Game Playing
Adversarial search with Game Playing
 
Lecture 23 alpha beta pruning
Lecture 23 alpha beta pruningLecture 23 alpha beta pruning
Lecture 23 alpha beta pruning
 
Alpha beta
Alpha betaAlpha beta
Alpha beta
 
Informed search
Informed searchInformed search
Informed search
 
Minmax Algorithm In Artificial Intelligence slides
Minmax Algorithm In Artificial Intelligence slidesMinmax Algorithm In Artificial Intelligence slides
Minmax Algorithm In Artificial Intelligence slides
 
Artificial Intelligence- TicTacToe game
Artificial Intelligence- TicTacToe gameArtificial Intelligence- TicTacToe game
Artificial Intelligence- TicTacToe game
 
Understanding AlphaGo
Understanding AlphaGoUnderstanding AlphaGo
Understanding AlphaGo
 

Similar to Chess Engine Programming

chess-algorithms-theory-and-practice_ver2017.pdf
chess-algorithms-theory-and-practice_ver2017.pdfchess-algorithms-theory-and-practice_ver2017.pdf
chess-algorithms-theory-and-practice_ver2017.pdfrajdipdas12
 
Ibm's deep blue chess grandmaster chips
Ibm's deep blue chess grandmaster chipsIbm's deep blue chess grandmaster chips
Ibm's deep blue chess grandmaster chipsNadeeshaan Gunasinghe
 
CptS 440/ 540 AI.pptx
CptS 440/ 540 AI.pptxCptS 440/ 540 AI.pptx
CptS 440/ 540 AI.pptxDrDejaVu2
 
Artificial intelligence games
Artificial intelligence gamesArtificial intelligence games
Artificial intelligence gamesSujithmlamthadam
 
GamePlaying.ppt
GamePlaying.pptGamePlaying.ppt
GamePlaying.pptVihaanN2
 
Analytics at Speed: Introduction to ClickHouse and Common Use Cases. By Mikha...
Analytics at Speed: Introduction to ClickHouse and Common Use Cases. By Mikha...Analytics at Speed: Introduction to ClickHouse and Common Use Cases. By Mikha...
Analytics at Speed: Introduction to ClickHouse and Common Use Cases. By Mikha...Altinity Ltd
 
Cache aware hybrid sorter
Cache aware hybrid sorterCache aware hybrid sorter
Cache aware hybrid sorterManchor Ko
 
Evolutionary Multi-Agent Systems for RTS Games
Evolutionary Multi-Agent Systems for RTS GamesEvolutionary Multi-Agent Systems for RTS Games
Evolutionary Multi-Agent Systems for RTS GamesAdrián Palacios Corella
 
A Development of Log-based Game AI using Deep Learning
A Development of Log-based Game AI using Deep LearningA Development of Log-based Game AI using Deep Learning
A Development of Log-based Game AI using Deep LearningSuntae Kim
 
A gentle introduction into AKKA and the actor model
A gentle introduction into AKKA and the actor modelA gentle introduction into AKKA and the actor model
A gentle introduction into AKKA and the actor modelMykhailo Kotsur
 

Similar to Chess Engine Programming (20)

chess-algorithms-theory-and-practice_ver2017.pdf
chess-algorithms-theory-and-practice_ver2017.pdfchess-algorithms-theory-and-practice_ver2017.pdf
chess-algorithms-theory-and-practice_ver2017.pdf
 
Ibm's deep blue chess grandmaster chips
Ibm's deep blue chess grandmaster chipsIbm's deep blue chess grandmaster chips
Ibm's deep blue chess grandmaster chips
 
CptS 440/ 540 AI.pptx
CptS 440/ 540 AI.pptxCptS 440/ 540 AI.pptx
CptS 440/ 540 AI.pptx
 
l3.pptx
l3.pptxl3.pptx
l3.pptx
 
Ai
AiAi
Ai
 
Artificial intelligence games
Artificial intelligence gamesArtificial intelligence games
Artificial intelligence games
 
AI.ppt
AI.pptAI.ppt
AI.ppt
 
M6 game
M6 gameM6 game
M6 game
 
Games.4
Games.4Games.4
Games.4
 
Computer Chess 2004
Computer Chess 2004Computer Chess 2004
Computer Chess 2004
 
cai
caicai
cai
 
Capgemini 1
Capgemini 1Capgemini 1
Capgemini 1
 
GamePlaying.ppt
GamePlaying.pptGamePlaying.ppt
GamePlaying.ppt
 
Games
GamesGames
Games
 
Analytics at Speed: Introduction to ClickHouse and Common Use Cases. By Mikha...
Analytics at Speed: Introduction to ClickHouse and Common Use Cases. By Mikha...Analytics at Speed: Introduction to ClickHouse and Common Use Cases. By Mikha...
Analytics at Speed: Introduction to ClickHouse and Common Use Cases. By Mikha...
 
Cache aware hybrid sorter
Cache aware hybrid sorterCache aware hybrid sorter
Cache aware hybrid sorter
 
Computer chess
Computer chessComputer chess
Computer chess
 
Evolutionary Multi-Agent Systems for RTS Games
Evolutionary Multi-Agent Systems for RTS GamesEvolutionary Multi-Agent Systems for RTS Games
Evolutionary Multi-Agent Systems for RTS Games
 
A Development of Log-based Game AI using Deep Learning
A Development of Log-based Game AI using Deep LearningA Development of Log-based Game AI using Deep Learning
A Development of Log-based Game AI using Deep Learning
 
A gentle introduction into AKKA and the actor model
A gentle introduction into AKKA and the actor modelA gentle introduction into AKKA and the actor model
A gentle introduction into AKKA and the actor model
 

More from Arno Huetter

The world's most famous programmers
The world's most famous programmersThe world's most famous programmers
The world's most famous programmersArno Huetter
 
Geschichte des Computers (1991)
Geschichte des Computers (1991)Geschichte des Computers (1991)
Geschichte des Computers (1991)Arno Huetter
 
Grundlagen der Volkswirtschaftslehre (1993)
Grundlagen der Volkswirtschaftslehre (1993)Grundlagen der Volkswirtschaftslehre (1993)
Grundlagen der Volkswirtschaftslehre (1993)Arno Huetter
 
Database Performance Tuning
Database Performance Tuning Database Performance Tuning
Database Performance Tuning Arno Huetter
 
Diplomarbeit: Software Reengineering (1995)
Diplomarbeit: Software Reengineering (1995)Diplomarbeit: Software Reengineering (1995)
Diplomarbeit: Software Reengineering (1995)Arno Huetter
 
Diplomarbeit: Generische und dynamische Hypertexte (2001)
Diplomarbeit: Generische und dynamische Hypertexte (2001)Diplomarbeit: Generische und dynamische Hypertexte (2001)
Diplomarbeit: Generische und dynamische Hypertexte (2001)Arno Huetter
 
Leading Software Development Teams
Leading Software Development TeamsLeading Software Development Teams
Leading Software Development TeamsArno Huetter
 
Windows Debugging with WinDbg
Windows Debugging with WinDbgWindows Debugging with WinDbg
Windows Debugging with WinDbgArno Huetter
 
Software Disasters
Software DisastersSoftware Disasters
Software DisastersArno Huetter
 
The History of the PC
The History of the PCThe History of the PC
The History of the PCArno Huetter
 
Führen von Software-Entwicklungsteams
Führen von Software-EntwicklungsteamsFühren von Software-Entwicklungsteams
Führen von Software-EntwicklungsteamsArno Huetter
 

More from Arno Huetter (13)

Abraham Lincoln
Abraham LincolnAbraham Lincoln
Abraham Lincoln
 
Augustus
AugustusAugustus
Augustus
 
The world's most famous programmers
The world's most famous programmersThe world's most famous programmers
The world's most famous programmers
 
Geschichte des Computers (1991)
Geschichte des Computers (1991)Geschichte des Computers (1991)
Geschichte des Computers (1991)
 
Grundlagen der Volkswirtschaftslehre (1993)
Grundlagen der Volkswirtschaftslehre (1993)Grundlagen der Volkswirtschaftslehre (1993)
Grundlagen der Volkswirtschaftslehre (1993)
 
Database Performance Tuning
Database Performance Tuning Database Performance Tuning
Database Performance Tuning
 
Diplomarbeit: Software Reengineering (1995)
Diplomarbeit: Software Reengineering (1995)Diplomarbeit: Software Reengineering (1995)
Diplomarbeit: Software Reengineering (1995)
 
Diplomarbeit: Generische und dynamische Hypertexte (2001)
Diplomarbeit: Generische und dynamische Hypertexte (2001)Diplomarbeit: Generische und dynamische Hypertexte (2001)
Diplomarbeit: Generische und dynamische Hypertexte (2001)
 
Leading Software Development Teams
Leading Software Development TeamsLeading Software Development Teams
Leading Software Development Teams
 
Windows Debugging with WinDbg
Windows Debugging with WinDbgWindows Debugging with WinDbg
Windows Debugging with WinDbg
 
Software Disasters
Software DisastersSoftware Disasters
Software Disasters
 
The History of the PC
The History of the PCThe History of the PC
The History of the PC
 
Führen von Software-Entwicklungsteams
Führen von Software-EntwicklungsteamsFühren von Software-Entwicklungsteams
Führen von Software-Entwicklungsteams
 

Recently uploaded

Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Steffen Staab
 
The title is not connected to what is inside
The title is not connected to what is insideThe title is not connected to what is inside
The title is not connected to what is insideshinachiaurasa2
 
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...WSO2
 
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park %in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park masabamasaba
 
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital TransformationWSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital TransformationWSO2
 
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfonteinmasabamasaba
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplatePresentation.STUDIO
 
%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in sowetomasabamasaba
 
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...WSO2
 
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...masabamasaba
 
Announcing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK SoftwareAnnouncing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK SoftwareJim McKeeth
 
Artyushina_Guest lecture_YorkU CS May 2024.pptx
Artyushina_Guest lecture_YorkU CS May 2024.pptxArtyushina_Guest lecture_YorkU CS May 2024.pptx
Artyushina_Guest lecture_YorkU CS May 2024.pptxAnnaArtyushina1
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisamasabamasaba
 
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrainmasabamasaba
 
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfonteinmasabamasaba
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...Health
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisamasabamasaba
 
WSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go PlatformlessWSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go PlatformlessWSO2
 
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburgmasabamasaba
 
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdfPayment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdfkalichargn70th171
 

Recently uploaded (20)

Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
 
The title is not connected to what is inside
The title is not connected to what is insideThe title is not connected to what is inside
The title is not connected to what is inside
 
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
 
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park %in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
 
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital TransformationWSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
 
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation Template
 
%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto
 
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
 
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
 
Announcing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK SoftwareAnnouncing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK Software
 
Artyushina_Guest lecture_YorkU CS May 2024.pptx
Artyushina_Guest lecture_YorkU CS May 2024.pptxArtyushina_Guest lecture_YorkU CS May 2024.pptx
Artyushina_Guest lecture_YorkU CS May 2024.pptx
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
 
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
 
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
 
WSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go PlatformlessWSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go Platformless
 
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
 
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdfPayment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
 

Chess Engine Programming

  • 2. About the Author Arno Huetter I wrote my first lines of code on a Sinclair ZX80 in 1984. My daytime job is at Dynatrace, where I work as Development Lead. Being a history buff in general, I also enjoy reading and writing about computer history.
  • 3. A brief History • Chess originated in 6th century India • 1770: Wolfgang von Kempelen's Mechanical Turk • 1928: John von Neumann describes MiniMax • 1948-51: Alan Turing's Turochamp / Paper Machine, later on Mark1 (2012: Kasparov vs. Turochamp, Kasparov wins in 15 moves) • 1950: Claude Shannon publishes "Programming a Computer for Playing Chess", builds limited relay chess machine • 1957: Bernstein Chess on IBM 704 • 1978: Ken Thompson's Belle • 1983: 1K ZX Chess
  • 4. A brief History • 1997: IBM's Deep Blue beats Gary Kasparov 3.5 : 2.5 • In 1996 Kasparov had won 4 : 2 • 32 node IBM RS/6000, 120 MHz P2SC + 8 VLSI chips per node for MiniMax / Alpha-Beta Pruning • 32GB transposition table • 200m moves / sec, Ply 8-12, ELO 2750 • 2002-2006: Hydra Project • FPGA-based, Dr. Christian Donninger (AT / JKU), similar to Deep Blue, 150m moves / sec • 2014: StockFish Computer Chess World Champion • 70m moves / sec on off-the-shelf hardware, Ply 20-45 (much better ordering/pruning), ELO 3400 • 1TB transposition table
  • 5. A brief History • 2017: AlphaZero Chess wins against StockFish • 28 wins, 72 draws, 0 losses • Controversy: StockFish on limited hardware (8 CPUs), too many threads for the HW (64), wrong time management settings and too small transposition table memory • AlphaZero running on 5000 1st gen TPUs to generate self-played games + 64 2nd gen TPUs (45 TFLOPS each) to train neural network (only 4 TPUs used) • Residual neural network, two outputs: board evaluation and move evaluation • Blank state reinforcement learning, playing against itself (nothing supervised from human chess history / knowledge) • Monte Carlo Tree Search (instead of MiniMax): expanding by applying board/move evaluations • 80k moves / sec, est. ELO 3750 Source: (8)
  • 7. Chess Engine Fundamentals • Board Representation • BitBoards • Evaluation Function • Material / Position / Mobility • MiniMax Algorithm • Search tree backtracking algorithm, minimize possible loss (expect opponent to make best possible move) • Move Generator / Iterator • Alpha-Beta Pruning • Decrease number of node evaluations in search tree • Opening Book • Performance Tuning
  • 8. BitBoards • 64bit int / bitmask • 1 bitboard for every piece-type and color => 12 bitboards for board state • Compact representation, perfect for bitwise operations, CPU pipelining • Used for lookup tables, bitmasks, move and attack tables, etc. // A pawn is backward when it is behind all pawns of the same color // on the adjacent files and cannot be safely advanced. backward = !(ourPawns & PawnAttackSpan[Them][s + Up]) && (stoppers & (leverPush | (s + Up))); Source: (10)
  • 10. Evaluation Position: Piece Square Tables Source: (1) Source: (1)
  • 11. Evaluation Mobility MobilityBonus[][32] = { // snip { S(-75,-76), S(-56,-54), S( -9,-26), S( -2,-10), S( 6, 5), S( 15, 11), // Knights S( 22, 26), S( 30, 28), S( 36, 29) }, // snip { S(-40,-35), S(-25,-12), S( 2, 7), S( 4, 19), S( 14, 37), S( 24, 55), // Queens S( 25, 62), S( 40, 76), S( 43, 79), S( 47, 87), S( 54, 94), S( 56,102), S( 60,111), S( 70,116), S( 72,118), S( 73,122), S( 75,128), S( 77,130), S( 85,133), S( 94,136), S( 99,140), S(108,157), S(112,158), S(113,161), S(118,174), S(119,177), S(123,191), S(128,199) } }; Source: (10)
  • 14. Move Generator • The Move Generator creates a list of legal moves or pseudo-legal moves (check not considered) • MiniMax usually just invokes a PickMove() method within a loop, which encapsulates move generation and iteration. After a move is applied, MiniMax calls itself again for the next ply. • Moves are often created lazily during iteration - in anticipation that early cutoffs will likely happen within the first few moves, and no unnecessary move generation work is done.
  • 15. Horizon Effects / Quiescence Search • Evaluation should always happen on "quiet positions, e.g. not immediately after a capture or a check. • This avoids erroneous move selection due to Horizon Effects, which are overly optimistic evaluations due to the fact that negative consequences are looming beyond the maximum search depth (or also pessimistic evaluations, in case of positive consequences). • Horizon effects can also lead to suicidal moves, e.g. sacrificing more pieces for material that is already lost for certain anyway, but is obscured as the certain loss is thus moved behind the horizon by the sacrifice. • Quiescence Search adds additional noisy moves (=capture moves, possibly also promotions and check-replies) on unstable positions at the end of the search tree, until no more captures are possible. This then prevents horizon effects.
  • 16. Alpha-Beta Pruning • Developed independently by several researchers in the 1950s. 1975: Donald Knuth: "An Analysis of Alpha-Beta Pruning" • Allows for ignoring paths not worth evaluating - just what the human mind does intuitively (ignoring moves that don't make sense) • Alpha: min. score for maximizing player - lower bound • Beta: max. score for minimizing player - upper bound • Alpha and beta are passed up and down during search tree traversal. Nodes outside those bounds don't need to be traversed ("cutoff"), and we can safely return prematurely. Savings: Source: (12)
  • 19. Move Ordering What if we would have found board evaluation "6" earlier in the example, instead of "3"? For Alpha-Beta pruning to perform well, the (supposedly) best moves must be searched first => early cutoff 1. Order according to previous depth-limited search results (see: Iterative deepening) 2. Hash move (if cached via transposition table, previous best move on same board or at least good enough to trigger cutoffs) 3. Winning captures (MVV-LVA: Most Valuable Victim - Least Valuable Aggressor), including pawn promotions 4. Killer moves (which caused earlier cutoffs at the same ply) 5. Quiet moves (sorted by positional delta) 6. Losing captures / opponent captures
  • 20. Transposition Tables Store and re-use results of previous searches via large Hashtable • Key: board representation, e.g. 64bit Zobrist hash • Zobrist hash: based on 12 (piece type) x 64 (positions) = 768 64bit random numbers. XOR hashes when pieces are moved => rapid incremental hash calculation • Value: depth, evaluation (exact, lower bound, upper bound), best move, depth Collision detection: check if stored move is pseudo-legal move Applied for • Re-using cached evaluations (check on each node before deeper search) • Move ordering (hash move)
  • 21. Iterative Deepening • Run depth-first search repeatedly with ever-increasing depths • Originally for time management reasons (has searched all moves, albeit not with the same depth, and can provide a good-enough intermediate result when time runs out) • Evaluations and best moves from previous runs can then be retrieved per transposition table • Return cached exact evaluations straight away, or adjust alpha/beta on cached bound evaluations • Apply caches hash moves for quick cutoffs in follow- up-run Source: (1)
  • 22. There is more… • NegaMax (simplified MiniMax implementation) • Incremental… • … Calculations (board hash (Zobrist), attack tables, etc) • … Move Generator • … Evaluation • Aspiration Windows • Null-Moves (unless under "Zugzwang") • Futility Pruning, Late Move Reduction • X-Rays • King Safety / Pawn Storms / Open Files • Piece Square Table interpolation depending on game stage (midgame vs. endgame) • Static Exchange Evaluation
  • 23. Tools and Protocols • Arena - Free Chess GUI (http://www.playwitharena.com/) • Universal Chess Interface (UCI) enables chess engines to communicate with user interfaces • Let two chess engines play against each other • Lichess (https://lichess.org/) • Great chess platform, online community, StockFish in browser, board editor/analysis • Forsyth–Edwards Notation (FEN) • Board position data • 1rbqkb2/2p2p1p/p1p1pp2/8/3P4/P1NQpP2/1PP3rP/2KR2NR w - - 0 13 • Portable Game Notation (PGN) • Chess game recording • 1. e4 e6 2. d4 d5 3. Nc3 dxe4 4. a3 Nc6 5. Bb5 Nf6 6. Be3 a6 7. Bxc6+ bxc6 8. Bg5 Rb8 9. Bxf6 gxf6 10. f3 e3 11. Qd3 Rg8 12. O-O-O
  • 24. Sources (1) https://www.chessprogramming.org/ (2) https://en.chessbase.com/post/reconstructing-turing-s-paper-machine (3) https://thebestschools.org/magazine/brief-history-of-computer-chess/ (4) https://www.eff.org/ai/metrics (5) https://www.slideshare.net/carlosjustiniano2/how-computers-play-chess-26552933 (6) https://medium.freecodecamp.org/simple-chess-ai-step-by-step-1d55a9266977 (7) http://members.home.nl/matador/Inside%20Rebel.pdf (8) https://www.chess.com/article/view/how-does-alphazero-play-chess (9) https://www.chess.com/article/view/whats-inside-alphazeros-brain (10) https://github.com/official-stockfish/Stockfish (11) https://en.wikipedia.org/wiki/Minimax (12) https://en.wikipedia.org/wiki/Alpha%E2%80%93beta_pruning (13) http://www.dcc.fc.up.pt/~fds/aulas/PPD/1314/project3.html
  • 25. Thank you! Twitter: https://twitter.com/ArnoHu Blog: http://arnosoftwaredev.blogspot.com Scratch Chess: https://scratch.mit.edu/projects/148769358/