Chess Algorithms Theory and Practice. Rune Djurhuus Chess Grandmaster runed@ifi.uio.no / runedj@microsoft.com September 23, 2014

Similar documents
Chess Algorithms Theory and Practice. Rune Djurhuus Chess Grandmaster / October 3, 2012

Game playing. Chapter 6. Chapter 6 1

Game Playing in the Real World. Next time: Knowledge Representation Reading: Chapter

FIRST EXPERIMENTAL RESULTS OF PROBCUT APPLIED TO CHESS

Acknowledgements I would like to thank both Dr. Carsten Furhmann, and Dr. Daniel Richardson for providing me with wonderful supervision throughout my

INTRODUCTION 4 1 OVERALL LOOK ON CHESS PROGRAMMING 8

Laboratory work in AI: First steps in Poker Playing Agents and Opponent Modeling

CS MidTerm Exam 4/1/2004 Name: KEY. Page Max Score Total 139

Measuring the Performance of an Agent

Creating a NL Texas Hold em Bot

Generalized Widening

The Cross-Platform Implementation

Roulette Wheel Selection Game Player

The Taxman Game. Robert K. Moniot September 5, 2003

Keywords-Chess gameregistration, Profile management, Rational rose, Activities management.

A Knowledge-based Approach of Connect-Four

Binary Search Trees. A Generic Tree. Binary Trees. Nodes in a binary search tree ( B-S-T) are of the form. P parent. Key. Satellite data L R

Mastering Quoridor. Lisa Glendenning THESIS. Submitted in Partial Fulfillment of the Requirements for the Degree of

Sequential lmove Games. Using Backward Induction (Rollback) to Find Equilibrium

Load Balancing. Load Balancing 1 / 24

Monte-Carlo Methods. Timo Nolle

COMPUTER ANALYSIS OF WORLD CHESS CHAMPIONS 1

Lecture 10: Regression Trees

Introduction Solvability Rules Computer Solution Implementation. Connect Four. March 9, Connect Four

Chapter 13: Query Processing. Basic Steps in Query Processing

CS104: Data Structures and Object-Oriented Design (Fall 2013) October 24, 2013: Priority Queues Scribes: CS 104 Teaching Team

ECON 459 Game Theory. Lecture Notes Auctions. Luca Anderlini Spring 2015

10/13/11 Solution: Minimax with Alpha-Beta Pruning and Progressive Deepening

CSE 326: Data Structures B-Trees and B+ Trees

Search methods motivation 1

BASIC RULES OF CHESS

10. Machine Learning in Games

Learning Example. Machine learning and our focus. Another Example. An example: data (loan application) The data and the goal

Algorithms and Data Structures

Near Optimal Solutions

Sense Making in an IOT World: Sensor Data Analysis with Deep Learning

Decision Making under Uncertainty

A Least-Certainty Heuristic for Selective Search 1

Chapter 11 Monte Carlo Simulation

How I won the Chess Ratings: Elo vs the rest of the world Competition

Optimization in ICT and Physical Systems

Dynamic programming formulation

Technology Update White Paper. High Speed RAID 6. Powered by Custom ASIC Parity Chips

Checkers Is Solved. *To whom correspondence should be addressed.

Treemaps for Search-Tree Visualization

TEACHER S GUIDE TO RUSH HOUR

Analysis of Algorithms I: Binary Search Trees

Class Notes CS Creating and Using a Huffman Code. Ref: Weiss, page 433

what operations can it perform? how does it perform them? on what kind of data? where are instructions and data stored?

GPUs for Scientific Computing

Distributed Caching Algorithms for Content Distribution Networks

External Sorting. Why Sort? 2-Way Sort: Requires 3 Buffers. Chapter 13

Temporal Difference Learning in the Tetris Game

The Progression from 4v4 to 11v11

Best-First and Depth-First Minimax Search in Practice

The Methodology of Application Development for Hybrid Architectures

Game Theory 1. Introduction

D-optimal plans in observational studies

The GMAT Guru. Prime Factorization: Theory and Practice

