You are on page 1of 36

Artificial Intelligence

&
Machineth
Learning
5 Semester
CSE, 21CS54

Bhaktavatsala Shivaram
Adjunct Faculty
21CS54, AI&ML - Bhaktavatsala Shivaram, Adjunct Faculty, CSE 2
21CS54, AI&ML - Bhaktavatsala Shivaram, Adjunct Faculty, CSE 3
21CS54, AI&ML - Bhaktavatsala Shivaram, Adjunct Faculty, CSE 4
21CS54, AI&ML - Bhaktavatsala Shivaram, Adjunct Faculty, CSE 5
21CS54, AI&ML - Bhaktavatsala Shivaram, Adjunct Faculty, CSE 6
21CS54, AI&ML - Bhaktavatsala Shivaram, Adjunct Faculty, CSE 7
Module 2
Informed Search Strategies
• Greedy best-first search
• A* search
• Heuristic function

Machine Learning
• Introduction
• Understanding Data

21CS54, AI&ML - Bhaktavatsala Shivaram, Adjunct Faculty, CSE 8


Model 2: Informed Search
B INFORMED SEARCH UNINFORMED SEARCH
a
• Search with Information • Search without Information
c
k • Use Knowledge to find steps to • No Knowledge
g solution
r
• Quick Solution • Time Consuming
o
u • Less Complexity • More Complexity (Time, Space)
n • Eg: A*, Heuristic DFS, Best First • Depth First Search, Breadth First
d
Search Search, etc..
• Also called as “Heuristic” search • Also called as “Blind” search

21CS54, AI&ML - Bhaktavatsala Shivaram, Adjunct Faculty, CSE 9


Model 2: Informed Search
B INFORMED SEARCH • Informed search algorithm
a
• Search with Information contains an array of knowledge
c
• Use Knowledge to find steps to such as how far we are from the
k
g solution goal, path cost, how to reach to
r goal node, etc. This knowledge
• Quick Solution
o help agents to explore less to
u • Less Complexity the search space and find more
n • Eg: A*, Heuristic DFS, Best First efficiently the goal node.
d
Search • The informed search algorithm
• Also called as “Heuristic” search is more useful for large search
space.
• Informed search algorithm uses
the idea of heuristic, so it is also
called Heuristic search.
21CS54, AI&ML - Bhaktavatsala Shivaram, Adjunct Faculty, CSE 10
Model 2: Informed Search Strategies – Greedy Best First Search
G Greedy Best First Search:
r • is an AI search algorithm that attempts to find the most promising path from a
e given starting point to a goal.
e • It prioritizes paths that appear to be the most promising, regardless of whether
d or not they are actually the shortest path.
y • The algorithm works by evaluating the cost of each possible path and then
expanding the path with the lowest cost.
B • This process is repeated until the goal is reached.
e
s
Greedy Best First Search AI Algorithm:
t • Use heuristic function to determine which path is the most promising.
• The heuristic function takes into account the cost of the current path and the
S estimated cost of the remaining paths.
e • If the cost of the current path is lower than the estimated cost of the
a remaining paths, then the current path is chosen.
r • This process is repeated until the goal is reached.
c 21CS54, AI&ML - Bhaktavatsala Shivaram, Adjunct Faculty, CSE 11
Model 2: Informed Search Strategies – Greedy Best First Search
G • Always selects the path which Algorithm
r appears best at the moment
e • Priority Queue “Y” containing
• It is a combination of depth-first
e initial states
d
search and breadth-first search Loop
y • It uses the heuristic function and • If Y= Empty Return Fail
• Else
search • Node <- Remove- First (Y)
B • With the help of best-first search, • If Node = Goal
e • Return path from Initial
at each step, we can choose the
s state
t
most promising node. • Else
• In the best first search algorithm, • Generate all successors of
NODE
S we expand the node which is • And insert newly generated
e closest to the goal node and the NODE
a minimum cost is estimated by • Into Y according to cost
r value
heuristic function. End Loop
c 21CS54, AI&ML - Bhaktavatsala Shivaram, Adjunct Faculty, CSE 12
Model 2: Informed Search Strategies – Greedy Best First Search
G • The evaluation function is f(n) = h(n)
r where, h(n) = estimated cost from node n to the goal
e
e • Greedy search ignores the cost of the path that has already been
d traversed to reach n.
y • Therefore, the solution given is not necessarily optimal.
B
e
s
t

