0% found this document useful (0 votes)
48 views67 pages

03 Heuristic Search

Uploaded by

czf1643605493
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
48 views67 pages

03 Heuristic Search

Uploaded by

czf1643605493
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 67

Heuristic search, A*

Kalev Kask

Read Beforehand: R&N 3.5-3.6

Based on slides by Profs. Dechter, Lathrop, Ihler


Outline
• Review limitations of uninformed search methods
• Heuristics
• A structured way to add “smarts” to your solution
• Provide *significant* speed-ups in practice

• Informed (or heuristic) search


• Best-first, A* (and if needed for memory limits, RBFS, SMA*)
• A* is optimal with admissible (tree)/consistent (graph) heuristics
• Branch & Bound DFS (preferred heuristic search algorithm)
• Still have worst-case exponential time complexity

• Techniques for generating heuristics

In AI, “NP-Complete” means “Formally interesting”


Limitations of uninformed search

• Search space size makes search tedious


– Combinatorial explosion
• Ex: 8-Puzzle
– Average solution cost is ~ 22 steps
– Branching factor ~ 3
– Exhaustive search to depth 22: 322 ≈ 3.1x1010 states
– 24-Puzzle: 1024 states (much worse!)
Recall: tree search

Arad

Sibiu Timisoara Zerind

Arad Fagaras Oradea Rimnicu… Arad Lugoj Arad Oradea

This “strategy” is what


differentiates different
function TREE-SEARCH (problem, strategy) : returns a solution or failure
initialize the search tree using the initial state of problem search algorithms
while (true):
if no candidates for expansion: return failure
choose a leaf node for expansion according to strategy
if the node contains a goal state: return the corresponding solution
else: expand the node and add the resulting nodes to the search tree
Heuristic function
• Heuristic
– a quick way to estimate how close we are to the solution
– a commonsense rule (“rule of thumb” = intuition) or rules intended to
increase the probability of solving some problem
– Same linguistic root as ancient Greek “Eureka” = “I have found it”
• “Eureka” = motto of the state of California
– Using “rules of thumb” (= guesstimates) to find answers
• Heuristic function h(n)
– Estimate of optimal (shortest) path cost from a (any) state to the goal
– Think of h(n) as (estimate of) “cost to go” or “distance to go” (from n to goal)
– Defined using only the state of node n
– h(n) = 0 if n is a goal node
– Example: straight line distance from n to Bucharest
• Not true state space distance, just estimate! Actual distance can be higher
• Provides problem-specific knowledge to the search algorithm
Heuristic function
• Idea: use a heuristic function h(n) for each node
– g(n) = known path cost so far to node n
– h(n) = estimate of optimal cost to goal from node (state) n
– f(n) = g(n)+h(n) = estimate of total cost to goal through n
– f(n) provides an estimate for the total cost

• “Best first” search implementation


– Order the nodes in frontier by an evaluation function
– Greedy Best-First: order by h(n)
– A* search: order by f(n)
• BnB DFS: prune by f(n) against Upper Bound

• Search efficiency depends on heuristic quality!


– The better your heuristic, the faster your search!
Example: 8-Puzzle
• 8-Puzzle
– Avg solution cost is about 22 steps
– Branching factor ~ 3
– Exhaustive search to depth 22 = 3.1 x 10^10 states
– A good heuristic function can reduce the search process
• Two commonly used heuristics
– h1: the number of misplaced tiles
h1(s) = 8
– h2: sum of the distances of the tiles from their goal
(“Manhattan distance”)
h2(s) = 3+1+2+2+2+3+3+2
= 18
Ex: Romania, straight-line distance
Straight-line dist to goal
Arad 366
Bucharest 0
Oradea Craiova 160
71
Neamt Drobeta 242
75 87 Eforie 161
Zerind 151
Iasi Fagaras 176
140 92
Arad Sibiu 99 Fagaras Giurgiu 77
Hirsova 151
118 Vaslui
80 Iasi 226
Timisoara Rimnicu Vilcea
Lugoj 244
97 211 142
111 Pitesti Mehadia 241
Lugoj
70 85 98 Neamt 234
Hirsova
Mehadia 146 101 Urziceni Oradea 380
75 138 86 Pitesti 100
120 Bucharest
Dobreta 90 Rimnicu Vilcea 193
Cralova Eforie Sibiu 253
Giurgiu
Timisoara 329
Urziceni 80
Vaslui 199
Zerind 374
How to implement straight-line
distance as a heuristic function h(n)?