AI Techniques Used in Computer Go

AN EXPLANATION OF JOINT DIAGRAMS

AI: A Modern Approach, Chpts. 3-4 Russell and Norvig

Chess Position Trainer Manual

Analysis of Algorithms I: Optimal Binary Search Trees

Data Structures. Jaehyun Park. CS 97SI Stanford University. June 29, 2015

Symbol Tables. Introduction

From Last Time: Remove (Delete) Operation

Data Mining Classification: Decision Trees

MAN VS. MACHINE. How IBM Built a Jeopardy! Champion x The Analytics Edge

A binary search tree or BST is a binary tree that is either empty or in which the data element of each node has a key, and:

Monte-Carlo Tree Search (MCTS) for Computer Go

- Easy to insert & delete in O(1) time - Don t need to estimate total memory needed. - Hard to search in less than O(n) time

How To Make A Credit Risk Model For A Bank Account

Minimax Strategies. Minimax Strategies. Zero Sum Games. Why Zero Sum Games? An Example. An Example

Secure Cloud Storage and Computing Using Reconfigurable Hardware

6.207/14.15: Networks Lecture 15: Repeated Games and Cooperation

TD-Gammon, A Self-Teaching Backgammon Program, Achieves Master-Level Play

CSE 517A MACHINE LEARNING INTRODUCTION

The Advantages and Disadvantages of Network Computing Nodes

Monte Carlo Tree Search and Opponent Modeling through Player Clustering in no-limit Texas Hold em Poker

Hooray for the Hundreds Chart!!

Blunder cost in Go and Hex

FAST INVERSE SQUARE ROOT

An Interactive Visualization Tool for the Analysis of Multi-Objective Embedded Systems Design Space Exploration

DESPITE the many advances in machine learning and

Stepwise Regression. Chapter 311. Introduction. Variable Selection Procedures. Forward (Step-Up) Selection

Growing testing skills using the Agile Testing Ecosystem. Dr Lee Hawkins Principal Test Architect Dell Software, Melbourne

Data Structures Fibonacci Heaps, Amortized Analysis

CUDA programming on NVIDIA GPUs

Game theory and AI: a unified approach to poker games

Monte-Carlo Tree Search Solver

Parquet. Columnar storage for the people

Overview. Lecture 1: an introduction to CUDA. Hardware view. Hardware view. hardware view software view CUDA programming

Guessing Game: NP-Complete?

Rethinking SIMD Vectorization for In-Memory Databases

Union-Find Algorithms. network connectivity quick find quick union improvements applications

Lecture 4 Online and streaming algorithms for clustering

Ganging Up on Big Data

COMP 590: Artificial Intelligence

22S:295 Seminar in Applied Statistics High Performance Computing in Statistics

Transcription:

Chess Algorithms Theory and Practice Rune Djurhuus Chess Grandmaster runed@ifi.uio.no / runedj@microsoft.com September 23, 2014 1

Content Complexity of a chess game Solving chess, is it a myth? History of computer chess Search trees and position evaluation Minimax: The basic search algorithm Negamax: «Simplified» minimax Node explosion Pruning techniques: Alpha-Beta pruning Analyze the best move first Killer-move heuristics Zero-move heuristics Iterative deeper depth-first search (IDDFS) Search tree extensions Transposition tables (position cache) Other challenges Endgame tablebases Demo 2

Complexity of a Chess Game 20 possible start moves, 20 possible replies, etc. 400 possible positions after 2 ply (half moves) 197 281 positions after 4 ply 7 13 positions after 10 ply (5 White moves and 5 Black moves) Exponential explosion! Approximately 40 legal moves in a typical position There exists about 10 120 possible chess games 3

