Zobrist Hashing
   HOME

TheInfoList



OR:

Zobrist hashing (also referred to as Zobrist keys or Zobrist signatures Bruce Moreland

/ref>) is a
hash function A hash function is any function that can be used to map data of arbitrary size to fixed-size values. The values returned by a hash function are called ''hash values'', ''hash codes'', ''digests'', or simply ''hashes''. The values are usually u ...
construction used in
computer program A computer program is a sequence or set of instructions in a programming language for a computer to execute. Computer programs are one component of software, which also includes documentation and other intangible components. A computer program ...
s that play
abstract board game Abstract strategy games admit a number of definitions which distinguish these from strategy games in general, mostly involving no or minimal narrative theme, outcomes determined only by player choice (with no randomness), and perfect information. ...
s, such as
chess Chess is a board game for two players, called White and Black, each controlling an army of chess pieces in their color, with the objective to checkmate the opponent's king. It is sometimes called international chess or Western chess to disti ...
and Go, to implement
transposition table {{no footnotes, date=November 2017 A transposition table is a cache of previously seen positions, and associated evaluations, in a game tree generated by a computer game playing program. If a position recurs via a different sequence of moves, the ...
s, a special kind of
hash table In computing, a hash table, also known as hash map, is a data structure that implements an associative array or dictionary. It is an abstract data type that maps keys to values. A hash table uses a hash function to compute an ''index'', als ...
that is indexed by a board position and used to avoid analyzing the same position more than once. Zobrist hashing is named for its inventor,
Albert Lindsey Zobrist Albert Lindsey Zobrist (born February 27, 1942) is an American computer scientist, games researcher, and inventor of the famous Zobrist Hashing, which was published in 1970. He is further author of the first Go program in 1968 as part of his P ...
.Albert Lindsey Zobrist
''A New Hashing Method with Application for Game Playing''
Tech. Rep. 88, Computer Sciences Department, University of Wisconsin, Madison, Wisconsin, (1969).
It has also been applied as a method for recognizing substitutional alloy configurations in simulations of crystalline materials. Zobrist hashing is the first known instance of the generally useful underlying technique called
tabulation hashing In computer science, tabulation hashing is a method for constructing universal families of hash functions by combining table lookup with exclusive or operations. It was first studied in the form of Zobrist hashing for computer games; later work b ...
.


Calculation of the hash value

Zobrist hashing starts by randomly generating
bitstring A bit array (also known as bitmask, bit map, bit set, bit string, or bit vector) is an array data structure that compactly stores bits. It can be used to implement a simple set data structure. A bit array is effective at exploiting bit-level par ...
s for each possible element of a board game, i.e. for each combination of a piece and a position (in the game of chess, that's 12 pieces × 64 board positions, or 18 × 64 if kings and rooks that may still castle, and pawns that may capture ''
en passant ''En passant'' (, "in passing") is a method of capturing in chess that occurs when a pawn captures a horizontally adjacent enemy pawn that has just made an initial two-square advance. The capturing pawn moves to the square that the enemy paw ...
'', are treated separately for both colors). Now any board configuration can be broken up into independent piece/position components, which are mapped to the random bitstrings generated earlier. The final Zobrist hash is computed by combining those bitstrings using bitwise
XOR Exclusive or or exclusive disjunction is a logical operation that is true if and only if its arguments differ (one is true, the other is false). It is symbolized by the prefix operator J and by the infix operators XOR ( or ), EOR, EXOR, , ...
. Example pseudocode for the game of chess: constant indices white_pawn := 1 white_rook := 2 # etc. black_king := 12 function init_zobrist(): # fill a table of random numbers/bitstrings table := a 2-d array of size 64×12 for i from 1 to 64: # loop over the board, represented as a linear array for j from 1 to 12: # loop over the pieces table j] := random_bitstring() table.black_to_move = random_bitstring() function hash(board): h := 0 if is_black_turn(board): h := h XOR table.black_to_move for i from 1 to 64: # loop over the board positions if board ≠ empty: j := the piece at board as listed in the constant indices, above h := h XOR table j] return h


Use of the hash value

If the bitstrings are long enough, different board positions will almost certainly hash to different values; however longer bitstrings require proportionally more computer resources to manipulate. The most commonly used bitstring (key) length is 64 bits. Many game engines store only the hash values in the transposition table, omitting the position information itself entirely to reduce memory usage, and assuming that
hash collision In computer science, a hash collision or hash clash is when two pieces of data in a hash table share the same hash value. The hash value in this case is derived from a hash function which takes a data input and returns a fixed length of bits. Al ...
s will not occur, or will not greatly influence the results of the table if they do. Zobrist hashing is the first known instance of
tabulation hashing In computer science, tabulation hashing is a method for constructing universal families of hash functions by combining table lookup with exclusive or operations. It was first studied in the form of Zobrist hashing for computer games; later work b ...
. The result is a 3-wise independent hash family. In particular, it is strongly
universal Universal is the adjective for universe. Universal may also refer to: Companies * NBCUniversal, a media and entertainment company ** Universal Animation Studios, an American Animation studio, and a subsidiary of NBCUniversal ** Universal TV, a ...
. As an example, in
chess Chess is a board game for two players, called White and Black, each controlling an army of chess pieces in their color, with the objective to checkmate the opponent's king. It is sometimes called international chess or Western chess to disti ...
, at any one time each of the 64 squares can at any time be empty, or contain one of the 6 game pieces, which are either black or white. Also, it can be either black's turn to play or white's turn to play. Thus one needs to generate 6 x 2 x 64 + 1 = 769 random bitstrings. Given a position, one obtains its Zobrist hash by finding out which pieces are on which squares, and combining the relevant bitstrings together. If the position is black to move, the black-to-move bitstring is also included in the Zobrist hash.


Updating the hash value

Rather than computing the hash for the entire board every time, as the pseudocode above does, the hash value of a board can be incrementally updated simply by XORing out the bitstring(s) for positions that have changed, and XORing in the bitstrings for the new positions. For instance, if a pawn on a chessboard square is replaced by a
rook Rook (''Corvus frugilegus'') is a bird of the corvid family. Rook or rooks may also refer to: Games *Rook (chess), a piece in chess *Rook (card game), a trick-taking card game Military * Sukhoi Su-25 or Rook, a close air support aircraft * USS ...
from another square, the resulting position would be produced by XORing the existing hash with the bitstrings for: 'pawn at this square' (XORing ''out'' the pawn at this square) 'rook at this square' (XORing ''in'' the rook at this square) 'rook at source square' (XORing ''out'' the rook at the source square) This makes Zobrist hashing very efficient for traversing a
game tree In the context of Combinatorial game theory, which typically studies sequential games with perfect information, a game tree is a graph representing all possible game states within such a game. Such games include well-known ones such as chess, check ...
. In
computer Go Computer Go is the field of artificial intelligence (AI) dedicated to creating a computer program that plays the traditional board game Go. The field is sharply divided into two eras. Before 2015, the programs of the era were weak. The best ...
, this technique is also used for superko detection.


Wider usage

More generically, Zobrist hashing can be applied over finite
set Set, The Set, SET or SETS may refer to: Science, technology, and mathematics Mathematics *Set (mathematics), a collection of elements *Category of sets, the category whose objects and morphisms are sets and total functions, respectively Electro ...
s of elements (in the chess example, these elements are (piece, position)
tuple In mathematics, a tuple is a finite ordered list (sequence) of elements. An -tuple is a sequence (or ordered list) of elements, where is a non-negative integer. There is only one 0-tuple, referred to as ''the empty tuple''. An -tuple is defi ...
s), as long as a random bitstring can be assigned to each possible element. This can be either done with a
random number generator Random number generation is a process by which, often by means of a random number generator (RNG), a sequence of numbers or symbols that cannot be reasonably predicted better than by random chance is generated. This means that the particular out ...
for smaller element spaces, or with a
hash function A hash function is any function that can be used to map data of arbitrary size to fixed-size values. The values returned by a hash function are called ''hash values'', ''hash codes'', ''digests'', or simply ''hashes''. The values are usually u ...
for larger ones. This method has been used to recognize
substitutional alloy An alloy is a mixture of chemical elements of which at least one is a metal. Unlike chemical compounds with metallic bases, an alloy will retain all the properties of a metal in the resulting material, such as electrical conductivity, ductility, ...
configurations during Monte Carlo simulations in order to prevent wasting computational effort on states that have already been calculated.{{Cite journal , last1 = Mason , first1 = D. R. , last2 = Hudson , first2 = T. S. , last3 = Sutton , first3 = A. P. , title = Fast recall of state-history in kinetic Monte Carlo simulations utilizing the Zobrist key , doi = 10.1016/j.cpc.2004.09.007 , journal = Computer Physics Communications , volume = 165 , pages = 37 , year = 2005 , bibcode = 2005CoPhC.165...37M


See also

*
Alpha-beta pruning Alphabeta is an Israeli musical group. Alphabeta or Alpha Beta may also refer to: *The Greek alphabet, from ''Alpha'' (Αα) and ''Beta'' (Ββ), the first two letters *Alpha Beta, a former chain of Californian supermarkets *Alpha and beta anomers ...


References

Computer chess Computer Go Game artificial intelligence Hash functions Articles with example pseudocode