• Heuristic function h(n) is called with node n.


• For example (your implementation may vary)
– Node n contains the city string name of its state
– We have a static hash table that maps city string
names to straight-line distances shown in the table
– h(n) uses the state city string name as a key into the
hash table to retrieve its straight-line distance
– That straight-line distance is the return value of h(n)
Relationship of search algorithms
• Notation:
– g(n) = known cost of the particular path to reach n
– h(n) = estimated optimal cost from n to goal
– h*(n) = true optimal cost from n to goal (unknown to agent)
– g*(n) = true optimal cost from initial state to state of n (may be unknown to agent)
– f(n) = g(n)+h(n) = estimated optimal total cost through n

• Uniform cost search: sort frontier by g(n)


• Greedy best-first search: sort frontier by h(n)
• A* search: sort frontier by f(n) = g(n) + h(n)
– Optimal for admissible / consistent heuristics
– Generally the preferred heuristic search framework
– Memory-efficient versions of A* are available: RBFS, SMA*
• Branch & Bound DFS
– Depth-First Search algorithm (linear memory of DFS!)
– We have Upper Bound on solution length (“solution cannot be more than this”)
• Cost of best known solution so far
– f(n) = g(n) + h(n)
– Prune n if f(n) ≥ Upper Bound
– Is complete, unlike (uninformed) DFS
Greedy Best-First Search
R&N 3.5.1
Greedy best-first search
(sometimes just called “best-first”)

• h(n) = estimate of cost from n to goal


– Ex: h(n) = straight line distance from n to Bucharest

• Greedy best-first search expands the node that


appears to be closest to goal
– Priority queue sort function = h(n)
Example: GBFS for Romania

Straight-line dist to goal


Arad 366
Bucharest 0
Craiova 160
Drobeta 242
Eforie 161
Fagaras 176
Oradea GBFS Giurgiu 77
71 Hirsova 151
path Neamt
87 Iasi 226
75 Zerind 151
Iasi Lugoj 244
140 92 Mehadia 241
Arad Sibiu 99 Fagaras
Neamt 234
118 Vaslui
Timisoara 80 Rimnicu Vilcea
Oradea 380
142 Pitesti 100
97 211
111 Lugoj Pitesti Rimnicu Vilcea 193
70 85 98 Sibiu 253
Hirsova
Mehadia 146 101 Urziceni Timisoara 329
75 138 86
120 Bucharest Urziceni 80
Dobreta 90 Vaslui 199
Cralova Eforie
Giurgiu Zerind 374
Example: GBFS for Romania

GBFS path: 450km


Straight-line dist to goal
Arad 366
Optimal path: 418 km Bucharest 0
Craiova 160
Drobeta 242
Eforie 161
Fagaras 176
Giurgiu 77
Hirsova 151
Iasi 226
Lugoj 244
Mehadia 241
Neamt 234
Oradea 380
Pitesti 100
Rimnicu Vilcea 193
Sibiu 253
Timisoara 329
Urziceni 80
Vaslui 199
Zerind 374
Properties of greedy best-first search
• Complete?
– Tree version can get stuck in loops (moderately easy to prevent)
– Graph version is complete in finite spaces
– Neither Tree nor Graph version is complete in infinite spaces
• Easy for a malicious demon to lead it astray with evil heuristic values
• Time? O(bm)
– A good heuristic can give dramatic improvement
• Space? O(bm)
– Keeps all nodes in memory
• Optimal? No
– Example:
Arad – Sibiu – Rimnicu Vilcea – Pitesti – Bucharest is shorter!
GBFS can be suboptimal even if h=h*
A*-search
Admissible & Consistent Heuristics
R&N 3.5.2
A* search
• Idea: avoid expanding paths that are already expensive
– Generally the preferred (simple) heuristic search
– Optimal if heuristic is:
• admissible (tree search) / consistent (graph search)
– Always terminates with a solution path (even if heuristic is not
admissible) if step costs   > 0 and branching factor is finite
• proof by Hart, Nilsson, and Raphael (1968)
• Evaluation function f(n) = g(n) + h(n)
– g(n) = cost so far to reach n
– h(n) = estimated cost from n to goal
– f(n) = g(n)+h(n) = estimated total cost of path through n to goal