S
e
a
r
c 21CS54, AI&ML - Bhaktavatsala Shivaram, Adjunct Faculty, CSE 13
Model 2: Informed Search Strategies – Greedy Best First Search
G Greedy Search Example1
r
e A 75 f(n) = h(n) = straight Open Closed
118 B
e 140 Line distance heuristic 1. [A] []
d [A]
C 2. [E,C,B]
y E
111
99 3. [F,G,C,B] [A, E]
B D 80
F 4. [I,G,C,B] [A, E, F]
e
G
s 211 5. [E,B,D] [A, E, F, I]
97
t

H 101
S I Traverse Path = A -> E -> F-> I
e Distance (A-E-F-I) =253+178+0 = 431
a
r
Path Cost (A-E-F-I) =140+99+211 = 450
c 21CS54, AI&ML - Bhaktavatsala Shivaram, Adjunct Faculty, CSE 14
Model 2: Informed Search Strategies – Greedy Best First Search
G Greedy Search Example2
r
e A 75 f(n) = h(n) = straight Open Closed
118 B
e 140 Line distance heuristic 1. [A] []
d [A]
C 2. [C,E,B]
y E
111
99 3. [D,E,B] [A, C]
80 ** 220
B D
F
e
s G
211 INFINITE LOOP
97
t

H 101
S I
e
a
r
c 21CS54, AI&ML - Bhaktavatsala Shivaram, Adjunct Faculty, CSE 15
Model 2: Informed Search Strategies – Greedy Best First Search
G • Greedy best first search can start down an infinite path and never
r return to try other possibilities, it is incomplete
e
e • Because of its greediness the search makes choice that can lead to a
d dead end; then one backs up in the search tree to the deepest
y unexpanded node.
▪ Greedy best first search resembles depth-first search in the way it prefers to follow
B
a single path all the way to the goal, but will back up when it hits a dead end.
e
s ▪ The quality of the heuristic function determines the practical usability of greedy
t search

S
e
a
r
c 21CS54, AI&ML - Bhaktavatsala Shivaram, Adjunct Faculty, CSE 16
Model 2: Informed Search Strategies – Greedy Best First Search
G Greedy Search Example3
r
e A 7 Straight Line Distance Open Closed
11 D
e 14 Given (say): 1. [A] []
d B 18 A -> G = 40 2. [C,B,D] [A]
25
y C B -> G = 32
10 C -> G = 25 3. [F,E,B,D] [A, C]
15
8
B
F
D -> G = 35 4. [G,E,B,D] [A, C, F]
e E E -> G = 19
s 20 F -> G = 17 5. [E,B,D] [A, C, F, G]
9
t G -> G = 0
H 10 H -> G = 10
S G Traverse Path = A -> C -> F-> G
e Path Cost (A-C-F-G) = 14+10+20 = 44
a
r
Distance (A-C-F-G) = 40+19+17 = 66
c 21CS54, AI&ML - Bhaktavatsala Shivaram, Adjunct Faculty, CSE 17
Model 2: Informed Search Strategies – Greedy Best First Search
G Greedy Search Example4
r
e S Node (n) ->H(n) Open Closed
3
e 2
A -> 12 1. [S] []
d B -> 4 [S]
A B 2. [B,A]
y 4 C -> 7
1
3 1
D -> 3 3. [F,E,A] [S, B]
B E -> 8 4.
C E F [G,I,E,A] [S, B, F]
e D 5 F -> 2
2 3
s G -> 0 5. [I,E,A] [S, B, F, G]
t H -> 4
H I
G I -> 9
S Traverse Path = S -> B -> F-> G
S -> 13
e Path Cost (S-B-F-G) = 2+1+3 = 6
a
r
Distance (S-B-F-G) = 13+4+2+0 = 19
c 21CS54, AI&ML - Bhaktavatsala Shivaram, Adjunct Faculty, CSE 18
Model 2: Informed Search Strategies – Greedy Best First Search
G • Greedy search is not optimal
r
e
• Greedy search is incomplete without systematic checking of repeated
e states.
d • In the worst case, the Time and Space Complexity of Greedy Search
y
are both same = O (bm)
B
where b – branching factor and m – max path length
e
s
t

S
e
a
r
c 21CS54, AI&ML - Bhaktavatsala Shivaram, Adjunct Faculty, CSE 19
Model 2: Informed Search Strategies – Greedy Best First Search
G
Advantages: Disadvantages:
r
e • Simple and Easy to • Inaccurate Results
e
Implement • Local Optima
d
y • Fast and Efficient • Heuristic Function
• Low Memory Requirements • Lack of Completeness
B
• Flexible
e
s • Efficiency
t

