CS 767: Advanced Topics in AI
Search
Instructor: Dr. Muhammad Aqib
University Institute of Information Technology
PMAS-Arid Agriculture University Rawalpindi
(slides courtesy Dan Klein, Pieter Abbeel, University of California, Berkeley)
Informed Search
o Uninformed Search
o DFS
o BFS
o UCS
▪ Informed Search
▪ Heuristics
▪ Greedy Search
▪ A* Search
▪ Graph Search
2
Search Heuristics
▪ A heuristic is:
▪ A function that estimates how close a state is to a goal
▪ Designed for a particular search problem
▪ Pathing?
▪ Examples: Manhattan distance, Euclidean distance for
pathing
10
5
11.
2
3
Example: Heuristic Function
h(x)
4
Greedy Search
5
Greedy Search
o Expand the node that seems closest…
o Is it optimal?
o No. Resulting path to Bucharest is not the shortest!
6
Greedy Search
o Strategy: expand a node that you think is
closest to a goal state
o Heuristic: estimate of distance to nearest
goal for each state
o A common case:
o Best-first takes you straight to the (wrong)
goal
o Worst-case: like a badly-guided DFS
…
b
…
b
[Demo: contours greedy empty (L3D1)]
[Demo: contours greedy pacman small maze (L3D4)]
7
A* Search
8
A* Search
UCS Greedy
A*
9
Combining UCS and Greedy
o Uniform-cost orders by path cost, or backward cost g(n)
o Greedy orders by goal proximity, or forward cost h(n)
o A* Search orders by the sum: f(n) = g(n) + h(n)
S a d
b
G
h=5
h=6
h=2
1
8
1
1
2
h=6
h=0
c
h=7
3
e h=1
1
Example: Teg
S
a
b
c
e
d
d
G
G
g = 0
h=6
g = 1
h=5
g = 2
h=6
g = 3
h=7
g = 4
h=2
g = 6
h=0
g = 9
h=1
g = 10
h=2
g = 12
h=0
10
When should A* terminate?
o Should we stop when we enqueue a goal?
o No: only stop when we dequeue a goal
S
B
A
G
2
3
2
2
h = 1
h = 2
h = 0
h = 3
S 0 3 3
g h +
S->A 2 2 4
S->B 2 1 3
S->B->G 5 0 5
S->A->G 4 0 4
11
Is A* Optimal?
o What went wrong?
o Actual bad goal cost < estimated good goal cost
o We need estimates to be less than actual costs!
A
G
S
1 3
h = 6
h = 0
5
h = 7
g h +
S 0 7 7
S->A 1 6 7
S->G 5 0 5
12
Admissible Heuristics
Idea: Admissibility
Inadmissible (pessimistic) heuristics
break optimality by trapping
good plans on the fringe
Admissible (optimistic) heuristics
slow down bad plans but
never outweigh true costs
Admissible Heuristics
o A heuristic h is admissible (optimistic) if:
where is the true cost to a nearest goal
o Examples:
o Coming up with admissible heuristics is most of what’s
involved in using A* in practice.
15 11.5
0.0
Optimality of A* Tree Search
Optimality of A* Tree Search
Assume:
o A is an optimal goal node
o B is a suboptimal goal node
o h is admissible
Claim:
o A will exit the fringe before B
…
Optimality of A* Tree Search: Blocking
Proof:
o Imagine B is on the fringe
o Some ancestor n of A is on the
fringe, too (maybe A!)
o Claim: n will be expanded before B
1. f(n) is less or equal to f(A)
Definition of f-cost
Admissibility of h
…
h = 0 at a goal
Optimality of A* Tree Search: Blocking
Proof:
o Imagine B is on the fringe
o Some ancestor n of A is on the
fringe, too (maybe A!)
o Claim: n will be expanded before B
1. f(n) is less or equal to f(A)
2. f(A) is less than f(B)
B is suboptimal
h = 0 at a goal
…
Optimality of A* Tree Search: Blocking
Proof:
o Imagine B is on the fringe
o Some ancestor n of A is on the
fringe, too (maybe A!)
o Claim: n will be expanded before B
1. f(n) is less or equal to f(A)
2. f(A) is less than f(B)
3. n expands before B
o All ancestors of A expand before B
o A expands before B
o A* search is optimal
…
Properties of A*
…
b
…
b
Uniform-Cost A*
UCS vs A* Contours
o Uniform-cost expands equally in
all “directions”
o A* expands mainly toward the
goal, but does hedge its bets to
ensure optimality
Start Goal
Start Goal
[Demo: contours UCS / greedy / A* empty (L3D1)]
[Demo: contours A* pacman small maze (L3D5)]
Graph Search
Tree Search: Extra Work!
o Failure to detect repeated states can cause exponentially more
work.
Search Tree
State Graph
Graph Search
o In BFS, for example, we shouldn’t bother expanding the circled nodes
(why?)
S
a
b
d p
a
c
e
p
h
f
r
q
q c G
a
q
e
p
h
f
r
q
q c G
a
Graph Search
o Idea: never expand a state twice
o How to implement:
o Tree search + set of expanded states (“closed set”)
o Expand the search tree node-by-node, but…
o Before expanding a node, check to make sure its state has
never been expanded before
o If not new, skip it, if new add to closed set
o Important: store the closed set as a set, not a list
o Can graph search wreck completeness? Why/why not?
o How about optimality?
A* Graph Search Gone Wrong?
S
A
B
C
G
1
1
1
2
3
h=2
h=1
h=4
h=1
h=0
S
(0+2)
A
(1+4)
B
(1+1)
C
(2+1)
G
(5+0)
C
(3+1)
G
(6+0)
State space
graph
Search
tree
Closed
Set:
S B C A
Consistency of Heuristics
o Main idea: estimated heuristic costs ≤ actual costs
o Admissibility: heuristic cost ≤ actual cost to goal
h(A) ≤ actual cost from A to G
o Consistency: heuristic “arc” cost ≤ actual cost for each
arc
h(A) – h(C) ≤ cost(A to C)
o Consequences of consistency:
o The f value along a path never decreases
h(A) ≤ cost(A to C) + h(C)
o A* graph search is optimal
3
A
C
G
h=4 h=1
1
h=2
Optimality of A* Search
o With a admissible heuristic, Tree A* is optimal.
o With a consistent heuristic, Graph A* is optimal.
o See slides, also video lecture from past years for details.
o With h=0, the same proof shows that UCS is optimal.
A*: Summary
A*: Summary
o A* uses both backward costs and (estimates of) forward
costs
o A* is optimal with admissible / consistent heuristics
o Heuristic design is key: often use relaxed problems
Tree Search Pseudo-Code
Graph Search Pseudo-Code
The One Queue
o All these search algorithms are
the same except for fringe
strategies
o Conceptually, all fringes are priority
queues (i.e. collections of nodes
with attached priorities)
o Practically, for DFS and BFS, you
can avoid the log(n) overhead from
an actual priority queue, by using
stacks and queues
o Can even code one implementation
that takes a variable queuing object
Search and Models
o Search operates over
models of the world
o The agent doesn’t
actually try all the plans
out in the real world!
o Planning is all “in
simulation”
o Your search is only as
good as your models…
Search Gone Wrong?
41
CS767_Lecture_03.pptx

