程序代写CS代考 algorithm Informed search algorithms

Informed search algorithms
Chapter 4, Sections 1–2
Chapter 4, Sections 1–2 1

♦ Best-first search ♦ A∗ search
♦ Heuristics
Outline
Chapter 4, Sections 1–2 2

Review: Tree search
function Tree-Search( problem, fringe) returns a solution, or failure fringe ← Insert(Make-Node(Initial-State[problem]), fringe) loop do
if fringe is empty then return failure
node ← Remove-Front(fringe)
if Goal-Test[problem] applied to State(node) succeeds return node fringe ← InsertAll(Expand(node, problem), fringe)
A strategy is defined by picking the order of node expansion
Chapter 4, Sections 1–2 3

Idea: use an evaluation function for each node – estimate of “desirability”
⇒ Expand most desirable unexpanded node Implementation:
Best-first search
fringe is a queue sorted in decreasing order of desirability
Special cases: greedy search
A∗ search
Chapter 4, Sections 1–2 4

Arad
140
118
99 80
75
Timisoara
Rimnicu Vilcea
111
97 146
Pitesti
Dobreta
120
Oradea
Straight−line distance to Bucharest
71
Zerind 151
Neamt
Arad Bucharest Craiova Dobreta
Eforie
Fagaras Giurgiu Hirsova
Iasi
Lugoj Mehadia Neamt
Oradea
Pitesti Rimnicu Vilcea Sibiu Timisoara Urziceni
366
0
160
242
161
178
77
151
226
244
241
234
380
98
193
253
329
80
199
374
70
98
75
138
86
Lugoj
Romania with step costs in km
Mehadia
Urziceni Bucharest
Sibiu
Fagaras
Craiova
Eforie
Vaslui Zerind
101
85
Hirsova
211
142
90
Giurgiu
87
Iasi
92
Vaslui
Chapter 4, Sections 1–2
5

Evaluation function h(n) (heuristic)
= estimate of cost from n to the closest goal
Greedy search
E.g., hSLD(n) = straight-line distance from n to Bucharest
Greedy search expands the node that appears to be closest to goal
Chapter 4, Sections 1–2 6

Greedy search example
Arad 366
Chapter 4, Sections 1–2 7

Sibiu 253
Timisoara Zerind 329 374
Greedy search example
Arad
Chapter 4, Sections 1–2 8

Arad Fagaras 366 176
Oradea 380
Rimnicu Vilcea 193
Sibiu
Timisoara
Greedy search example
Arad
Zerind 329 374
Chapter 4, Sections 1–2 9

Arad 366
Fagaras
Oradea 380
Rimnicu Vilcea 193
Sibiu 253
Bucharest 0
Sibiu
Timisoara
Greedy search example
Arad
Zerind 329 374
Chapter 4, Sections 1–2 10

Complete??
Properties of greedy search
Chapter 4, Sections 1–2 11

Properties of greedy search
Complete?? No–can get stuck in loops, e.g., with Oradea as goal, Iasi → Neamt → Iasi → Neamt →
Complete in finite space with repeated-state checking
Time??
Chapter 4, Sections 1–2 12

Properties of greedy search
Complete?? No–can get stuck in loops, e.g., Iasi → Neamt → Iasi → Neamt →
Complete in finite space with repeated-state checking
Time?? O(bm), but a good heuristic can give dramatic improvement Space??
Chapter 4, Sections 1–2 13

Properties of greedy search
Complete?? No–can get stuck in loops, e.g., Iasi → Neamt → Iasi → Neamt →
Complete in finite space with repeated-state checking
Time?? O(bm), but a good heuristic can give dramatic improvement Space?? O(bm)—keeps all nodes in memory
Optimal??
Chapter 4, Sections 1–2 14

Properties of greedy search
Complete?? No–can get stuck in loops, e.g., Iasi → Neamt → Iasi → Neamt →
Complete in finite space with repeated-state checking
Time?? O(bm), but a good heuristic can give dramatic improvement Space?? O(bm)—keeps all nodes in memory
Optimal?? No
Chapter 4, Sections 1–2 15

Idea: avoid expanding paths that are already expensive Evaluation function f(n) = g(n) + h(n)
g(n) = cost so far to reach n
h(n) = estimated cost to goal from n
f(n) = estimated total cost of path through n to goal
A∗ search uses an admissible heuristic
i.e., h(n) ≤ h∗(n) where h∗(n) is the true cost from n. (Also require h(n) ≥ 0, so h(G) = 0 for any goal G.)
E.g., hSLD(n) never overestimates the actual road distance Theorem: A∗ search is optimal
A∗ search
Chapter 4, Sections 1–2 16

A∗ search example
Arad 366=0+366
Chapter 4, Sections 1–2 17

Sibiu 393=140+253
Timisoara Zerind 447=118+329 449=75+374
A∗ search example Arad
Chapter 4, Sections 1–2 18

Arad Fagaras
Oradea Rimnicu Vilcea
Sibiu
Timisoara
Zerind
646=280+366 415=239+176 671=291+380 413=220+193
A∗ search example Arad
447=118+329
449=75+374
Chapter 4, Sections 1–2 19

Arad Fagaras
646=280+366 415=239+176 671=291+380
Sibiu
Timisoara
Zerind
Oradea
Rimnicu Vilcea
A∗ search example Arad
Craiova
526=366+160 417=317+100 553=300+253
Pitesti
Sibiu
447=118+329
449=75+374
Chapter 4, Sections 1–2 20

