Transposition Table in Connect Four

Definition

A transposition table is a cache that stores the evaluation of every position the engine has previously computed. Looking up a cached position is much faster than recomputing it.

Explanation

A transposition table is a cache that stores the evaluation of every position the engine has previously evaluated. Connect Four positions can be reached via many different move orders (transpositions). Without a transposition table, the engine wastes time recomputing the same position multiple times. With a transposition table, each unique position is computed once and looked up afterwards. The speedup is enormous, often 10x or more in practice.

The implementation uses a hash table indexed by a hash of the board position. When the engine reaches a position during search, it computes the hash and checks the table. If the position is in the table, the engine retrieves the cached evaluation and skips the recomputation. If not, the engine computes the evaluation normally and stores the result in the table for future use. The hash function must be carefully designed to minimize collisions where two different positions accidentally get the same hash.

Memory management for the transposition table is a tradeoff. A larger table holds more positions and produces more cache hits, but it uses more RAM. Modern engines typically allocate hundreds of megabytes to the transposition table, storing tens of millions of positions. When the table fills up, the engine uses replacement strategies to decide which positions to evict. Common strategies prefer to keep deeply-searched positions over shallowly-searched ones because the deeper analysis was more expensive to compute.

Beyond search acceleration, transposition tables enable other engine features. The engine can use the table to extract a "principal variation" showing the expected sequence of best moves several plies ahead. It can also use the table to detect repetitions and other special positions. For Connect Four engines, transposition tables combined with iterative deepening and alpha-beta pruning produce strong play within tight time budgets.

Example

The engine evaluates a position via move sequence A. Later, it reaches the same position via sequence B (a transposition). It looks up the position in the table, retrieves the cached evaluation in microseconds, and skips re-searching the entire subtree.

Related Articles

How the Engine Works

Put It Into Practice

Understanding transposition table is one thing. Applying it is another.