Unit II - AI Notes - Search Methods
Unit II - AI Notes - Search Methods
In general, search techniques are used to find a sequence of actions that will get us from some
initial state to some goal state(s).
The actions may be:
Physical actions (such as move from town A to town B, or put block C on the table)
Or may be more abstract actions, like theorem proving steps (we may be searching for a
sequence of steps that will allow us to prove X from the set of facts S).
There are two basic approaches to using search in problem solving.
In the first (state-space search) a node in the search tree is a state of the world. Successor
nodes in the search tree will be possible new states.
In the second (problem reduction) a node in the search tree is a goal (or set of goals) to be
satisfied. Successor nodes will be the different sub goals that can be used to satisfy that
goal. These two approaches will be discussed in turn (though we'll emphasize state-space
search).
Page 1 of 22
AI/Unit-I(b)
State space search techniques are often illustrated by showing how they can be used in
solving puzzles of the sort you find in intelligence tests. One such puzzle is the water jug
problem:
“You are given two jugs, a 4-gallon one and a 3-gallon one. Neither have any measuring
markers on it. There is a tap that can be used to fill the jugs with water. How can you get exactly
2 gallons of water into the 4-gallon jug”.
Given such a problem, we have to decide
How to represent the problem state (e.g., amount of water in each jug),
What are the initial and final states in this representation, and
How to represent the actions available in the problem, in terms of how they change the
problem state.
This particular puzzle is based on a simple problem-solving domain where the problem state can
be represented simply as a pair of numbers giving the amount of water in each jug (e.g., [4, 3]
means that there is 4 gallons in the 4-gallon jug and 3 in the 3 gallon jug). The initial state is [0,0]
and the final state is [2,X] where X can take any value. There are only a small number of
available actions (e.g., fill the 4-gallon jug), and these can be simple represented as rules or
operators, which show how the problem state changes given the different actions:
Page 2 of 22
AI/Unit-I(b)
Rules such as [X, Y] [X, 0] mean that we can get from a state where there are X gallons in the
first jug and Y in the second jug to a state where there are X gallons in the first and none in the
second, using the given action. If there is a condition such as (if X+Y < 3) this means that we can
only apply the rule if that condition holds. We'll also only consider actions that cause a change in
the current state.
We have to look at all our actions, and find ones that apply given the current state. We
can then use the rules above to find the state resulting from that action. A particular node in the
search tree, rather than being just the name of a town, will now be the representation of a
particular state (e.g., [2, 3]).
One more solution is to fill the 4 gallon jug, fill the 3 gallon from the 4 gallon, empty the
3 gallon, empty the 4 gallon into the 3 gallon, fill the 4 gallon and fill the 3 gallon from the 4
gallon again.
In fact, You have to decide on a representation of the state of the world, of the available
actions in the domain, and systematically going through all possible sequences to find a sequence
of actions that will get you from your initial to target state.
In the water jug problem there is no real need to use a heuristic search technique - the
domain is sufficiently constrained that you can go through all possibilities pretty quickly. In fact,
its hard to think what a good evaluation function would be. If you want 2 gallons in the jug, does
it mean you are close to a solution if you have 1 or 3 gallons? Many problems have the property
that you seem to have to undo some of the apparent progress you have made if you are to
eventually get to the solution (e.g., empty out the jug when you've got 1 gallon in it).
There are lots of other problems that have been solved using similar techniques, and
which are often found in AI textbooks. The ``Missionaries and cannibals'' problem, the
``Monkeys and bananas'' problem and the ``8 puzzle'' are three that are often quoted.
However, search techniques are used in many more serious AI applications, such as:
Language understanding, where we may search through possible syntactic structures;
Robotics, where we may search for a good path for the robot to take;
Vision, where we may search for meaningful interpretations of features of the object to
be recognized.
Page 3 of 22
AI/Unit-I(b)
To solve real problems you generally have to put a lot more effort into representing the problem.
You have to think exactly
What a representation of a world state should be,
What the initial and goal states are, and
What the available operators/actions are, that allow you to transform one state into
another.
You also need to decide whether or not state space search is appropriate, or whether, for
example, problem reduction, described above, might be better.
State Space Representation Example
Games: tic-tac-toe
Page 4 of 22
AI/Unit-I(b)
Page 5 of 22
AI/Unit-I(b)
(0,0)
(3,0) (0,4)
(3,0) (0,4) (3,3) (3,0) (0,0) (0,4) (3,4) (3,0) (0,1) (0,4)
Page 6 of 22
AI/Unit-I(b)
Searching/Search Methods:
There are two basic category of search methods:
Uninformed/Blind search – A blind or uninformed search is one that uses no
information other than the initial state, goal state, and a test for a solution. It can only
move according to position in search.
Informed/Heuristic search - Use domain-specific information to decide where to search
next.
Uninformed/Blind Search
Depth-First Search (DFS):
1. Set L to be a list of the initial nodes in the problem.
2. If L is empty, fail otherwise pick the first node n from L
3. If n is a goal state, quit and return path from initial node.
4. Otherwise remove n from L and add to the front of L all of n's children. Label each child
with its path from initial node.
5. Return to 2
Page 7 of 22
AI/Unit-I(b)
Advantages of DFS:
1. Depth first require less memory since only thenode on the current path are stored.
2. It may find a solution without examining much of the search space at all.
Disadvantages of DFS:
1. In DFS, sometime we may follw a single unfriteful path for a very long time.
2. DFS may find a long path to a solution in one part of the tree, when a shorter path exists
in some other unexplored part of the tree.
Breadth-First Search (BFS):
1. Set L to be a list of the initial nodes in the problem.
2. If L is empty, fail otherwise pick the first node n from L
3. If n is a goal state, quit and return path from initial node.
4. Otherwise remove n from L and add to the end of L all of n's children. Label each child
with its path from initial node.
5. Return to 2.
123gggg
Page 8 of 22
AI/Unit-I(b)
Advantages of BFS:
1. BFS will not get trapped exploring a blind alley.
2. If there is a solution, then BFS is guaranteed to find it. Furthermore, if there are multiple
solutions, then a minimal solution (that require minimum number of steps) will be found.
Disadvantages of BFS:
1. Require more memory since all of the tree that has been generated must be stored.
2. All parts of the tree must be examined to level n before any nodes on level n+1 can be
examined.
Heuristic Search
A heuristic is a method that
might not always find the best solution
but is guaranteed to find a good solution in reasonable time.
By sacrificing completeness it increases efficiency.
Useful in solving tough problems which could not be solved any other way. Solutions
take an infinite time or very long time to compute.
The classic example of heuristic search methods is the travelling salesman problem.
The basic idea of heuristic search is that, rather than trying all possible search paths, you
try and focus on paths that seem to be getting you nearer your goal state. Of course, you
generally can't be sure that you are really near your goal state - it could be that you'll have to take
some amazingly complicated and circuitous sequence of steps to get there. But we might be able
to have a good guess. Heuristics are used to help us make that guess.
To use heuristic search you need an evaluation function that scores a node in the search
tree according to how close to the target/goal state it seems to be. This will just be a guess, but it
should still be useful. For example, for finding a route between two towns a possible evaluation
function might be a ‘as the crow flies’ distance between the town being considered and the target
town. It may turn out that this does not accurately reflect the actual (by road) distance - maybe
there aren't any good roads from this town to your target town. However, it provides a quick way
of guessing that helps in the search.
In order to find the most promising nodes a heuristic function is needed called f' where f'
is an approximation to f and is made up of two parts g and h' where
g is the cost of going from the initial state to the current node. g is considered simply in
this context to be the number of arcs traversed each of which is treated as being of unit
weight.
h' is an estimate of the initial cost of getting from the current node to the goal state.
The function f' is the approximate value or estimate of getting from the initial state to the goal
state. Both g and h' are positive valued variables.
i.e. f’(n) = g(n) + h’(n) : n is node
We'll assume that we are searching trees rather than graphs (i.e., there aren't any loops etc).
However, all the algorithms can be simply extended for graph search by using a closed list of
nodes as described above (though this is unnecessary for hill climbing).
Note that there are many minor variants of these algorithms, which are described in many
different ways. Don't expect all the algorithms in different textbooks to look identical.
2. test to see if this possible solution is a real solution by comparing the state reached with
the set of goal states.
3. if it is a real solution, return. Otherwise repeat from 1.
This method is basically a depth first search as complete solutions must be created before
testing. It is often called the British Museum method as it is like looking for an exhibit at
random. A heuristic is needed to sharpen up the search. The most straightforward way to
implement generate and test is as a depth first search tree with backtracking
Hill climbing
Here the generate and test method is augmented by an heuristic function which measures the
closeness of the current state to the goal state.
Algorithm:
1. Evaluate the initial state if it is goal state quit otherwise current state is initial state.
2. Select a new operator for this state and generate a new state.
3. Evaluate the new state
(i) If it is goal state, then return it and quit.
(ii) If it is not a goal state but it is better than the current state, then make
it the current state.
(iii) If it is not better than the current state, then continue in the loop.
Note: Hill climbing may fail to find a solution. Algorithm may terminate by getting a state
from which no better states can be generated.
Problems with Hill climbing:
Sometime algorithm may terminate by getting a state from which no better states can be
generated, not by finding a goal state. This will happen if the program has reached to the
following problems:
Local Maximum: A local maximum is state that is better than all its neighbors but not better
than some other states further away.
One solution to this problem is backtracking to an ancestor node and trying a secondary path
choice.
Plateau: A plateau is a flat area of the search in which a whole set of neighboring states has the
same value. So it is not possible to determine the best direction in which to move by making
local comparisons.
One solution to this problem is make a big jump in some direction to try to get to a new section
of the search space
Ridge: A ridge is a special kind of local maximum. It is an area of the search space that is higher
than surrounding area and that itself has a slope.
One solution to this problem is applying two or more rule before doing the test. This corresponds
to moving in several directions at once
Page 10 of 22
AI/Unit-I(b)
Page 11 of 22
AI/Unit-I(b)
Note: In hill climbing, one move is sected and all the others are rejected, never to be
reconsidered.
In best first search, one move is selected, but the others are kept around so that they can be
revisited later.
Page 12 of 22
AI/Unit-I(b)
A* Search
The Best First algorithm is a simplified form of the A* algorithm.
The A* search algorithm will involve an OR graph which avoids the problem of node
duplication and assumes that each node has a parent link to give the best node from which it
came and a link to all its successors. In this way if a better node is found this path can be
propagated down to the successors.
This method of using an OR graph requires 2 lists of nodes:
OPEN is a priority queue of nodes that have been evaluated by the heuristic function but
which have not yet been expanded into successors. The most promising nodes are at the
front.
CLOSED are nodes that have already been generated and these nodes must be stored
because a graph is being used in preference to a tree.
Heuristics: In order to find the most promising nodes a heuristic function is needed called f'
where f' is an approximation to f and is made up of two parts g and h' where
g is the cost of going from the initial state to the current node. g is considered simply in
this context to be the number of arcs traversed each of which is treated as being of unit
weight.
h' is an estimate of the initial cost of getting from the current node to the goal state.
The function f' is the approximate value or estimate of getting from the initial state to the goal
state. Both g and h' are positive valued variables. i.e. f’(n) = g(n) + h’(n) : n is node
Algorithm:
1. Place the starting node S on OPEN.
2. If OPEN is empty, stop and return failure.
3. Pick the BEST node from OPEN such that f’ = g + h’ is minimal.
4. If BEST is goal node quit and return the path from initial to BEST Otherwise,
5. Expand BEST, generate all of its successors BEST* and place BEST on CLOSED.
Page 13 of 22
AI/Unit-I(b)
Page 14 of 22
AI/Unit-I(b)
Now consider the situation shown in Figure below. Again we expand B on the first step. On the
second step we again expand E. At the next step we expand F, and finally we generate G, for a
solution path of length 4. But suppose there is a direct path from D to a solution, giving a path of
length 2. We will never find it. By overestimating h' (D) we make D look so bad that we may
find some other, worse solution without ever expanding D.
In general, if h' might overestimate h, we cannot be guaranteed of finding the cheapest path
solution unless we expand the entire graph until all paths are longer than the best solution. An
interesting question is,
"The practical significance of the theorem is-- if h' never overestimates h then A * is
admissible?"
Page 15 of 22
AI/Unit-I(b)
Problem Reduction
Page 16 of 22
AI/Unit-I(b)
2. Another-way is to effectively transform them back into OR graphs where each node
represents a whole set of goals to be satisfied. So, in terms of our search algorithms each
item on the LIST is a set of goals.
Finding a successor to an item on the LIST involves picking a non-primitive goal
(say G) from this set of goals and finding possible sub goals for that goal.
If the node (corresponding to G) was an AND node then there is a single
successor which is a set of goals with the goal G replaced by its sub goals.
If the node was an OR node then there will be several possible successor node,
each being a set of goals with G replaced by a possible successor.
A final ‘goal state’ in these terms will be a set of directly executable or primitive
goals/actions, and our node lists will be lists of lists of goals, where each sub list
will represent a partially developed possible plan.
Things get slightly more complex when you want to use heuristic search. Then we have to
evaluate the ‘goodness’ of a whole set of goals. If each goal has an associated (approximate) cost,
then the cost of a set of goals will just be the sum of these costs.
Note:
1. The above is simpler, but will lead to non-optimal performance. In particular, it will
result in some redundant work.
2. AND-OR graph (or tree) search techniques are needed if we want to use backward
chaining to prove something given a set of rules of the form “IF A AND B AND ...
THEN C”. The problem of proving (say) C is being reduced to the problems of proving A
and proving B etc.
3. And-Or Graphs are useful for certain problems where :
The solution involves decomposing the problem into smaller problems.
Page 17 of 22
AI/Unit-I(b)
AO* Algorithm
1. Let GRAPH consist only of the node representing the initial state. (Call this node INIT.)
Compute h'(INIT).
2.Until INIT is labeled SOLVED or until INIT’s h’ value becomes greater than FUTILITY,
repeat the following procedure:
a.) Trace the labeled arcs from INIT and select for expansion one of the as yet
unexpanded nodes that occurs on this path. Call the selected node NODE.
b.) Generate the successors of NODE. If there are none, then assign FUTILITY as the h’
value of NODE. This is equivalent to saying that NODE is not solvable. If there are
successors, then for each one (called SUCCESSOR) that is not also an ancestor of NODE
do the following:
i.) Add SUCCESSOR to GRAPH.
ii.) If SUCCESSOR is a terminal node, label it SOLVED and assign it an h’ value
of 0.
iii.) If SUCCESSOR is not a terminal node, compute its h' value.
c.) Propagate the newly discovered information up the graph by doing the following: Let
S be a set of nodes that have been labeled SOLVED or whose, h’ values have been
changed and, so need to have values propagated back to their parents. Initialize S to
NODE. Until S is empty, repeat the following procedure:
i.) If possible, select from S a node none of whose descendants in GRAPH occurs in
S. If there is no such node, select any node from S. Call this node CURRENT, and
remove it from S.
ii.) Compute the cost of each of the arcs emerging from CURRENT. The cost of
each arc is equal to the sum of the h’ values of each of the nodes at the end of the arc
plus whatever the cost of the arc itself is. Assign as CURRENT's new h’ value the
minimum of the costs just computed for the arcs emerging from it.
iii.) Mark the best path out of CURRENT by marking the arc that had the minimum
cost as computed in the previous step.
iv.) Mark CURRENT SOLVED if all of the nodes connected to it through the new-
labeled arc has been labeled SOLVED.
v.) If CURRENT has been labeled SOLVED or if the cost of CURRENT was just
changed, then its new status must be propagated back up the graph. So add all of the
ancestors of CURRENT to S.
Page 18 of 22
AI/Unit-I(b)
Generally speaking, state space search may be good when the solution to a problem is
naturally expressed in terms of either a final state, or a path from an initial state to a final state.
We also need to be able to define rules for transforming one state into another, based on
available actions in the domain.
Problem reduction may be better if it is easy to decompose a problem into independent
sub problems - we would have to define rules to do this. It may also allow a more natural
explanation of the decision making process which allowed you to arrive at a solution, and may
result in less search than state-space approaches (so may be better for very complex problems).
Constraint Satisfaction
Many problem in AI can be viewed as problem of constraint satisfaction in which:
To find a solution that satisfies a set of constraints.
Heuristics used not to estimate the distance to the goal but to decide what node to expand
next.
Examples of this technique are design problem, labelling graphs, robot path planning and
cryptarithmetic puzzles.
Constraint satisfaction is a search procedure that operates in a space of constraiont sets. The
initial state contains the constraint that are given in the problem. A goal state is any state that has
been constraint “enough”, where ‘enough’ must be defined for each problem.
E.g. For cryptarithmatic problem, enough means that each latter has been assigned a unique
numeric value.
Constraint satisfaction is a two-step process:
1. Constraints are discovered and propagated as far as possible throught the system. Then if
there is still not a solution, search begins.
2. A guess about something is made and added as a new constraint. Propagation can then
begin with this new constraint.
Algorithm:
1. Propagate available constraints. To do this, first set OPEN to the set of all objects that must
have values assigned to them in a complete solution. Then do until an inconsistency is detected
or until OPEN is empty:
a.) Select an object OB from OPEN. Strengthen as much as possible the set of constraint
that apply to OB.
b.) If this set is different from the set that was assigned the last time OB was examined or
if this is the first time OB has been examined, then add to OPEN all objects that share
any constraints with OB.
c.) Remove OB from OPEN.
2. If the union of the constraints discovered above defines a solution, then quit and report the
solution.
3. If the union of the constraints discovered above defines a contradiction, then return failure.
4. If neither of the above occurs, then it is necessary to make a guess at something in order to
proceed. To do this, loop until a solution is found or all possible solutions have been eliminated:
a) Select an object whose value is not yet determined and select a way of strengthening the
constraints on that object.
b) Recursively invoke constraint satisfaction with the current set of constraints augmented
by the strengthening constraint just selected.
Page 19 of 22
AI/Unit-I(b)
9567
1085
10652
Page 20 of 22
AI/Unit-I(b)
Search and knowledge representation form the basis for many AI techniques.
We have studied search in some detail.
Here are a few pointers as to where specific search methods are used in this course.
Page 22 of 22