SlideShare a Scribd company logo
1 of 22
HOW I TAUGHT A COMPUTER
TO PLAY CHESS
The Algorithms and
Techniques• March 18, 2017
Claude Shannon predicted the number of chess games
Number of chess games are 10120 for 40 pair moves
Number of atoms in Universe 1078
𝑡𝑜 1082
Number of seconds in a year 𝜋 𝑥 107
Number of nanoseconds in year 109
Number of years in the universe 1010
Shannon's Number
Complexity of the Game
The Objective is to check mate the opponent King
One may also want to draw a game after some evaluation
Seek draw by repeated moves if the game is not making progress
Objective of game of Chess
Objective
Is it a solved problem?
Is it a computationally solvable problem?
Can we make a computer play a chess game?
It is a game with perfect information?
What do we know about Chess
NO Answers
YES Answers
Representation: Algebraic Notation
Notations
Develop Knights before Bishops Kc3 Kf3 Kc6 Kf6
Develop minor pieces first
Control the center D4 D5 E4 E5
Castle and Protect the King 0-0-0 or 0-0
No double pawns
Knights at the rim is dim. A and H file
Use standard openings – Ruy Lopez, Kings Pawn Opening, Queens Gamit, Burlin etc.
Centralize your King during the end game
General rule to improve your chances of winning
General Chess Theory
Position evaluation
Searching for the next move and choosing the next best move
Building a Chess Engine
Two components
• 𝐸𝑣𝑎𝑙 𝑠 = 𝑤𝑖 𝑓1 𝑠 + 𝑤2 𝑓2 𝑠 + ⋯ + 𝑤 𝑛 𝑓𝑛 𝑠 = 𝑖=1
𝑛
𝑤𝑖 𝑓𝑖 𝑆
• 𝑓𝑖 : Number of each kind of piece
• 𝑤𝑖 : Is the weight of piece
Position Evaluation in Game of Chess
Linear Function
(1*9)+(2*3.25)*+(2*3)+(1*6)+(2*5)+(1*20) = 57.5
(1*9)+(2*3.25)+(1*3)+(1*8)+(2*5)+(1*20) = 56.5
By searching. But its exhaustive
By using a heuristics.
Decision Tree
Min Max Algorithm
Pruning based approach - Heuristics
Alpha beta Pruning
Solution Approaches : Searching for Next Move
Algorithm
Advanced Techniques – Adversarial Search Algorithms
Mini - Max algorithms
Adversarial Search
Min- Max Algorithm on Chess board
Adversarial Search
Alpha Beta - Pruning
Adversarial Search
1 2
3 4
5 6
• Examine capturing moves before development moves
• Killer heuristics
Alpha Beta - Pruning with Heuristics
Adversarial Search
1 2 3 4
Solution Approaches
• Min-Max and Alpha
Beta Pruning
• Adversarial Search
better models a zero
sum game where either
party wins
• Alpha Beta Pruning
helps you remove
entire branches and
work with interesting
branches only
• Alpha Beta can be
applied to trees of any
depth
• Opening Book
• End Game
• Chess openings are well
developed in chess
theory
• End games are
completely solved
because its manageable
with limited number of
pieces left on the board
• Progressive
Deepening
• Iterative Deepening is
DFS with benefits of BFS.
• A Depth limited version
of DFS with progressive
increments
• It’s a time management
strategy in DFS
• Proves to be beneficial
with Move ordering
techniques
• Uneven tree
development
• Some situation at the
bottom of the tree may
be dynamic
• You may capture a queen
• You may want to double
check by developing the
tree
Uneven Tree Development
Classic Chess Puzzle
Alpha Go: Notice patterns which humans cannot see
AI are now correcting novels
AI are now improving music by adding some notes
Creativity !!!
Creativity
Research in AI -> Algorithms have numerous applications in health care
Adversarial Search helps in building strategies. Business decisions
Smarter Software: Will it unveil patterns? Like in Biology
Parkinson's
Cancer
Research in Math has benefited numerous fields of sciences.
AI can help us find some cool solutions
Benefits
Research
https://github.com/rohitvvv/ChessAnalyzer.git
https://www.youtube.com/watch?v=Km024eldY1A
http://www.universetoday.com/36302/atoms-in-the-universe/
https://en.wikipedia.org/wiki/Portable_Game_Notation
https://www.chess.com/blog/zaifrun/creating-a-chess-engine-from-scratch-part-1
https://en.lichess.org
https://en.wikipedia.org/wiki/Simulated_annealing
https://en.wikipedia.org/wiki/Pruning_(decision_trees)
https://en.wikipedia.org/wiki/Minimax
Resources
Web Resources
• https://www.youtube.com/watch?v=STjW3eH0Cik MIT OCW
• https://chessprogramming.wikispaces.com/
Resources
Web Resources
Machine Learning – Tom Mitchell
Artificial Intelligence A Modern Approach – Peter Norvig, Stuart Russell
Pattern Classification – Richard Duda
Introduction to Algorithms – Thomas Cormen
Resources
Books
YOU
THANK
CONTACT ME
Rohit_vaidya@epam.com
Rohit_vijay_vaidya
Rohit Vaidya
Rohit_elan

