The blocks world has two kinds of components:
• A table top with three places p, q, and r
• A variable number of blocks A, B, C, etc., that can be arranged in places on the table or stacked on one another
A legal move is to transfer a block from one place or block onto another place or block, with these restrictions:
• The moved block must not have another block on top of it
• No other blocks are moved in the process
Here is a simple blocks world problem:

And here is its shortest solution:

A two-dimensional array may be appropriate (as in the 8-puzzle), however:
• Unlike our previous problems, which had fixed elements, the blocks world has multiple problems with multiple numbers of blocks
• Different size arrays are required for problems with different numbers of blocks
Stacks are a natural structure for piles of blocks.
This section discusses blocks world move representation and its effect on the state space search space.
It is straightforward to think of a move in the blocks world as transferring from one place (the source) to another place (the destination).

So the name of the block is not necessary to uniquely specify a move.

The three moves used in the example (see to the left) are:

• Move block from p to r
• Move block from p to q
• Move block from r to q
The doMove method in the blocks world move class must return null if there is no block on the source place.
The number of move objects to create is dependent on the number of places, but not on the number of blocks.

In general, if there are n places, there should be n*(n-1) move objects.

In our blocks world there are 3 places and so 6 move objects.

Since a blocks world search tree node can expand to as many as 6 children while an 8-puzzle search tree node can expand to a maximum of 4, blocks problems can generate larger search spaces.

You will need to devise an informed blocks world heuristic function.

This section discusses heuristics for the blocks world.

We show a heuristic that significantly underestimates, and one that overestimates.

As with the 8-puzzle, a natural heuristic to consider is the number of blocks that are out of place relative to the final state.

For example, in the current state below there are three blocks out of place (shown in red):

Since the actual number of moves required is five, this is not a poor estimate.

However, the heuristic is naive because it does not take into account whether a block, even if it is in the correct place, has correctly placed blocks under it, as some counterexamples show.

Consider this situation:

The number of blocks out of place is two, but the actual number of moves required is at least ten — five to get place p clear and at least five more to move blocks back into place.

So the heuristic underestimates considerably.

As a second example, consider:

The current state has only three blocks out of place but the number of moves required is 21.

You should implement the number-of-blocks-out-of-place heuristic for testing purposes, but you should also come up with a more informed heuristic that does not overestimate.

Note that the "sum of Manhattan distances" heuristic does overestimate for the blocks world problem. Suppose:

The "Manhattan distance" of A from its destination is six, but only one move is required to get it there.

This section shows three simple two-block problems with the minimum number of moves required to solve them (the Heuristic Analysis Tool will be described at another time).

If your heuristic can distinguish these situations and make good estimates for them that don't overestimate, you should be on your way to solving more complex problems efficiently.

I have experimented with several blocks world heuristics:
• Number of blocks out of place (h1), which is not very informed
• Number of blocks that must be moved (h2), which is somewhat informed
• A heuristic that attempts to be very informed (h3)
The performance of these heuristics, along with a heuristic of zero (h0, resulting in an uninformed breadth-first search), is shown in this section in terms of the number of priority queue operations and the maximum priority queue size. Shown are statistics for both regular and enhanced A* search. OVER means the search resulted in a heap overflow.

You can compare your own heuristic's performance against these.

Problem Solution
Length
Heuristic Regular A* Enhanced A*
PQ Ops PQ Size PQ Ops PQ Size
3 h0 55 18 21 5
5 h0 349 158 96 21
6 h0 5,216 2,647 442 96
8 h0 14,050 7,071 690 103
12 h0 OVER OVER 35,897 4,727
17 h0 OVER OVER OVER OVER
21 h0 OVER OVER OVER OVER
Problem Solution
Length
Heuristic Regular A* Enhanced A*
PQ Ops PQ Size PQ Ops PQ Size
3 h0 55 18 21 5
h1 17 8 14 5
5 h0 349 158 96 21
h1 60 29 37 14
6 h0 5,216 2,647 442 96
h1 135 74 107 44
8 h0 14,050 7,071 690 103
h1 490 253 194 71
12 h0 OVER OVER 35,897 4,727
h1 55,982 33,873 3,397 1,220
17 h0 OVER OVER OVER OVER
h1 913,121 562,042 82,842 34,711
21 h0 OVER OVER OVER OVER
h1 OVER OVER 1,727,285 565,120
Problem Solution
Length
Heuristic Regular A* Enhanced A*
PQ Ops PQ Size PQ Ops PQ Size
3 h0 55 18 21 5
h1 17 8 14 5
h2 17 8 14 5
5 h0 349 158 96 21
h1 60 29 37 14
h2 47 24 33 14
6 h0 5,216 2,647 442 96
h1 135 74 107 44
h2 108 57 80 35
8 h0 14,050 7,071 690 103
h1 490 253 194 71
h2 329 180 139 62
12 h0 OVER OVER 35,897 4,727
h1 55,982 33,873 3,397 1,220
h2 55,586 33,551 2,989 1,048
17 h0 OVER OVER OVER OVER
h1 913,121 562,042 82,842 34,711
h2 434,162 266,555 45,840 19,153
21 h0 OVER OVER OVER OVER
h1 OVER OVER 1,727,285 565,120
h2 OVER OVER 1,179,017 418,151
Problem Solution
Length
Heuristic Regular A* Enhanced A*
PQ Ops PQ Size PQ Ops PQ Size
3 h0 55 18 21 5
h1 17 8 14 5
h2 17 8 14 5
h3 13 6 11 4
5 h0 349 158 96 21
h1 60 29 37 14
h2 47 24 33 14
h3 28 13 20 7
6 h0 5,216 2,647 442 96
h1 135 74 107 44
h2 108 57 80 35
h3 36 17 23 8
8 h0 14,050 7,071 690 103
h1 490 253 194 71
h2 329 180 139 62
h3 213 108 90 33
12 h0 OVER OVER 35,897 4,727
h1 55,982 33,873 3,397 1,220
h2 55,586 33,551 2,989 1,048
h3 511 294 180 83
17 h0 OVER OVER OVER OVER
h1 913,121 562,042 82,842 34,711
h2 434,162 266,555 45,840 19,153
h3 988 531 132 67
21 h0 OVER OVER OVER OVER
h1 OVER OVER 1,727,285 565,120
h2 OVER OVER 1,179,017 418,151
h3 71,368 33,889 15,192 4,799