Skip to content

How far a machine looks: the horizon effect and deciding with less

Why a machine that looks only a few moves ahead gives pieces away, how quiescence search saves it and how engines decide by looking at less. AI course 0.7.

Ramón Invarato
Un caballo de ajedrez de cristal oscuro dentro de un círculo de luz sobre un suelo negro que lo refleja; fuera del círculo, otras piezas se ven solo como siluetas en la penumbra, ante un fondo azul oscuro con un resplandor cálido en el horizonte

In Playing against a machine, the machine looked at the whole tic-tac-toe tree, but in Connect 4 it could only look a few moves ahead, and there it estimated how much the position was worth. Where it stops matters more than it seems: if it cuts off too early, it will probably play badly; if it looks too far, it either gains an advantage or wastes computation. And since searching deeper costs something, game engines use tricks to look at less and decide the same.

Try this first

Open the Game tab of the Search tool with Los Alamos chess. It is 6×6 chess without bishops, the variant in which, in 1956, a program written at Los Alamos for the MANIAC I computer was the first to play something like chess (Los Alamos chess, on Wikipedia). The machine looks 3 moves ahead, and the Quiescence search switch is on (quiescence means continuing to look until there is nothing left to capture; it is explained below).

Squares are named with a letter for the column, from a to f, and a number for the row, from 1 to 6, counting from White, which is the machine’s side. The tool’s board is seen from your side, Black, and has the letters and numbers on the edge: row 1 is at the top and column a on the right.

Press Let the machine start. It advances the pawn from square a2 to square a3 (a2–a3), and below it shows what it would have played with quiescence and without it. “The line it expects” is the move it would make and the ones it expects to come after, one from each side: its own, yours, its own. A “×” means that move captures: knight c3×b5 is “the knight on c3 captures whatever is on b5”.

Would playThinks it is worthThe line it expects
With quiescencea2–a3+8a2–a3 a5–a4 b2–b3
Without quiescenceknight b1–c3+102knight b1–c3, d5–d4, knight c3×b5

To rate a position, the machine counts the pieces: a pawn is worth 100, a knight 300 and a queen 900 (the tool itself says so below the table), and it subtracts yours from its own; that is why the values are in hundredths of a pawn. +102 means that, at the end of the line it expects, it thinks it will have one pawn more than you: that it will have captured one without losing anything in return. Look at the end of the line: the knight captures your pawn on b5 and that is where it stops. But that pawn is defended by your queen on c6, which would capture the knight on the next move. The machine would give a knight, worth 300, for a pawn, worth 100. It does not see it because that move lies beyond the 3 it looks at.

Los Alamos in the Search tool after the machine starts, with a2–a3 played and the coordinates on the edge of the board. Below, the table of what it thought: with quiescence, a2–a3, +8, 532 positions; without quiescence, boxed, knight b1–c3, +102, 509 positions, with the line knight b1–c3, d5–d4, knight c3×b5, and the warning "Without quiescence it would have played something else"

Move by move, from your side of the board, it looks like this:

Four Los Alamos boards seen from Black, with row 1 at the top and the columns from f to a. 1: the machine, as White, plays the knight from b1 to c3. 2: it expects Black to play the pawn from d5 to d4. 3: the knight on c3 captures the black pawn on b5; "Its 3 moves end here. Count the pieces: one pawn more, +102". 4, set apart by a dashed red line, what it does not see: the black queen on c6 captures the knight on b5; "It has given a knight, worth 300, for a pawn, worth 100: 200 down"

Now move How many moves it looks ahead to 4, or open the same game looking 4 ahead, and let it start again. Without quiescence it goes wrong the other way round: it thinks the knight on c3 loses a pawn (−98), because its line ends with your knight capturing the pawn on c2, and it does not see that afterward it can capture that knight. With quiescence it goes back to a2–a3.

Los Alamos in the Search tool looking 4 moves ahead, with a2–a3 played and the coordinates on the edge of the board. Below, the table: with quiescence, a2–a3, 0, 5,157 positions, with the line a2–a3 a5–a4 b2–b3 b5–b4; without quiescence, boxed, knight b1–c3, −98, 1,398 positions, with the line knight b1–c3, knight e6–d4, a2–a3, knight d4×c2, and the warning "Without quiescence it would have played something else". At the bottom, the piece values: a pawn 100, a knight 300, a bishop 320, a rook 500, a queen 900

What just happened

This is called the horizon effect, and it was named by Hans Berliner in 1973 (Horizon effect, on Wikipedia). The machine looks to a fixed depth and there it estimates with a simple count: it adds up what each piece is worth. If the search stops right after a capture, the count says “I’m winning” or “I’m losing”, even though the obvious reply turns it around on the next move. What lies beyond the limit does not exist for it.

Quiescence search fixes it without searching deeper. On reaching the limit it does not estimate right away: it keeps looking only at captures, one after another, until it reaches a quiet position, with nothing to capture. Only then does it estimate. It costs little, because captures are few: with 3 moves, 532 positions against 509. With 4, 5,157 against 1,398: the difference grows, but it is still less than looking one more move without quiescence, which with 5 is 8,170.

The other problem is size. Open checkers (the tool’s tab is called Draughts), on a 6×6 board, and press Let the machine start. The machine looks 8 moves ahead, and the table says how many positions it cost to decide with each technique. The value and the move come out the same with all of them:

