Lecture 5a Heuristic Functions

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 31

Heuristic Functions

How to evaluate them?


How to find them?
Admissibility
Examples
search

Heuristic function
 A function which applies an algorithm to
the nodes and assigns a value to them that
indicates how close the evaluated node is
to the goal node.
 Difficulty however is in determining the
heuristics
 Given a heuristic guidance is still required
as to how to use the heuristic values
2
search

Finding Heuristics
Tic - Tac - Toe
which one to choose? X

X
X
Heuristic:
calculate winning lines and move to state with most wining lines.
Note that here we have a maximizing heuristic, maximum is
chosen, otherwise minimum is chosen for minimizing heuristic
3
search

Maximum value is chosen

Maximizing Minimizing

Minimum value is chosen


Heuristic Heuristic
(choosing a (distance from
move that the goal)
increases wining
chances)

4
search

Calculating winning lines

X X
X

3 winning 4 winning 3 winning


lines lines lines

Always choose the state with maximum heuristic value


Maximizing heuristics

5
search

Example TSP

Choosing city with minimum distance from the goal:


Always choose the city with minimum heuristic value
Minimizing heuristics (hi<hi-1)
6
search

8 Queen Problem

 8-queens problem (complete-state


formulation).
 Successor function: move a single queen to
another square in the same column.
7
search

Min-conflicts Example 1

h=5 h=3 h=1

min-conflicts heuristic function h(n) the

Count the number of queens attacking a


given queen in a row or a column.
8
Min-conflicts Example 2 (Grid
search

Score)

 A two-step solution for an 8-queens problem using min-conflicts


heuristic

 At each stage a queen is chosen for reassignment in its column

 The algorithm moves the queen to the min-conflict square breaking


ties randomly. 9
search

8 Puzzle Problem: Heuristic functions

 8-puzzle
 Avg. solution cost is about 22 steps (branching factor
+/- 3)
 Exhaustive search to depth 22: 3.1 x 1010 states.
 A good heuristic function can reduce the search process.
 CAN YOU THINK OF A HEURISTIC ?

10
search

8 Puzzle

 Two commonly used heuristics


 h1 = the number of misplaced tiles
 h1(s)=8
 h2 = the sum of the distances of the tiles from
their goal positions (manhattan distance).
 h2(s)=3+1+2+2+2+3+3+2=18

11
search

Heuristic quality

 Effective branching factor b*


 Is the branching factor that a uniform tree of depth
d would have in order to contain N+1 nodes.

N  1  1 b * (b*) 2  ... (b*) d


 A good heuristic should have b* as low as possible
 This measure is fairly constant for sufficiently hard
problems.
  Can thus provide a good guide to the heuristic’s overall
usefulness.
 A good value of b* is 1.

12
search

Heuristic quality and dominance

h1 h2 h1 h2

 Effective branching factor of h2 is lower than h1


 If h2(n) >= h1(n) for all n then h2 dominates h1 and is
better for search (value of h2 and h1 e.g. 18 vs 8)
13
search

Heuristic Function: Costs


 Applying heuristic function reduces the
branching factor, thus reduces the
computational cost
 The cost of applying the heuristic is
much less than the cost of evaluating
the complete search tree

14
search

Admissibility: 8 Puzzle

 Two commonly used heuristics


 h1 = the number of misplaced tiles h1(s)=8
 h2 = the sum of the distances of the tiles from
their goal positions (manhattan distance).
 h2(s)=3+1+2+2+2+3+3+2=18
• What is the actual number of moves required to solve this problem
• For tile ‘1’ alone almost 14 moves are required
• This is higher than total value for h1 or h2 (for all tiles)
• So h1 and h2 under estimate the actual cost. 15
search

Admissible heuristics
 A heuristic h(n) is admissible if for every
node n, h(n) ≤ h*(n), where h*(n) is the
true cost to reach the goal state from n.

 An admissible heuristic never


overestimates the cost to reach the goal,
i.e., it is optimistic

16
search

Why not Overestimate ?

May take the search


away from goal

17
search

Which Heuristic is Admissible?

 h1 = the number of misplaced tiles


 h1(s)=8
 h2 = the sum of the distances of the tiles from their
goal positions (manhattan distance).
 h (s)=3+1+2+2+2+3+3+2=18
2

 BOTH
18
search

Manhattan Distance: is it Admissible

3 1 1 3

Manhattan distance is 2+2=4 moves


19
search

Manhattan Distance: is it Admissible

3 1 1 3

Manhattan distance is 2+2=4 moves


20
search

Manhattan Distance: is it Admissible

3 1 3
1

Manhattan distance is 2+2=4 moves


21
search

Manhattan Distance: is it Admissible

3 1 3
1

Manhattan distance is 2+2=4 moves


22
search

Manhattan Distance: is it Admissible

3 1 3
1

Manhattan distance is 2+2=4 moves


23
search

Manhattan Distance: is it Admissible

1 3 1 3

Manhattan distance is 2+2=4 moves


24
search

Manhattan Distance: is it Admissible

1 3 1 3

Manhattan distance is 2+2=4 moves,


But actually 2 additional moves are required. 25
search

B
Consider the state-space on the
1 5
Figure.
S G

Provide an analysis of whether the 2 4


following heuristic functions are A
admissible or not for this state-
space. 3

S A B G
h1: 0 0 0 0
h2: 2 3 5 0
h3: 3 4 3 0
h4: 4 0 0 0

ANS: h* (S) = 3, h*(A) = 4, h*(B) = 5, h*(G) = 0.


Admissible = {h1, h2, h3}
26
search

Finding admissible heuristics


 Relaxed Problem:
 Admissible heuristics can be derived from the exact
solution cost of a relaxed version of the problem:
 Relaxed 8-puzzle for h1 : a tile can move anywhere As a
result, h1(n) gives the shortest solution
 Relaxed 8-puzzle for h2 : a tile can move to any
adjacent square.
 As a result, h2(n) gives the shortest solution.

The optimal solution cost of a relaxed problem


is no greater than the optimal solution cost of
the real problem.
27
search

Example: Relaxed Problem

5 8 1 2 3

4 2 1 x x x

7 3 6 x x

STATE(N) GOAL STATE

Goal contains partial information, the sequence in the


first row is important.
28
search
Relaxed Problem: Example

5 8 1 2 3
4 2 1 4 5 6 8

7 3 6 7 8

5 6
6

h = h8 + h5 + h6 +…
29
search
Complex: Relaxed Problem
 Consider two more complex relaxed problems:
5 8 1 2 3

4 2 1 4 5 6

7 3 6 7 8

1 2 3 5 8

4 2 1 4 5 6

3 7 6 7 8

 h = d1234 + d5678 [disjoint pattern heuristic]


 These distances could have been pre-computed in a
database 30
search

Summary: Admissible Heuristic

 Defining and Solving Sub-problem:


 Admissible heuristics can also be derived from the
solution cost of a sub-problem of a given problem.
 This cost is a lower bound on the cost of the real
problem.
 Pattern databases store the exact solution for every
possible sub-problem instance.
 The complete heuristic is constructed using the
patterns in the DB
 Learning through experience
31

You might also like