0% found this document useful (0 votes)
32 views17 pages

AI Lecture Three Search

Download as docx, pdf, or txt
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 17

‫ اسعد نوري هاشم‬.‫د‬.

‫إعداد م‬ ‫محاضرات في الذكاء االصطناعي‬

STRUCTURES AND STRATEGIES FOR


STATE SPACE SEARCH
Previous lecture introduced predicate calculus as an example of an
artificial intelligence representation language. Well-formed predicate
calculus expressions provide a means of describing objects and relations
in a problem domain, and inference rules such as modus ponens allow us
to infer new knowledge from these descriptions. These inferences define
a space that is searched to find a problem solution. Here there is an
introduction to the theory of state space search.

The theory of state space search is our primary tool. By


representing a problem as a state space graph, we can use graph theory to
analyze the structure and complexity of both the problem and the search
procedures that we employ to solve it. A graph consists of a set of nodes
and a set of arcs or links connecting pairs of nodes. In the state space
model of problem solving, the nodes of a graph are taken to represent
discrete states in a problem-solving process, such as the results of logical
inferences or the different configurations of a game board. The arcs of the
graph represent transitions between states.

Graph theory is our best tool for reasoning about the structure of
objects and relations; indeed, this is precisely the need that led to its
creation in the early eighteenth century. The Swiss mathematician
Leonhard Euler invented graph theory to solve the “bridges of
Königsberg problem.” The city of Königsberg occupied both banks and
two islands of a river. The islands and the riverbanks were connected by
seven bridges, as indicated in Figure 3.1.

1
‫إعداد م‪.‬د‪ .‬اسعد نوري هاشم‬ ‫محاضرات في الذكاء االصطناعي‬

‫‪2‬‬
‫ اسعد نوري هاشم‬.‫د‬.‫إعداد م‬ ‫محاضرات في الذكاء االصطناعي‬

DEFINITION

STATE SPACE SEARCH

A state space is represented by a four-tuple [N,A,S,GD], where:

N is the set of nodes or states of the graph. These correspond to the


states in a problem-solving process.

A is the set of arcs (or links) between nodes. These correspond to the
steps in a

problem-solving process.

S, a nonempty subset of N, contains the start state(s) of the problem.

GD, a nonempty subset of N, contains the goal state(s) of the problem.


The states in GD are described using either:

A number of interesting aspects of this game have made it useful to


researchers in problem solving. The state space is large enough to be
interesting but is not completely intractable (16! if symmetric states are
treated as distinct).

In order to apply a move, we must make sure that it does not move the
blank off the board. Therefore, all four moves are not applicable at all
times; for example, when the blank is in one of the corners only two
moves are possible. The legal moves are:

move the blank up ↑

move the blank right →

move the blank down ↓

move the blank left ←

3
‫ اسعد نوري هاشم‬.‫د‬.‫إعداد م‬ ‫محاضرات في الذكاء االصطناعي‬

If we specify a beginning state and a goal state for the 8-puzzle, it is


possible to give a state space accounting of the problem-solving process ).
States could be

3.2 Strategies for State Space Search

3.2.1 Data-Driven and Goal-Driven Search

A state space may be searched in two directions: from the given data
of a problem instance toward a goal or from a goal back to the data. In
data-driven search, sometimes called forward chaining, the problem
solver begins with the given facts of the problem and a set of legal moves
or rules for changing state. Search proceeds by applying rules to facts to
produce new facts, which are in turn used by the rules to generate more
new facts. This process continues until (we hope!) it generates a path that
satisfies the goal condition.

An alternative approach is possible: take the goal that we want to


solve. See what rules or legal moves could be used to generate this goal

4
‫ اسعد نوري هاشم‬.‫د‬.‫إعداد م‬ ‫محاضرات في الذكاء االصطناعي‬

and determine what conditions must be true to use them. These


conditions become the new goals, or subgoals, for the search. Search
continues, working backward through successive subgoals until (we
hope!) it works back to the facts of the problem. This finds the chain of
moves or rules leading from data to a goal, although it does so in
backward order. This approach is called goal-driven reasoning, or
backward chaining, and it recalls the simple childhood trick of trying to
solve a maze by working back from the finish to the start.

To summarize: data-driven reasoning takes the facts of the problem