Solving Chess, is it a myth? Chess Complexity Space The estimated number of possible chess games is 10 120 Claude E. Shannon 1 followed by 120 zeroes!!! The estimated number of reachable chess positions is 10 47 Shirish Chinchalkar, 1996 Modern GPU s performs 10 13 flops If we assume one million GPUs with 10 flops per position we can calculate 10 18 positions per second It will take us 1 600 000 000 000 000 000 000 years to solve chess Assuming Moore s law works in the future Todays top supercomputers delivers 10 16 flops Assuming 100 operations per position yields 10 14 positions per second Doing retrograde analysis on supercomputers for 4 months we can calculate 10 21 positions. When will Moore s law allow us to reach 10 47 positions? Answer: in 128 years, or around year 2142! http://chessgpgpu.blogspot.no/2013/06/solving-chessfacts-and-fiction.html 4

History of Computer Chess Chess is a good fit for computers: Clearly defined rules Game of complete information Easy to evaluate (judge) positions Search tree is not too small or too big 1950: Programming a Computer for Playing Chess (Claude Shannon) 1951: First chess playing program (on paper) (Alan Turing) 1958: First computer program that can play a complete chess game 1981: Cray Blitz wins a tournament in Mississippi and achieves master rating 1989: Deep Thought loses 0-2 against World Champion Garry Kasparov 1996: Deep Blue wins a game against Kasparov, but loses match 2-4 1997: Upgraded Dee Blue wins 3.5-2.5 against Kasparov 2005: Hydra destroys GM Michael Adams 5.5-0.5 2006: World Champion Vladimir Kramnik looses 2-4 against Deep Fritz (PC chess engine) 5

Search Trees and Position Evaluation Search trees (nodes are positions, edges are legal chess moves) Leaf nodes are end positions which needs to be evaluated (judged) A simple judger: Check mate? If not, count material Nodes are marked with a numeric evaluation value 6

Minimax: The Basic Search Algorithm Minimax: Assume that both White and Black plays the best moves. We maximizes White s score Perform a depth-first search and evaluate the leaf nodes Choose child node with highest value if it is White to move Choose child node with lowest value if it is Black to move Branching factor is 40 in a typical chess position White Black White Black White ply = 0 ply = 1 ply = 2 ply = 3 ply = 4 7

NegaMax Simplified Minimax Minimax int maxi( int depth ) { if ( depth == 0 ) return evaluate(); int max = -oo; for ( all moves) { score = mini( depth - 1 ); if( score > max ) max = score; } return max; } int mini( int depth ) { if ( depth == 0 ) return -evaluate(); int min = +oo; for ( all moves) { score = maxi( depth - 1 ); if( score < min ) min = score; } return min; } NegaMax max(a, b) == -min(-a, -b) int negamax( int depth ) { if ( depth == 0 ) return evaluate(); int max = -oo; for ( all moves) { score = -negamax( depth - 1 ); if( score > max ) max = score; } return max; } 8

Node explosion A typical middle-game position has 40 legal moves. Depth Node count Time at 10M nodes / s 1 40 0.000004 s 2 1 600 0.00016 s 3 64 000 0.0064 s 4 2 560 000 0.256 s 5 102 400 000 10.24 s 6 4 096 000 000 6 min 49,6 s 7 163 840 000 000 4 h 33 min 4 s 10 M nodes per second (nps) is realistic for modern chess engines Modern engines routinely reach depths 20-25 ply at tournament play But they only have a few minutes per move, so they should be able to go only 5-6 ply deep How do they then get to depth 20 so easily? 8 6 553 600 000 000 7 d 14 h 2 min 40 s 9

Pruning Techniques The complexity of searching d ply ahead is O(b*b* *b) = O(b d ) With a branching factor (b) of 40 it is crucial to be able to prune the search tree 10

Alpha-Beta Pruning Position is so good for White (or Black) that the opponent with best play will not enter the variation that gives the position. Use previous known max and min values to limit the search tree Alpha value: White is guaranteed this score or better (start value: - ) Beta value: Black is guaranteed this score or less (start value: + ) If Alpha is higher than Beta, then the position will never occur assuming best play If search tree below is evaluated left to right, then we can skip the greyedout sub trees Regardless of what values we get for the grey nodes, they will not influence the root node score White Black White Black White ply = 0 ply = 1 ply = 2 ply = 3 ply = 4 11