S
e
a
r
c 21CS54, AI&ML - Bhaktavatsala Shivaram, Adjunct Faculty, CSE 20
Model 2: Informed Search Strategies – Greedy Best First Search
G Few applications of Greedy Best First Search
r
Pathfinding: used to find the shortest path between two points in a graph. It is used
e
in many applications such as video games, robotics, and navigation systems.
e
Machine Learning: used in machine learning algorithms to find the most promising
d
path through a search space.
y
Optimization: used to optimize the parameters of a system in order to achieve the
desired result.
B
Game AI: used in game AI to evaluate potential moves and chose the best one.
e
Navigation: used to navigate to find the shortest path between two locations.
s
Natural Language Processing: used in natural language processing tasks such as
t
language translation or speech recognition to generate the most likely sequence of
words.
S
Image Processing: used in image processing to segment image into regions of
e
interest.
a
r
c 21CS54, AI&ML - Bhaktavatsala Shivaram, Adjunct Faculty, CSE 21
Model 2: Informed Search Strategies – A*Search
A A* Search
*
Greedy Best First Search minimizes a heuristic h(n) which is an
S estimated cost from a current state n to the goal state
e Greedy Best First Search is efficient but it is not optimal and not
a complete
r
Uniform Cost Search minimizes the cost g(n) from the initial state to
c
h the current state n.
Uniform cost search is optimal and complete but not efficient
A* SEARCH:
It combines Greedy Best Search and Uniform Cost Search to
get an efficient algorithm which is complete and optimal.

21CS54, AI&ML - Bhaktavatsala Shivaram, Adjunct Faculty, CSE 22


Model 2: Informed Search Strategies – A* Search
A • A* evaluates nodes by combining Algorithm
*
g(n), the cost to reach the node i. Enter Starting node in OPEN List
S and h(n), the cost to get from the ii. If OPEN List is empty Return FAIL
e node to the goal iii. Collect node from OPEN list which
has smallest value (g+h)
a • f(n)=g(n)+h(n)
r a. If node = Goal . Return Success
c
f(n) is the evaluation function which iv. Expand node n and generate all
h gives the cheapest solution cost. successors
g(n) is the exact cost to reach node a. Complete (g+h) for each
successors
n from the initial state
v. If node n is already OPEN/CLOSE,
h(n) is an estimation of the attach to backpointer
assumed cost from current state (n) vi. Continue iii.
to reach the goal.
21CS54, AI&ML - Bhaktavatsala Shivaram, Adjunct Faculty, CSE 23
Model 2: Informed Search Strategies – A* Search
A Greedy Search Example 1
*
A 75 f(n) = g(n)+ h(n)
118 B
S 140
[447] [449] f(c) = g(c)+ h(c)
e C f(c) = 118+329
a E
111 [393] f(c) = 447
r 99
c D 80 f(e) = g(e)+ h(e)
F
h f(e) = 140+253
G f(e) = 393
211
97
f(b) = g(b)+ h(b)
H 101 f(b) = 75+374
I
f(b) = 449

21CS54, AI&ML - Bhaktavatsala Shivaram, Adjunct Faculty, CSE 24


Model 2: Informed Search Strategies – A* Search
A Greedy Search Example 1
*
A 75 f(n) = g(n)+ h(n)
118 B
S 140
[447] [449] f(g) = g(g)+ h(g)
e C f(g) = (140+80)+193
a E
111 [393] f(g) = 413
r 99
c D 80 f(f) = g(f)+ h(f)
[417] F
h f(f) = (140+99)+178
[413] G f(f) = 417
211
97

H 101
I

21CS54, AI&ML - Bhaktavatsala Shivaram, Adjunct Faculty, CSE 25


Model 2: Informed Search Strategies – A* Search
A Greedy Search Example 1
*
A 75 f(n) = g(n)+ h(n)
118 B
S 140
[447] [449] f(h) = g(h)+ h(h)
e C f(h) = (140+80+97)+98
a E
111 [393] f(h) = 415
r 99
c D 80
[417] F
h
[413] G
211
97

H 101
[415] I

21CS54, AI&ML - Bhaktavatsala Shivaram, Adjunct Faculty, CSE 26