and applies the rules or legal moves to produce new facts that lead to a
goal; goal-driven reasoning focuses on the goal, finds the rules that could
produce the goal, and chains backward through successive rules and
subgoals to the given facts of the problem.The decision to choose
between data- and goal-driven search is based on the structure

of the problem to be solved. Goal-driven search is suggested if:

1. A goal or hypothesis is given in the problem statement or can easily be

formulated. In a mathematics theorem prover, for example, the goal is the

theorem to be proved. Many diagnostic systems consider potential


diagnoses in a systematic fashion, confirming or eliminating them using
goal-driven reasoning.

2. There are a large number of rules that match the facts of the problem
and thus produce an increasing number of conclusions or goals. Early
selection of a goal can eliminate most of these branches, making goal-
driven search more effective in pruning the space (Figure 3.12). In a
theorem prover, for example, the total number of rules used to produce a

5
‫ اسعد نوري هاشم‬.‫د‬.‫إعداد م‬ ‫محاضرات في الذكاء االصطناعي‬

given theorem is usually much smaller than the number of rules that may
be applied to the entire set of axioms.

3. Problem data are not given but must be acquired by the problem solver.
In this case, goal-driven search can help guide data acquisition. In a
medical diagnosis program, for example, a wide range of diagnostic tests
can be applied. Doctors order only those that are necessary to confirm or
deny a particular hypothesis.

6
‫ اسعد نوري هاشم‬.‫د‬.‫إعداد م‬ ‫محاضرات في الذكاء االصطناعي‬

Goal-driven search thus uses knowledge of the desired goal to guide the
search through relevant rules and eliminate branches of the space. Data-
driven search is appropriate for problems in which:

1. All or most of the data are given in the initial problem statement.
Interpretation problems often fit this mold by presenting a collection of
data and asking the system to provide a high-level interpretation.

2. There are a large number of potential goals, but there are only a few
ways to use the facts and given information of a particular problem
instance. The DENDRAL program, an expert system that finds the
molecular structure of organic compounds based on their formula, mass
spectrographic data, and knowledge of chemistry, is an example of this.
For any organic compound, there are an enormous number of possible
structures. However, the mass spectrographic data on a compound allow
DENDRAL to eliminate all but a few of these.

3. It is difficult to form a goal or hypothesis. In using DENDRAL, for


example, lit- tle may be known initially about the possible structure of a
compound. Data-driven search uses the knowledge and constraints found
in the given data of a problem to guide search along lines known to be
true. To summarize, there is no substitute for careful analysis of the
particular problem to be solved, considering such issues as the branching
factor of rule applications; on average, how many new states are
generated by rule applications in both directions?), availability of data,
and ease of determining potential goals.

7
‫ اسعد نوري هاشم‬.‫د‬.‫إعداد م‬ ‫محاضرات في الذكاء االصطناعي‬

Depth-First and Breadth-First Search

In addition to specifying a search direction (data-driven or goal-


driven), a search algorithm must determine the order in which states are
examined in the tree or the graph. This section considers two possibilities
for the order in which the nodes of the graph are considered: depth-first
and breadth-first search.

Consider the graph represented in Figure 3.15. States are labelled


(A, B, C, . . .) so that they can be referred to in the discussion that
follows. In depth-first search, when a state is examined, all of its children
and their descendants are examined before any of its siblings. Depth-first
search goes deeper into the search space whenever this is possible. Only
when no further descendants of a state can be found are its siblings
considered. Depth-first search examines the states in the graph of Figure
3.15 in the order A, B, E, K,S, L, T, F, M, C, G, N, H, O, P, U, D, I, Q, J,
R. The backtrack algorithm of Section

Implemented depth-first search.

Breadth-first search, in contrast, explores the space in a level-by-


level fashion. Only when there are no more states to be explored at a
given level does the algorithm move on to the next deeper level. A
breadth-first search of the graph of Figure 3.15 considers the states in the
order A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U. We
implement breadth-first search using lists, open and closed, to keep track
of progress through the state space. open, in backtrack, lists states that
have been generated but whose children have not been examined. The
order in which states are removed from open determines the order of the
search, closed records states already examined.

8
‫ اسعد نوري هاشم‬.‫د‬.‫إعداد م‬ ‫محاضرات في الذكاء االصطناعي‬