Arad 646=280+366
Fagaras
Oradea 671=291+380
Rimnicu Vilcea
Sibiu
Timisoara 447=118+329
Zerind 449=75+374
A∗ search example Arad
Sibiu
591=338+253 450=450+0 526=366+160 417=317+100 553=300+253
Bucharest
Craiova
Pitesti
Sibiu
Chapter 4, Sections 1–2 21

Arad
Fagaras
Oradea
Rimnicu Vilcea
646=280+366
671=291+380
Sibiu
Bucharest
Craiova
Pitesti
Sibiu
Sibiu
Timisoara
Zerind
591=338+253 450=450+0 526=366+160
553=300+253
A∗ search example Arad
Bucharest
Craiova
Rimnicu Vilcea
418=418+0 615=455+160 607=414+193
447=118+329
449=75+374
Chapter 4, Sections 1–2 22

Optimality of A∗ (standard proof )
Suppose some suboptimal goal G2 has been generated and is in the queue.
Let n be an unexpanded node on a shortest path to an optimal goal G1. Start
f(G2) = g(G2) > g(G1)
since h(G2) = 0
n
G G2
since G2 is suboptimal since h is admissible
≥ f(n)
Since f(G2) > f(n), A∗ will never select G2 for expansion
Chapter 4, Sections 1–2 23

Optimality of A∗ (more useful) Lemma: A∗ expands nodes in order of increasing f value∗
Gradually adds “f-contours” of nodes (cf. breadth-first adds layers) Contour i has all nodes with f = fi, where fi < fi+1 A I T R L Z N 380 S O D E M U B 400 C F P 420 G V H Chapter 4, Sections 1–2 24 Complete?? Properties of A∗ Chapter 4, Sections 1–2 25 Properties of A∗ Complete?? Yes, unless there are infinitely many nodes with f ≤ f(G) Time?? Chapter 4, Sections 1–2 26 Properties of A∗ Complete?? Yes, unless there are infinitely many nodes with f ≤ f(G) Time?? Exponential in [relative error in h × length of soln.] Space?? Chapter 4, Sections 1–2 27 Properties of A∗ Complete?? Yes, unless there are infinitely many nodes with f ≤ f(G) Time?? Exponential in [relative error in h × length of soln.] Space?? Keeps all nodes in memory Optimal?? Chapter 4, Sections 1–2 28 A∗ expands all nodes with f(n) < C∗ A∗ expands some nodes with f(n) = C∗ A∗ expands no nodes with f(n) > C∗
Properties of A∗
Complete?? Yes, unless there are infinitely many nodes with f ≤ f(G) Time?? Exponential in [relative error in h × length of soln.]
Space?? Keeps all nodes in memory
Optimal?? Yes—cannot expand fi+1 until fi is finished
Chapter 4, Sections 1–2 29

A heuristic is consistent if h(n) ≤ c(n, a, n′) + h(n′)
n c(n,a,n’)
If h is consistent, we have
Proof of lemma: Consistency
f(n′) = g(n′)+h(n′)
= g(n)+c(n,a,n′)+h(n′) ≥ g(n) + h(n)
= f(n)
h(n) n’
I.e., f(n) is nondecreasing along any path.
h(n’)
G
Chapter 4, Sections 1–2 30

E.g., for the 8-puzzle:
h1(n) = number of misplaced tiles h2(n) = total Manhattan distance
h1(S) =?? h2(S) =??
Admissible heuristics
(i.e., no. of squares from desired location of each tile)
7 2 4 51 2 3
56456
831 78
Start State Goal State
Chapter 4, Sections 1–2 31

E.g., for the 8-puzzle:
h1(n) = number of misplaced tiles h2(n) = total Manhattan distance
h1(S) =?? 6
h2(S) =?? 4+0+3+3+1+0+2+1 = 14
Admissible heuristics
(i.e., no. of squares from desired location of each tile)
7 2 4 51 2 3
56456
831 78
Start State Goal State
Chapter 4, Sections 1–2 32

If h2(n) ≥ h1(n) for all n (both admissible) then h2 dominates h1 and is better for search
Typical search costs:
d = 14 d = 24
IDS = 3,473,941 nodes A∗(h1) = 539 nodes
A∗(h2) = 113 nodes
IDS ≈ 54,000,000,000 nodes A∗(h1) = 39,135 nodes A∗(h2) = 1,641 nodes
Given any admissible heuristics ha, hb, h(n) = max(ha(n), hb(n))
is also admissible and dominates ha, hb
Dominance
Chapter 4, Sections 1–2 33

Relaxed problems
Admissible heuristics can be derived from the exact solution cost of a relaxed version of the 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
Key point: the optimal solution cost of a relaxed problem
is no greater than the optimal solution cost of the real problem
Chapter 4, Sections 1–2 34

Relaxed problems contd.
Well-known example: travelling salesperson problem (TSP) Find the shortest tour visiting all cities exactly once
Minimum spanning tree can be computed in O(n2) and is a lower bound on the shortest (open) tour
Chapter 4, Sections 1–2 35

Heuristic functions estimate costs of shortest paths
Good heuristics can dramatically reduce search cost
Greedy best-first search expands lowest h – incomplete and not always optimal
Summary
A∗ search expands lowest g + h
– complete and optimal
– also optimally efficient (up to tie-breaks, for forward search)
Admissible heuristics can be derived from exact solution of relaxed problems
Chapter 4, Sections 1–2 36