Model 2: Informed Search Strategies – A* Search
A Greedy Search Example 1
*
A 75 f(n) = g(n)+ h(n)
118 B
S 140
[447] [449] f(i) = g(i)+ h(i)
e C f(i) = (140+80+97+101)+0
a E
111 [393] f(i) = 418
r 99
c D 80
[417] F
h
[413] G
211
97

H 101
[415] I Traverse Path = A -> E -> G-> H->I
[418]

21CS54, AI&ML - Bhaktavatsala Shivaram, Adjunct Faculty, CSE 27


Model 2: Informed Search Strategies – A* Search
A A* with f() not admissible when h(n) overestimates the cost to
*
reach the goal state
S
e
a
r
c
h

21CS54, AI&ML - Bhaktavatsala Shivaram, Adjunct Faculty, CSE 28


Model 2: Informed Search Strategies – A* Search
A Greedy Search Example 2
*
A 7 Straight Line Distance
11 D
S 14 Given (say):
e B 18 A -> G = 40
25
a C B -> G = 32
r 10 C -> G = 25
15
8
c
F
D -> G = 35
h E E -> G = 19
20 F -> G = 17
9
G -> G = 0
H 10 H -> G = 10
G Traverse Path = ?

21CS54, AI&ML - Bhaktavatsala Shivaram, Adjunct Faculty, CSE 29


Model 2: Informed Search Strategies – A* Search
A A* Search Example 3
*
S Node (n) ->H(n)
3 2
S A -> 12
e A B B -> 4
a 4 C -> 7
3 1
r 1 D -> 3
c C E -> 8 .
E F
h D 5 F -> 2
2 3
G -> 0
H I H -> 4
G I -> 9
Traverse Path = ?
S -> 13

21CS54, AI&ML - Bhaktavatsala Shivaram, Adjunct Faculty, CSE 30


Model 2: Informed Search Strategies – A*Search
A
Advantages: Disadvantages:
*
• Best Searching Algorithms • Doesn’t always produces
S
• Optimal and Complete shortest
e
a • Solving Complex Problems • Complexity issues
r • Requires memory
c
h

21CS54, AI&ML - Bhaktavatsala Shivaram, Adjunct Faculty, CSE 31


Model 2: Informed Search – Heuristic Function
B Heuristic Function
a • It takes the current state of the agent as its input and produces the estimation of
c how close agent is from the goal.
k • The heuristic method, however, might not always give the best solution, but it
g guaranteed to find a good solution in reasonable time.
r • Heuristic function estimates how close a state is to the goal.
o • It is represented by h(n), and it calculates the cost of an optimal path between the
u pair of states.
n • The value of the heuristic function is always positive.
d
Admissibility of the heuristic function is given as:
h(n) <= h*(n)
Where, h(n) is heuristic cost, and h*(n) is the estimated cost.

Heuristic cost should be less than or equal to the estimated


cost.
21CS54, AI&ML - Bhaktavatsala Shivaram, Adjunct Faculty, CSE 32
Model 2: Heuristic Search and Heuristic Function
B
a
Heuristic Search and Heuristic function are used
c in informed search
k
g
r • Heuristic search is a simple searching technique that
o tries to optimize a problem using Heuristic Function
u
n
d
• Optimization means
• Solve problem in minimum number of steps or
cost

21CS54, AI&ML - Bhaktavatsala Shivaram, Adjunct Faculty, CSE 33


Model 2: Heuristic Function
B
a
Heuristic function
c ▪ Function H(n) that gives an estimation on the cost of getting
k from node ‘n’ to the goal state.
g ▪ It helps in selecting optimal node for expansion
r [175]
o
u R1
35 Km 175 Km
n
d

City A City B
(Start) (Goal)

20 Km 215 Km
R2

[215]
21CS54, AI&ML - Bhaktavatsala Shivaram, Adjunct Faculty, CSE 34
Model 2: Heuristic Function
B
a
Types of Heuristic
c
k
Admissible Non-Admissible
g • Never overestimates the • Overestimates
r cost of reaching the goal • h(n) is always greater than
o
u
• H(n) is always less than or the actual cost of lowest
n equal to actual cost of cost path from node n to
d lowest cost path from goal
node n to goal

21CS54, AI&ML - Bhaktavatsala Shivaram, Adjunct Faculty, CSE 35


Module 2
Informed Search Strategies
• Greedy best-first search
• A* search
• Heuristic Function

Machine Learning
• Introduction
• Understanding Data

21CS54, AI&ML - Bhaktavatsala Shivaram, Adjunct Faculty, CSE 36

You might also like