# Modeling AI problems as search

### The wolf-sheep-cabbage problem

Description
You are on the bank of a river with a boat, a cabbage, a sheep, and a wolf.
Restrictions:
1. only you can handle the boat
2. when you're in the boat, there is only space for one more item
3. you can't leave the sheep alone with the wolf, nor with the cabbage (or something will be eaten)

Model
Model the state by 4 bits (for boat, cabbage, sheep and wolf).
A 1 means that the item is on this bank, a 0 means it's on the other bank.

Notes
Restriction 1 is in the state model: "you" are not modeled separate from the boat.
Restriction 2 determines the possible moves. All moves can be made in both directions. (This is special for this problem. Normally, moves are directed from one state to another.)
Restriction 3 is in the shaded states: these are not allowed.

Solution
Find a path from the initial state to the final state. Since this is a toy problem, it's easy.
In a real problem
• constructing the tree and searching it is done at the same time.
• memory use is often more of a problem than time (thus the number of bits representing a state is important).
• parts of the tree that are "done" are thrown away. Sometimes parts that are not "done" are also thrown away, and recreated later.

Symmetry
What if we replace the wolf by another cabbage? The problem stays the same, but maybe we would have made a smaller model.

## Formalization

Important terminology (Luger page 84-85)
A directed graph consists of nodes and arcs (edges).
A tree has a root and leaves. There is a unique path from the root to each leaf. There are no cycles (loops).
Analogy with family trees (graphs): parent, child, ancestor, descendent, sibling.

Finite state acceptor / state space
S - the set of states
s0  - initial state
G  - set of goal states - subset of S
F (flow) - transition function. There are some variations: F is a subset of
• S x S
• S x Name x S - each move has a name. A sequence of moves has a sequence of names (word).
The moves can be input (as in Luger, where Name is called I, for input) or output.
• S x S x Cost - each move has a cost. Find not just any solution, but the cheapest solution.
• S x Name x S x Cost
Solution
A sequence of states s0, s1, ..., sn such that sn in G, and (si,si+1) in F (0 ≤ i < n).
A sequence of moves (word) a1, ..., an such that there is a sequence of states s0, s1, ..., sn such that sn in G, and (si,ai+1,si+1) in F (0 ≤ i < n).
The cost of a solution is (usually) the sum of the cost of the moves.

How to solve it
1. What are the states? How are the states represented?
2. What are the moves? How are the moves represented?
3. What search strategy? (Coming 3 lectures.)
Search space and search tree
The search space is the part of the state space that can be reached by the search strategy.
The search tree is the part of the search space that is actually searched (can be unfolded as a tree).
Since state space and search space can be very large, they are almost never created explicitly.
Usually only parts of the search tree are present in memory during the search.

## More examples

### The Mr. Wolf, Mr. sheep, and Mr. Cabbage problem

Description
You are on the beginning of a wobbly bridge with Mr. Wolf, Mr. sheep, and Mr. Cabbage.
It's dark, and the group has only one flashlight.Your task is to get everyone to the other side.
Restrictions:
1. at most two people can be on the bridge at the same time
2. people need the flashlight to cross the bridge
Cost:
You can cross the bridge in 1 minute, Mr. Wolf needs 2 minutes, Mr sheep needs 5 minutes, and Mr Cabbage needs 10 minutes.
If two people cross the bridge together, they need as much time as the slowest of them.

Model (part)

(similar problem - different values)

### The 8-puzzle (15 puzzle, 107 puzzle)

See Luger 89-91. Notable:
- representation of moves: record the moves of the hole
- only half the state space is reachable

See Luger 91-93.