SlideShare a Scribd company logo
Heuristic search, A*
CS171, Fall 2016
Introduction to Artificial Intelligence
Prof. Alexander Ihler
Reading: R&N 3.5-3.7
Outline
• Review limitations of uninformed search methods
• Informed (or heuristic) search
• Problem-specific heuristics to improve efficiency
• Best-first, A* (and if needed for memory limits, RBFS, SMA*)
• Techniques for generating heuristics
• A* is optimal with admissible (tree)/consistent (graph) heuristics
• A* is quick and easy to code, and often works *very* well
• Heuristics
• A structured way to add “smarts” to your solution
• Provide *significant* speed-ups in practice
• Still have worst-case exponential time complexity
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: 3.1x1010
states
– 24-Puzzle: 1024
states (much worse!)
Recall: tree search
Sibiu Timisoara Zerind
Arad
Rimnicu… Lugoj Oradea
Oradea Arad Arad
Fagaras
Arad
tion TREE-SEARCH (problem, strategy) : returns a solution or failure
initialize the search tree using the initial state of problem
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 sol
else: expand the node and add the resulting nodes to the search
This “strategy” is what
differentiates different
search algorithms
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 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)
• Search efficiency depends on heuristic quality!
– The better your heuristic, the faster your search!
Heuristic function
• Heuristic
– Def’n: a commonsense rule or rules intended to increase the probability of
solving some problem
– Same linguistic root as “Eureka” = “I have found it”
– Using rules of thumb to find answers
• Heuristic function h(n)
– Estimate of (optimal) remaining cost 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
Ex: 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 f’n can reduce the search process
• Two commonly used heuristics
– h1: the number of misplaced tiles
– h2: sum of the distances of the tiles from their goal (“Manhattan
distance”)
h1(s) = 8
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
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
86
98
142
92
87
90
85
101
211
138
146
97
120
75
70
111
118
140
151
71
75
Oradea
Zerind
Arad
Timisoara
Lugoj
Mehadia
Dobreta
Sibiu Fagaras
Rimnicu Vilcea
Pitesti
Cralova
Bucharest
Giurgiu
Urziceni
Neamt
Iasi
Vaslui
Hirsova
Eforie
99
80
Relationship of search algorithms
• Notation
– g(n) = known cost so far to reach n
– h(n) = estimated (optimal) cost from n to goal
– 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)
– Optimal for admissible / consistent heuristics
– Generally the preferred heuristic search framework
– Memory-efficient versions of A* are available: RBFS, SMA*
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)
86
98
142
92
87
90
85
101
211
138
146
97
120
75
70
111
118
140
151
71
75
Oradea
Zerind
Arad
Timisoara
Lugoj
Mehadia
Dobreta
Sibiu Fagaras
Rimnicu Vilcea
Pitesti
Cralova
Bucharest
Giurgiu
Urziceni
Neamt
Iasi
Vaslui
Hirsova
Eforie
99
80
Ex: GBFS for Romania
Straight-line dist to goal
Arad 366
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
86
98
142
92
87
90
85
101
211
138
146
97
120
75
70
111
118
140
151
71
75
Oradea
Zerind
Arad
Timisoara
Lugoj
Mehadia
Dobreta
Sibiu Fagaras
Rimnicu Vilcea
Pitesti
Cralova
Bucharest
Giurgiu
Urziceni
Neamt
Iasi
Vaslui
Hirsova
Eforie
99
80
Ex: GBFS for Romania
Straight-line dist to goal
Arad 366
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
GBFS: 450km
Optimal path: 418 km
Greedy best-first search
• With tree-search, will become stuck in this loop:
– Order of node expansion: S A D S A D S A D …
– Path found: none
– Cost of path found: none
B
D
G
S
A C
h=5
h=7
h=6
h=8 h=9
h=0
Properties of greedy best-first search
• Complete?
– Tree version can get stuck in loops
– Graph version is complete in finite spaces
• Time? O(bm
)
– A good heuristic can give dramatic improvement
• Space? O(bm
)
– Keeps all nodes in memory
• Optimal? No
– Ex: Arad – Sibiu – Rimnicu Vilcea – Pitesti – Bucharest shorter!
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)
• 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
• Priority queue sort function = f(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 optimistic (or, never pessimistic)
– Ex: straight-line distance never overestimates road distance
• Theorem:
if h(n) is admissible, A* using Tree-Search is optimal
Admissible heuristics
• Two commonly used heuristics
– h1: the number of misplaced tiles
– h2: sum of the distances of the tiles from their goal
(“Manhattan distance”)
h1(s) = 8
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,
h(n) ≤ c(n,a,n') + h(n')
• If h 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.
• Consistent ) admissible (stronger condition)
• Theorem: If h(n) is consistent, A* using Graph-Search is optimal
(Triangle inequality)
Optimality conditions
• Tree search optimal if admissible
• Graph search optimal if 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 are also consistent
• Could we do optimal graph search with an admissible
heuristic?
– Yes, but you would have to do additional work to update descendants when a
cheaper path to a node is found
– A consistent heuristic avoids this problem
Ex: A* for Romania
86
98
142
92
87
90
85
101
211
138
146
97
120
75
70
111
118
140
151
71
75
Oradea
Zerind
Arad
Timisoara
Lugoj
Mehadia
Dobreta
Sibiu Fagaras
Rimnicu Vilcea
Pitesti
Cralova
Bucharest
Giurgiu
Urziceni
Neamt
Iasi
Vaslui
Hirsova
Eforie
99
80
Straight-line dist to goal
Arad 366
Bucharest 0
Craiova 160
Drobeta 242
Fagaras 176
Lugoj 244
Mehadia 241
Oradea 380
Pitesti 100
Rimnicu Vilcea 193
Sibiu 253
Timisoara 329
Zerind 374
86
98
142
92
87
90
85
101
211
138
146
97
120
75
70
111
118
140
151
71
75
Oradea
Zerind
Arad
Timisoara
Lugoj
Mehadia
Dobreta
Sibiu Fagaras
Rimnicu Vilcea
Pitesti
Cralova
Bucharest
Giurgiu
Urziceni
Neamt
Iasi
Vaslui
Hirsova
Eforie
99
80
Ex: A* for Romania
(Simulated queue)
Expanded: None
Children: None
Frontier: Arad/366 (0+366),
Straight-line dist to goal
Arad 366
Bucharest 0
Craiova 160
Drobeta 242
Fagaras 176
Lugoj 244
Mehadia 241
Oradea 380
Pitesti 100
Rimnicu Vilcea 193
Sibiu 253
Timisoara 329
Zerind 374
Ex: A* for Romania
86
98
142
92
87
90
85
101
211
138
146
97
120
75
70
111
118
140
151
71
75
Oradea
Zerind
Arad
Timisoara
Lugoj
Mehadia
Dobreta
Sibiu Fagaras
Rimnicu Vilcea
Pitesti
Cralova
Bucharest
Giurgiu
Urziceni
Neamt
Iasi
Vaslui
Hirsova
Eforie
99
80
Straight-line dist to goal
Arad 366
Bucharest 0
Craiova 160
Drobeta 242
Fagaras 176
Lugoj 244
Mehadia 241
Oradea 380
Pitesti 100
Rimnicu Vilcea 193
Sibiu 253
Timisoara 329
Zerind 374
86
98
142
92
87
90
85
101
211
138
146
97
120
75
70
111
118
140
151
71
75
Oradea
Zerind
Arad
Timisoara
Lugoj
Mehadia
Dobreta
Sibiu Fagaras
Rimnicu Vilcea
Pitesti
Cralova
Bucharest
Giurgiu
Urziceni
Neamt
Iasi
Vaslui
Hirsova
Eforie
99
80
Ex: A* for Romania
(Simulated queue)
Expanded: Arad/366 (0+366),
Children: Sibiu/393 (140+253), Timisoara/447 (118+329), Zerind/449 (75+374),
Frontier: Arad/366 (0+366), Sibiu/393 (140+253), Timisoara/447 (118+329), Zerind/449
(75+374),
Straight-line dist to goal
Arad 366
Bucharest 0
Craiova 160
Drobeta 242
Fagaras 176
Lugoj 244
Mehadia 241
Oradea 380
Pitesti 100
Rimnicu Vilcea 193
Sibiu 253
Timisoara 329
Zerind 374
Ex: A* for Romania
86
98
142
92
87
90
85
101
211
138
146
97
120
75
70
111
118
140
151
71
75
Oradea
Zerind
Arad
Timisoara
Lugoj
Mehadia
Dobreta
Sibiu Fagaras
Rimnicu Vilcea
Pitesti
Cralova
Bucharest
Giurgiu
Urziceni
Neamt
Iasi
Vaslui
Hirsova
Eforie
99
80
Straight-line dist to goal
Arad 366
Bucharest 0
Craiova 160
Drobeta 242
Fagaras 176
Lugoj 244
Mehadia 241
Oradea 380
Pitesti 100
Rimnicu Vilcea 193
Sibiu 253
Timisoara 329
Zerind 374
86
98
142
92
87
90
85
101
211
138
146
97
120
75
70
111
118
140
151
71
75
Oradea
Zerind
Arad
Timisoara
Lugoj
Mehadia
Dobreta
Sibiu Fagaras
Rimnicu Vilcea
Pitesti
Cralova
Bucharest
Giurgiu
Urziceni
Neamt
Iasi
Vaslui
Hirsova
Eforie
99
80
Ex: A* for Romania
(Simulated queue)
Expanded: Arad/366 (0+366), Sibiu/393 (140+253),
Children: Arad/646 (280+366), Fagaras/415 (239+176), Oradea/671 (291+380),
RimnicuVilcea/413 (220+193),
Frontier: Arad/366 (0+366), Sibiu/393 (140+253), Timisoara/447 (118+329), Zerind/449
(75+374), Arad/646 (280+366), Fagaras/415 (239+176), Oradea/671 (291+380),
RimnicuVilcea/413 (220+193),
Straight-line dist to goal
Arad 366
Bucharest 0
Craiova 160
Drobeta 242
Fagaras 176
Lugoj 244
Mehadia 241
Oradea 380
Pitesti 100
Rimnicu Vilcea 193
Sibiu 253
Timisoara 329
Zerind 374
Ex: A* for Romania
86
98
142
92
87
90
85
101
211
138
146
97
120
75
70
111
118
140
151
71
75
Oradea
Zerind
Arad
Timisoara
Lugoj
Mehadia
Dobreta
Sibiu Fagaras
Rimnicu Vilcea
Pitesti
Cralova
Bucharest
Giurgiu
Urziceni
Neamt
Iasi
Vaslui
Hirsova
Eforie
99
80
Straight-line dist to goal
Arad 366
Bucharest 0
Craiova 160
Drobeta 242
Fagaras 176
Lugoj 244
Mehadia 241
Oradea 380
Pitesti 100
Rimnicu Vilcea 193
Sibiu 253
Timisoara 329
Zerind 374
86
98
142
92
87
90
85
101
211
138
146
97
120
75
70
111
118
140
151
71
75
Oradea
Zerind
Arad
Timisoara
Lugoj
Mehadia
Dobreta
Sibiu Fagaras
Rimnicu Vilcea
Pitesti
Cralova
Bucharest
Giurgiu
Urziceni
Neamt
Iasi
Vaslui
Hirsova
Eforie
99
80
Ex: A* for Romania
(Simulated queue)
Expanded: Arad/366 (0+366), Sibiu/393 (140+253), RimnicuVilcea/413 (220+193),
Children: Craiova/526 (366+160), Pitesti/417 (317+100), Sibiu/553 (300+253),
Frontier: Arad/366 (0+366), Sibiu/393 (140+253), Timisoara/447 (118+329), Zerind/449
(75+374), Arad/646 (280+366), Fagaras/415 (239+176), Oradea/671 (291+380),
RimnicuVilcea/413 (220+193), Craiova/526 (366+160), Pitesti/417 (317+100), Sibiu/553
(300+253),
Straight-line dist to goal
Arad 366
Bucharest 0
Craiova 160
Drobeta 242
Fagaras 176
Lugoj 244
Mehadia 241
Oradea 380
Pitesti 100
Rimnicu Vilcea 193
Sibiu 253
Timisoara 329
Zerind 374
Ex: A* for Romania
86
98
142
92
87
90
85
101
211
138
146
97
120
75
70
111
118
140
151
71
75
Oradea
Zerind
Arad
Timisoara
Lugoj
Mehadia
Dobreta
Sibiu Fagaras
Rimnicu Vilcea
Pitesti
Cralova
Bucharest
Giurgiu
Urziceni
Neamt
Iasi
Vaslui
Hirsova
Eforie
99
80
Straight-line dist to goal
Arad 366
Bucharest 0
Craiova 160
Drobeta 242
Fagaras 176
Lugoj 244
Mehadia 241
Oradea 380
Pitesti 100
Rimnicu Vilcea 193
Sibiu 253
Timisoara 329
Zerind 374
Note: search does
not “backtrack”;
both routes are
pursued.
86
98
142
92
87
90
85
101
211
138
146
97
120
75
70
111
118
140
151
71
75
Oradea
Zerind
Arad
Timisoara
Lugoj
Mehadia
Dobreta
Sibiu Fagaras
Rimnicu Vilcea
Pitesti
Cralova
Bucharest
Giurgiu
Urziceni
Neamt
Iasi
Vaslui
Hirsova
Eforie
99
80
Ex: A* for Romania
(Simulated queue)
Expanded: Arad/366 (0+366), Sibiu/393 (140+253), RimnicuVilcea/413 (220+193),
Fagaras/415 (239+176),
Children: Bucharest/450 (450+0), Sibiu/591 (338+253),
Frontier: Arad/366 (0+366), Sibiu/393 (140+253), Timisoara/447 (118+329), Zerind/449
(75+374), Arad/646 (280+366), Fagaras/415 (239+176), Oradea/671 (291+380),
RimnicuVilcea/413 (220+193), Craiova/526 (366+160), Pitesti/417 (317+100), Sibiu/553
(300+253), Bucharest/450 (450+0), Sibiu/591 (338+253),
Straight-line dist to goal
Arad 366
Bucharest 0
Craiova 160
Drobeta 242
Fagaras 176
Lugoj 244
Mehadia 241
Oradea 380
Pitesti 100
Rimnicu Vilcea 193
Sibiu 253
Timisoara 329
Zerind 374
Ex: A* for Romania
86
98
142
92
87
90
85
101
211
138
146
97
120
75
70
111
118
140
151
71
75
Oradea
Zerind
Arad
Timisoara
Lugoj
Mehadia
Dobreta
Sibiu Fagaras
Rimnicu Vilcea
Pitesti
Cralova
Bucharest
Giurgiu
Urziceni
Neamt
Iasi
Vaslui
Hirsova
Eforie
99
80
Straight-line dist to goal
Arad 366
Bucharest 0
Craiova 160
Drobeta 242
Fagaras 176
Lugoj 244
Mehadia 241
Oradea 380
Pitesti 100
Rimnicu Vilcea 193
Sibiu 253
Timisoara 329
Zerind 374
86
98
142
92
87
90
85
101
211
138
146
97
120
75
70
111
118
140
151
71
75
Oradea
Zerind
Arad
Timisoara
Lugoj
Mehadia
Dobreta
Sibiu Fagaras
Rimnicu Vilcea
Pitesti
Cralova
Bucharest
Giurgiu
Urziceni
Neamt
Iasi
Vaslui
Hirsova
Eforie
99
80
Ex: A* for Romania
(Simulated queue)
Expanded: Arad/366 (0+366), Sibiu/393 (140+253), RimnicuVilcea/413 (220+193),
Fagaras/415 (239+176), Pitesti/417 (317+100),
Children: Bucharest/418 (418+0), Craiova/615 (455+160), RimnicuVilcea/607 (414+193),
Frontier: Arad/366 (0+366), Sibiu/393 (140+253), Timisoara/447 (118+329), Zerind/449
(75+374), Arad/646 (280+366), Fagaras/415 (239+176), Oradea/671 (291+380),
RimnicuVilcea/413 (220+193), Craiova/526 (366+160), Pitesti/417 (317+100), Sibiu/553
(300+253), Bucharest/450 (450+0), Sibiu/591 (338+253), Bucharest/418 (418+0),
Craiova/615 (455+160), RimnicuVilcea/607 (414+193)
Straight-line dist to goal
Arad 366
Bucharest 0
Craiova 160
Drobeta 242
Fagaras 176
Lugoj 244
Mehadia 241
Oradea 380
Pitesti 100
Rimnicu Vilcea 193
Sibiu 253
Timisoara 329
Zerind 374
Ex: A* for Romania
86
98
142
92
87
90
85
101
211
138
146
97
120
75
70
111
118
140
151
71
75
Oradea
Zerind
Arad
Timisoara
Lugoj
Mehadia
Dobreta
Sibiu Fagaras
Rimnicu Vilcea
Pitesti
Cralova
Bucharest
Giurgiu
Urziceni
Neamt
Iasi
Vaslui
Hirsova
Eforie
99
80
Straight-line dist to goal
Arad 366
Bucharest 0
Craiova 160
Drobeta 242
Fagaras 176
Lugoj 244
Mehadia 241
Oradea 380
Pitesti 100
Rimnicu Vilcea 193
Sibiu 253
Timisoara 329
Zerind 374
Shorter, more
expensive path
remains on queue
Cheaper path will
be found &
returned
86
98
142
92
87
90
85
101
211
138
146
97
120
75
70
111
118
140
151
71
75
Oradea
Zerind
Arad
Timisoara
Lugoj
Mehadia
Dobreta
Sibiu Fagaras
Rimnicu Vilcea
Pitesti
Cralova
Bucharest
Giurgiu
Urziceni
Neamt
Iasi
Vaslui
Hirsova
Eforie
99
80
Ex: A* for Romania
(Simulated queue)
Expanded: Arad/366 (0+366), Sibiu/393 (140+253), RimnicuVilcea/413 (220+193),
Fagaras/415 (239+176), Pitesti/417 (317+100), Bucharest/418 (418+0)
Children: None (goal test succeeds)
Frontier: Arad/366 (0+366), Sibiu/393 (140+253), Timisoara/447 (118+329), Zerind/449
(75+374), Arad/646 (280+366), Fagaras/415 (239+176), Oradea/671 (291+380),
RimnicuVilcea/413 (220+193), Craiova/526 (366+160), Pitesti/417 (317+100), Sibiu/553
(300+253), Bucharest/450 (450+0), Sibiu/591 (338+253), Bucharest/418 (418+0),
Craiova/615 (455+160), RimnicuVilcea/607 (414+193)
Straight-line dist to goal
Arad 366
Bucharest 0
Craiova 160
Drobeta 242
Fagaras 176
Lugoj 244
Mehadia 241
Oradea 380
Pitesti 100
Rimnicu Vilcea 193
Sibiu 253
Timisoara 329
Zerind 374
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=fi , where fi < fi+1
85
Oradea
Zerind
Arad
Timisoara
Lugoj
Mehadia
Dobreta
Sibiu Fagaras
Rimnicu Vilcea
Pitesti
Cralova
Bucharest
Giurgiu
Urziceni
Neamt
Iasi
Vaslui
Hirsova
Eforie
Properties of A* search
• Complete? Yes
– Unless infinitely many nodes with f < f(G)
– Cannot happen if step-cost ¸ ² > 0
• Time/Space? O(bm
)
– Except if |h(n) – h*(n)| · O( log h*(n) )
• 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*
• 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
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?
• 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:
BFS changes its mind
ery often in practice.
his is because the
=g+h become more
ccurate (less optimistic)
s we approach the goal.
ence, higher level nodes
ave smaller f-values and
ill be explored first.
roblem: We should keep
n memory whatever we can.
best alternative
over frontier nodes,
which are not children:
i.e. do I want to back up?
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.
• 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
A solution is not reachable if
a single path from root to goal
does not fit in memory
function SMA*(problem) returns a solution sequence
inputs: problem, a problem
static: Queue, a queue of nodes ordered by f-cost
Queue  MAKE-QUEUE({MAKE-NODE(INITIAL-STATE[problem])})
loop do
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
SMA* Pseudocode
Note: not in
2nd
edition of
R&N
24+0=24
A
B G
C D
E F
H
J
I
K
0+12=12
10+5=15
20+5=25
30+5=35
20+0=20
30+0=30
8+5=13
16+2=18
24+0=24 24+5=29
10 8
10 10
10 10
8 16
8 8
g+h = f
(Example with 3-node memory)
Progress of SMA*. Each node is labeled with its current f-cost.
Values in parentheses show the value of the best forgotten
descendant.
Algorithm can tell you when best solution found within memory constraint is optimal or not.
☐ = goal
Search space
maximal depth is 3, since
memory limit is 3. This
branch is now useless.
best forgotten node
A
12
A
B
12
15
A
B G
13
15 13
H

