Download as pdf or txt
Download as pdf or txt
You are on page 1of 79

Artificial Intelligence

Informed Search
Outline

 Best-first search
 Greedy best-first search
 A* search
 Heuristics
 Memory Bounded A* Search
SEARCH TECHNIQUES

Search techniques

Blind Heuristic

Breadth Hill
Depth first A*
first climbing
Search search
Search Search
( DFS )
( BFS )
Best-First Greedy
Search Search
Informed Search
 A search strategy which searches the most promising branches of the
state-space first can:
 find a solution more quickly,
 find solutions even when there is limited time available,
 often find a better solution, since more profitable parts of the state-
space can be examined, while ignoring the unprofitable parts.
Search Heuristics
 A heuristic is:
 A function that estimates how close a state is to a goal
 Designed for a particular search problem
 Examples: Manhattan distance, Euclidean distance for pathing
HEURISTIC SEARCH ALGORITHM

Heuristic : we use heuristic function or knowledge in order to explore


the most promising state first.
• It is a Problem solving method that used shortcuts/calculated
guess to provide good enough solution.
• Reduce time complexity to reach solution
• This may lead to sub-optimal solution or fail to find any
solution.
(i.e., heuristics do not guarantee best solution or even a solution)

Example: in route planning the estimate of the cost of the cheapest path might be the straight line
distance between two cities
Example Heuristics
Straight-line distance Manhattan Distance
 The distance between two locations on a  The smallest number of
map can be known without knowing how vertical and horizontal moves
they are linked by roads (i.e. the absolute needed to get to the goal
path to the goal). (ignoring obstacles).

E A B C Manhattan
D Distance
Problem S E F A=4
Space E=2
C G H X
A
S =3
B 2= E A =4
D 1= H B =3
Search
E C 2= G C =2
Tree
B F =1
A X
EXAMPLE
Heuristic function for 8-tile puzzle
1 . The number of states out of place.
[the state that has fewest tiles out of place is probably closer to the desired goal
and would be best to examine next]

2 . The summation distance between each tile and it’s correct


position in the goal state.
Start State
State = a Goal
F(a) = 4 State G(n) =0

State = b State =c State = d G(n) = 1


F(b) = 5 F(c) = 3 F(d) = 5

State = e State = f State = g G(n) = 2


F(e) = 4 F(f) = 4 F(g) = 5

State = h State = i State = j State = k


F(h) = 5 F(i) = 6 F(j) = 4 F(k) = 6 G(n) = 3

State = l
F(l) = 4 G(n) = 4

State = n
G(n) = 5
State = m
F(m) = 4 F(n) = 6

Goal
2 8 3 1 2 3
1 6 4 8 4
7 5 7 6 5
Open and closed as they appear after the 3rd iteration of heuristic search
BEST FIRST SEARCH
Definition
• Is another more informed heuristic algorithm.
• Best-first search in its most general form is a simple heuristic search
algorithm.
• “Heuristic” here refers to a general problem-solving rule or set of rules
that do not guarantee the best solution or even any solution, but serves
as a useful guide for problem-solving.
• Best-first search is a graph-based search algorithm, meaning that the
search space can be represented as a series of nodes connected by
paths.
BEST FIRST SEARCH (cont.)

How it works
• The name “best-first” refers to the method of exploring the node with the
best “score” first.
• An evaluation function is used to assign a score to each candidate node.
 The algorithm maintains two lists, one containing a list of candidates yet to