• A* algorithm is identical to UCS except that the priority


queue sort function is f(n) = g(n) + h(n)
Admissible heuristics
• A heuristic h(n) is admissible if, for every node n,
h(n) ≤ h*(n)
h*(n) = 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 never pessimistic
– Example: straight-line distance never overestimates the actual
road distance

• Theorem:
if h(n) is admissible, A* using Tree-Search is optimal
Example: Admissible heuristics

• Two commonly used admissible heuristics


– h1: the number of misplaced tiles
h1(s) = 8
– h2: sum of the distances of the tiles from their goal
(“Manhattan distance”)
h2(s) = 3+1+2+2+2+3+3+2
= 18
Consistent heuristics
• A heuristic is consistent (or monotone) if for every node n, every
successor n' of n generated by any action a,

c(n,a,n') + h(n')  h(n)

• Thus, if h(n) is consistent, we have


f(n') = g(n') + h(n')
= g(n) + c(n,a,n') + h(n')
≥ g(n) + h(n)
= f(n)
i.e., f(n) is non-decreasing along any path. (Triangle inequality)

• Consistent  admissible (consistency is a stronger condition)

• Theorem: If h(n) is consistent, A* using Graph-Search is optimal


Consistent  Admissible

• Any Consistent heuristic is also Admissible.


– Let n be a node, n’ be a successor of n, and the
function h() be a consistent heuristic
– By definition of consistent, c(n,a,n’)  h(n) − h(n’)
– Basis step: let n’ be a goal node G, then h(n’) = 0
• Then h*(n) = C(n,a,G)  h(n) − 0 = h(n)
• Thus, h*(n)  h(n) so h() is admissible at n
– Induction step: let n’ be such that h*(n’)  h(n’)
• Then h(n)  c(n,a,n’) + h(n’)  c(n,a,n’) + h*(n’) = h*(n)
• Thus, h*(n)  h(n) so h() is admissible at n
– Consequently, h() is admissible at every node
Admissible 
/ Consistent
• Most Admissible heuristics are Consistent
– “one has to work quite hard to concoct heuristics that
are admissible but not consistent.” ⎯ R&N p. 95
• 8-puzzle: Admissible, but not Consistent, heuristic
– h1(n) = number of misplaced tiles
– h2(n) = sum of (Manhattan) distances of tiles to goal
– hex(n) = Choose_randomly( h1(n), h2(n) )
• hex(n) is admissible but not (necessarily) consistent
• hex(n) is (probably) not non-decreasing along all paths
• h1(n) and h2(n) are not necessarily related to each other
• Random combination may not satisfy triangle inequality

Example adapted from “Inconsistent Heuristics in Theory and Practice”


by Felner, Zahavi, Holte, Schaeffer, Sturtevant, & Zhang
Example: Admissible, but not Consistent, heuristic
SB:
g=2
h=5 SBA: SBAG:
S: g=3 g=7
g=0 f=7
h*=5 h=1 h=0
h=7 B f=4 f=7
f=7 c=2 h=5 c=1 h*=4 h*=0
h*=7
S A G
h=7 c=4 h=1 c=4 h=0
SA: SAG:
g=4 g=8
h=1 h=0
f=5 f=8
h*=4 h*=0

• h(n) is admissible because h(n)  h*(n) for all n


• h(n) is NOT consistent because paths SA and BA are not
monotone (i.e., f(.) is NOT non-decreasing along BA-AG and
SA-AG)
– c(n,a,n') + h(n')  h(n) ≡ c(n,a,n’)  h(n) - h(n’) = Δh
Optimality conditions
• A* Tree Search is optimal if heuristic is admissible
• A* Graph Search is optimal if heuristic is consistent
• Why two different conditions?
– In graph search you often find a long cheap path to a node after a short
expensive one, so you might have to update all of its descendants to use
the new cheaper path cost so far
– A consistent heuristic avoids this problem (it can’t happen)
– Consistent is slightly stronger than admissible
– Almost all admissible heuristics also are consistent
• Could we do optimal A* Graph Search with an
admissible heuristic?
– Yes, but we would have to do additional work to update descendants
when a cheaper path to a node is found
– A consistent heuristic avoids this problem
Example: A* Tree Search for Romania
Red triangle:
Node to expand next

Straight-line dist to goal


Oradea Arad 366
71 Neamt Bucharest 0
87
75 Zerind 151 Craiova 160
Iasi
140 Drobeta 242
Arad Sibiu 99 92
Fagaras Fagaras 176
118 Vaslui Lugoj 244
Timisoara 80 Rimnicu Vilcea Mehadia 241
97 211 142 Oradea 380
111 Lugoj Pitesti Pitesti 100
70 85 98
Hirsova Rimnicu Vilcea 193
Mehadia 146 101 Urziceni
75 86 Sibiu 253
120 138 Bucharest Timisoara 329
Dobreta 90
Cralova Eforie Zerind 374
Giurgiu
Example: A* Tree Search for Romania

Straight-line dist to goal


Oradea Arad 366
71 Neamt Bucharest 0
87
75 Zerind 151 Craiova 160
Iasi
140 Drobeta 242
Arad Sibiu 99 92
Fagaras Fagaras 176
118 Vaslui Lugoj 244
Timisoara 80 Rimnicu Vilcea Mehadia 241
97 211 142 Oradea 380
111 Lugoj Pitesti Pitesti 100
70 85 98
Hirsova Rimnicu Vilcea 193
Mehadia 146 101 Urziceni
75 86 Sibiu 253
120 138 Bucharest Timisoara 329
Dobreta 90
Cralova Eforie Zerind 374
Giurgiu
Example: A* Tree Search for Romania

Straight-line dist to goal


Oradea Arad 366
71 Neamt Bucharest 0
87
75 Zerind 151 Craiova 160
Iasi
140 Drobeta 242
Arad Sibiu 99 92
Fagaras Fagaras 176
118 Vaslui Lugoj 244
Timisoara 80 Rimnicu Vilcea Mehadia 241
97 211 142 Oradea 380
111 Lugoj Pitesti Pitesti 100
70 85 98
Hirsova Rimnicu Vilcea 193
Mehadia 146 101 Urziceni
75 86 Sibiu 253
120 138 Bucharest Timisoara 329
Dobreta 90
Cralova Eforie Zerind 374
Giurgiu
Example: A* Tree Search for Romania

The loop at Sibiu could


be detected by noticing
Straight-line dist to goal
Oradea the other Sibiu on the Arad 366
71 Neamt path from child to root. Bucharest 0
87
75 Zerind 151 Craiova 160
Iasi
140 Drobeta 242
Arad Sibiu 99 92
Fagaras Fagaras 176
118 Vaslui Lugoj 244
Timisoara 80 Rimnicu Vilcea Mehadia 241
97 211 142 Oradea 380
111 Lugoj Pitesti Pitesti 100
70 85 98
Hirsova Rimnicu Vilcea 193
Mehadia 146 101 Urziceni
75 86 Sibiu 253
120 138 Bucharest Timisoara 329
Dobreta 90
Cralova Eforie Zerind 374
Giurgiu
Example: A* Tree Search for Romania

Remove the higher-cost


of identical nodes.
Straight-line dist to goal
Oradea Arad 366
71 Neamt Note: search does Bucharest 0
87
75 Zerind 151
Iasi
not “backtrack”; Craiova 160
140 both routes are Drobeta 242
Arad Sibiu 99 92
Fagaras Fagaras 176
118
pursued at once. Lugoj 244
Vaslui
Timisoara 80 Rimnicu Vilcea Mehadia 241
97 211 142 Oradea 380
111 Lugoj Pitesti Pitesti 100
70 85 98
Hirsova Rimnicu Vilcea 193
Mehadia 146 101 Urziceni
75 86 Sibiu 253
120 138 Bucharest Timisoara 329
Dobreta 90
Cralova Eforie Zerind 374
Giurgiu
Example: A* Tree Search for Romania

Remove the higher-cost


of identical nodes.
Straight-line dist to goal
Oradea Arad 366
71 Neamt Bucharest 0
87
75 Zerind 151 Craiova 160
Iasi
140 Drobeta 242
Arad Sibiu 99 92
Fagaras Fagaras 176
118 Vaslui Lugoj 244
Timisoara 80 Rimnicu Vilcea Mehadia 241
97 211 142 Oradea 380
111 Lugoj Pitesti Pitesti 100
70 85 98
Hirsova Rimnicu Vilcea 193
Mehadia 146 101 Urziceni
75 86 Sibiu 253
120 138 Bucharest Timisoara 329
Dobreta 90
Cralova Eforie Zerind 374
Giurgiu
Example: A* Tree Search for Romania

Straight-line dist to goal


Oradea Arad 366
71 Neamt Bucharest 0
87
75 Zerind 151 Craiova 160
Iasi
140 Drobeta 242
Arad Sibiu 99 92
Fagaras Fagaras 176
118 Vaslui Lugoj 244
Timisoara 80 Rimnicu Vilcea Mehadia 241
97 211 142 Oradea 380
111 Lugoj Pitesti Pitesti 100
70 85 98
Hirsova Rimnicu Vilcea 193
Mehadia 146 101 Urziceni
75 86 Sibiu 253
120 138 Bucharest Timisoara 329
Dobreta 90
Cralova Eforie Zerind 374
Giurgiu
Contours of A* search
• For consistent heuristic, A* expands in order of increasing f value
• Gradually adds “f-contours” of nodes
• Contour i has all nodes with f(n)  fi , where fi < fi+1
• Contours at f = 380, 400, & 420 starting at Arad. Nodes inside a
given contour have f-costs  the contour value.
Oradea
Neamt

Zerind
Iasi
Arad 380 Sibiu Fagaras
Vaslui
Timisoara Rimnicu Vilcea
400
Lugoj Pitesti
85 Hirsova
Mehadia Urziceni
Bucharest
Dobreta
420
Cralova Eforie
Giurgiu
Properties of A* search
• Complete? Yes
– Unless infinitely many nodes with f < f(G)
– Cannot happen if step-cost ≥ ε > 0
• Time/Space? Depends on heuristic. Informally, O(bm).
– Except if |h(n) – h*(n)| ≤ O( log h*(n) )
• Unlikely to have such an excellent heuristic function
– More formally, it depends on the heuristic function (see R&N p. 98)
• In simplest cases, complexity is O(bh*-h)  O( (b(h*-h)/h*))d )
• Thus, in simplest cases, b(h*-h)/h*) is the effective branching factor
(discussed below)
• Optimal? Yes
– With: Tree-Search, admissible heuristic; Graph-Search, consistent
heuristic
• Optimally efficient? Yes
– No optimal algorithm with same heuristic is guaranteed to expand
fewer nodes
Optimality of A* tree search with an
admissible heuristic
• Proof:
– Suppose some suboptimal goal G2 has been generated & is
on the frontier. Let n be an unexpanded node on the path
to an optimal goal G
– Show: f(n) < f(G2) (so, n is expanded before G2)
f(G2) = g(G2) since h(G2) = 0
f(G) = g(G) since h(G) = 0
g(G2) > g(G) since G2 is suboptimal
f(G2) > f(G) from above, with h=0