18
13
A
G
13[15]
A
B G
15
15 24

A
B
C
15[24]
15
25
A
B
D
8
20
20[24]
20[]
A
G
24[]
I
15[15]
24
best estimated solution
so far for that node
Simple memory-bounded A* (SMA*)
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 for this start & goal: 26
• Two commonly used heuristics
– h1: the number of misplaced tiles
– h2: sum of the distances of the tiles from their goal
(“Manhattan distance”)
h1(s) = 8
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
• Ex: 8-Puzzle / sliding tiles
– h1: the number of misplaced tiles
– h2: sum of the distances of the tiles from their goal
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 number of nodes expanded
Average over 100 randomly generated 8-puzzle problems
h1 = number of tiles in the wrong position
h2 = sum of Manhattan distances
Ex: 8-Puzzle
Effective branching factor, b*
• Let A* generate N nodes to find a goal at depth d
– Effective branching b* is the branching factor a uniform tree of
depth d would have in order to contain 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
Designing heuristics
• Often constructed via problem relaxations
– A problem with fewer restrictions on actions
– Cost of an optimal solution to a relaxed problem is an
admissible heuristic for the original problem
• Ex: 8-Puzzle
– Relax rules so a tile can move anywhere: h1(n)
– Relax rules so tile can move to any adjacent square: h2(n)
• A useful way to generate heuristics
– Ex: ABSOLVER (Prieditis 1993) discovered the first useful
heuristic for the Rubik’s cube
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…)
– Read R&N Ch 4 before next lecture
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

