Learning Center
Plans & pricing Sign in
Sign Out

Heuristic Search


 Introduction to AI

Instructor: Ho Sooi Hock

                 Heuristic Searches
         Heuristic Search

• Has some domain knowledge beyond
  the problem definition
• Usually more efficient than blind
• Sometimes known as informed search
• Heuristic search works by deciding
  which is the next best node to expand
  (there is no guarantee that it is the best
          Best-First Search
• Node selected for expansion based on an
  evaluation function ( f(n) )
  – greedy search uses the cost estimate from
    the current position to the goal, known as
    heuristic function ( h(n) )
  – A* search uses a combination of the actual
    cost to reach the current node and the
    estimated cost to proceed from the current
    node to the goal ( g(n) + h(n) )
• Compare this with uniform cost search
  which chooses the lowest cost node thus
  far ( g(n) )
        Heuristic Search - Example


Heuristic Search - Example

   City       SLD      Town       SLD
    Arad      366      Mehadai    241
  Bucharest    0       Neamt      234
   Craiova    160      Oradea     380
   Dobreta    242       Pitesti   100
   Eforie     161      Rimnicu    193
   Fagaras    176       Sibiu     253
   Giurgiu    77      Timisoara   329
   Hirsova    151      Urziceni   80
    Iasi      226       Vaslui    199
   Lugoj      244      Zerind     374
SLD: Straight line distance (as birds fly)
     between a given city and Bucharest
              Greedy Search
The 1st node to be selected from the generated
nodes is Sibiu because it is closer to Bucharest
than either Zerind or Timisoara.

     Zerind     h=374       Oradeo

          h=366               Fagaras
              Sibiu               h=176
         Timisoara         Rimnicu
                           Vilcea         Bucharest
          h=329                           h=0
            Greedy Search
Notice that : It finds solution without ever
              expanding a node that is NOT on the
              solution path.

             The solution path is not optimal

 Arad Sibiu Rimnicu Vilcea Pitesti Bucharest
 with path cost of (140+80+97+101 = 418) is
 32km LESS than
 Arad Sibiu Fagaras Bucharest
 (path cost = 140+99+211 = 450)
          Greedy Search
• It is only concerned with short term
• It is possible to get stuck in an infinite
  loop (consider being in Iasi and trying
  to get to Fagaras) unless mechanism
  for avoiding repeated states is in place
• It is not optimal
• It is not complete
   Time and space complexity is O(Bm); where
         m is the depth of the search tree
Uniform cost search (UCS) using g(n) is OPTIMAL

UCS is a special case of breath-first search, therefore

Greedy search behaves like depth-first search as it
prefers to follow a single path [guided by h(n)] all
the way to the goal. It is thus EFFICIENT but it is
NOT optimal NOR complete

The A* (A Star) search combines the above two
strategies to get both advantages, using the
evaluation function f(n) = g(n) + h(n)
              A* Search
• Combines the cost so far and the
  estimated cost to the goal, i.e. f(n) =
  g(n) + h(n). This gives an estimated cost
  of the cheapest solution through n
• It can be proved to be optimal and
  complete providing that the heuristic
  function is admissible, i.e. h(n) never
  overestimate the cost to reach the goal
  A* Search - Admissibility

Is the heuristic SLD ADMISSIBLE?
 Serdang                           KL

         Serdang-KL Kommuter - 28 km

         Serdang-KL highway - 35 km

         Straight Line Distance as the birds
         fly from Serdang to KL - 18 km
A* Search - Example
A* Search - Example

   Oradea         f = 291+380
Zerind              = 671
         f = 75+374
           = 449
                                                    f = 239+176
                  Sibiu                               = 415
  Arad                                                               Bucharest
                  f = 140+253      Rimnicu
   f = 0+366        = 393                                               f = 450+0
     = 366                                 f = 220+193                    = 450
                                             = 413
Timiosara             Craiova                                         Bucharest
                 f = 366+160                         f = 317+100   f = 418+0
   f = 118+329     = 526                               = 417         = 418
     = 447
                                            f = 455+160
                                              = 615
      General Implementation
• Implementation is achieved by sorting the
  nodes based on the evaluation function,
 Function BEST-FIRST-SEARCH(problem, EVAL-FN)
   returns a solution sequence
   Inputs : problem, a problem
           Eval-Fn, an evaluation function
   Queueing-Fn <= a function that orders nodes by

   Return GENERAL-SEARCH(problem, Queueing-Fn)
       Specific Implementation

function GREEDY-SEARCH(problem) returns a
  solution or failure
     return BEST-FIRST-SEARCH(problem, h)

function A*-SEARCH (problem) returns a solution or
     return BEST-FIRST-SEARCH(problem, g+h)
Theorem: If h(n) is admissible,
A* using TREE-SEARCH is

Suppose a suboptimal goal node, G2 has been
generated and is in the fringe. Let n be an unexpanded
node in the fringe such that n is on a shortest path to
an optimal goal G.
     f(G) = g(G)+h(G) = g(G) < g(G2) < g(G2)+h(G2) = f(G2)
If h(n) is admissible, then
     f(n) = g(n)+h(n) ≤ g(G) = g(G)+h(G) = f(G)

f(n) ≤ f(G) < f(G2) and so A* will never select G2 for
expansion. Hence A* always return an optimal solution.
  Monotonicity (Consistency)
Theorem: If h(n) is consistent, A* using
GRAPH-SEARCH is optimal
A heuristic h(n) is consistent if
              h(n) ≤ c(n, a, n’) + h(n’)
If h(n) is consistent, then the values of f(n) along
any path are non-decreasing.
              g(n’) = g(n) + c(n, a, n’)
    f(n’) = g(n’)+h(n’) = g(n)+c(n, a, n’) + h(n’)
                 ≥ g(n)+h(n) = f(n)

Hence the first goal node selected for expansion
must be an optimal solution.
   Monotonicity and Admissibility
Any monotonic heuristics is also admissible
This argument considers any path in the search space as a
sequence of states s1, s2,……sg, where s1 is that start state and
sg is the goal. For a sequence of moves in this arbitrarily
selected path, monotonicity dictates that:

           s1 to s2        h(s1) – h(s2) ≤ c(s1, a, s2)
           s2 to s3        h(s2) – h(s3) ≤ c(s2, a, s3)
           s3 to s4        h(s3) – h(s4) ≤ c(s3, a, s4)
            sg-1 to sg    h(sg-1) – h(sg) ≤ c(sg-1, sg)

Summing each column and using the monotone property of
h(sg) = 0
                  Path s1 to sg   h(s1) ≤ g(sg)
       Optimality of A* Search

 A* expands nodes in order of increasing f value
 Gradually adds "f-contours" of nodes
 Contour i has all nodes with f = fi, where fi < fi+1

Heuristic Searches - Example

       Initial State   Goal State

       5 4             1 2 3
       6 1 8           8   4
       7 3 2           7 6 5
         Heuristic Searches
           A* Algorithm
Typical solution is about twenty steps
Branching factor is approximately three.
Therefore a complete search would need to
search 320 states. But by keeping track of
repeated states we would only need to search 9!
(362,880) states
But even this is a lot (imagine having all these in
Our aim is to develop a heuristic that does not
overestimate (it is admissible) so that we can
use A* to find the optimal solution
           A* Algorithm
        Possible Heuristics

h1 = the number of tiles that are in the
wrong position (=7)
h2 = the sum of the distances of the tiles
from their goal positions using the
Manhattan Distance (=18)
  Both are admissible but which one is
For two A* heuristics h1 and h2, if h1(n) <=
h2(n), for all states n in the search space,
we say h2 dominates h1 or heuristic h2 is
more informed than h1.
Domination translate to efficiency: A* using
h2 will never expand more nodes than A*
using h1.
Hence it is always better to use a heuristic
function with higher values, provided it does
not over-estimate and that the computation
time for the heuristic is not too large
    Generating Heuristics with
       Relaxed Problems
• A problem with fewer restrictions on the
  actions is called a relaxed problem
• The cost of an optimal solution to a
  relaxed problem is an admissible
  heuristic for the original problem
• If the rules of the 8-puzzle are relaxed
  so that a tile can move anywhere, then
  h1(n) gives the shortest solution
• If the rules are relaxed so that a tile
  can move to any adjacent square, then
  h2(n) gives the shortest solution
     Test From 100 Runs with
     Varying Solution Depths
               Depth    IDS      A*(h1) A*(h2)
                    2       10        6      6
                    4      112       13     12
                    6      680       20     18
                    8     6384       39     25
                   10    47127       93     39
                   12 364404        227     73
                   14 3473941       539   113
                   16              1301   211
                   18              3056   363
                   20              7276   676
                   22             18094  1219
                   24             39135  1641

h2 looks better as fewer nodes are expanded. But why?
      Effective Branching Factor
                       Search                  EBF
     Depth    IDS      A*(h1) A*(h2) IDS      A*(h1) A*(h2)
          2       10        6      6 2.45       1.79   1.79
          4      112       13     12 2.87       1.48   1.45
          6      680       20     18 2.73       1.34   1.30
          8     6384       39     25 2.80       1.33   1.24
         10    47127       93     39 2.79       1.38   1.22
         12  364404       227     73 2.78       1.42   1.24
         14 3473941       539   113 2.83        1.44   1.23

 Effective branching factor: average number of branches expanded
 h2 has a lower branching factor and so fewer nodes are expanded
 Therefore, one way to measure the quality of a heuristic is to find
  its average branching factor
 h2 has a lower EBF and is therefore the better heuristic
Summary of Heuristic Search

 • Heuristic search
   • Characteristics
   • h(n), g(n)
 • Heuristics
   • Best-first-search
      • Greedy-search
      • A*
 • Read chapter 4 in AIMA book

 Most of the lecture slides are
adapted from the same module
   taught in Nottingham UK
  Professor Graham Kendall,
         Dr. Rong Qu
      Dr. Andrew Parker

To top