08 Search
08 Search
08 Search
18/10/04
State-Space Search
Many problems in AI take the form of state-space search. The states might be legal board configurations in a game, towns and cities in some sort of route map, collections of mathematical propositions, etc. The state-space is the configuration of the possible states and how they connect to each other e.g. the legal moves between states. When we don't have an algorithm which tells us definitively how to negotiate the state-space we need to search the statespace to find an optimal path from a start state to a goal state. We can only decide what to do (or where to go), by considering the possible moves from the current state, and trying to look ahead as far as possible. Chess, for example, is a very difficult state-space search problem.
18/10/04 AIPP Lecture 8: State-Space Search 2
C
Goal State
State-Space Representation
link(g,h). link(g,d). link(e,d). link(h,f). link(e,f). link(a,e). link(a,b). link(b,f). link(b,c). link(f,c). A Initial state is the root B E
F C
F C
An abstract representation of a state-space is a downwards growing tree. Connected nodes represent states in the domain. The branching factor denotes how many new states you can move to from any state. This problem has an average of 2. The depth of a node denotes how many moves away from the initial state it is. C has two depths, 2 or 3.
18/10/04 AIPP Lecture 8: State-Space Search 4
Implementing
To implement state-space search in Prolog, we need:
1. A way of representing a state e.g. the board configuration link(a,e). 2. A way of generating all of the next states reachable from a given state;
go(X,Y,[X|T]):- link(X,Z), go(Z,Y,T).
3. A way of determining whether a given state is the one we're looking for. Sometimes this might be the goal state (a finished puzzle, a completed route, a checkmate position); other times it might simply be the state we estimate is the best, using some evaluation function; go(X,X,[X]).
Depth-First Search
go(X,X,[X]). go(X,Y,[X|T]):link(X,Z), go(Z,Y,T). | ?- go(a,c,X). X = [a,e,f,c] ? ; X = [a,b,f,c] ? ; X = [a,b,c] ? ; no A E B
F C
F C
This simple search algorithm uses Prologs unification routine to find the first link from the current node then follows it. It always follows the left-most branch of the search tree first; following it down until it either finds the goal state or hits a dead-end. It will then backtrack to find another branch to follow. = depth-first search.
18/10/04 AIPP Lecture 8: State-Space Search 7
Iterative Deepening
If the optimal solution is the shortest path from the initial state to the goal state depth-first search will usually not find this. We need to vary the depth at which we look for a solution; increasing the depth every time we have exhausted all nodes at a particular depth. We can take advantage of Prologs backtracking to implement this very simply.
Depth-First Iterative Deepening
go(X,X,[X]).
go(X,Y,[X|T]):link(X,Z), go(Z,Y,T).
18/10/04
go(X,X,[X]).
go(X,Y,[Y|T]):go(X,Z,T), link(Z,Y).
Check if current node is goal. Find an intermediate node. Check whether intermediate links with goal.
8
|?- go(a,c,S).
18/10/04
go(a,Z1,Sol).
Z1=a
go(a,a,[a]).
link(a,Z).
|?- go(a,c,S).
18/10/04
10
go(a,Z1,[a]).
Z1=a
go(a,a,[a]).
|?- go(a,c,S).
18/10/04
11
go(a,Z1,[a]).
Z1=a
go(a,a,[a]).
18/10/04
12
go(a,Z1,Sol).
go(a,Z2,Sol).
go(a,a,[a]).
go(a,a,[a]). link(g,h). link(g,d). link(e,d). link(h,f). link(e,f). link(a,e). link(a,b). link(b,f). link(b,c). link(f,c).
18/10/04
13
go(a,e,Sol).
go(a,a,Sol).
go(a,a,[a]).
go(a,a,[a]). link(g,h). link(g,d). link(e,d). link(h,f). link(e,f). link(a,e). link(a,b). link(b,f). link(b,c). link(f,c).
18/10/04
14
However:
on each iteration it has to re-compute all previous levels and extend them to the new depth; may not terminate (e.g. loop); may not be able to handle complex state-spaces; cant be used in conjunction with problem-specific heuristics as keeps no memory of optional paths.
18/10/04 AIPP Lecture 8: State-Space Search 15
Breadth-First Search
| ?- go(a,c,X). X = [a,b,c] ? ; X = [a,e,f,c] ? ; X = [a,b,f,c] ? ; no A E B
1st
C
F C
F C
2nd
3rd
A simple, common alternative to depth-first search is: breadth-first search. This checks every node at one level of the space, before moving onto the next level. It is distinct from iterative deepening as it maintains a list of alternative candidate nodes that can be expanded at each depth
18/10/04 AIPP Lecture 8: State-Space Search 16
More complex to implement; Needs a lot of memory for storing the state space if the search space has a high branching factor.
17
Agenda-based search
Both depth-first and breadth-first search can be implemented using an agenda (breadth-first can only be implemented with an agenda). The agenda holds a list of states in the state space, as we generate (expand') them, starting with the initial state. We process the agenda from the beginning, taking the first state each time. If that state is the one we're looking for, the search is complete. Otherwise, we expand that state, and generate the states which are reachable from it. We then add the new nodes to the agenda, to be dealt with as we come to them.
18/10/04
18
is_goal/1,
which succeeds if its argument is the goal state. get_successors/2 which generates all of the states which are reachable from a given state (should take advantage of findall/3, setof/3 or bagof/3 to achieve this). update_agenda/2, which adds new states to the agenda (usually using append/3).
18/10/04 AIPP Lecture 8: State-Space Search 20
Adding newly-generated agenda items to the end of the agenda implements breadth-first search:
update_agenda(OldAgenda, NewStates, NewAgenda) :append(OldAgenda, NewStates, NewAgenda).
= We control how the search proceeds, by changing how the agenda is updated.
18/10/04 AIPP Lecture 8: State-Space Search 21
Summary
State-Space Search can be used to find optimal paths through problem spaces. A state-space is represented as a downwards-growing tree with nodes representing states and branches as legal moves between states. Prologs unification strategy allows a simple implementation of depth-first search. The efficiency of this can be improved by performing iterative deepening search (using backtracking). Breadth-first search always finds the shortest path to the goal state. Both depth and breadth-first search can be implemented using an agenda: depth-first adds new nodes to the front of the agenda; breadth-first adds new nodes to the end.
18/10/04 AIPP Lecture 8: State-Space Search 22