More Related Content

PDF
Informed-search TECHNIQUES IN ai ml data science
PPTX
3.informed search
PDF
Artificial intelligence and machine learning
PPTX
Informed Search.pptx
PDF
shamwari dzerwendo.mmmmmmfmmfmfkksrkrttkt
PPTX
22 sch Artificial intelligence Module-3 1st part.pptx
PDF
lect03-informed-searchhhhhhhhhhhhhhhhh.pdf
PDF
informed_search.pdf
Informed-search TECHNIQUES IN ai ml data science
3.informed search
Artificial intelligence and machine learning
Informed Search.pptx
shamwari dzerwendo.mmmmmmfmmfmfkksrkrttkt
22 sch Artificial intelligence Module-3 1st part.pptx
lect03-informed-searchhhhhhhhhhhhhhhhh.pdf
informed_search.pdf

Similar to Heuristic Search Algorithm in AI and its Techniques (20)

PPTX
Heuristic Search, Best First Search.pptx
PDF
BCS515B Module3 vtu notes : Artificial Intelligence Module 3.pdf
PPTX
CS767_Lecture_03.pptx
PPT
4 informed-search
PPT
M4 heuristics
PPT
Jarrar.lecture notes.aai.2011s.ch4.informedsearch
PPT
Presentacion nro 1 redes y comunicaciones de datos
PPT
m4-heuristics.ppt
PPTX
AI UNIT-1-BREADTH and BEST FIRST SEARCH.pptx
PPTX
A Star Search
PDF
Heuristic Searching: A* Search
PPT
M4 Heuristics
PPTX
Introduction to artificial intelligence
PPTX
Jarrar: Informed Search
PPTX
BFS,DFS, BEST FIRST,A-STAR,AO-STAR SEARCH.pptx
PPTX
Artificial intelligence(06)
PPTX
Artificial intelligence(06)
PPT
Informed search (heuristics)
PPT
Different Search Techniques used in AI.ppt
Heuristic Search, Best First Search.pptx
BCS515B Module3 vtu notes : Artificial Intelligence Module 3.pdf
CS767_Lecture_03.pptx
4 informed-search
M4 heuristics
Jarrar.lecture notes.aai.2011s.ch4.informedsearch
Presentacion nro 1 redes y comunicaciones de datos
m4-heuristics.ppt
AI UNIT-1-BREADTH and BEST FIRST SEARCH.pptx
A Star Search
Heuristic Searching: A* Search
M4 Heuristics
Introduction to artificial intelligence
Jarrar: Informed Search
BFS,DFS, BEST FIRST,A-STAR,AO-STAR SEARCH.pptx
Artificial intelligence(06)
Artificial intelligence(06)
Informed search (heuristics)
Different Search Techniques used in AI.ppt
Ad