More Related Content

Similar to How i taught a computer to play chess

Game Analysis, lecture 1
Game Analysis, lecture 1Game Analysis, lecture 1
Game Analysis, lecture 1
Petri Lankoski
 
Adversarial search
Adversarial searchAdversarial search
Adversarial search
Nilu Desai
 

Similar to How i taught a computer to play chess (20)

Games
GamesGames
Games
 
cai
caicai
cai
 
games, infosec, privacy, adversaries .ppt
games, infosec, privacy, adversaries .pptgames, infosec, privacy, adversaries .ppt
games, infosec, privacy, adversaries .ppt
 
M6 game
M6 gameM6 game
M6 game
 
Games.4
Games.4Games.4
Games.4
 
Chess Merit Badge Presentation
Chess Merit Badge PresentationChess Merit Badge Presentation
Chess Merit Badge Presentation
 
Collaborative Techniques
Collaborative TechniquesCollaborative Techniques
Collaborative Techniques
 
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
 
Game Analysis, lecture 1
Game Analysis, lecture 1Game Analysis, lecture 1
Game Analysis, lecture 1
 
Computer Chess 2004
Computer Chess 2004Computer Chess 2004
Computer Chess 2004
 
Game playing.ppt
Game playing.pptGame playing.ppt
Game playing.ppt
 
It is an artificial document, please. regarding Ai topics
It is an artificial document, please. regarding Ai topicsIt is an artificial document, please. regarding Ai topics
It is an artificial document, please. regarding Ai topics
 
Adversarial search
Adversarial searchAdversarial search
Adversarial search
 
484-7.ppt
484-7.ppt484-7.ppt
484-7.ppt
 
Adversarial search
Adversarial searchAdversarial search
Adversarial search
 
Quest for Progress (GDC Europe 2016)
Quest for Progress (GDC Europe 2016)Quest for Progress (GDC Europe 2016)
Quest for Progress (GDC Europe 2016)
 
Distributed processing of large graphs in python
Distributed processing of large graphs in pythonDistributed processing of large graphs in python
Distributed processing of large graphs in python
 
Better Chess through Genetic Programming
Better Chess through Genetic ProgrammingBetter Chess through Genetic Programming
Better Chess through Genetic Programming
 
Blank out the squares
Blank out the squaresBlank out the squares
Blank out the squares
 
Videogame Optimization
Videogame OptimizationVideogame Optimization
Videogame Optimization
 

Recently uploaded

Call Girls in Netaji Nagar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Netaji Nagar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort ServiceCall Girls in Netaji Nagar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Netaji Nagar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
Integrated Test Rig For HTFE-25 - Neometrix
Integrated Test Rig For HTFE-25 - NeometrixIntegrated Test Rig For HTFE-25 - Neometrix
Integrated Test Rig For HTFE-25 - Neometrix
Neometrix_Engineering_Pvt_Ltd
 
Standard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power PlayStandard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power Play
Epec Engineered Technologies
 
notes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.pptnotes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.ppt
MsecMca
 
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
ssuser89054b
 

Recently uploaded (20)

22-prompt engineering noted slide shown.pdf
22-prompt engineering noted slide shown.pdf22-prompt engineering noted slide shown.pdf
22-prompt engineering noted slide shown.pdf
 
Design For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the startDesign For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the start
 
Unit 1 - Soil Classification and Compaction.pdf
Unit 1 - Soil Classification and Compaction.pdfUnit 1 - Soil Classification and Compaction.pdf
Unit 1 - Soil Classification and Compaction.pdf
 
Thermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - VThermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - V
 
Call Girls in Netaji Nagar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Netaji Nagar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort ServiceCall Girls in Netaji Nagar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Netaji Nagar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
 
Work-Permit-Receiver-in-Saudi-Aramco.pptx
Work-Permit-Receiver-in-Saudi-Aramco.pptxWork-Permit-Receiver-in-Saudi-Aramco.pptx
Work-Permit-Receiver-in-Saudi-Aramco.pptx
 
Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024
 