h(n) ≤ h*(n) since h is admissible (under-estimate)


g(n) + h(n) ≤ g(n) + h*(n) from above
f(n) ≤ f(G) since g(n)+h(n)=f(n) & g(n)+h*(n)=f(G)
f(n) < f(G2) from above

R&N pp. 95-98 proves the optimality of A*


graph search with a consistent heuristic
Requirements for Optimality

• Tree search
– Need admissibility
• Graph search, without re-opening closed nodes
– Need consistency
• Graph search, with re-opening closed nodes
– Admissibility is enough
Complexity of A*
• A* is optimally efficient (Dechter and Pearl 1985):
– It can be shown that all algorithms that do not expand a node which A*
did expand (inside the contours) may miss an optimal solution
• A* worst-case time complexity:
– is exponential unless the heuristic function is very accurate
• If h is exact (h = h*)
– search focus only on optimal paths
• Main problem:
– space complexity is exponential
– Not anytime; all or nothing … but largest f expanded is lower bound on C*
• Effective branching factor:
– Number of nodes generated by a “typical” search node
– Approximately : b* = N^(1/d)
• Q: what if you are given a solution (not necessarily optimal); can
you improve A* performance?
A* properties
• A* expands every path along which f(n) < C*

• If h is consistent A* will expand any node such that