SearchPositions looked atCompared with pruning only
Minimax without pruning55,850×11 more
Alpha-beta pruning4,915—
Pruning + move ordering2,984×1.6
Pruning + transposition table3,369×1.5
All three2,248×2.2
Checkers in the Search tool after the machine starts, looking 8 moves ahead. Below, the table of techniques: minimax without pruning, 55,850 positions; and boxed, alpha-beta pruning 4,915, pruning plus move ordering 2,984, pruning plus transposition table 3,369 and all three together 2,248, ×2.2 less than pruning alone

Move ordering helps the alpha-beta pruning from 0.6. Pruning stops looking at a move as soon as it sees it is worse than another one it already has. If the best one is looked at first, all the others are discarded. The tool tries captures first and, at each level of the tree, the move that caused a cut-off last time (Alpha–beta pruning, on Wikipedia).

The transposition table is memory. The same position can be reached by different paths: if you move piece A and then B, or B first and then A, the board ends up the same. The table notes what each position already seen was worth, so as not to calculate it again (Transposition table, on Wikipedia).

Three Los Alamos boards seen from Black. On the left, path A: White has played the knight from b1 to c3. On the right, path B: it has played the knight from e1 to f3. In the center, the same position both reach, with Black's a5–a4 and the other knight: white knights on c3 and f3 and the black pawn on a4. Below: "Transposition table. The first time it reaches this position, by path A, it calculates it and notes what it is worth and how far it looked. When it reaches it again by path B, it finds it in the table and does not calculate it again"

The deeper the search, the more they pay off. With 12 moves, pruning alone looks at 108,860 positions and the three together at 34,524, three times fewer. In real checkers, on 8×8, there is a famous program, Chinook, from Jonathan Schaeffer’s team at the University of Alberta. In 1994 it was declared man-machine world champion, and in 2007 its team proved that checkers, played perfectly by both sides, ends in a draw (Chinook, on Wikipedia).

A real chess program puts all of this together, and more. Open full chess and press New game as Black, so that the machine, playing White, moves first. It does not look to a fixed depth but by time: it searches to 1 move, then to 2, to 3…, and uses what it learnt in each pass to order the moves of the next one. This is iterative deepening (Iterative Deepening, on the Chess Programming Wiki). When time runs out, it keeps the last complete pass. In a game in my browser, with 1 second per move, it reached 7 moves and looked at 527,669 positions; on your computer the figures will be different, because they depend on how fast it is. Besides the pruning, the move ordering, the transposition table and the quiescence search of this article, it uses tricks to prune even more, such as the null move: if the position is still good even after passing the turn, there is no need to look at it in depth (Null move pruning, on the Chess Programming Wiki).

The "What it thought for its last move" table of full chess, after the machine's first move with 1 second: depth 1, knight c3, +0.5, 20 positions; 2, 464 positions; 3, 1,999; 4, 12,873; 5, 36,373; 6, 198,485; and 7, +0.2, 527,669 positions. Below: "Iterative deepening: it searches to 1, then 2, then 3… and uses what it learnt in each pass to order the next one. When time runs out, it keeps the last complete pass"

The classic mistake

You might think the horizon is fixed by looking at more moves. It is not: it moves somewhere else. With 3 moves, the machine thinks it wins a pawn; with 4, that it loses it. And there will always be a move just behind the limit. The fix is never to stop and estimate in the middle of an exchange of pieces.

With the checkers tricks it is the other way round. They do not change the decision, only what it costs to reach it. The advantage is what is done with the time saved: searching deeper in the same time, and that does make it play better.

Try it yourself

  • Open Los Alamos chess, turn off Quiescence search and play a game. Can you get the machine to leave a piece hanging? Look at the table after each of its moves: when it says “Without quiescence it would have played something else”, look at how its line ends.

Test yourself

Open Los Alamos looking 5 moves ahead and let the machine start. What would it play with quiescence and what would it play without it?

With quiescence it plays [a2–a3 / knight b1–c3 / b2–b3]. Without quiescence it plays [a2–a3 / knight b1–c3 / b2–b3], and it thinks it is worth ___.

Look at the table of what it thought, below the board.

See the answer

Answers: a2–a3, b2–b3 and 108.

Look at how its line ends without quiescence: b2–b3, knight e6–f4, knight b1–c3, d5–d4, knight c3×b5. It is the same trap as with 3 moves, two moves further on: it captures the pawn on b5 and does not see that your queen captures the knight. The horizon has moved somewhere else, but it is still there. With quiescence it goes back to a2–a3.

Test yourself

In checkers, raise the depth one at a time from 8 and let the machine start each time. At what depth does the table stop measuring minimax without pruning? How many positions does All three look at with 10?

It stops measuring it at ___ moves. With 10, the three techniques together look at ___ positions.

In the Minimax without pruning row it says “not measured: it would take too long”.

See the answer

Answers: 9 and 9025.

Without pruning, each extra move multiplies the work: with 8 it was already 55,850 positions, and with 9 the tool prefers not to wait. With the three techniques, 10 moves cost 9,025 positions, six times fewer than minimax without pruning with 8.

Further reading

“How far a machine looks: the horizon effect and deciding with less” by Ramón Invarato at jarroba.com, licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0. Registered at Safe Creative.

Ramón Invarato

I design and build data platforms and intelligent systems, and I enjoy sharing that experience here.

You may also like

Comments

Leave a comment


This site uses Akismet to reduce spam. Learn how your comment data is processed.