Figure 3.15 Graph for breadth- and depth-first search examples

Child states are generated by inference rules, legal moves of a game, or


other state transition operators. Each iteration produces all children of the
state X and adds them to open. Note that open is maintained as a queue,
or first-in-first-out (FIFO) data structure. States are added to the right of
the list and removed from the left. This biases search toward the states
that have been on open the longest, causing the search to be breadth-first.
Child states that have already been discovered (already appear on either

open or closed) are discarded. If the algorithm terminates because the


condition of the “while” loop is no longer satisfied (open = [ ]) then it has
searched the entire graph with out finding the desired goal: the search has

9
‫ اسعد نوري هاشم‬.‫د‬.‫إعداد م‬ ‫محاضرات في الذكاء االصطناعي‬

failed. A trace of breadth_first_search on the graph of Figure 3.15


follows. Each successsive number, 2,3,4, . . . , represents an iteration of
the “while” loop. U is the goal state.

Figure 3.16 illustrates the graph of Figure 3.15 after six iterations of

breadth first_search. The states on open and closed are highlighted. States
not shaded have not been discovered by the algorithm. Note that open
records the states on the “frontier” of the search at any stage and that
closed records states already visited. Because breadth-first search
considers every node at each level of the graph before going deeper into
the space, all states are first reached along the shortest path from the start
state. Breadth-first search is therefore guaranteed to find the shortest path
from the start state to the goal. Furthermore, because all states are first
found along the shortest path, any states encountered a second time are
found along a path of equal or greater length.

Because there is no chance that duplicate states were found along


a better path, the algorithm simply discards any duplicate states. It is
often useful to keep other information on open and closed besides the
names of the states. For example, note that breadth_first_search does not
maintain a list of states on the current path to a goal as backtrack did on
the list SL; all visited states are kept on closed. If a solution path is
required, it can not be returned by this algorithm. The solution can be
found by storing ancestor information along with each state. A state may
10
‫ اسعد نوري هاشم‬.‫د‬.‫إعداد م‬ ‫محاضرات في الذكاء االصطناعي‬

be saved along with a record of its parent state, e.g., as a (state, parent)
pair. If this is done in the search of Figure 3.15, the contents of open and
closed at the fourth iteration would be:

open = [(D,A), (E,B), (F,B), (G,C), (H,C)]; closed = [(C,A), (B,A),


(A,nil)]

Figure 3.16 Graph of Figure 3.15 at iteration 6 of breadth-first search.

States on open and closed are highlighted. The path (A, B, F) that led
from A to F could easily be constructed from this information. When a
goal is found, the algorithm can construct the solution path by tracing
back along parents from the goal to the start state. Note that state A has a
parent of nil, indicating that it is a start state; this stops reconstruction of
the path.

Because breadth-first search finds each state along the shortest path
and retains the first version of each state, this is the shortest path from a
start to a goal. Figure 3.17 shows the states removed from open and
examined in a breadth-first search of the graph of the 8-puzzle. As before,
arcs correspond to moves of the blank up, to the right, down, and to the
left. The number next to each state indicates the order in which it was
removed from open. States left on open when the algorithm halted are
not shown. Next, we create a depth-first search algorithm, a simplification

11
‫ اسعد نوري هاشم‬.‫د‬.‫إعداد م‬ ‫محاضرات في الذكاء االصطناعي‬

of the backtrack algorithm already presented. In this algorithm, the


descendant states are added and removed from the left end of open: open
is maintained as a stack, or last-in-first-out (LIFO) structure. The
organization of open as a stack directs search toward the most recently
generated states, producing a depth-first search order:

A trace of depth_first_search on the graph of Figure 3.15 appears below.


Each successive iteration of the “while” loop is indicated by a single line
(2, 3, 4, . . .). The initial states of open and closed are given on line 1.
Assume U is the goal state.

12
‫ اسعد نوري هاشم‬.‫د‬.‫إعداد م‬ ‫محاضرات في الذكاء االصطناعي‬

Figure 3.17 Breadth-first search of the 8-puzzle, showing order in which


