006 - State Space Search and FWGC
006 - State Space Search and FWGC
A state refers to the status of a puzzle or game at a given moment. It could be a the positions of the pieces on a chessboard.
In solving a puzzle or a problem one starts from some initial state and tries to reach a goal state by passing through a series
of intermediate states. In game playing, each move on the game board is a transition from one state to another.
If you think of each state being connected to those states which can follow from it, you have a graph. Such a collection of
interconnected states is called a state space graph.
In state based search, a computer program may start from an initial state, then look at one of its successor or children states
and so on until it reaches a goal state. It may reach a dead end state from where it cannot proceed further. In such a
situation the program may “backtrack”, i.e. undo its last move and try an alternative successor to its previous state. A path
from the initial state to the goal state constitutes a solution.
There are a number of ways of searching through the state space to find a solution. In depth first search, the program will
examine a state, then proceed from it to one of its successor states and then repeats this process with the successor state,
going a level deeper each time.
Breadth first search, BFS, is an alternative approach is where one examines all the successor states of a given state before
proceeding to a new level.
Best first search chooses the next state based on some measure of how promising it is. It does a calculation for each
possible next state and then chooses the best one. Best first search uses a priority queue.
1
Sate Space Search
In solving a puzzle or a problem (such as the farmer, wolf, goat and cabbage one – fwgc from hereon) one starts from
some initial state and tries to reach a goal state (e.g. all on west bank) by passing through a series of intermediate states. In
game playing, each move transfers the system from one state to the next. For example in the fwgc problem, crossing the
river in a boat either on his own or with one of the others changes the state of the system. There is usually a set of rules
which tell one how to go from one state to another. Each state may be succeeded by one of a number of others.
r r
i farmer crosses with goat i
fwgc v w c v f g
e e
r r
In state based search, a computer program may start from an initial state, then look at one of its successor states and so on
until it reaches a goal state. It may reach a dead end state from where it cannot proceed further. In such a case the program
may “backtrack”, i.e. undo its last move and try an alternative successor to its previous state.
s1
s2 s3 s4
g s8
s5 s6
s7
In the state space example above, starting with state s1, DFS with backtracking and would examine the states in the
following order: s1, s2, s5, s6, s3, s4. DFS uses a stack to store states for examination. On examining a state, it pushes all
its successor states onto a stack. The stack is then popped for next state to examine.
For example, on examining s1, stack is [s2, s3, s4], next s2 is removed and examined to give [s5, s6, s3, s4], then s5
removed and examined and so on. The stack contents will vary as follows:
[s1]
s1 [s2, s3, s4]
s2 [s5, s6, s3, s4]
s5 [s6, s3, s4]
s6 [s7, s3, s4]
s7 [s3, s4]
s3 [s4]
s4 [g, s8]
2
Sate Space Search
Once the goal state is found, the puzzle is solved. However, it is important that the path from the initial state (s1) to the
goal state (g) is stored and maybe displayed so that whoever run the program can find out the set of moves that lead to the
solution, otherwise the person just finds out that a computer program can solve the puzzle.
For example, on examining s1, queue is [s2, s3, s4], next s2 is removed and examined to give [s3, s4, s5, s6], then s3
removed and examined and so on.
[s1]
s1 [s2, s3, s4]
s2 [s3, s4, s5, s6]
s3 [s4, s5, s6]
s4 [s5, s6, g, s8]
s5 [s6, g, s8]
s6 [g, s8, s7]
g [s8, s7] -- goal found
In searching through a state space graph, care must be taken so that the program does not go round in cycles. This can be
done by keeping a list of states visited so far in getting to current state and checking that a possible next state is not in the
list.
3
Sate Space Search
fwgc | |
wgc | | f gc | | fw wc | | fg wg | | fc
fwc | | g
c | | fwg w | | fgc
fc | | wg fgc | | w
fwg | | c fw | | gc
gc | | fw
g | | fwc wg | | fc
fg | | wc
| | fwgc
4
Sate Space Search
fwgc | |
1
wgc | | f gc | | fw wc | | fg wg | | fc
2
fwc | | g
3
c | | fwg w | | fgc
4
7
fc | | wg fgc | | w
5 fwg | | c fw | | gc
gc | | fw 6
g | | fwc wg | | fc
8
fg | | wc
| | fwgc
5
Sate Space Search
fwgc | |
1
wgc | | f gc | | fw wc | | fg wg | | fc
2
fwc | | g
3
c | | fwg w | | fgc
4
fc | | wg fgc | | w
5 fwg | | c fw | | gc
gc | | fw
g | | fwc wg | | fc
fg | | wc
| | fwgc
6
Sate Space Search
fwgc | |
1
wgc | | f gc | | fw wc | | fg wg | | fc
2
fwc | | g
3 4
c | | fwg w | | fgc
5 6
fc | | wg fgc | | w
7 fwg | | c fw | | gc
gc | | fw
g | | fwc wg | | fc
8
fg | | wc
| | fwgc
Exercise
Consider how a given state of this puzzle can be represented in Haskell. Then define a function safe which returns true or
false depending on whether a state is safe or not. Safe means the wolf cannot eat the goat and the goat cannot eat the
cabbage.
Exercise
Do you think you could adapt the Haskell code that solves the 8-puzzle to this one? Have a try!