CS767_Lecture_03.pptx

  • 1.
    CS 767: AdvancedTopics in AI Search Instructor: Dr. Muhammad Aqib University Institute of Information Technology PMAS-Arid Agriculture University Rawalpindi (slides courtesy Dan Klein, Pieter Abbeel, University of California, Berkeley)
  • 2.
    Informed Search o UninformedSearch o DFS o BFS o UCS ▪ Informed Search ▪ Heuristics ▪ Greedy Search ▪ A* Search ▪ Graph Search 2
  • 3.
    Search Heuristics ▪ Aheuristic is: ▪ A function that estimates how close a state is to a goal ▪ Designed for a particular search problem ▪ Pathing? ▪ Examples: Manhattan distance, Euclidean distance for pathing 10 5 11. 2 3
  • 4.
  • 5.
  • 6.
    Greedy Search o Expandthe node that seems closest… o Is it optimal? o No. Resulting path to Bucharest is not the shortest! 6
  • 7.
    Greedy Search o Strategy:expand a node that you think is closest to a goal state o Heuristic: estimate of distance to nearest goal for each state o A common case: o Best-first takes you straight to the (wrong) goal o Worst-case: like a badly-guided DFS … b … b [Demo: contours greedy empty (L3D1)] [Demo: contours greedy pacman small maze (L3D4)] 7
  • 8.
  • 9.
  • 10.
    Combining UCS andGreedy o Uniform-cost orders by path cost, or backward cost g(n) o Greedy orders by goal proximity, or forward cost h(n) o A* Search orders by the sum: f(n) = g(n) + h(n) S a d b G h=5 h=6 h=2 1 8 1 1 2 h=6 h=0 c h=7 3 e h=1 1 Example: Teg S a b c e d d G G g = 0 h=6 g = 1 h=5 g = 2 h=6 g = 3 h=7 g = 4 h=2 g = 6 h=0 g = 9 h=1 g = 10 h=2 g = 12 h=0 10
  • 11.
    When should A*terminate? o Should we stop when we enqueue a goal? o No: only stop when we dequeue a goal S B A G 2 3 2 2 h = 1 h = 2 h = 0 h = 3 S 0 3 3 g h + S->A 2 2 4 S->B 2 1 3 S->B->G 5 0 5 S->A->G 4 0 4 11
  • 12.
    Is A* Optimal? oWhat went wrong? o Actual bad goal cost < estimated good goal cost o We need estimates to be less than actual costs! A G S 1 3 h = 6 h = 0 5 h = 7 g h + S 0 7 7 S->A 1 6 7 S->G 5 0 5 12
  • 13.
  • 14.
    Idea: Admissibility Inadmissible (pessimistic)heuristics break optimality by trapping good plans on the fringe Admissible (optimistic) heuristics slow down bad plans but never outweigh true costs
  • 15.
    Admissible Heuristics o Aheuristic h is admissible (optimistic) if: where is the true cost to a nearest goal o Examples: o Coming up with admissible heuristics is most of what’s involved in using A* in practice. 15 11.5 0.0
  • 16.
    Optimality of A*Tree Search
  • 17.
    Optimality of A*Tree Search Assume: o A is an optimal goal node o B is a suboptimal goal node o h is admissible Claim: o A will exit the fringe before B …
  • 18.
    Optimality of A*Tree Search: Blocking Proof: o Imagine B is on the fringe o Some ancestor n of A is on the fringe, too (maybe A!) o Claim: n will be expanded before B 1. f(n) is less or equal to f(A) Definition of f-cost Admissibility of h … h = 0 at a goal
  • 19.
    Optimality of A*Tree Search: Blocking Proof: o Imagine B is on the fringe o Some ancestor n of A is on the fringe, too (maybe A!) o Claim: n will be expanded before B 1. f(n) is less or equal to f(A) 2. f(A) is less than f(B) B is suboptimal h = 0 at a goal …
  • 20.
    Optimality of A*Tree Search: Blocking Proof: o Imagine B is on the fringe o Some ancestor n of A is on the fringe, too (maybe A!) o Claim: n will be expanded before B 1. f(n) is less or equal to f(A) 2. f(A) is less than f(B) 3. n expands before B o All ancestors of A expand before B o A expands before B o A* search is optimal …
  • 21.
  • 22.
    UCS vs A*Contours o Uniform-cost expands equally in all “directions” o A* expands mainly toward the goal, but does hedge its bets to ensure optimality Start Goal Start Goal [Demo: contours UCS / greedy / A* empty (L3D1)] [Demo: contours A* pacman small maze (L3D5)]
  • 23.
  • 24.
    Tree Search: ExtraWork! o Failure to detect repeated states can cause exponentially more work. Search Tree State Graph
  • 25.
    Graph Search o InBFS, for example, we shouldn’t bother expanding the circled nodes (why?) S a b d p a c e p h f r q q c G a q e p h f r q q c G a
  • 26.
    Graph Search o Idea:never expand a state twice o How to implement: o Tree search + set of expanded states (“closed set”) o Expand the search tree node-by-node, but… o Before expanding a node, check to make sure its state has never been expanded before o If not new, skip it, if new add to closed set o Important: store the closed set as a set, not a list o Can graph search wreck completeness? Why/why not? o How about optimality?
  • 27.
    A* Graph SearchGone Wrong? S A B C G 1 1 1 2 3 h=2 h=1 h=4 h=1 h=0 S (0+2) A (1+4) B (1+1) C (2+1) G (5+0) C (3+1) G (6+0) State space graph Search tree Closed Set: S B C A
  • 28.
    Consistency of Heuristics oMain idea: estimated heuristic costs ≤ actual costs o Admissibility: heuristic cost ≤ actual cost to goal h(A) ≤ actual cost from A to G o Consistency: heuristic “arc” cost ≤ actual cost for each arc h(A) – h(C) ≤ cost(A to C) o Consequences of consistency: o The f value along a path never decreases h(A) ≤ cost(A to C) + h(C) o A* graph search is optimal 3 A C G h=4 h=1 1 h=2
  • 29.
    Optimality of A*Search o With a admissible heuristic, Tree A* is optimal. o With a consistent heuristic, Graph A* is optimal. o See slides, also video lecture from past years for details. o With h=0, the same proof shows that UCS is optimal.
  • 30.
  • 31.
    A*: Summary o A*uses both backward costs and (estimates of) forward costs o A* is optimal with admissible / consistent heuristics o Heuristic design is key: often use relaxed problems
  • 32.
  • 33.
  • 34.
    The One Queue oAll these search algorithms are the same except for fringe strategies o Conceptually, all fringes are priority queues (i.e. collections of nodes with attached priorities) o Practically, for DFS and BFS, you can avoid the log(n) overhead from an actual priority queue, by using stacks and queues o Can even code one implementation that takes a variable queuing object
  • 35.
    Search and Models oSearch operates over models of the world o The agent doesn’t actually try all the plans out in the real world! o Planning is all “in simulation” o Your search is only as good as your models…
  • 36.