f(n) < C*

• A* will never expand any node such that f(n) > C*

• Therefore, A* expands all the nodes for which f(n) <


C* and a subset of the nodes for which f(n) = C*.

• Therefore, if h1(n) < h2(n) clearly the subset of nodes


expanded by h2 is smaller.
Non-admissible heuristics
• Adjust weights of g and h
• fw(n) = (1-w)∙g(n) + w∙h(n)
– w=0 → Uniform Cost Search
– w=½ → A*
– w=1 → Greedy Best-First Search
• If h is admissible then fw is admissible for 0 ≤ w ≤ ½
Memory Bounded Heuristic Search
R&N 3.5.5
Memory-bounded heuristic search
• Memory is a major limitation of A*
– Usually run out of memory before run out of time
• How can we solve the memory problem?

• R&N section 3.5.5, pp. 92-95, gives methods for A* that use
less memory (but more time & book-keeping)
– Iterative-deepening A* (IDA*), recursive best-first search (RBFS), memory-
bounded A* (MA*), simplified MA* (SMA*)
• Idea: recursive best-first search (RBFS)
– Try something like depth-first search, but don’t forget everything
about the branches we have partially explored
– Remember the best f(n) value we have found so far in the branch
we’re deleting
RBFS:
best alternative
over frontier nodes,
which are not children:
i.e. do I want to back up?

