3 SOLVING PROBLEMS BYSEARCHING
3.2 E XAMPLE P ROBLEMS
3.2.1 Toy problems
The first example we examine is the vacuum world first introduced in Chapter 2. (See Figure 2.2.) This can be formulated as a problem as follows:
• States: The state is determined by both the agent location and the dirt locations. The agent is in one of two locations, each of which might or might not contain dirt. Thus, there are2×22 = 8possible world states. A larger environment with nlocations has n·2nstates.
• Initial state: Any state can be designated as the initial state.
• Actions: In this simple environment, each state has just three actions: Left,Right, and Suck. Larger environments might also includeUpandDown.
• Transition model: The actions have their expected effects, except that movingLeftin the leftmost square, movingRightin the rightmost square, andSucking in a clean square have no effect. The complete state space is shown in Figure 3.3.
• Goal test: This checks whether all the squares are clean.
• Path cost: Each step costs 1, so the path cost is the number of steps in the path.
Compared with the real world, this toy problem has discrete locations, discrete dirt, reliable cleaning, and it never gets any dirtier. Chapter 4 relaxes some of these assumptions.
The8-puzzle, an instance of which is shown in Figure 3.4, consists of a 3×3 board with
8-PUZZLE
eight numbered tiles and a blank space. A tile adjacent to the blank space can slide into the space. The object is to reach a specified goal state, such as the one shown on the right of the figure. The standard formulation is as follows:
Section 3.2. Example Problems 71
2
Start State Goal State
1
3 4
6 7
5 1
2
3 4 6 7
8 5
8
Figure 3.4 A typical instance of the 8-puzzle.
• States: A state description specifies the location of each of the eight tiles and the blank in one of the nine squares.
• Initial state: Any state can be designated as the initial state. Note that any given goal can be reached from exactly half of the possible initial states (Exercise 3.4).
• Actions: The simplest formulation defines the actions as movements of the blank space Left,Right,Up, orDown. Different subsets of these are possible depending on where the blank is.
• Transition model: Given a state and action, this returns the resulting state; for example, if we applyLeftto the start state in Figure 3.4, the resulting state has the 5 and the blank switched.
• Goal test: This checks whether the state matches the goal configuration shown in Fig- ure 3.4. (Other goal configurations are possible.)
• Path cost: Each step costs 1, so the path cost is the number of steps in the path.
What abstractions have we included here? The actions are abstracted to their beginning and final states, ignoring the intermediate locations where the block is sliding. We have abstracted away actions such as shaking the board when pieces get stuck and ruled out extracting the pieces with a knife and putting them back again. We are left with a description of the rules of the puzzle, avoiding all the details of physical manipulations.
The 8-puzzle belongs to the family ofsliding-block puzzles, which are often used as
SLIDING-BLOCK PUZZLES
test problems for new search algorithms in AI. This family is known to be NP-complete, so one does not expect to find methods significantly better in the worst case than the search algorithms described in this chapter and the next. The 8-puzzle has9!/2 = 181,440reachable states and is easily solved. The 15-puzzle (on a4×4board) has around 1.3 trillion states, and random instances can be solved optimally in a few milliseconds by the best search algorithms.
The 24-puzzle (on a5×5board) has around 1025states, and random instances take several hours to solve optimally.
The goal of the8-queens problemis to place eight queens on a chessboard such that
8-QUEENS PROBLEM
no queen attacks any other. (A queen attacks any piece in the same row, column or diago- nal.) Figure 3.5 shows an attempted solution that fails: the queen in the rightmost column is attacked by the queen at the top left.
72 Chapter 3. Solving Problems by Searching
Figure 3.5 Almost a solution to the 8-queens problem. (Solution is left as an exercise.)
Although efficient special-purpose algorithms exist for this problem and for the whole n-queens family, it remains a useful test problem for search algorithms. There are two main kinds of formulation. Anincremental formulationinvolves operators thataugmentthe state
INCREMENTAL FORMULATION
description, starting with an empty state; for the 8-queens problem, this means that each action adds a queen to the state. Acomplete-state formulationstarts with all 8 queens on
COMPLETE-STATE FORMULATION
the board and moves them around. In either case, the path cost is of no interest because only the final state counts. The first incremental formulation one might try is the following:
• States: Any arrangement of 0 to 8 queens on the board is a state.
• Initial state: No queens on the board.
• Actions: Add a queen to any empty square.
• Transition model: Returns the board with a queen added to the specified square.
• Goal test: 8 queens are on the board, none attacked.
In this formulation, we have64·63· · ·57≈1.8×1014possible sequences to investigate. A better formulation would prohibit placing a queen in any square that is already attacked:
• States: All possible arrangements ofn queens(0 ≤ n ≤ 8), one per column in the leftmostncolumns, with no queen attacking another.
• Actions: Add a queen to any square in the leftmost empty column such that it is not attacked by any other queen.
This formulation reduces the 8-queens state space from1.8×1014to just 2,057, and solutions are easy to find. On the other hand, for 100 queens the reduction is from roughly10400states to about1052states (Exercise 3.5)—a big improvement, but not enough to make the problem tractable. Section 4.1 describes the complete-state formulation, and Chapter 6 gives a simple algorithm that solves even the million-queens problem with ease.
Section 3.2. Example Problems 73 Our final toy problem was devised by Donald Knuth (1964) and illustrates how infinite state spaces can arise. Knuth conjectured that, starting with the number 4, a sequence of fac- torial, square root, and floor operations will reach any desired positive integer. For example, we can reach 5 from 4 as follows:
(4!)!
= 5.
The problem definition is very simple:
• States: Positive numbers.
• Initial state: 4.
• Actions: Apply factorial, square root, or floor operation (factorial for integers only).
• Transition model: As given by the mathematical definitions of the operations.
• Goal test: State is the desired positive integer.
To our knowledge there is no bound on how large a number might be constructed in the pro- cess of reaching a given target—for example, the number 620,448,401,733,239,439,360,000 is generated in the expression for 5—so the state space for this problem is infinite. Such state spaces arise frequently in tasks involving the generation of mathematical expressions, circuits, proofs, programs, and other recursively defined objects.