More from RaghavendraPrasad179187 (10)

PPT
gmatrix distro_gmatrix distro_gmatrix distro
PPT
Project Planning and control in Software Engineering
PPT
spatial surveillance techniques in artificial intelligence
PPT
hillclimb algorithm for heuristic search
PPT
bayessian structures and its role in artificial intelligence
PPT
bayesian in artificial intelligence and search methods
PPT
HEURISTIC SEARCH IN ARTIFICIAL INTELLEGENCE
PPT
Dijkstra_Algorithm with illustarted example
PPTX
Linked list data structures and algorithms
PPTX
lecture-inference-in-first-order-logic.pptx
gmatrix distro_gmatrix distro_gmatrix distro
Project Planning and control in Software Engineering
spatial surveillance techniques in artificial intelligence
hillclimb algorithm for heuristic search
bayessian structures and its role in artificial intelligence
bayesian in artificial intelligence and search methods
HEURISTIC SEARCH IN ARTIFICIAL INTELLEGENCE
Dijkstra_Algorithm with illustarted example
Linked list data structures and algorithms
lecture-inference-in-first-order-logic.pptx
Ad

Recently uploaded (20)

PPTX
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
PDF
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
PPTX
CH1 Production IntroductoryConcepts.pptx
PPTX
Geodesy 1.pptx...............................................
PPTX
Foundation to blockchain - A guide to Blockchain Tech
PDF
Embodied AI: Ushering in the Next Era of Intelligent Systems
PDF
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
PPTX
Welding lecture in detail for understanding
PDF
R24 SURVEYING LAB MANUAL for civil enggi
PPTX
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
PDF
Digital Logic Computer Design lecture notes
PPT
CRASH COURSE IN ALTERNATIVE PLUMBING CLASS
PPT
Mechanical Engineering MATERIALS Selection
PDF
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
PPTX
Lecture Notes Electrical Wiring System Components
PDF
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
PDF
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
DOCX
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
PPTX
Construction Project Organization Group 2.pptx
PDF
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
CH1 Production IntroductoryConcepts.pptx
Geodesy 1.pptx...............................................
Foundation to blockchain - A guide to Blockchain Tech
Embodied AI: Ushering in the Next Era of Intelligent Systems
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
Welding lecture in detail for understanding
R24 SURVEYING LAB MANUAL for civil enggi
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
Digital Logic Computer Design lecture notes
CRASH COURSE IN ALTERNATIVE PLUMBING CLASS
Mechanical Engineering MATERIALS Selection
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
Lecture Notes Electrical Wiring System Components
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
Construction Project Organization Group 2.pptx
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf

