Zobrist hashing

Last updated

Zobrist hashing (also referred to as Zobrist keys or Zobrist signatures [1] ) is a hash function construction used in computer programs that play abstract board games, such as chess and Go, to implement transposition tables, a special kind of hash table 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. [2] It has also been applied as a method for recognizing substitutional alloy configurations in simulations of crystalline materials. [3] Zobrist hashing is the first known instance of the generally useful underlying technique called tabulation hashing.

Contents

Calculation of the hash value

Zobrist hashing starts by randomly generating bitstrings 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,[ dubious ] and pawns that may capture en passant , 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. Example pseudocode for the game of chess:[ citation needed ]

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[i][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[i] ≠ empty:             j := the piece at board[i], as listed in the constant indices, above             h := h XOR table[i][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. [1] 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 collisions 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. The result is a 3-wise independent hash family. In particular, it is strongly universal.

As an example, in chess, 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. [1]

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. [1] For instance, if a pawn on a chessboard square is replaced by a rook 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 computer Go, this technique is also used for superko detection.

Wider usage

More generically, Zobrist hashing can be applied over finite sets of elements (in the chess example, these elements are tuples), as long as a random bitstring can be assigned to each possible element. This can be either done with a random number generator for smaller element spaces, or with a hash function for larger ones. This method has been used to recognize substitutional alloy configurations during Monte Carlo simulations in order to prevent wasting computational effort on states that have already been calculated. [3]

See also

Related Research Articles

<span class="mw-page-title-main">Hash function</span> Mapping arbitrary data to fixed-size values

A hash function is any function that can be used to map data of arbitrary size to fixed-size values, though there are some hash functions that support variable length output. The values returned by a hash function are called hash values, hash codes, hash digests, digests, or simply hashes. The values are usually used to index a fixed-size table called a hash table. Use of a hash function to index a hash table is called hashing or scatter storage addressing.

Chess strategy is the aspect of chess play concerned with evaluation of chess positions and setting goals and long-term plans for future play. While evaluating a position strategically, a player must take into account such factors as the relative value of the pieces on the board, pawn structure, king safety, position of pieces, and control of key squares and groups of squares. Chess strategy is distinguished from chess tactics, which is the aspect of play concerned with the move-by-move setting up of threats and defenses. Some authors distinguish static strategic imbalances, which tend to persist for many moves, from dynamic imbalances, which are temporary. This distinction affects the immediacy with which a sought-after plan should take effect. Until players reach the skill level of "master", chess tactics tend to ultimately decide the outcomes of games more often than strategy. Many chess coaches thus emphasize the study of tactics as the most efficient way to improve one's results in serious chess play.

<span class="mw-page-title-main">Chess piece</span> Game piece for playing chess

A chess piece, or chessman, is a game piece that is placed on a chessboard to play the game of chess. It can be either white or black, and it can be one of six types: king, queen, rook, bishop, knight, or pawn.

<span class="mw-page-title-main">Pawn (chess)</span> Chess piece

The pawn is the most numerous and weakest piece in the game of chess. It may move one square directly forward, it may move two squares directly forward on its first move, and it may capture one square diagonally forward. Each player begins a game with eight pawns, one on each square of their second rank. The white pawns start on a2 through h2; the black pawns start on a7 through h7.

<span class="mw-page-title-main">Rules of chess</span> Rules of play for the game of chess

The rules of chess govern the play of the game of chess. Chess is a two-player abstract strategy board game. Each player controls sixteen pieces of six types on a chessboard. Each type of piece moves in a distinct way. The object of the game is to checkmate the opponent's king; checkmate occurs when a king is threatened with capture and has no escape. A game can end in various ways besides checkmate: a player can resign, and there are several ways a game can end in a draw.

<span class="mw-page-title-main">Fischer random chess</span> Chess variant invented by Bobby Fischer

Fischer random chess, also known as Chess960 and freestyle chess, is a variation of the game of chess invented by the former world chess champion Bobby Fischer. Fischer announced this variation on June 19, 1996, in Buenos Aires, Argentina. Fischer random chess employs the same board and pieces as classical chess, but the starting position of the pieces on the players' home ranks is randomized, following certain rules. The random setup makes gaining an advantage through the memorization of openings impracticable; players instead must rely more on their skill and creativity over the board.

The endgame is the final stage of a chess game which occurs after the middlegame. It begins when few pieces are left on the board.

This glossary of chess explains commonly used terms in chess, in alphabetical order. Some of these terms have their own pages, like fork and pin. For a list of unorthodox chess pieces, see Fairy chess piece; for a list of terms specific to chess problems, see Glossary of chess problems; for a list of named opening lines, see List of chess openings; for a list of chess-related games, see List of chess variants; for a list of terms general to board games, see Glossary of board games.

Capablanca chess is a chess variant invented in the 1920s by World Chess Champion José Raúl Capablanca. It incorporates two new pieces and is played on a 10×8 board. Capablanca believed that chess would be played out in a few decades. This threat of "draw death" for chess was his main motivation for creating a more complex version of the game.

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 value of the position is retrieved from the table, avoiding re-searching the game tree below that position. Transposition tables are primarily useful in perfect-information games. The usage of transposition tables is essentially memoization applied to the tree search and is a form of dynamic programming.

A bitboard is a specialized bit array data structure commonly used in computer systems that play board games, where each bit corresponds to a game board space or piece. This allows parallel bitwise operations to set or query the game state, or determine moves or plays in the game.

In chess, a relative value is a standard value conventionally assigned to each piece. Piece valuations have no role in the rules of chess but are useful as an aid to assessing a position.

<span class="mw-page-title-main">Promotion (chess)</span> Chess rule

In chess, promotion is the replacement of a pawn with a new piece when the pawn is moved to its last rank. The player replaces the pawn immediately with a queen, rook, bishop, or knight of the same color. The new piece does not have to be a previously captured piece. Promotion is mandatory when moving to the last rank; the pawn cannot remain as a pawn.

<span class="mw-page-title-main">Touch-move rule</span> Chess rule requiring a player to move or capture a piece deliberately touched

The touch-move rule in chess specifies that a player, having the move, who deliberately touches a piece on the board must move or capture that piece if it is legal to do so. If it is the player's piece that was touched, it must be moved if the piece has a legal move. If the opponent's piece was touched, it must be captured if it can be captured with a legal move. If the touched piece cannot be legally moved or captured, there is no penalty. This is a rule of chess that is enforced in all formal over-the-board competitions.

<span class="mw-page-title-main">Board representation (computer chess)</span>

Board representation in computer chess is a data structure in a chess program representing the position on the chessboard and associated game state. Board representation is fundamental to all aspects of a chess program including move generation, the evaluation function, and making and unmaking moves as well as maintaining the state of the game during play. Several different board representations exist. Chess programs often utilize more than one board representation at different times, for efficiency. Execution efficiency and memory footprint are the primary factors in choosing a board representation; secondary considerations are effort required to code, test and debug the application.

<span class="mw-page-title-main">Checkmate pattern</span> Chess patterns

In chess, several checkmate patterns occur frequently enough to have acquired specific names in chess commentary. By definition, a checkmate pattern is a recognizable/particular/studied arrangements of pieces that delivers checkmate. The diagrams that follow show these checkmates with White checkmating Black.

<span class="mw-page-title-main">Outline of chess</span> Strategy board game

The following outline is provided as an overview of and topical guide to chess:

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 by Carter and Wegman extended this method to arbitrary fixed-length keys. Generalizations of tabulation hashing have also been developed that can handle variable-length keys such as text strings.

Chess on a really big board is a large chess variant invented by Ralph Betza around 1996. It is played on a 16×16 chessboard with 16 pieces and 16 pawns per player. Since such a board can be constructed by pushing together four standard 8×8 boards, Betza also gave this variant the alternative names of four-board chess or chess on four boards.

References

  1. 1 2 3 4 Bruce Moreland. Zobrist keys: a means of enabling position comparison.
  2. Albert Lindsey Zobrist, A New Hashing Method with Application for Game Playing, Tech. Rep. 88, Computer Sciences Department, University of Wisconsin, Madison, Wisconsin, (1969).
  3. 1 2 Mason, D. R.; Hudson, T. S.; Sutton, A. P. (2005). "Fast recall of state-history in kinetic Monte Carlo simulations utilizing the Zobrist key". Computer Physics Communications. 165 (1): 37–48. Bibcode:2005CoPhC.165...37M. doi:10.1016/j.cpc.2004.09.007.