states were removed from open. and so on until either U is discovered or
open = [ ]. As with breadth_first_search, open lists all states discovered
but not yet evaluated (the current “frontier” of the search), and closed
records states already considered. Figure 3.18 shows the graph of Figure
3.15 at the sixth iteration of the depth_first_search. The contents of open
and closed are highlighted. As with breadth_first_search, the algorithm
could store a record of the parent along with each state, allowing the
algorithm to reconstruct the path that led from the start state to a goal.

Unlike breadth-first search, a depth-first search is not guaranteed to find


the shortest path to a state the first time that state is encountered. Later in
the search, a different path may be found to any state. If path length

13
‫ اسعد نوري هاشم‬.‫د‬.‫إعداد م‬ ‫محاضرات في الذكاء االصطناعي‬

matters in a problem solver, when the algorithm encounters a duplicate


state, the algorithm should save the version reached along the shortest
path. This could be done by storing each state as a triple: (state, parent,

Figure 3.18 Graph of Figure 3.15 at iteration 6 of depth-first search.

States on open and closed are highlighted. length_of_path). When


children are generated, the value of the path length is simply incremented
by one and saved with the child. If a child is reached along multiple
paths, this information can be used to retain the best version. This is
treated in more detail in the discussion of algorithm A in Chapter 4. Note
that retaining the best version of a state in a simple depth-first search does
not guarantee that a goal will be reached along the shortest path.

Figure 3.19 gives a depth-first search of the 8-puzzle. As noted


previously, the space is generated by the four “move blank” rules (up,
down, left, and right). The numbers next to the states indicate the order in

14
‫ اسعد نوري هاشم‬.‫د‬.‫إعداد م‬ ‫محاضرات في الذكاء االصطناعي‬

which they were considered, i.e., removed from open. States left on open
when the goal is found are not shown. A depth bound of 5 was

imposed on this search to keep it from getting lost deep in the space.

As with choosing between data- and goal-driven search for evaluating a


graph, the choice of depth-first or breadth-first search depends on the
specific problem being solved. Significant features include the importance
of finding the shortest path to a goal, the branching factor of the space,
the available compute time and space resources, the average length of
paths to a goal node, and whether we want all solutions or only the first
solution. In making these decisions, there are advantages and
disadvantages for each approach.

Breadth-First Because it always examines all the nodes at level n


before proceeding to level n + 1, breadth-first search always finds the
shortest path to a goal node. In a problem where it is known that a simple
solution exists, this solution will be found. Unfortunately, if there is a bad
branching factor, i.e., states have a high average number of children, the

15
‫ اسعد نوري هاشم‬.‫د‬.‫إعداد م‬ ‫محاضرات في الذكاء االصطناعي‬

Figure 3.19 Depth-first search of the 8-puzzle with a depth bound of 5.

combinatorial explosion may prevent the algorithm from finding a


solution using available memory. This is due to the fact that all
unexpanded nodes for each level of the search must be kept on open. For
deep searches, or state spaces with a high branching factor, this can
become quite cumbersome. The space utilization of breadth-first search,
measured in terms of the number of states on open, is an exponential
function of the length of the path at any time.
If each state has an average of B children, the number of states on a
given level is B times the number of states on the previous level. This
gives Bn states on level n. Breadth-first search would place all of these on
open when it begins examining level n. This can be prohibitive if solution
paths are long, in the game of chess, for example.
Depth-First Depth-first search gets quickly into a deep search space.
If it is known that the solution path will be long, depth-first search will

16
‫ اسعد نوري هاشم‬.‫د‬.‫إعداد م‬ ‫محاضرات في الذكاء االصطناعي‬

not waste time searching a large number of “shallow” states in the graph.
On the other hand, depth-first search can get “lost” deep in a graph,
missing shorter paths to a goal or even becoming stuck in an infinitely
long path that does not lead to a goal.
Depth-first search is much more efficient for search spaces with many
branches because it does not have to keep all the nodes at a given level on
the open list. The space usage of depth-first search is a linear function of
the length of the path. At each level, open retains only the children of a
single state. If a graph has an average of B children per state, this requires
a total space usage of B × n states to go n levels deep into the space.
The best answer to the “depth-first versus breadth-first” issue is to
examine the problem space and consult experts in the area. In chess, for
example, breadth-first search simply is not possible. In simpler games,
breadth-first search not only may be possible but, because it gives the
shortest path, may be the only way to avoid losing.

17

You might also like