Ai PPT Un It 2
Ai PPT Un It 2
SEARCH
Issues in the Design of Search Programs
• The tree can be searched forward from the initial node to the goal state or backwards from the goal
state to the initial state.
• To select applicable rules, it is critical to have an efficient procedure for matching rules against
states.
• How to represent each node of the search process? This is the knowledge representation problem
or the frame problem. In games, an array suffices; in other problems, more complex data
structures are needed.
• Finally in terms of data structures, considering the water jug as a typical problem do we use a graph
or tree? The breadth-first structure does take note of all nodes generated but the depth-first one can be
modified.
Check duplicate nodes
1.Observe all nodes that are already generated, if a new node is present.
a. Set the node that is being expanded to the point to the already existing node corresponding to
its successor rather than to the new one. The new one can be thrown away.
b. If the best or shortest path is being determined, check to see if this path is better or worse
than the old one. If worse, do nothing.
Better save the new path and work the change in length through the chain of successor nodes if
necessary.
• Example: Tic-Tac-Toe
• State spaces are good representations for board games such as Tic-Tac-Toe. The position of a game can be explained
by the contents of the board and the player whose turn is next. The board can be represented as an array of 9 cells,
each of which may contain an X or O or be empty.
• State:
Player to move next: X or O.
Board configuration:
• Terminal States: Three X’s in a row; Three O’s in a row; All cells full.
•
Search Tree
The sequence of states formed by possible moves is called a search tree. Each level of the tree is called a ply.
Since the same state may be reachable by different sequences of moves, the state space may in general be a graph. It may be treated as a tree for
simplicity, at the cost of duplicating states.
Solving problems using search
• Given an informal description of the problem, construct a formal description as a state space:
Define a data structure to represent the state.
Make a representation for the initial state from the given data.
Write programs to represent operators that change a given state representation to a new state
representation.
Write a program to detect terminal states.
• The algorithms that use heuristic functions are called heuristic algorithms.
• Heuristic algorithms are not really intelligent; they appear to be intelligent because they achieve better
performance.
• Heuristic algorithms are more efficient because they take advantage of feedback from the data to direct the
search path.
• Uninformed search algorithms or Brute-force algorithms, search through the search space all possible
candidates for the solution checking whether each candidate satisfies the problem’s statement.
• Informed search algorithms use heuristic functions that are specific to the problem, apply them to guide the
search through the search space to try to reduce the amount of time spent in searching.
• Some prominent intelligent search algorithms are stated below:
1. Generate and Test Search
2. Best-first Search
3. Greedy Search
4. A* Search
5. Constraint Search
6. Means-ends analysis
• There are some more algorithms. They are either improvements or combinations of these.
• Uninformed Search: Also called blind, exhaustive or brute-force search, it uses no information about the problem to
guide the search and therefore may not be very efficient.
• Informed Search: Also called heuristic or intelligent search, this uses information about the problem to guide the search
—usually guesses the distance to a goal state and is therefore efficient, but the search may not be always possible.
• The first requirement is that it causes motion, in a game playing program, it moves on the
board and in the water jug problem, filling water is used to fill jugs.
• It means the control strategies without the motion will never lead to the solution.
• The second requirement is that it is systematic, that is, it corresponds to the need for global
motion as well as for local motion.
• This is a clear condition that neither would it be rational to fill a jug and empty it repeatedly,
nor it would be worthwhile to move a piece round and round on the board in a cyclic way in
a game.
• We shall initially consider two systematic approaches for searching. Searches can be
classified by the order in which operators are tried: depth-first, breadth-first, bounded depth-
first.
• Breadth-first search
• A Search strategy, in which the highest layer of a decision tree is searched completely before proceeding to the
next layer is called Breadth-first search (BFS).
• In this strategy, no viable solutions are omitted and therefore it is guaranteed that an optimal solution is
found.
• This strategy is often not feasible when the search space is large.
• Algorithm
a. Remove the first element from the LIST and call it E. If the LIST is empty, quit.
(ii) If the new state is a goal state, quit and return this state.
1. Guaranteed to find an optimal solution (in terms of shortest number of steps to reach the goal).
• Disadvantages
Depth-first search
• A search strategy that extends the current path as far as possible before backtracking to the last choice
point and trying the next alternative path is called Depth-first search (DFS).
• This strategy does not guarantee that the optimal solution has been found.
• In this strategy, search reaches a satisfactory solution more rapidly than breadth first, an advantage
• Depth-first search applies operators to each newly generated state, trying to drive directly toward the goal.
a. Generate a successor E to the starting state. If there are no more successors, then signal failure.
2. Easily programmed: function call stack does most of the work of maintaining state of the search.
• Disadvantages
1. May find a sub-optimal solution (one that is deeper or more costly than the best solution).
2. Incomplete: without a depth bound, may not find a solution even if one exists.
1.Heuristic search techniques
• For complex problems, the traditional algorithms, presented above, are unable to find the solution
within some practical time and space limits. Consequently, many special techniques are developed,
using heuristic functions.
• Blind search is not always possible, because it requires too much time or Space (memory).
• Heuristic Search is a weak technique but can be effective if applied correctly; it requires
domain specific information.
goal.
Heuristics might (for reasons) underestimate or overestimate the merit of a state with respect to goal.
• Heuristic evaluation function estimates likelihood of given state leading to goal state.
• Heuristic search function estimates cost from current state to goal, presuming function is efficient.
• The Heuristic search is compared with Brute force or Blind search techniques below:
•• Comparision
Brute force / of algorithms
Blind search Heuristic search
• Can only search what it has knowledge Estimates ‘distance’ to goal state about already
through explored nodes
• No knowledge about how far a node Guides search process toward goal node
from goal state Prefers states (nodes) that lead close to
and not away from goal state
Example: Travelling salesman
• A salesman has to visit a list of cities and he must visit each city only once. There are different routes
between the cities.
• The problem is to find the shortest route between the cities so that the salesman visits all the cities at
once.
• Suppose there are N cities, then a solution would be to take N! possible combinations to find the shortest
distance to decide the required route.
• This is not efficient as with N=10 there are 36,28,800 possible routes. This is an example of
combinatorial explosion.
• There are better methods for the solution of such problems: one is called branch and bound. First,
generate all the complete paths and find the distance of the first complete path.
• If the next path is shorter, then save it and proceed this way avoiding the path when its length exceeds
the saved shortest path length, although it is better than the previous method.
Generate and Test Strategy
• Generate-And-Test Algorithm
• Generate-and-test search algorithm is a very simple algorithm that guarantees to find a solution if
done systematically and there exists a solution.
• Algorithm: Generate-And-Test
• Potential solutions that need to be generated vary depending on the kinds of problems. For some
problems the possible solutions may be particular points in the problem space and for some
problems, paths from the start state.
Figure: Generate And Test
Generate-and-test, like depth-first
search, requires that complete
solutions be generated for testing.
• Given a large set of inputs and a good heuristic function, it tries to find a sufficiently good solution to
the problem. This solution may not be the global optimal maximum.
In the above definition, mathematical optimization problems implies that hill climbing solves the
problems where we need to maximize or minimize a given real function by choosing values from the
given inputs.
Example-Travelling salesman problem where we need to minimize the distance traveled by salesman.
‘Heuristic search’ means that this search algorithm may not find the optimal solution to the problem.
However, it will give a good solution in reasonable time.
• A heuristic function is a function that will rank all the possible alternatives at any branching step in
search algorithm based on the available information. It helps the algorithm to select the best route out of
possible routes.
Features of Hill Climbing
1. Variant of generate and test algorithm : It is a variant of generate and test algorithm. The generate and test
algorithm is as follows :
• Hence we call Hill climbing as a variant of generate and test algorithm as it takes the feedback from test procedure. Then
this feedback is utilized by the generator in deciding the next move in search space.
• 2. Uses the Greedy approach : At any point in state space, the search moves in that direction only which
optimizes the cost of function with the hope of finding the optimal solution at the end.
Types of Hill Climbing
1. Simple Hill climbing : It examines the neighboring nodes one by one and selects the first neighboring node which
• Step 1 : Evaluate the initial state. If it is a goal state then stop and return success. Otherwise, make initial state as
current state.
• Step 2 : Loop until the solution state is found or there are no new operators present which can be applied to current state.
a) Select a state that has not been yet applied to the current state and apply it to produce a new state.
i. If the current state is a goal state, then stop and return success.
ii. If it is better than the current state, then make it current state and proceed further.
iii. If it is not better than the current state, then continue in the loop until a solution is found.
• Step 3 : Exit.
2.Steepest-Ascent Hill climbing : It first examines all the neighboring nodes and then selects the node closest
to the solution state as next node.
• Step 1 : Evaluate the initial state. If it is goal state then exit else make the current state as initial state
• Step 2 : Repeat these steps until a solution is found or current state does not change
i. Let ‘target’ be a state such that any successor of the current state will be better than it;
c. if this state is goal state then quit else compare with ‘target’
It does not examine all the neighboring nodes before deciding which node to select .It just selects a
neighboring node at random, and decides (based on the amount of improvement in that neighbor)
whether to move to that neighbor or to examine another.
• State space diagram is a graphical representation of the set of states our search algorithm can
reach vs the value of our objective function(the function which we wish to maximize).
X-axis : denotes the state space ie states or configuration our algorithm may reach.
• The best solution will be that state space where objective function has maximum value(global
maximum).
Different regions in the State Space Diagram
5.Current state : The region of state space diagram where we are currently present during the search.
• Hill climbing cannot reach the optimal/best state(global maximum) if it enters any of the following regions
1.Local maximum : At a local maximum all neighboring states have a values which is worse than than the
current state. Since hill climbing uses greedy approach, it will not move to the worse state and terminate itself.
The process will end even though a better solution may exist.
• To overcome local maximum problem : Utilize backtracking technique. Maintain a list of visited states. If
the search reaches an undesirable state, it can backtrack to the previous configuration and explore a new
path.
2.Plateau : On plateau all neighbors have same value . Hence, it is not possible
to select the best direction.
• To overcome plateaus : Make a big jump. Randomly select a state far away
from current state. Chances are that we will land at a non-plateau region
3.Ridge : Any point on a ridge can look like peak because movement in all
possible directions is downward. Hence the algorithm stops when it
reaches this state.
• To overcome Ridge : In this kind of obstacle, use two or more rules
before testing. It implies moving in several directions at once.
Best First Search (Informed Search)
In BFS and DFS, when we are at a node, we can consider any of the adjacent as next node. So both BFS and DFS
blindly explore paths without considering any cost function. The idea of Best First Search is to use an evaluation
function to decide which adjacent is most promising and then explore. Best First Search falls under the category of
Heuristic Search or Informed Search.
• We use a priority queue to store costs of nodes. So the implementation is a variation of BFS, we just need to change
pq initially contains S
We remove s from and process
unvisited neighbors of S to pq.
pq now contains {A, C, B} (C is
put before B because C has lesser
cost)
• Analysis :
The worst case time complexity for Best First Search is O(n * Log n) where n is number of nodes.
In worst case, we may have to visit all nodes before we reach goal. Note that priority queue is
implemented using Min(or Max) Heap, and insert and remove operations take O(log n) time.
Performance of the algorithm depends on how well the cost or evaluation function is designed.
• A* Search Algorithm
• A* is a type of search algorithm. Some problems can be solved by representing the world in the initial state,
and then for each action we can perform on the world we generate states for what the world would be like if
we did so.
• If you do this until the world is in the state that we specified as a solution, then the route from the start to this
goal state is the solution to your problem.
Some terminology
• A node is a state that the problem's world can be in. In pathfinding a node would be just a 2d coordinate of
where we are at the present time.
• In the 8-puzzle it is the positions of all the tiles. Next all the nodes are arranged in a graph where links
between nodes represent valid steps in solving the problem.
• These links are known as edges. In the 8-puzzle diagram the edges are shown as blue lines. See figure 1
below.
• State space search, then, is solving a problem by beginning with the start state, and then for each node we
expand all the nodes beneath it in the graph by applying all the possible moves that can be made at each point.
Heuristics and Algorithms
• At this point we introduce an important concept, the heuristic. This is like an algorithm, but with a key difference.
An algorithm is a set of steps which you can follow to solve a problem, which always works for valid input.
• For example you could probably write an algorithm yourself for multiplying two numbers together on paper. A
heuristic is not guaranteed to work but is useful in that it may solve a problem for which there is no algorithm.
• But the 8-puzzle is more difficult. There is no known algorithm for calculating from a given position how many
moves it will take to get to the goal state. So various heuristics have been devised. The best one that I know of is
known as the Nilsson score which leads fairly directly to the goal most of the time, as we shall see.
Cost
• When looking at each node in the graph, we now have an idea of a heuristic, which can estimate how close the
state is to the goal.
• Another important consideration is the cost of getting to where we are. In the case of pathfinding we often assign
a movement cost to each square. The cost is the same then the cost of each square is one.
• 8 Puzzle
Let's look at the 8 puzzle in more detail. This is a simple sliding tile puzzle on a 3*3 grid where one
tile is missing and you can move the other tiles into the gap until you get the puzzle into the goal
position. See figure 1.
The 8 puzzle game state is as simple as representing a list of the 9 squares and what's in them.
Here are two states for example; the last one is the GOAL state, at which point we've found
the solution. The first is a jumbled up example that you may start from.
F, E
• The rules that you can apply to the puzzle are also simple. If there is a blank tile above, below,
to the left or to the right of a given tile, then you can move that tile into the space. To solve the
puzzle you need to find the path from the start state, through the graph down to the goal state.
• We are now ready to look at the operation of the A* algorithm. What we need to do is start with the goal
state and then generate the graph downwards from there.
• Let's take the 8-puzzle in figure 1. We ask how many moves can we make from the start state? The
answer is 2, there are two directions we can move the blank tile, and so our graph expands.
• If we were just to continue blindly generating successors to each node, we could potentially fill the
computer's memory before we found the goal node. Obviously we need to remember the best nodes and
search those first.
• We also need to remember the nodes that we have expanded already, so that we don't expand the same
state repeatedly.
• Let's start with the OPEN list. This is where we will remember which nodes we haven't yet expanded.
When the algorithm begins the start state is placed on the open list, it is the only state we know about and
we have not expanded it. So we will expand the nodes from the start and put those on the OPEN list too.
• Now we are done with the start node and we will put that on the CLOSED list. The CLOSED list is a list
of nodes that we have expanded.
• f=g+h
• Using the OPEN and CLOSED list lets us be more selective about what we look at next in the
search. We want to look at the best nodes first. We will give each node a score on how good we
think it is.
• This score should be thought of as the cost of getting from the node to the goal plus the cost of
getting to where we are. Traditionally this has been represented by the letters f, g and
• h. 'g' is the sum of all the costs it took to get here, 'h' is our heuristic function, the estimate of what
it will take to get to the goal. 'f' is the sum of these two. We will store each of these in our nodes.
• Using the f, g and h values the A* algorithm will be directed, subject to conditions we will look at
further on, towards the goal and will find it in the shortest route possible.
Advantages:
• It is the best one from other techniques. It is used to solve very complex problems.
• It is optimally efficient, i.e. there is no other optimal algorithm guaranteed to expand fewer nodes
than A*.
Disadvantages:
• This algorithm is complete if the branching factor is finite and every action has fixed cost.
• The speed execution of A* search is highly dependant on the accuracy of the heuristic algorithm
that is used to compute h (n).
AO* Search: (And-Or) Graph
• The Depth first search and Breadth first search given earlier for OR trees or graphs can be easily adopted by
AND-OR graph. The main difference lies in the way termination conditions are determined, since all goals
following an AND nodes must be realized; where as a single goal node following an OR node will do. So for
this purpose we are using AO* algorithm.
• Like A* algorithm here we will use two arrays and one heuristic function.
• OPEN:
• It contains the nodes that has been traversed but yet not been marked solvable or unsolvable.
• CLOSE:
• Step 3: Select a node n that is both on OPEN and a member of T0. Remove it from OPEN and
place it in
• CLOSE
• Step 4: If n is the terminal goal node then leveled n as solved and leveled all the ancestors of n as
solved. If the starting node is marked as solved then success and exit.
• Step 5: If n is not a solvable node, then mark n as unsolvable. If starting node is marked as
unsolvable, then return failure and exit.
• Step 6: Expand n. Find all its successors and find their h (n) value, push them into OPEN.
• Step 8: Exit.
Implementation:
Step 1:
In the above graph, the solvable nodes are A, B, C, D, E, F and the unsolvable nodes are G, H.
Take A as the starting node. So place A into OPEN.
Advantages:
It is an optimal algorithm.
• When a problem can be divided into a set of sub problems, where each sub problem can be solved separately
and a combination of these will be a solution, AND-OR graphs or AND - OR trees are used for representing the
solution.
• The decomposition of the problem or problem reduction generates AND arcs. One AND are may point to any
number of successor nodes.
• All these must be solved so that the arc will rise to many arcs, indicating several possible solutions. Hence the
graph is known as AND - OR instead of AND. Figure shows an AND - OR graph
1.traverse the graph starting at the initial node and following the current best
path, and accumulate the set of nodes that are on the path and have not yet been
expanded.
2.Pick one of these unexpanded nodes and expand it. Add its successors to the
graph and computer f ' (cost of the remaining distance) for each of them.
3.Change the f ' estimate of the newly expanded node to reflect the new information produced by its
successors. Propagate this change backward through the graph. Decide which of the current best
path.
• The propagation of revised cost estimation backward is in the tree is not necessary in A*
algorithm. This is because in AO* algorithm expanded nodes are re-examined so that the current
best path can be selected. The working of AO* algorithm is illustrated in figure as follows
• Example
• With help of f(n) = g(n) + h(n) evaluation function,
Start from node A, f(A⇢B) = g(B) + h(B) = 1 + 5 ……here g(n)=1
is taken by default
for path cost = 6
f(A⇢C+D) = g(c) + h(c) + g(d) + h(d) = 1 + 2 + 1 + 4 ……
here we have added C & D because they are in AND = 8 So,
by calculation A⇢B path is chosen which is the minimum path,
i.e f(A⇢B)
According to the answer of step 1,
explore node B Here the value of E & F are calculated as follows,
f(B⇢E) = g(e) + h(e)
f(B⇢E) = 1 + 7 = 8
f(B⇢f) = g(f) + h(f) f(B⇢f) = 1 + 9 = 10 So,
by above calculation B⇢E path is chosen which is minimum path,
i.e f(B⇢E) because B's heuristic value is different from its actual value The heuristic
is updated and the minimum cost path is selected.
The minimum value in our situation is 8. Therefore, the heuristic for A must be updated
due to the change in B's heuristic. So we need to calculate it again. f(A⇢B) = g(B) +
updated h(B) = 1 + 8 = 9 We have Updated all values in the above tree.
By comparing f(A⇢B) & f(A⇢C+D) f(A⇢C+D) is shown to be smaller.
i.e 8 < 9 Now explore f(A ⇢C+D) So, the current node is C f(C⇢G) = g(g) + h(g) f(C⇢G) = 1 + 3 = 4 f(C ⇢H+I) = g(h) + h(h) + g(i)
+ h(i)
f(C⇢H+I) = 1 + 0 + 1 + 0 ……here we have added H & I because they are in AND = 2 f(C⇢H+I) is selected as the path with the lowest
cost and the heuristic is also left unchanged because it matches the actual cost.
Paths H & I are solved because the heuristic for those paths is 0,
but Path A⇢D needs to be calculated because it has an AND. f(D⇢J) = g(j) + h(j) f(D ⇢J) = 1 + 0 = 1 the heuristic of node D
needs to be updated to 1.
f(A⇢C+D) = g(c) + h(c) + g(d) + h(d) = 1 + 2 + 1 + 1 = 5 as we can see that path f(A⇢C+D) is get solved and this tree has become
a solved tree now.
In simple words, the main flow of this algorithm is that we have to find firstly level 1st heuristic value and then level 2nd
and after that update the values with going upward means towards the root node. In the above tree diagram, we have updated all
these values.
CONSTRAINT SATISFACTION:-
• Many problems in AI can be considered as problems of constraint satisfaction, in which the goal state
satisfies a given set of constraint. constraint satisfaction problems can be solved by using any of the
search strategies. The general form of the constraint satisfaction procedure is as follows:
• Until a complete solution is found or until all paths have led to lead ends, do
2.Apply the constraint inference rules to the selected node to generate all possible new constraints.
3.If the set of constraints contains a contradiction, then report that this path is a dead end.
4.If the set of constraints describes a complete solution then report success.
5.If neither a constraint nor a complete solution has been found then apply the rules to generate new
partial solutions. Insert these partial solutions into the search graph.
• Example: consider the crypt arithmetic problems.
• SEND
• + MORE
• -----------
• Money
• ----------
Assign decimal digit to each of the letters in such a way that the answer to the problem is correct to the
same letter occurs more than once , it must be assign the same digit each time . no two different letters
may be assigned the same digit. Consider the crypt arithmetic problem.
• SEND
• + MORE
• ----------
• MONEY
CONSTRAINTS
1.no two digit can be assigned to same letter.
2.Assumption can be made at various levels such that they do not contradict each other.
3.The problem can be decomposed into secured constraints. A constraint satisfaction approach may
be used.
1. initial guess m=1 because the sum of two single digits can
generate at most a carry '1’.
2. When n=1 o=0 or 1 because the largest single digit number added to m=1 can generate the sum of either 0 or 1 depend on
the carry received from the carry sum. By this we conclude that o=0 because m is already 1 hence we cannot assign same
digit another letter(rule no.)
• Most of the search strategies either reason forward of backward however, often a mixture o the two directions is
appropriate.
• Such mixed strategy would make it possible to solve the major parts of problem first and solve the smaller problems
the arise when combining them together. Such a technique is called "Means - Ends Analysis".
• The means -ends analysis process centers around finding the difference between current state and goal state.
• The problem space of means - ends analysis has an initial state and one or more goal state, a set of operate with a set
of preconditions their application and difference functions that computes the difference between two state a(i) and s(j).
A problem is solved using means - ends analysis by
1.Computing the current state s1 to a goal state s2 and computing their difference D12.
2.Satisfy the preconditions for some recommended operator op is selected, then to reduce the difference D12.
3.The operator OP is applied if possible. If not the current state is solved a goal is created and means- ends analysis is
1.If in out current state we are hungry , and in our goal state we are not hungry , then either the
"visit hotel" or "visit Canteen " operator is recommended.
2.If our current state we do not have money , and if in your goal state we have money, then the
"Visit our bank" operator or the "Visit secretary" operator is recommended.
3.If our current state we do not know where something is , need in our goal state we do know,
then either the "visit office enquiry" , "visit secretary" or "visit co worker " operator is
recommended.