Analyze the Best Move First Even with alpha-beta pruning, if we always start with the worst move, we still get O(b*b*..*b) = O(b d ) If we always start with the best move (also recursive) it can be shown that complexity is O(b*1*b*1*b*1 ) = O(b d/2 ) = O( b d ) We can double the search depth without using more resources Conclusion: It is very important to try to start with the strongest moves first 12

Killer-Move Heuristics Killer-move heuristics is based on the assumption that a strong move which gave a large pruning of a sub tree, might also be a strong move in other nodes in the search tree Therefore we start with the killer moves in order to maximize search tree pruning 13

Zero-Move Heuristics Alpha-Beta cutoff: The position is so good for White (or Black) that the opponent with best play will avoid the variation resulting in that position Zero-Move heuristics is based on the fact that in most positions it is an advantage to be the first player to move Let the player (e.g. White) who has just made a move, play another move (two moves in a row), and perform a shallower (2-3 ply less) and therefore cheaper search from that position If the shallower search gives a cutoff value (e.g. bad score for White), it means that most likely the search tree can be pruned at this position without performing a deeper search, since two moves in a row did not help Very effective pruning technique! Cavecats: Check and endgames (where a player can be in trekktvang every move worsens the position) 14

Iterative Deeper Depth-First Search (IDDFS) Since it is so important to evaluate the best move first, it might be worthwhile to execute a shallower search first and then use the resulting alpha/beta cutoff values as start values for a deeper search Since the majority of search nodes are on the lowest level in a balanced search tree, it is relatively cheap to do an extra shallower search 15

Search Tree Extensions PC programs today can compute 18-22 ply ahead (Deep Blue computed 12 ply against Kasparov in 1997, Hydra (64 nodes with FPGAs) computed at least 18 ply) It is important to extend the search in leaf nodes that are unstable Good search extensions includes all moves that gives check or captures a piece The longest search extensions are typically double the average length of the search tree! 16

Transposition Table Same position will commonly occur from different move orders All chess engines therefore has a transposition table (position cache) Implemented using a hash table with chess position as key Doesn t have to evaluate large sub trees over and over again Chess engines typically uses half of available memory to hash table proves how important it is 17

Other challenges Move generator (hardware / software) Hydra (64 nodes Xeon cluster, FPGA chips) computed 200 millions positions per second, approximately the same as Deep Blue (on older ASIC chip sets) Hydra computes 18 ply ahead while Deep Blue only managed 12 (Hydra prunes search tree better) Fritz 13 chess engine (single processor/core) manages 2.3 mill moves/second on my laptop and computes 15+ ply Efficient data structure for a chess board (0x88, bitboards) Opening library suited for a chess computer Position evaluation: Traditionally chess computers has done deep searches with a simple evaluation function But one of the best PC chess engines today, Rybka, sacrifices search depth for a complex position evaluation and better search heuristics 18

Endgame Tablebases Chess engines plays endgames with 3-7 pieces left on the board perfectly by looking up best move in huge tables These endgame databases are called Tablebases Retrograde analyses: Tablebases are generated by starting with final positions (check mate, steal mate or insufficient mating material (e.g. king vs. king)) and then compute backwards until all nodes in search tree are marked as win, draw or loose Using complex compression algorithms (Eugene Nalimov) All 3-5 piece endgames and some 6 piece endgames are stored in just 21 GB http://en.wikipedia.org/wiki/nalimov_tablebase 19

Lomonosov Tablebases All 7 piece endgames (except 6 pieces vs a lone king) calculated for the first time in 2013 on the Lomonosov supercomputer in Moscow State University. Took 6 months to generate Needed 140 TB of storage Longest forced mate: White to mate in 545 moves! See http://chessok.com/?page_id=27966 20

Demo Demo: ChessBase with chess engine Deep Rybka 4, Houdini 4, Komodo 8 and Fritz 13 Best open source UCI chess engine (and may be best overall): Stockfish (stockfishchess.org) 21

Thank you Presenter: Rune Djurhuus Contact: runed@ifi.uio.no runed@microsoft.com Version: Autumn 2014 22