RBFS changes its mind


very often in practice.

This is because the


f=g+h become more
accurate (less optimistic)
as we approach the goal.
Hence, higher level nodes
have smaller f-values and
will be explored first.

Problem: We should keep


in memory whatever we can.
Simple Memory Bounded A* (SMA*)
• Memory limited, but uses available memory well:
– Like A*, but if memory full: delete the worst node (largest f-val)
– Like RBFS, remember the best descendent in deleted branch
– If there is a tie (equal f-values) we delete the oldest nodes first.
– SMA* finds the optimal reachable solution given memory
constraint.
– Time can still be exponential. A solution is not reachable if
a single path from root to goal
does not fit in memory

• Best of search algorithms we’ve seen


– Using memory avoids double work; heuristic guides exploration
– If memory is not a problem, basic A* is easy to code &
performs well
SMA* Pseudocode
function SMA*(problem) returns a solution sequence
inputs: problem, a problem
static: Queue, a queue of nodes ordered by f-cost
Note: not in
Queue  MAKE-QUEUE({MAKE-NODE(INITIAL-STATE[problem])})
2nd edition of loop do
R&N if Queue is empty then return failure
n  deepest least-f-cost node in Queue
if GOAL-TEST(n) then return success
s  NEXT-SUCCESSOR(n)
if s is not a goal and is at maximum depth then
f(s)  
else
f(s)  MAX(f(n),g(s)+h(s))
if all of n’s successors have been generated then
update n’s f-cost and those of its ancestors if necessary
if SUCCESSORS(n) all in memory then remove n from Queue
if memory is full then
delete shallowest, highest-f-cost node in Queue
remove it from its parent’s successor list
insert its parent on Queue if necessary
insert s in Queue
end
Simple memory-bounded A* (SMA*)
(Example with 3-node memory) maximal depth is 3, since
memory limit is 3. This
Progress of SMA*. Each node is labeled with its current f-cost. branch is now useless.
Values in parentheses show the value of the best forgotten descendant.
best forgotten node
Search space best estimated solution
so far for that node
g+h = f ☐ = goal A
13[15]
A
0+12=12 A A A
12 12
10 8 13
G
B G 13
10+5=15 8+5=13
B B G
10 10 8 16 15
18 H
20+5=25
C D
16+2=18
H I 15 13

20+0=20 24+0=24
10 10 A A A
8 8 15[15] 15[24] 20[24]
E F J K
A 8
15
30+5=35 30+0=30 24+0=24 24+5=29 G B B
15 20[]
24[]

B G
I D
15 24 C 25
24
 20

