Sub topic 5
Puzzles as State Spaces
This chapter assumes you can write Python functions, use tuples, sets and dictionaries, and know what a graph is (vertices joined by edges). Earlier chapters covered grids as graphs. Here the vertices stop being places and become situations.
States, actions, transitions
A puzzle can be described with three pieces:
- A state is a complete snapshot of the puzzle at one moment.
- An action is a legal move you can make from a state.
- A transition is the pair (state, action) and the new state it produces.
If you treat each state as a vertex and each transition as a directed edge, solving the puzzle means finding a path from the start state to any goal state. The same graph ideas apply, even though nobody drew the graph.
Worked example: two jugs
You have a 3-litre jug and a 5-litre jug, both empty. You want exactly 4 litres in one of them. The only moves are to fill a jug, empty a jug, or pour one into another until the source is empty or the target is full.
A state is (a, b), the litres in each jug. The start is (0, 0). A goal is any state where a or b equals 4.
def jug_moves(state, caps=(3, 5)):
a, b = state
ca, cb = caps
yield (ca, b) # fill jug A
yield (a, cb) # fill jug B
yield (0, b) # empty jug A
yield (a, 0) # empty jug B
pour = min(a, cb - b) # pour A into B
yield (a - pour, b + pour)
pour = min(b, ca - a) # pour B into A
yield (a + pour, b - pour)
print(list(jug_moves((3, 0))))This prints:
[(3, 0), (3, 5), (0, 0), (3, 0), (0, 3), (3, 0)]Some moves return the same state, such as filling a jug that is already full. These are self-loops. They are harmless, because a search that remembers visited states will skip them.
The function never lists the whole graph. It answers one question: from here, where can I go? Later chapters call this an implicit graph.
What makes a good state
Check your state definition against two rules:
- Complete. It must contain everything that affects which moves are legal or whether you have won. If you left out a variable, two situations that behave differently would be merged into one vertex, and the search would give wrong answers.
- Minimal. Leave out anything that does not change future options. Extra detail, such as the order in which you reached a situation, splits one vertex into many and wastes time.
In the jug puzzle, the capacities never change, so they are not part of the state. They are parameters of the move function.
Common errors
- Mutable states. A list cannot go in a set or serve as a dictionary key. Use tuples, or convert before storing.
- Mutating the current state while generating moves. Always build a new state for each move.
- Illegal pours. Forgetting the
minlets a jug hold negative or excess water. - Goal test on the wrong thing.
4 in stateworks for jugs. For other puzzles the goal may depend on the whole configuration.
Exercise
A puzzle has a robot on a 4x4 grid that can also pick up one key. Moves are up, down, left and right, and the exit opens only if the robot holds the key. Is the state just the robot's position (r, c)? If not, what should it be?
Show answer
No. Position alone is incomplete, because the same cell means different things with and without the key. Use (r, c, haskey). The exit is the goal only when haskey is true. This also doubles the number of states, from 16 to 32 (or fewer, if the key can only be picked up in one cell).