Heuristic Search Algorithm in AI and its Techniques

  • 1. Heuristic search, A* CS171, Fall 2016 Introduction to Artificial Intelligence Prof. Alexander Ihler Reading: R&N 3.5-3.7
  • 2. Outline • Review limitations of uninformed search methods • Informed (or heuristic) search • Problem-specific heuristics to improve efficiency • Best-first, A* (and if needed for memory limits, RBFS, SMA*) • Techniques for generating heuristics • A* is optimal with admissible (tree)/consistent (graph) heuristics • A* is quick and easy to code, and often works *very* well • Heuristics • A structured way to add “smarts” to your solution • Provide *significant* speed-ups in practice • Still have worst-case exponential time complexity In AI, “NP-Complete” means “Formally interesting”
  • 3. 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: 3.1x1010 states – 24-Puzzle: 1024 states (much worse!)
  • 4. Recall: tree search Sibiu Timisoara Zerind Arad Rimnicu… Lugoj Oradea Oradea Arad Arad Fagaras Arad tion TREE-SEARCH (problem, strategy) : returns a solution or failure initialize the search tree using the initial state of problem 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 sol else: expand the node and add the resulting nodes to the search This “strategy” is what differentiates different search algorithms
  • 5. 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 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) • Search efficiency depends on heuristic quality! – The better your heuristic, the faster your search!
  • 6. Heuristic function • Heuristic – Def’n: a commonsense rule or rules intended to increase the probability of solving some problem – Same linguistic root as “Eureka” = “I have found it” – Using rules of thumb to find answers • Heuristic function h(n) – Estimate of (optimal) remaining cost 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
  • 7. Ex: 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 f’n can reduce the search process • Two commonly used heuristics – h1: the number of misplaced tiles – h2: sum of the distances of the tiles from their goal (“Manhattan distance”) h1(s) = 8 h2(s) = 3+1+2+2+2+3+3+2 = 18
  • 8. Ex: Romania, straight-line distance Straight-line dist to goal Arad 366 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 86 98 142 92 87 90 85 101 211 138 146 97 120 75 70 111 118 140 151 71 75 Oradea Zerind Arad Timisoara Lugoj Mehadia Dobreta Sibiu Fagaras Rimnicu Vilcea Pitesti Cralova Bucharest Giurgiu Urziceni Neamt Iasi Vaslui Hirsova Eforie 99 80
  • 9. Relationship of search algorithms • Notation – g(n) = known cost so far to reach n – h(n) = estimated (optimal) cost from n to goal – 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) – Optimal for admissible / consistent heuristics – Generally the preferred heuristic search framework – Memory-efficient versions of A* are available: RBFS, SMA*
  • 10. 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)
  • 11. 86 98 142 92 87 90 85 101 211 138 146 97 120 75 70 111 118 140 151 71 75 Oradea Zerind Arad Timisoara Lugoj Mehadia Dobreta Sibiu Fagaras Rimnicu Vilcea Pitesti Cralova Bucharest Giurgiu Urziceni Neamt Iasi Vaslui Hirsova Eforie 99 80 Ex: GBFS for Romania Straight-line dist to goal Arad 366 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
  • 12. 86 98 142 92 87 90 85 101 211 138 146 97 120 75 70 111 118 140 151 71 75 Oradea Zerind Arad Timisoara Lugoj Mehadia Dobreta Sibiu Fagaras Rimnicu Vilcea Pitesti Cralova Bucharest Giurgiu Urziceni Neamt Iasi Vaslui Hirsova Eforie 99 80 Ex: GBFS for Romania Straight-line dist to goal Arad 366 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 GBFS: 450km Optimal path: 418 km
  • 13. Greedy best-first search • With tree-search, will become stuck in this loop: – Order of node expansion: S A D S A D S A D … – Path found: none – Cost of path found: none B D G S A C h=5 h=7 h=6 h=8 h=9 h=0
  • 14. Properties of greedy best-first search • Complete? – Tree version can get stuck in loops – Graph version is complete in finite spaces • Time? O(bm ) – A good heuristic can give dramatic improvement • Space? O(bm ) – Keeps all nodes in memory • Optimal? No – Ex: Arad – Sibiu – Rimnicu Vilcea – Pitesti – Bucharest shorter!
  • 15. 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) • 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 • Priority queue sort function = f(n)
  • 16. 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 optimistic (or, never pessimistic) – Ex: straight-line distance never overestimates road distance • Theorem: if h(n) is admissible, A* using Tree-Search is optimal
  • 17. Admissible heuristics • Two commonly used heuristics – h1: the number of misplaced tiles – h2: sum of the distances of the tiles from their goal (“Manhattan distance”) h1(s) = 8 h2(s) = 3+1+2+2+2+3+3+2 = 18
  • 18. Consistent heuristics • A heuristic is consistent (or monotone) if for every node n, every successor n' of n generated by any action a, h(n) ≤ c(n,a,n') + h(n') • If h 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. • Consistent ) admissible (stronger condition) • Theorem: If h(n) is consistent, A* using Graph-Search is optimal (Triangle inequality)
  • 19. Optimality conditions • Tree search optimal if admissible • Graph search optimal if 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 are also consistent • Could we do optimal graph search with an admissible heuristic? – Yes, but you would have to do additional work to update descendants when a cheaper path to a node is found – A consistent heuristic avoids this problem
  • 20. Ex: A* for Romania 86 98 142 92 87 90 85 101 211 138 146 97 120 75 70 111 118 140 151 71 75 Oradea Zerind Arad Timisoara Lugoj Mehadia Dobreta Sibiu Fagaras Rimnicu Vilcea Pitesti Cralova Bucharest Giurgiu Urziceni Neamt Iasi Vaslui Hirsova Eforie 99 80 Straight-line dist to goal Arad 366 Bucharest 0 Craiova 160 Drobeta 242 Fagaras 176 Lugoj 244 Mehadia 241 Oradea 380 Pitesti 100 Rimnicu Vilcea 193 Sibiu 253 Timisoara 329 Zerind 374
  • 21. 86 98 142 92 87 90 85 101 211 138 146 97 120 75 70 111 118 140 151 71 75 Oradea Zerind Arad Timisoara Lugoj Mehadia Dobreta Sibiu Fagaras Rimnicu Vilcea Pitesti Cralova Bucharest Giurgiu Urziceni Neamt Iasi Vaslui Hirsova Eforie 99 80 Ex: A* for Romania (Simulated queue) Expanded: None Children: None Frontier: Arad/366 (0+366), Straight-line dist to goal Arad 366 Bucharest 0 Craiova 160 Drobeta 242 Fagaras 176 Lugoj 244 Mehadia 241 Oradea 380 Pitesti 100 Rimnicu Vilcea 193 Sibiu 253 Timisoara 329 Zerind 374
  • 22. Ex: A* for Romania 86 98 142 92 87 90 85 101 211 138 146 97 120 75 70 111 118 140 151 71 75 Oradea Zerind Arad Timisoara Lugoj Mehadia Dobreta Sibiu Fagaras Rimnicu Vilcea Pitesti Cralova Bucharest Giurgiu Urziceni Neamt Iasi Vaslui Hirsova Eforie 99 80 Straight-line dist to goal Arad 366 Bucharest 0 Craiova 160 Drobeta 242 Fagaras 176 Lugoj 244 Mehadia 241 Oradea 380 Pitesti 100 Rimnicu Vilcea 193 Sibiu 253 Timisoara 329 Zerind 374
  • 23. 86 98 142 92 87 90 85 101 211 138 146 97 120 75 70 111 118 140 151 71 75 Oradea Zerind Arad Timisoara Lugoj Mehadia Dobreta Sibiu Fagaras Rimnicu Vilcea Pitesti Cralova Bucharest Giurgiu Urziceni Neamt Iasi Vaslui Hirsova Eforie 99 80 Ex: A* for Romania (Simulated queue) Expanded: Arad/366 (0+366), Children: Sibiu/393 (140+253), Timisoara/447 (118+329), Zerind/449 (75+374), Frontier: Arad/366 (0+366), Sibiu/393 (140+253), Timisoara/447 (118+329), Zerind/449 (75+374), Straight-line dist to goal Arad 366 Bucharest 0 Craiova 160 Drobeta 242 Fagaras 176 Lugoj 244 Mehadia 241 Oradea 380 Pitesti 100 Rimnicu Vilcea 193 Sibiu 253 Timisoara 329 Zerind 374
  • 24. Ex: A* for Romania 86 98 142 92 87 90 85 101 211 138 146 97 120 75 70 111 118 140 151 71 75 Oradea Zerind Arad Timisoara Lugoj Mehadia Dobreta Sibiu Fagaras Rimnicu Vilcea Pitesti Cralova Bucharest Giurgiu Urziceni Neamt Iasi Vaslui Hirsova Eforie 99 80 Straight-line dist to goal Arad 366 Bucharest 0 Craiova 160 Drobeta 242 Fagaras 176 Lugoj 244 Mehadia 241 Oradea 380 Pitesti 100 Rimnicu Vilcea 193 Sibiu 253 Timisoara 329 Zerind 374
  • 25. 86 98 142 92 87 90 85 101 211 138 146 97 120 75 70 111 118 140 151 71 75 Oradea Zerind Arad Timisoara Lugoj Mehadia Dobreta Sibiu Fagaras Rimnicu Vilcea Pitesti Cralova Bucharest Giurgiu Urziceni Neamt Iasi Vaslui Hirsova Eforie 99 80 Ex: A* for Romania (Simulated queue) Expanded: Arad/366 (0+366), Sibiu/393 (140+253), Children: Arad/646 (280+366), Fagaras/415 (239+176), Oradea/671 (291+380), RimnicuVilcea/413 (220+193), Frontier: Arad/366 (0+366), Sibiu/393 (140+253), Timisoara/447 (118+329), Zerind/449 (75+374), Arad/646 (280+366), Fagaras/415 (239+176), Oradea/671 (291+380), RimnicuVilcea/413 (220+193), Straight-line dist to goal Arad 366 Bucharest 0 Craiova 160 Drobeta 242 Fagaras 176 Lugoj 244 Mehadia 241 Oradea 380 Pitesti 100 Rimnicu Vilcea 193 Sibiu 253 Timisoara 329 Zerind 374
  • 26. Ex: A* for Romania 86 98 142 92 87 90 85 101 211 138 146 97 120 75 70 111 118 140 151 71 75 Oradea Zerind Arad Timisoara Lugoj Mehadia Dobreta Sibiu Fagaras Rimnicu Vilcea Pitesti Cralova Bucharest Giurgiu Urziceni Neamt Iasi Vaslui Hirsova Eforie 99 80 Straight-line dist to goal Arad 366 Bucharest 0 Craiova 160 Drobeta 242 Fagaras 176 Lugoj 244 Mehadia 241 Oradea 380 Pitesti 100 Rimnicu Vilcea 193 Sibiu 253 Timisoara 329 Zerind 374
  • 27. 86 98 142 92 87 90 85 101 211 138 146 97 120 75 70 111 118 140 151 71 75 Oradea Zerind Arad Timisoara Lugoj Mehadia Dobreta Sibiu Fagaras Rimnicu Vilcea Pitesti Cralova Bucharest Giurgiu Urziceni Neamt Iasi Vaslui Hirsova Eforie 99 80 Ex: A* for Romania (Simulated queue) Expanded: Arad/366 (0+366), Sibiu/393 (140+253), RimnicuVilcea/413 (220+193), Children: Craiova/526 (366+160), Pitesti/417 (317+100), Sibiu/553 (300+253), Frontier: Arad/366 (0+366), Sibiu/393 (140+253), Timisoara/447 (118+329), Zerind/449 (75+374), Arad/646 (280+366), Fagaras/415 (239+176), Oradea/671 (291+380), RimnicuVilcea/413 (220+193), Craiova/526 (366+160), Pitesti/417 (317+100), Sibiu/553 (300+253), Straight-line dist to goal Arad 366 Bucharest 0 Craiova 160 Drobeta 242 Fagaras 176 Lugoj 244 Mehadia 241 Oradea 380 Pitesti 100 Rimnicu Vilcea 193 Sibiu 253 Timisoara 329 Zerind 374
  • 28. Ex: A* for Romania 86 98 142 92 87 90 85 101 211 138 146 97 120 75 70 111 118 140 151 71 75 Oradea Zerind Arad Timisoara Lugoj Mehadia Dobreta Sibiu Fagaras Rimnicu Vilcea Pitesti Cralova Bucharest Giurgiu Urziceni Neamt Iasi Vaslui Hirsova Eforie 99 80 Straight-line dist to goal Arad 366 Bucharest 0 Craiova 160 Drobeta 242 Fagaras 176 Lugoj 244 Mehadia 241 Oradea 380 Pitesti 100 Rimnicu Vilcea 193 Sibiu 253 Timisoara 329 Zerind 374 Note: search does not “backtrack”; both routes are pursued.
  • 29. 86 98 142 92 87 90 85 101 211 138 146 97 120 75 70 111 118 140 151 71 75 Oradea Zerind Arad Timisoara Lugoj Mehadia Dobreta Sibiu Fagaras Rimnicu Vilcea Pitesti Cralova Bucharest Giurgiu Urziceni Neamt Iasi Vaslui Hirsova Eforie 99 80 Ex: A* for Romania (Simulated queue) Expanded: Arad/366 (0+366), Sibiu/393 (140+253), RimnicuVilcea/413 (220+193), Fagaras/415 (239+176), Children: Bucharest/450 (450+0), Sibiu/591 (338+253), Frontier: Arad/366 (0+366), Sibiu/393 (140+253), Timisoara/447 (118+329), Zerind/449 (75+374), Arad/646 (280+366), Fagaras/415 (239+176), Oradea/671 (291+380), RimnicuVilcea/413 (220+193), Craiova/526 (366+160), Pitesti/417 (317+100), Sibiu/553 (300+253), Bucharest/450 (450+0), Sibiu/591 (338+253), Straight-line dist to goal Arad 366 Bucharest 0 Craiova 160 Drobeta 242 Fagaras 176 Lugoj 244 Mehadia 241 Oradea 380 Pitesti 100 Rimnicu Vilcea 193 Sibiu 253 Timisoara 329 Zerind 374
  • 30. Ex: A* for Romania 86 98 142 92 87 90 85 101 211 138 146 97 120 75 70 111 118 140 151 71 75 Oradea Zerind Arad Timisoara Lugoj Mehadia Dobreta Sibiu Fagaras Rimnicu Vilcea Pitesti Cralova Bucharest Giurgiu Urziceni Neamt Iasi Vaslui Hirsova Eforie 99 80 Straight-line dist to goal Arad 366 Bucharest 0 Craiova 160 Drobeta 242 Fagaras 176 Lugoj 244 Mehadia 241 Oradea 380 Pitesti 100 Rimnicu Vilcea 193 Sibiu 253 Timisoara 329 Zerind 374
  • 31. 86 98 142 92 87 90 85 101 211 138 146 97 120 75 70 111 118 140 151 71 75 Oradea Zerind Arad Timisoara Lugoj Mehadia Dobreta Sibiu Fagaras Rimnicu Vilcea Pitesti Cralova Bucharest Giurgiu Urziceni Neamt Iasi Vaslui Hirsova Eforie 99 80 Ex: A* for Romania (Simulated queue) Expanded: Arad/366 (0+366), Sibiu/393 (140+253), RimnicuVilcea/413 (220+193), Fagaras/415 (239+176), Pitesti/417 (317+100), Children: Bucharest/418 (418+0), Craiova/615 (455+160), RimnicuVilcea/607 (414+193), Frontier: Arad/366 (0+366), Sibiu/393 (140+253), Timisoara/447 (118+329), Zerind/449 (75+374), Arad/646 (280+366), Fagaras/415 (239+176), Oradea/671 (291+380), RimnicuVilcea/413 (220+193), Craiova/526 (366+160), Pitesti/417 (317+100), Sibiu/553 (300+253), Bucharest/450 (450+0), Sibiu/591 (338+253), Bucharest/418 (418+0), Craiova/615 (455+160), RimnicuVilcea/607 (414+193) Straight-line dist to goal Arad 366 Bucharest 0 Craiova 160 Drobeta 242 Fagaras 176 Lugoj 244 Mehadia 241 Oradea 380 Pitesti 100 Rimnicu Vilcea 193 Sibiu 253 Timisoara 329 Zerind 374
  • 32. Ex: A* for Romania 86 98 142 92 87 90 85 101 211 138 146 97 120 75 70 111 118 140 151 71 75 Oradea Zerind Arad Timisoara Lugoj Mehadia Dobreta Sibiu Fagaras Rimnicu Vilcea Pitesti Cralova Bucharest Giurgiu Urziceni Neamt Iasi Vaslui Hirsova Eforie 99 80 Straight-line dist to goal Arad 366 Bucharest 0 Craiova 160 Drobeta 242 Fagaras 176 Lugoj 244 Mehadia 241 Oradea 380 Pitesti 100 Rimnicu Vilcea 193 Sibiu 253 Timisoara 329 Zerind 374
  • 33. Shorter, more expensive path remains on queue Cheaper path will be found & returned 86 98 142 92 87 90 85 101 211 138 146 97 120 75 70 111 118 140 151 71 75 Oradea Zerind Arad Timisoara Lugoj Mehadia Dobreta Sibiu Fagaras Rimnicu Vilcea Pitesti Cralova Bucharest Giurgiu Urziceni Neamt Iasi Vaslui Hirsova Eforie 99 80 Ex: A* for Romania (Simulated queue) Expanded: Arad/366 (0+366), Sibiu/393 (140+253), RimnicuVilcea/413 (220+193), Fagaras/415 (239+176), Pitesti/417 (317+100), Bucharest/418 (418+0) Children: None (goal test succeeds) Frontier: Arad/366 (0+366), Sibiu/393 (140+253), Timisoara/447 (118+329), Zerind/449 (75+374), Arad/646 (280+366), Fagaras/415 (239+176), Oradea/671 (291+380), RimnicuVilcea/413 (220+193), Craiova/526 (366+160), Pitesti/417 (317+100), Sibiu/553 (300+253), Bucharest/450 (450+0), Sibiu/591 (338+253), Bucharest/418 (418+0), Craiova/615 (455+160), RimnicuVilcea/607 (414+193) Straight-line dist to goal Arad 366 Bucharest 0 Craiova 160 Drobeta 242 Fagaras 176 Lugoj 244 Mehadia 241 Oradea 380 Pitesti 100 Rimnicu Vilcea 193 Sibiu 253 Timisoara 329 Zerind 374
  • 34. 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=fi , where fi < fi+1 85 Oradea Zerind Arad Timisoara Lugoj Mehadia Dobreta Sibiu Fagaras Rimnicu Vilcea Pitesti Cralova Bucharest Giurgiu Urziceni Neamt Iasi Vaslui Hirsova Eforie
  • 35. Properties of A* search • Complete? Yes – Unless infinitely many nodes with f < f(G) – Cannot happen if step-cost ¸ ² > 0 • Time/Space? O(bm ) – Except if |h(n) – h*(n)| · O( log h*(n) ) • 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
  • 36. Optimality of A* • 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
  • 37. 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? • 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
  • 38. RBFS: BFS changes its mind ery often in practice. his is because the =g+h become more ccurate (less optimistic) s we approach the goal. ence, higher level nodes ave smaller f-values and ill be explored first. roblem: We should keep n memory whatever we can. best alternative over frontier nodes, which are not children: i.e. do I want to back up?
  • 39. 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. • 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 A solution is not reachable if a single path from root to goal does not fit in memory
  • 40. function SMA*(problem) returns a solution sequence inputs: problem, a problem static: Queue, a queue of nodes ordered by f-cost Queue  MAKE-QUEUE({MAKE-NODE(INITIAL-STATE[problem])}) loop do 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 SMA* Pseudocode Note: not in 2nd edition of R&N
  • 41. 24+0=24 A B G C D E F H J I K 0+12=12 10+5=15 20+5=25 30+5=35 20+0=20 30+0=30 8+5=13 16+2=18 24+0=24 24+5=29 10 8 10 10 10 10 8 16 8 8 g+h = f (Example with 3-node memory) Progress of SMA*. Each node is labeled with its current f-cost. Values in parentheses show the value of the best forgotten descendant. Algorithm can tell you when best solution found within memory constraint is optimal or not. ☐ = goal Search space maximal depth is 3, since memory limit is 3. This branch is now useless. best forgotten node A 12 A B 12 15 A B G 13 15 13 H  18 13 A G 13[15] A B G 15 15 24  A B C 15[24] 15 25 A B D 8 20 20[24] 20[] A G 24[] I 15[15] 24 best estimated solution so far for that node Simple memory-bounded A* (SMA*)
  • 42. 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 for this start & goal: 26 • Two commonly used heuristics – h1: the number of misplaced tiles – h2: sum of the distances of the tiles from their goal (“Manhattan distance”) h1(s) = 8 h2(s) = 3+1+2+2+2+3+3+2 = 18
  • 43. 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 • Ex: 8-Puzzle / sliding tiles – h1: the number of misplaced tiles – h2: sum of the distances of the tiles from their goal
  • 44. 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 number of nodes expanded Average over 100 randomly generated 8-puzzle problems h1 = number of tiles in the wrong position h2 = sum of Manhattan distances Ex: 8-Puzzle
  • 45. Effective branching factor, b* • Let A* generate N nodes to find a goal at depth d – Effective branching b* is the branching factor a uniform tree of depth d would have in order to contain 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
  • 46. Designing heuristics • Often constructed via problem relaxations – A problem with fewer restrictions on actions – Cost of an optimal solution to a relaxed problem is an admissible heuristic for the original problem • Ex: 8-Puzzle – Relax rules so a tile can move anywhere: h1(n) – Relax rules so tile can move to any adjacent square: h2(n) • A useful way to generate heuristics – Ex: ABSOLVER (Prieditis 1993) discovered the first useful heuristic for the Rubik’s cube
  • 47. 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
  • 48. 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…) – Read R&N Ch 4 before next lecture
  • 49. 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