Algorithm can tell you when best solution found within memory constraint is optimal or not.
Branch-and-Bound Depth-First
Search
Branch-and-Bound (DFS)
• Upper and Lower bounds
– U upper bound, corresponding to current best known solution
– L lower bound = admissible f(n)
• Pick a node to expand, then
– If L=f(n) ≥ U, prune n
– Otherwise keep
• BnB DFS
– Not Best-First algorithm!
– Always expand deepest node
– Linear space complexity
– Exp time complexity
– Complete and optimal if run to completion
Path finding
1 4
A B C
2

S 2 5 G

5 3
2 4
D E F

A 10.4 B C
6.7 4.0

11.0 G
S

8.9 3.0
6.9
D E F
Example of Branch and Bound in Action
S 1 5+8.9=13.9
2+10.4=12.4
A 2 D
3+6.7=9.7 8
7+4=11
B 3 D 4+8.9=12.9

8+6.9=14.9 5
C 4 E 9
E 6+6.9=12.9
10+8.9=18.9
12+3=15
F 6
D 11+6.7=17.7
10+3=13 F 10 B
U=15+0=15
G 7 A10.4 B C
6.7 4.0
U=13+0=13
A 1 B 4 C G 11 S 11.0 G
2
S 5 G
2 8.9 3.0
5 3 D E 6.9 F
D 2 E 4 F
Example of A* Algorithm in Action
1
2 +10.4 = 12.4
S 5 + 8.9 = 13.9
2 D
3 + 6.7 = 9.7 A 5
B 3 D 4 + 8.9 = 12.9

7 + 4 = 11 4 8 + 6.9 = 14.9 6
C E 6 + 6.9 = 12.9
E
7
Dead End
B F 10 + 3.0 = 13
1 4 11 + 6.7 = 17.7
A B C 8
2 A10.4 B C
6.7 4.0 G
5 G 13 + 0 = 13
2
S S 11.0 G

5 3
2 4 8.9 3.0
D E F D E 6.9 F
Properties of Branch-and-Bound
• Not guaranteed to terminate unless
– admissible f and reasonable upper bound U
• Note : U imposes a depth-bound
• Optimal:
– finds an optimal solution (f is admissible)
• Time complexity: exponential
• Space complexity: can be linear
• Advantage:
– anytime property
• Note : unlike A*, BnB may (will) expand nodes f>C*.
Effectiveness of Heuristic Search
R&N 3.6.1
Effectiveness of heuristic search
• How quality of the heuristic impacts search?

• What is the time and space complexity?

• Is any heuristic better? Worse?

• Case study: the 8-puzzle


Heuristic functions
• 8-Puzzle
– Avg solution cost is about 22 steps
– Branching factor ~ 3
– Exhaustive search to depth 22 = 3.1 x 10^10 states
– A good heuristic f’n can reduce the search process
– True cost (h*) for this start & goal: 26

• Two commonly used heuristics


– h1: the number of misplaced tiles
h1(s) = 8
– h2: sum of the distances of the tiles from their goal
(“Manhattan distance”)
h2(s) = 3+1+2+2+2+3+3+2
= 18
Dominance
• Definition:
If h2(n) ≥ h1(n) for all n
then h2 dominates h1
– h2 is almost always better for search than h1
– h2 is guaranteed to expand no more nodes than h1
– h2 almost always expands fewer nodes than h1
– Not useful unless are h1 , h2 are admissible / consistent
• Example: 8-Puzzle / sliding tiles
– h1: the number of misplaced tiles
– h2: sum of the distances of the tiles from their goal
– h2 dominates h1
• Extreme cases
– h=0
– h = h*
Example: 8-Puzzle
Average number of nodes expanded
d IDS A*(h1) A*(h2)
2 10 6 6
4 112 13 12
8 6384 39 25
12 364404 227 73
14 3473941 539 113
20 ------------ 7276 676
24 ------------ 39135 1641

Average over 100 randomly generated 8-puzzle problems


h1 = number of tiles in the wrong position
h2 = sum of Manhattan distances
Effective branching factor, b*
• Let A* generate N nodes to find a goal at depth d
– Effective branching factor b* is the branching factor of a
uniform tree of depth d that contains N+1 nodes:

– For sufficiently hard problems, b* is often fairly constant


