170 likes | 502 Views
Transposition Tables. Jos Uiterwijk May 3, 2006. Transpositions. A transposition is the re-occurrence of a position in a search process. For example, in Chess the position after 1. e4 e5 2. Nf3 is the same as after 1. Nf3 e5 2. e4. Transposition tables.
E N D
Transposition Tables Jos Uiterwijk May 3, 2006
Transpositions • A transposition is the re-occurrence of a position in a search process. • For example, in Chess the position after 1. e4 e5 2. Nf3 is the same as after 1. Nf3 e5 2. e4.
Transposition tables • So, the search tree actually is a search graph • Therefore, storing the information in a table can give huge gains. E.g., in an alpha-beta search process, store the: • Value • Best move/action • Search depth • Flag (real value, upper bound or lower bound) • Identification (hash key, see further)
Transposition tables • Normally the number of possible positions largely exceeds the available memory for a transposition table. E.g., Chess has some 1050 possible positions. • Solution: hashing. • Requirements: • Unique mapping from position to table • Quick calculation of table entry • Uniform distribution of positions over the table
Zobrist hashing • Uses de XOR operator to calculate the table entry • Properties of XOR-operator: • a XOR (b XOR c) = (a XOR b) XOR c • a XOR b = b XOR a • a XOR a = 0 • If si= r1 XOR r2 XOR … XOR rn with ri random numbers, then { si } also is random • { si } has a uniform distribution
Zobrist hashing • Example: suppose we want to hash words of 3 letters, only using ‘A’ – ‘Z’. We start with 78 random numbers: s1,1 means a letter ‘A’ in position 1 s1,2 means a letter ‘A’ in position 2 s1,3 means a letter ‘A’ in position 3 … s26,1 means a letter ‘Z’ in position 1 s26,2 means a letter ‘Z’ in position 2 s26,3 means a letter ‘Z’ in position 3
Zobrist hashing • The hash of the word ‘CAT’ is obtained by XORing the concerning random numbers, thus: hash value(CAT) = s3,1 XOR s1,2 XOR s20,3 • For a board game: • Suppose m different possible pieces on a square (Chess: m = 12, Go: m = 2) • Suppose n squares (Chess: n = 64, Go: n = 361) • Then m x n different combinations of pieces/square • So m x n random numbers needed for calculating the hash value of a position
Zobrist hashing • Often possible for incremental updating the hash value: • Adding one piece (Go): hash value (new_position) = hash value (old_position) XOR random number (new piece) • Moving a piece (Chess): hash value (new_position) = hash value (old_position) XOR random number (from_square) XOR random number (to_square) • For “difficult” moves sometimes more operations needed
Transposition table mapping • The hash value is used to map a position to a table: ↓
Hash key • Since the number of possible hash values normally far exceeds the number of entries, we only use part of the hash value (say, k bits) as a the entry. This is called the primary hash code. Therefore, transposition tables typically have 2k entries. • Another hash value (or typically the remaining bits) are used for identifications purposes (secondary hash code or hash key). • E.g., for 64-bits random numbers 20 bits are used as primary hash code for the mapping on a 220 entry transposition table, and the remaining 44 bits are used as hash key.
Errors • Two types of error: • Type-1 error: two positions having the same hash code (primary + secondary). This is serious since this can remain undetected! • Type-2 error: two positions having the same primary hash code. This is called a clash or collision. Now we should use a replacement scheme, e.g., keep the deepest investigated, or the newest, or others.
Example 1 • Example of a midgame position in Chess:
Example 2 • Example of an endgame position in Chess:
Conclusions • Transposition tables can be of great importance, with huge savings • Importance depends on type of game and type of position • Zobrist hashing is a convenient way of storing positions • The number of bits must be sufficiently large to avoid errors