Why does a board-game AI with a fixed time per move use iterative deepening instead of one fixed-depth alpha-beta search?
answer
- always have an answer ready
- depth 1, then 2, then 3
- earlier passes are a small fraction
- previous best move searched first
basics
~20 sIterative deepening runs alpha-beta at depth 1, 2, 3 and so on until time runs out, so a complete answer is always ready. Shallow passes cost little, and their best moves order the next pass so alpha-beta cuts more.
solid answer
~50 sThe time a fixed-depth search needs varies enormously between quiet and tactical positions, so a fixed depth either wastes the budget or overruns it. Iterative deepening searches depth 1, then 2, then 3, keeping the best move of the last completed iteration, and stops when the clock says so; that makes the search **anytime**. Repeating shallow work is cheap: in a uniform tree each iteration costs about `b` times the last, so all earlier ones add about `1/(b-1)` of it. Better still, each iteration hands the next its best moves, through the transposition table, the principal variation, killers and history, and with alpha-beta that ordering often makes the deeper search cheaper than a cold one. Iterations also supply an aspiration window around the previous score and a growth estimate for deciding whether the next depth can finish in time.
go deeper
Recall the loop: search depth 1, 2, 3 and so on, keep the best move of each finished pass, and stop when time runs out.
Explain why the repeated work is small, about one over b minus one of the last pass, and how earlier passes improve move ordering for alpha-beta.
Show sound time management: predicting the next depth's cost, when a partial iteration's move can be trusted, aspiration-window re-searches and reacting to a fail low.
Decide how the time budget is shared across a game, such as spending more on critical moves, and justify it with match results rather than node counts.
## The problem with one fixed depth A board-game AI is usually given a **time budget per move**, say two seconds. The number of nodes alpha-beta needs at a given depth varies enormously from one position to the next: a quiet position with few captures may finish depth 8 quickly, while a sharp tactical position with many captures and long quiescence searches may not finish depth 6. A **fixed depth** therefore either finishes early and wastes the budget, or overruns it and leaves the AI with no answer, or with half a search whose result cannot be trusted. ## How iterative deepening works **Iterative deepening** wraps the search in a loop: 1. Search the root to depth 1 with alpha-beta and record the best move and its score. 2. Search again to depth 2, then 3, and so on, each time recording the best move of the **completed** iteration. 3. Before starting a new depth, estimate whether it can finish in the time left; if not, stop. 4. If the clock runs out in the middle of an iteration, abort it and play the best move you can trust. This makes the search **anytime**: whenever it is interrupted, a complete answer from a slightly shallower search is available. ## Why repeating the shallow searches is cheap In a uniform tree with branching factor `b`, the iteration at depth `k` costs about `b` times the one at depth `k-1`. The earlier iterations together therefore cost about `1/(b-1)` of the last one, and the whole loop about `b/(b-1)` times a single search at the final depth: | Effective branching factor | Extra work from earlier iterations | |---|---| | 30 (full width) | about 3.4% | | about 5.5 (square root of 30, ideal alpha-beta) | about 22% | Alpha-beta shrinks the effective branching factor, so the overhead in this model grows. In practice the whole loop often costs **less** than one cold search straight to the final depth, because of what the earlier iterations hand it. ## What each iteration hands the next - **The principal variation (PV)**, the line of best play found so far. Searching that line first at each node gives alpha-beta strong early bounds. - **Transposition-table moves**: the best move stored for each position searched last time, tried first when the position comes up again. - **Killer and history tables**, which keep learning across iterations which quiet moves cause cutoffs. - **The previous score**, which seeds an **aspiration window**: search the next depth with a narrow window such as `(score - 50, score + 50)` in the evaluation's units. If the result lands inside, many nodes were saved; if it falls outside (a fail low or fail high), re-search with a wider window. Good move ordering is what brings alpha-beta near its best case, and iterative deepening is the cheapest way to get it. ## Time management and aborting - **Predict the next iteration.** Its cost is roughly the last iteration's time multiplied by the effective branching factor observed so far. Do not start a depth that cannot finish. - **Check the clock cheaply**, for example every few thousand nodes rather than at every node. - **Use a partial iteration carefully.** The previous best move is searched first, so once it has finished at the new depth, any other root move that also finished and scored higher is safe to play. A move that was only partly searched is not. - **React to a fail low.** If the previous best move suddenly scores much worse at the new depth, many engines spend extra time to find an alternative rather than playing into the trouble. - **Compare like with like.** Scores often oscillate between odd and even depths, because one side makes the last move before evaluation, so a jump between two consecutive iterations is not always a real change. ## Fixed depth versus iterative deepening | Concern | Fixed-depth search | Iterative deepening | |---|---|---| | Time per move | unpredictable | bounded by the budget | | Interrupted search | no usable answer | last completed iteration | | Move ordering | cold, guesses only | warmed by earlier iterations | | Extra cost | none | earlier iterations, usually repaid by ordering |
- Time runs out during depth 9. When may the AI play a move from that unfinished iteration?Only when it is backed by complete depth-9 searches: the previous best move is searched first, so once it has finished, any other root move that also finished and scored higher is reliably better at depth 9. A move whose subtree was cut off by the clock carries no trustworthy score, so otherwise the AI plays the depth-8 move.
- What happens when an aspiration-window search fails high or low?The true score lies outside the narrow window, so the returned value is only a bound. The search widens the window on the failing side, or opens it fully, and re-searches that depth. Frequent re-searches cost more than the window saves, so the window width is tuned, and positions with unstable scores often need a wider window.
- Iterative deepening wastes most of its time repeating shallow searches.
- Earlier iterations prune moves outright from deeper searches.
- When time expires, play the move being searched at that moment.
- A fixed depth gives a predictable time per move in every position.
- The transposition table makes repeated shallow iterations completely free.