Integrated Test Rig For HTFE-25 - Neometrix
Integrated Test Rig For HTFE-25 - NeometrixIntegrated Test Rig For HTFE-25 - Neometrix
Integrated Test Rig For HTFE-25 - Neometrix
 
Standard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power PlayStandard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power Play
 
KubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlyKubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghly
 
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
 
DC MACHINE-Motoring and generation, Armature circuit equation
DC MACHINE-Motoring and generation, Armature circuit equationDC MACHINE-Motoring and generation, Armature circuit equation
DC MACHINE-Motoring and generation, Armature circuit equation
 
notes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.pptnotes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.ppt
 
Employee leave management system project.
Employee leave management system project.Employee leave management system project.
Employee leave management system project.
 
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
 
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
 
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
 
data_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdfdata_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdf
 
Unit 2- Effective stress & Permeability.pdf
Unit 2- Effective stress & Permeability.pdfUnit 2- Effective stress & Permeability.pdf
Unit 2- Effective stress & Permeability.pdf
 
chapter 5.pptx: drainage and irrigation engineering
chapter 5.pptx: drainage and irrigation engineeringchapter 5.pptx: drainage and irrigation engineering
chapter 5.pptx: drainage and irrigation engineering
 

How i taught a computer to play chess

Editor's Notes

  1. Lets just try to understand what we are dealing with here. The number of possible chess games is 10^120. This is a conservative lower bound. This bound is an assumption for games which lasts 40 moves The solution space is simply huge. The number is bigger than number of atom in the observable universe. The number is Ten quadrillion vigintillion and one-hundred thousand quadrillion vigintillion atoms. Lets add up Number of atoms + seconds in a year + nanoseconds +age of universe .Even then 10^106 its 14 orders of magnitude short Reference: https://www.youtube.com/watch?v=Km024eldY1A Reference: http://www.universetoday.com/36302/atoms-in-the-universe/ Reference: http://vision.unipv.it/IA1/ProgrammingaComputerforPlayingChess.pdf
  2. Chess is in that sense very formal The Objective function is to check mate an opponent or at-least draw if your position is not very good
  3. Chess by no means is a solved problem There are computers that can beat humans at chess but that does not mean, its solved So what is a computationally solved game? A game is computationally solved if a computer can explore every possible move in that game. This will help computer generate the best move and its certain computer knows that the game tree will eventually result in a win, assuming its not playing against itself or at least playing with a mere mortal(human) Its practically impossible to do that. Because combinatorically it’s a huge problem. In-fact its popularly termed has combinatorial explosion. Chess is a game with a fully observable environment. This is different from game of cards like black jack, solatire, pocker, bridge and backgammon. Partially observable and imperfect information. TODO: Statistics on how much time it will take
  4. We should be able to formally represent the problem so that we can solve it with a computer For example if you are solving a graph you will come up with representation like edge list, adjacency list or adjacency matrix etc Chess games are documented in a standard format called algebraic notation Reference: https://en.wikipedia.org/wiki/Portable_Game_Notation
  5. These are some general guidelines which chess grandmasters have for amateur and novice players They are no rules which guarantee success but are sure to improve your chances of winning You can easily build heuristics around these rules. And if you want to teach computer how to play chess, you must certainly have these rules. But what can we do with these rules and how will they help us?
  6. Position Evaluation: It’s a process of evaluating any position and gives it a numerically value Positive means white is better, negative means black is better, while a evaluation value of 0.0 means the position is equal This leads us to a natural question on how a position is evaluated Adding the pieces on the board. 1.0 for a pawn, 3.0 for knight and bishop, 5.0 for a Rook, 9.0 for a Queen - negative values are used for black. We will discuss more in the next slide Searching the next best move is a difficult problem The Shannon’s number clearly indicates that we cannot explore every branch as the tree grows exponentially So we have to cut out a few branches and only look at branches which are promising We humans also do the same. We look at 1 or 2 branches to evaluate our next move But what algorithmic techniques can we use to narrow down these branches. Lets discuss them in detail in the following presentation Resources: https://www.chess.com/blog/zaifrun/creating-a-chess-engine-from-scratch-part-1 Resources: https://en.lichess.org
  7. Performance of chess playing program depends on the evaluation function. An inaccurate evaluation function will guide an agent towards positions that turn out to be loss Also the computation should not take long. The whole point is to search faster What we are looking at is a weighted linear function F are the features and w is the weight associated with the feature. So the natural question is what is f? Lets evaluate for white Number of White Pawns Number of White Major pieces (Knight + Bishop) Number of Queens King safety Pawn structure And what is W? Weight is the weight associated with every feature we are considering Pawn – 1 Knight – 3 Bishop - 3.25 R – 5 Q – 9 Good pawn structure and King safety are ½ pawn each Lets follow the order Queen Bishop Night Pawn Rook King in the linear equation expansion As you can see Black is slightly better in this case As you can see the Stockfish, the best chess engine is also is in blacks favor. But evaluation is just one step and we still need to find the best move. Reference: https://en.lichess.org
  8. We can use Decision Trees. It’s like if then rules in programming. But how big will that tree be. Remember Shannons number? Lets put things in perspective. On average each player has 30 legal moves For two moves. Which is 1st player makes a move then then 2nd player makes a move you have 30*30= 900 moves. And 3rd move 900*30 =2700. and so on. Can we have a decision tree that big which could have explored the entire possibilities. No. Neural Networks. Well they can learn anything.  From speech recognition, hand gesture recognition, face recognition. Neural Networks are good at classification problems. They learn and they can fire based on what function they are trained to learn Min Max search is the good fit in adversarial problems where there is a zero sum game. Each player tries to improve his/her chances of winning. Reference: https://www.youtube.com/watch?v=Km024eldY1A (Numberphile YouTube channel)
  9. Lets image a simple game where one player makes a move and the second player makes his move and the game is over. In this game each player has 3 moves to choose from. Max moves to the maximum value. Min moves to the minimum value Pink triangle player goes first. He is the maximizer. He has three choices and he chooses a1 leading to game state B. In game state B Minimizer chooses the state which goes in his favor. It’s 3 in this case. Optimal play for Max assumes Min also plays optimally. If MIN does not play optimally. MAX will do even better. If in state B Min chooses to go with 8. Well Max has already won. Now with a Branching factor of 3. The maximum depth of 2. The number of nodes to be evaluated is b^d. In this case its 9 Reference: Image made with draw.io
  10. We are looking at a portion of the min max tree. Every node is the game state. At each level the evaluation function is used. This evaluation function determines how the game is going to pan out for Maximizer and Minimizer The problem with Min-Max is the number of games sates it has to examine is exponential in the depth of the tree 30^10 is 590,490,000,000,000. And Stockfish managed to analyze till the depth of 22!!! How did it do that? Pruning comes to your rescue which we will discuss in the next slides
  11. Class of branch and bound algorithms. Starts with α been negative infinity and β been positive infinity. Both players are starting with lowest possible score Alpha Beta Pruning: This slides shows stages of calculation of the optimal decision tree for the game tree. The first leaf below B has value of 3. Hence B is a min node and has a value of at-most 3. The second leaf below B has 12. B will avoid this move. So B is still at-most 3. The third leaf below B has a value of 8. We have seen all B successors and hence value is 3 Leaf below C has value 2. Hence C been min node has value 2. But we know B is 3. So Max will never choose C. There is no point looking at other successors of B First leaf below D has value 14. Now D is at-most 14. This is higher than the max alternative 3. So explore D successors. The second successor of D is 5 which is bigger than min 3. Hence explore more and we find 2. So D is 2. Max decision at the root is to move to D Reference: Artificial Intelligence a Modern Approach –Peter Norvig
  12. Capture moves may be more interesting. Evaluating them and exploring the tree first may lead to cutoff or development Alpha beta Pruning time complexity is again exponential. Hence a killer heuristics will help examine the interesting moves first. It only needs to consider the other player's possible responses to that best move And can skip evaluation of responses to (worse) moves it will not make. There are some other concepts like Refutation tables and transposition tables. Reference: https://en.wikipedia.org/wiki/Alpha%E2%80%93beta_pruning Reference: https://en.wikipedia.org/wiki/Killer_heuristic
  13. Is it model of our intelligence?
  14. White to play Rook to A8. Q x A8. BF3+. King has to move. And the queen is taken. Well interesting situations like these need to be explored in greater depth. Rather than relying on simple rule of capturing the rook blindly. Reference: https://twitter.com/SusanPolgar Puzzle by Susan on Twitter timeline
  15. Systems are learning patterns in from the data set without knowing the data Earlier AI would have rules books based learning (decision trees). So it knows what decision AI has recently been able to come up with the concept of Cat by just studying YouTube videos. This learning was un-supervised Reference: https://www.youtube.com/watch?v=5lur9fY86xM
  16. This slide is more philosophical in nature Knowing how we can play certain complex games the field of Algorithms can explore and learn new way of doing things