explore (OPEN), and one containing a list of already visited nodes (CLOSED).
States in OPEN are ordered according to some heuristic estimate of their
“closeness” to a goal. This ordered OPEN list is referred to as priority queue.`
How it works

• Since all unvisited successor nodes of every visited node are included in
the OPEN list, the algorithm is not restricted to only exploring successor
nodes of the most recently visited node. In other words, the algorithm
always chooses the best of all unvisited nodes that have been graphed,
rather than being restricted to only a small subset, such as immediate
neighbors. Other search strategies, such as depth-first and breadth-first,
have this restriction.

• The advantage of this strategy is that if the algorithm reaches a dead-end


node, it will continue to try other nodes.
BEST FIRST SEARCH (cont.)
Algorithm (con.)
The algorithm is represented here in pseudo-code:
1. Define a list, OPEN, consisting solely of a single node, the start node, s.
2. IF the list is empty, return failure.
3. Remove from the list the node n with the best score (the node where f is the minimum),
and move it to a list, CLOSED.
4. Expand node n.
5. IF any successor to n is the goal node, return success and the solution (by tracing the
path from the goal node to s).
6. FOR each successor node:
a) apply the evaluation function, f, to the node.
b) IF the node has not been in either list, add it to OPEN.
7. Looping structure by sending the algorithm back to the 2nd step.
GREEDY BEST FIRST SEARCH
 Greedy best-first search is a best first search but uses heuristic estimate h(n) rather than
cost function.
 Both follow the same process but Greedy uses heuristic function whereas best first uses
cost function.
NOTE: similar to Best first search
but uses heuristic values. S
 EXAMPLE:
4 1
 S: Initial state, G1,G2: goal.
A B
 Table shows the heuristic estimates:
2 3
1 2
node h(n) node h(n) node h(n)
A 11 D 8 H 7 C D E F
B 5 E 4 I 3 4 3 2 1
C 9 F 2
H G1 I G2
GREEDY BEST FIRST SEARCH (cont.)
node h(n) node h(n) node h(n) S
A 11 D 8 H 7
B 5 E 4 I 3
C 9 F 2 A B
open=[S]; closed=[ ] Search Path h(n)=11 h(n)=5
B
open=[B5 , A11]; closed=[S]
open=[F2 , E4, A11 ]; closed=[S , B] S
open=[G20 , I3 , E4 , A11]; closed=[S , B , F]
open=[I3 , E4 , A11]; closed=[S , B , F , G2] 1 E F
Solution: B h(n)=4 h(n)=2
S , B , F , G2 F
Cost = 1+3+1=5 3
F
1 I G2
 Obtain best solution than best-first.
 But not guaranteed the optimum solution G h(n)=3 h(n)=0
2
Another example
10
D
2

A 8 1
E
K
6
13 0
B F
L
S
14
5
5 G
I 1
C
M
H

7
J

OPEN={ S} 6
CLOSED={}
10
D
2

A 8 1
E
K
6
13 0
B F
L
S
14
5
5 G
I 1
C
M
H

7
J

OPEN={ A(2),C(5),B(6)} 6
CLOSED={S}
10
D
2

A 8 1
E
K
6
13 0
B F
L
S
14
5
5 G
I 1
C
M
H

7
J

OPEN={ C(5), B(6), E(8), D(10)} 6


CLOSED={S,A}
10
D
2

A 8 1
E
K
6
13 0
B F
L
S
14
5
5 G
I 1
C
M
H

7
J

OPEN={ B(6), H(7),E(8), D(10)} 6


CLOSED={S,A, C}
10
D
2

A 8 1
E
K
6
13 0
B F
L
S
14
5
5 G
I 1
C
M
H

7
J

OPEN={H(7),E(8), D(10), F(13),G(14)} 6


CLOSED={S,A, C, B}
10
D
2

A 8 1
E
K
6
13 0
B F
L
S
14
5
5 G
I 1
C
M
H

7
J

OPEN={I(5), J(6),H(7),E(8), D(10), F(13),G(14)} 6


CLOSED={S,A, C, B,H}
10
D
2

A 8 1
E
K
6
13 0
B F
L
S
14
5
5 G
I 1
C
M
H

7
J

OPEN={L(0),K(1),M(1),I(5), J(6),H(7),E(8), D(10), F(13),G(14)}


6
CLOSED={S,A, C, B,H,I}
10
D
2

A 8 1
E
K
6
13 0
B F
L
S
14
5
5 G
I 1
C
M
H

7
J

OPEN={K(1),M(1),I(5), J(6),H(7),E(8), D(10), F(13),G(14)} 6


CLOSED={S,A, C, B,H,I,L}
10
D
2

A 8 1
E
K
6
13 0
B F
L
S
14
5
5 G
I 1
C
M
H

7
J

OPEN={K(1),M(1),I(5), J(6),H(7),E(8), D(10), F(13),G(14)} 6


CLOSED={S,A, C, B,H,I,L} GOAL NODE FOUND!!
Greedy best-first search example
Expand the node that has the lowest value of the heuristic function h(n)
Greedy best-first search example
Greedy best-first search example
Greedy best-first search
example
Greedy best-first search
example
Greedy best-first search

Arad  Sibiu  Fagaras  Bucharest


A* Algorithm
• A* algorithm is similar to Best first Search.
• Only difference: Best First Search takes h(n) as evaluation
function/heuristic value.
• In Best first search h(n)= estimated cost of current node ’n’ from the goal
node.
• A* takes the evaluation function as:
F(n)= g(n) + h(n)

• where, Actual cost start


state Node to n
Estimation cost
n to goal Node

• g(n)= cost(or distance) of the current node from start state.


• h(n)= estimated cost of current node from goal node.
A* ALGORITHM
 If we guarantee that there is no overestimate for h(n), we will
guarantee that f(n) = h(n) + g(n) give us the shortest path,
 If h(n) <= h*(n) for all n.
 If h(n) <= h*(n) then h(n) is called Admissible heuristic

 where : h(n) is the heuristic value .


h*(n) is the actual value .

 This algorithm called A* algorithm .


 A* Algorithm  f(n) = h(n) + g(n)
EXAMPLE on A* ALGORITHM (cont.)
EXAMPLE: Use A* search algorithm to find the solution.
Initial state: S, Goal state: G1 or G2
node h(n) node h(n) node h(n) S
A 11 D 8 H 7 4 1
B 5 E 4 I 3
C 9 F 2 A g(A)=4 B g(B)=1
2 3
Solution: 1 2
g(F)=3
g(E)=2
f(A)=h(A)+g(A)=11+4=15 C D E F
f(B)=h(B)+g(B)=5+1=6 g(C)=1 g(D)=2 2
4 3 1
f(E)=h(E)+g(E)=4+1+2=7
f(F)=h(F)+g(F)=2+1+3=6 g(H)=4 H G
1
I G
2

f(I)=h(I)+g(I)=3+1+3+2=9 g(G1)=3 g(I)=2 g(G2)=1


f(G2)=h(G2)+g(G2)=0+1+3+1=5
S , B , F , G2
The start state, first moves, and goal state for an example-8 puzzle.
Three heuristics applied to states in the 8-puzzle.
The heuristic f applied to states in the 8-puzzle.
State space generated in heuristic search of the 8-puzzle graph.
Start State
State = a Goal
F(a) = 4 State G(n) =0

State = b State =c State = d G(n) = 1


F(b) = 6 F(c) =4 F(d) = 6

State = e State = f State = g G(n) = 2


F(e) = 5 F(f) = 5 F(g) = 6

State = h State = i State = j State = k


F(h) = 6 F(i) = 7 F(j) = 5 F(k) = 7 G(n) = 3

State = l
F(l) = 5 G(n) = 4

State = n
G(n) = 5
State = m
F(m) = 5 F(n) = 7

Goal
10
4 D
2

A 8
2 18
E

6 6 K
13 1
3 0
B F
2 1 L
S 8
1 2 8
2 14
5
5 G 2
5
I 1
C
7 M
4 1
H

7 1 J

6
10
4 D
2

A 8
2 18
E 2

5 6 K
13 1
3 0
B F
2 1 L
S 8
1 2 8
2 14
5
5 G 2
5
I 1
C
7 M
4 1
H

7 1 J
OPEN={S}
CLOSED={}
6
10
4 D
2

A 8
2 18
E 2

5 6 K
13 1
3 0
B F
2 1 L
S 8
1 2 8
2 14
5
5 G 2
5
I 1
C
7 M
4 1
H

7 1 J

6
OPEN={A(5+2=7}, B(6+2=8), C(5+5=10)}
CLOSED={S}
10
4 D
2

A 8
2 18
E 2

5 6 K
13 1
3 0
B F
2 1 L
S 8
1 2 8
2 14
5
5 G 2
5
I 1
C
7 M
4 1
H

7 1 J

6
OPEN={B(8), C(10),E(7+8=15), D(9+10=19)}
CLOSED={S,A}
10
4 D
2

A 8
2 18
E 2

5 6 K
13 1
3 0
B F
2 1 L
S 8
1 2 8
2 14
5
5 G 2
5
I 1
C
7 M
4 1
H

7 1 J

6
OPEN={C(10),E(15), F(5+13=18),G(4+14=18), D(19)}
CLOSED={S,A,B}
10
4 D
2

A 8
2 18
E 2

5 6 K
13 1
3 0
B F
2 1 L
S 8
1 2 8
2 14
5
5 G 2
5
I 1
C
7 M
4 1
H

7 1 J

6
OPEN={E(15), F(18),G(18), D(19),H(5+7+7=19)}
CLOSED={S,A,B,C}
10
4 D
2

A 8
2 18
E 2

5 6 K
13 1
3 0
B F
2 1 L
S 8
1 2 8
2 14
5
5 G 2
5
I 1
C
7 M
4 1
H

7 1 J

6
OPEN={K(5+2+2+1=10), F(18),G(18), D(19),H(19)}
CLOSED={S,A,B,C,E}
10
4 D
2

A 8
2 18
E 2

5 6 K
13 1
3 0
B F
2 1 L
S 8
1 2 8
2 14
5 G 2
5 5
I 1
C
7 M
4 1
H

7 1 J

6
OPEN={ F(18),G(18), D(19),H(19), I(21+5=26)}
CLOSED={S,A,B,C,E,K}
10
4 D
2

A 8
2 18
E 2

5 6 K
13 1
3 0
B F
2 1 L
S 8
1 2 8
2 14
5 G 2
5 5
I 1
C
7 M
4 1
H

7 1 J

6
OPEN={ I(2+3+8+5=18),(F(18),G(18), D(19),H(19), I(26)}
CLOSED={S,A,B,C,E,K,F}
10
4 D
2

A 8
2 18
E 2

5 6 K
13 1
3 0
B F
2 1 L
S 8
1 2 8
2 14
5 G 2
5 5
I 1
C
7 M
4 1
H

7 1 J

6
OPEN={ M(2+3+8+1+1=15),F(18),G(18), D(19),H(19),L(21+0=21) ,I(26)}
CLOSED={S,A,B,C,E,K,F,I}
10
4 D
2

A 8
2 18
E 2

5 6 K
13 1
3 0
B F
2 1 L
S 8
1 2 8
2 14
5 G 2
5 5
I 1
C
7 M
4 1
H

7 1 J

6
OPEN={ L(2+3+8+1+2+0=16), F(18),G(18), D(19),H(19),L(21) ,I(26)}
CLOSED={S,A,B,C,E,K,F,I,M}
10
4 D
2

A 8
2 18
E 2

5 6 K
13 1
3 0
B F
2 1 L
S 8
1 2 8
2 14
5 G 2
5 5
I 1
C
7 M
4 1
H

7 1 J

6
OPEN={F(18),G(18), D(19),H(19),L(21) ,I(26)} GOAL FOUND!!
CLOSED={S,A,B,C,E,K,F,I,M,L}
Romania with step costs in km, Shortest Path Example
A* Shortest Path Example

56
A* Shortest Path Example

57
A* Shortest Path Example
A* Shortest Path Example
A* Shortest Path Example
A* Shortest Path Example
A* search example
A * search example
A * search example tracing
A* search
 Properties of A*
 Complete?? Yes, unless there are infinitely many
nodes with f  f(G)
 Time?? Exponential in
[relative error in h x length of solution.]
 Space?? Keeps all nodes in memory
 Optimal?? Yes – cannot expand f i+1 until fi is
finished
A* expands all nodes with f(n) < C*
A* expands some nodes with f(n) = C*
A* expands no nodes with f(n) > C*
A*: Summary
 A* uses both backward costs and (estimates of) forward costs

 A* is optimal with admissible / consistent heuristics

 Heuristic design is key: often use relaxed problems


Greedy best-first search example Home work
Expand the node that has the lowest value of the heuristic function h(n)
Greedy best-first search example
Greedy best-first search example
Greedy best-first search example
Properties of greedy best-first search
 Complete?
No – can get stuck in loops

start
goal
Properties of greedy best-first search
 Complete?
No – can get stuck in loops
 Optimal?
No
Properties of greedy best-first search
 Complete?
No – can get stuck in loops
 Optimal?
No
 Time?
Worst case: O(bm)
Best case: O(bd) – If h(n) is 100% accurate
 Space?
Worst case: O(bm)
HILL-CLIMBING SEARCH

- Expand the current state in the search and evaluate it’s children .

- The best child is selected for further expansion .

- neither it sibling nor its parent are retained .

- Search halts when it reaches a state that is better than any of its children .
(i.e. The process ends when all operators have been applied and none of the resulting states
are better than the current state)
- Ve :
1 . Local maximum .
2 . Flat area .
3 . Cant backtrack. (WHY?)
+ Ve :
Low memory requirement .
EXAMPLE on HILL-CLIMBING

SEARCH PATH = [S0, B1, E2, G13] Cost = 1+2+3=6


S → B → E → G1
S NOTE: The values on arcs are
4 1 evaluation function values or cost
function values (NOT heuristic
A B estimate values).
2 3
1 2
NOTE: in Hill climbing NO revising or
C D E F backtracking. This is the only
4 3 2 1 difference b/w Hill climbing and Best
first search.
G G
H 1
I 2
HILL-CLIMBING SEARCH (cont.)

 Hill climbing is an optimization technique which belongs to the


family of local search.
 It is best used in problems with “the property that the state
description itself contains all the information needed for a solution”
 The algorithm is memory efficient since it does not maintain a
search tree: It looks only at the current state and immediate future
states.
 Hill climbing attempts to iteratively improve the current state by
means of an evaluation function.
HILL-CLIMBING SEARCH (cont.)

For example, hill climbing can be applied to the travelling salesman problem. It is easy to
find an initial solution that visits all the cities but will be very poor compared to the
optimal solution.

The algorithm starts with such a solution and makes small improvements to it, such as
switching the order in which two cities are visited.

Eventually, a much shorter route is likely to be obtained.


TSP Example
A, B, C and D are cities. Visit all cities with shortest path.

A 6 B
1 2
5 3

D 4 C
HILL-CLIMBING SEARCH (cont.)
 Hill Climbing can get stuck at local maxima. Consider the following tree. a is an
initial state and h and k are final states. The numbers near the states are the
heuristic values.

 When hill climbing is run on the tree, we get a -> f -> g and here we get stuck at
local maxima g. Hill climbing can't go back and make a new choice (for example j or
e) because it keeps no history. So how to avoid this stuck in order to get global
maxima.
Comparison of search strategies
Space
Algorithm Complete? Optimal? Time complexity
complexity
Yes If all step O(bd) O(bd)
BFS
costs are equal

UCS Yes Yes Number of nodes with g(n) ≤


C*

DFS No No O(bm) O(bm)

If all step
IDS Yes O(bd) O(bd)
costs are equal

Worst case: O(bm)


Greedy No No
Best case: O(bd)

A* Yes Yes Number of nodes with g(n)+h(n) ≤


C*

You might also like