across different problem instances
• A good guide to the heuristic’s overall usefulness
• A good way to compare different heuristics
(Optional Mathematical Details)
• R&N discuss effective branching factor but give no formula
(R&N p. 103, see also p. 111).
• Let N + 1 = 1 + b* + (b*)2 + … + (b*)d
– Then N = b* + (b*)2 + … + (b*)d + (b*)d
– (b*)N = (b*)2 + … + (b*)d + (b*)d+1
– (b*)N - N = N(b* − 1)
= (b*)d+1 − b*
• So N = [ (b*)d+1 − b* ] / (b* − 1)
– No closed form solution
• Approximately, N  (b*)d, which is solvable directly.
– This approach over-estimates b* when b* close to 1.
• If you need a better estimate, use Binary Search
– https://en.wikipedia.org/wiki/Binary_search_algorithm
Effectiveness of heuristics
• Branching factors for 8-Puzzle heuristics
Automatic Generation of Heuristics
R&N 3.6.2
Designing heuristics
• Often constructed via problem relaxations
– A relaxed problem has fewer restrictions on actions
– Cost of an optimal solution to a relaxed problem is an admissible
heuristic for the original problem
• Why? Relaxed state-space graph has strictly more edges…
• Ex: 8-Puzzle
– Rule : a tile can be moved from A to B, iff
• A and B are adjacent
• B is blank
– Relax rules so a tile can move anywhere (eliminate “A and B are
adjacent”) → “# of misplaced tiles” heuristic h1(n)
– Relax rules so tile can move to any adjacent square (eliminate “B is
blank”) → “Manhattan distance” heuristic h2(n)
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)
(number of misplaced tiles) gives the
shortest solution

• If the rules are relaxed so that a tile can


move to any h/v adjacent square, then
h2(n) (Manhatten distance) gives the
shortest solution
Heuristic generation
• Theorem: Heuristics that are generated from relaxed models are
consistent.

• Proof: h is true shortest path in a relaxed model


– h(n) <=c’(n,n’)+h(n’) (c’ are shortest distances in relaxed graph)
– c’(n,n’) <=c(n,n’)
– → h(n) <= c(n,n’)+h(n’)
Notes on heuristic generation
• Total (time) complexity = heuristic computation + nodes
expanded
• More powerful heuristic – harder to compute, but more pruning
power (fewer nodes expanded)
• Problem:
– not every relaxed problem is easy
• How to recognize a relaxed easy problem
• A proposal: a problem is easy if it can be solved optimally
by a greedy algorithm
• Q: what if neither h1 nor h2 is clearly better? max(h1, h2)
• Often, a simpler problem which is more constrained is easier;
will provide a good upper-bound.
More on heuristics
• Combining heuristics
– H(n) = max { h1(n), h2(n), … , hk(n) }
– “max” chooses the least optimistic heuristic at each node

• Pattern databases
– Solve a subproblem of the true problem
( = a lower bound on the cost of the true problem)
– Store the exact solution for each possible subproblem
Summary
• Uninformed search has uses but also severe limitations
• Heuristics are a structured way to make search smarter

• Informed (or heuristic) search uses problem-specific heuristics to


improve efficiency
– Best-first, A* (and if needed for memory, RBFS, SMA*)
– Techniques for generating heuristics
– A* is optimal with admissible (tree) / consistent (graph heuristics

• Can provide significant speed-ups in practice


– Ex: 8-Puzzle, dramatic speed-up
– Still worst-case exponential time complexity (NP-complete)

• Next: local search techniques (hill climbing, GAs, annealing…)


– R&N Ch 4
You should know…
• evaluation function f(n) and heuristic function h(n) for each node n
– g(n) = known path cost so far to node n.
– h(n) = estimate of (optimal) cost to goal from node n.
– f(n) = g(n)+h(n) = estimate of total cost to goal through node n.

• Heuristic searches: Greedy-best-first, A*


– A* is optimal with admissible (tree)/consistent (graph) heuristics
– Prove that A* is optimal with admissible heuristic for tree search
– Recognize when a heuristic is admissible or consistent

• h2 dominates h1 iff h2(n) ≥ h1(n) for all n


• Effective branching factor: b*
• Inventing heuristics: relaxed problems; max or convex combination

You might also like