Artificial Intelligence Informed Search Techniques (Heuristic Functions, Best First Search, 8-Puzzle Problem With Heuristics)

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

ARTIFICIAL INTELLIGENCE

LECTURE 5

INFORMED SEARCH TECHNIQUES

(Heuristic Functions, Best First Search, 8-Puzzle Problem with Heuristics)

What is Heuristics??
It is a technique designed to solve a problem quickly. A heuristic is an approximate measure of
how close you are to the target. A heuristic guides you in the right direction.
e.g. in mathematics if question is given like selling price of an item is twice the cost or interest
rate is 5% so find out principal amount. To solve such type of questions we start by an
assumption that let x=100 and so on and this assumption is actually helping us to find out the
answer quickly.
Why Heuristics is used?
So, in AI as discussed in previous lecture that in uninformed search, we explore each and every
state of problem until we reach goal. The problem with this approach is that our problem grows
exponentially and it takes non polynomial time to reach at goal in worst case.
e.g. in 8 puzzle problem, in worst case, we may need to explore 320 states. In 15 puzzle it can
go upto 1013 and in chess it can be up to (3580) which is very huge numbers. So, this type of
problems are known as NP (non polynomial) problems.
If we want to solve our problem quickly i.e. in polynomial time then we need to use some greedy
method or some type of guess. This is possible by using heuristics. So, in informed search we
have heuristic values for our states and if we have multiple states to go then instead of exploring
all the states we explore those states having minimum heuristic value. e.g.

In all the informed search algorithms, We have some type of information to find our solution
quickly that information is Heuristics. The solution found using heuristics will always be a
good solution but it may or may not be optimal.
How to Calculate Heuristics?
- Using Euclidian Distance

- Using Manhattan Distance


We use it if we have to find vertical or horizontal distance. e.g. in 8 puzzle problem,
manhattan distance will be that for each tile we will find out that it is how much away
from goal state.

Here in Figure,
0 (1 is at right place) + 0 + 0 + 1 (4 needs to move one step to be at right place) + 1 + 0 + 0 + 1
= 3
Calculate No. of misplaced Tiles:
In the given figure, The no. of misplaced tiles in initial state as compared to goal state are 3 as
tile no. 4, 5, and 8 are not at right places.
POINTS TO REMEMBER:
Uninformed search always generates guaranteed optimal solution. But informed search will
generate good solution but it does not give guarantee of optimal solution. The solution may or
may not be optimal. So, we use Heuristics to solve a problem quickly i.e. in polynomial time.
Let’s say, in the figure below, Straight line distance from S to G will be minmum. But, if a
hurdle arises along the path, then the path will not be optimal.

Best First Search:


Recall: BFS and DFS pick the next node off the frontier based on which was "first in" or "last
in". Best First picks the "best" node according to some rule of thumb, called a heuristic.
Best first search uses the concept of a priority queue and heuristic search. Best first search is a
traversal technique that decides which node is to be visited next by checking which node is the
most promising one and then check it. For this it uses an evaluation function to decide the
traversal. The aim is to reach the goal from the initial state via the shortest path.
To search the graph space, the BFS method uses two lists for tracking the traversal. An ‘Open’
list which keeps track of the current ‘immediate’ nodes available for traversal and ‘CLOSED’ list
that keeps track of the nodes already traversed.  The cost of nodes is stored in a priority queue.
This makes implementation of best-first search is same as that of breadth First search. We will
use the priority queue just like we use a queue for BFS.

Best First Search Algorithm:


 Step 1: Place the starting node into the OPEN list.
 Step 2: If the OPEN list is empty, Stop and return failure.
 Step 3: Remove the node n, from the OPEN list which has the lowest value of h(n), and
places it in the CLOSED list.
 Step 4: Expand the node n, and generate the successors of node n.
 Step 5: Check each successor of node n, and find whether any node is a goal node or not.
If any successor node is goal node, then return success and terminate the search, else
proceed to Step 6.
 Step 6: For each successor node, algorithm checks for evaluation function f(n), and then
check if the node has been in either OPEN or CLOSED list. If the node has not been in
both list, then add it to the OPEN list.
 Step 7: Return to Step 2.
Example:
Node H(n)
A G 40
B G 32
C G 25
D G 35
E G 19
F G 17
H G 10
G G 0

Priority Closed List


Queue
A
C, B ,D A
F, E, B, D A, C
G, E, B, D A, C, F
A, C, F, G
Stop

Path = A C F G
Cost = 14 + 10+ 20 = 44 (Check if it is optimal solution or not ???)
Optimality: Best first search with heuristics will always give a good solution but it will not
give an optimal solution always. E.g. in above example, cost of path ACEHG is 41 which is
less than the path generated by algorithm.
Time Complexity: In best case/average case, it will always be better than uninformed
search algorithms as its complexity will be polynomial. But in worst case it can also take
O(bd) time complexity and that depends upon heuristics. If heuristics is good then it will be
good but if you have bad heuristics then it will perform same like uninformed search
techniques.
e.g. If u go from one city to other and u don’t have google map then u will use heuristics i.e.
u will ask someone if that person will guide you right then that will be good heuristics and
you will reach your destination easily and if that person will not tell you right path then that
will be bad heuristics.
Discussion Questions:
What are Strengths and weaknesses of Best First Search?
How can we improve Best First Search?
By taking into account both the heuristic values and the actual costs of each node.

8-PUZZLE PROBLEM (WITH HEURISTICS)


To solve the problem with Heuristic search or informed search we have to calculate Heuristic
values of each node. For calculation of heuristics, we will use no. of misplaced tiles.

Note: See the initial state and goal state carefully all values except (4,5 and 8) are at their
respective places. So, the heuristic value for first node is 3.

You might also like