AI.02a - Solving Problems by Searching - T
AI.02a - Solving Problems by Searching - T
by Searching
CONTENTS
Search Problems
Uninformed Search
Informed Search
Artificial Intelligence 2
Search problems
Artificial Intelligence 3
Problem solving agents
Artificial Intelligence 4
Well-defined problems and solutions
Artificial Intelligence 5
Example problems
Artificial Intelligence 6
Example problems
State space: A state description specifies the location of each of the eight tiles and the
blank in one of the nine squares.
Initial state: Any state
Actions: Left, Right, Up, or Down
Transition model: Given a state and action, this returns the resulting state
Goal test: check whether the state matches the goal configuration
Path cost: each step costs 1, so the path cost is the number of steps in the path.
Artificial Intelligence 7
Example problems
Artificial Intelligence 8
Example problems
States:
Initial state:
Actions:
Transition model:
Goal test:
Path cost:
Artificial Intelligence 9
Real-world problems
Visit every city in Figure 3.2 at least once, starting and ending in Bucharest.
The traveling salesperson problem (TSP) is a touring problem in which each
city must be visited exactly once. The aim is to find the shortest tour.
Artificial Intelligence 10
…Problem solving agents
Problem solving agent function
Artificial Intelligence 11
Search Trees
a G d e p
b c
b c e h r q
e
d f a a h r p q f
S h
p q f q c G
p q r We construct both on
demand – and we construct q c G a
as little as possible.
a
Artificial Intelligence 12
Search Trees
Artificial Intelligence 13
…Search Trees
For each node n of the tree, we have a structure that contains four components:
n.STATE: the state in the state space to which the node corresponds;
n.PARENT: the node in the search tree that generated this node;
n.ACTION: the action that was applied to the parent to generate the node;
n.PATH-COST: the cost, traditionally denoted by g(n), of the path from the initial state
to the node, as indicated by the parent pointers.
The function CHILD-NODE takes a parent node and an action and returns the resulting child node
Artificial Intelligence 14
…Search Trees
Artificial Intelligence 15
…Search Trees
Artificial Intelligence 16
…Search Trees
Artificial Intelligence 17
…Search Trees
Artificial Intelligence 18
…Search Trees
Example a G
b c
e
d f
S h
p q r
Artificial Intelligence 19
…Search Trees
a G
Example b c
e
d f
S h
p q r
S s
sd
d e p se
sp
b c e h r q sdb
sdc
a a h r p q f sde
sdeh
p q f q c G
sder
a sderf
q c G
sderfc
a sderfG
Artificial Intelligence 20
…Search Trees
Artificial Intelligence 21
…Search Trees
Artificial Intelligence 22
…Search Trees
Artificial Intelligence 23
…Search Trees
Thiết kế giải thuật tìm kiếm
Giải thuật tìm kiếm được xác định bằng việc chọn trình tự phát triển
các nút trên cây
Giải thuật tìm kiếm được đánh giá theo các tiêu chí:
• Time? - Thờigian: - Số lượng các nút được sinh ra
• Space? - Bộ nhớ: Số lượng tối đa các nút đượclưu trong bộ nhớ
• Complete? - Hoàn chỉnh : Có đảm bảo tìm được một lời giải?
• Optimal ? - Tối ưu: Có đảm bảo tìm được lời giải với chi phí thấp nhất?
Search Problems
Uninformed Search
Informed Search
Artificial Intelligence 25
CONTENTS
Uninformed search
Depth-First Search (Tìm kiếm theo độ sâu )
Breadth-First Search (Tìm kiếm theo chiều rộng)
Iterative-Deepening Search (Tìm kiếm sâu dần)
Uniform-Cost Search (Tìm kiếm với chi phí cực tiểu)
Artificial Intelligence 26
Depth-First Search (DFS)
Artificial Intelligence 27
… Depth-First Search (DFS)
B C
D E F G
H I J K L M N O
GOAL
Artificial Intelligence 28
… Depth-First Search (DFS)
a G
b c
e
d f
S h
p q r
d e p
b c e h r q
a a h r p q f
p q f q c G
q c G a
a
Artificial Intelligence 29
…Depth-First Search (DFS)
Properties
• Complete: Guaranteed to find a solution if one exists?
• Optimal: Guaranteed to find the least cost path?
• Time complexity?
• Space complexity?
b 1 node
… b nodes
• Cartoon of search tree:
b2 nodes
• b is the branching factor
m tiers
• m is the maximum depth
• solutions at various depths
bm nodes
• Number of nodes in entire tree?
• 1 + b + b2 + …. bm = O(bm)
Artificial Intelligence 30
…Depth-First Search (DFS)
Properties
1 node
• What nodes DFS expand? b
… b nodes
• Some left prefix of the tree. b2 nodes
• Could process the whole tree! m tiers
• Time: If m is finite, takes time O(bm)
• Space: Only has siblings on path to root, so O(bm)
bm nodes
• complete?
• m could be infinite, so only if we prevent cycles
(more later)
• optimal?
• No, it finds the “leftmost” solution, regardless of
depth or cost
Artificial Intelligence 31
Breadth-First Search (BFS)
Artificial Intelligence 32
…Breadth-First Search (BFS)
Artificial Intelligence 33
…Breadth-First Search (BFS)
Artificial Intelligence 34
…Breadth-First Search (BFS)
Artificial Intelligence 35
…Breadth-First Search (BFS)
Artificial Intelligence 36
…Breadth-First Search (BFS)
Artificial Intelligence 37
…Breadth-First Search (BFS)
a G
b c
e
d f
S h
p q r
d e p
Search
b c e h r q
Tiers
a a h r p q f
p q f q c G
q c G a
a
Artificial Intelligence 38
…Breadth-First Search (BFS)
Artificial Intelligence 39
…Breadth-First Search (BFS)
Artificial Intelligence 40
…Breadth-First Search (BFS)
Artificial Intelligence 41
…Breadth-First Search (BFS)
Artificial Intelligence 42
…Breadth-First Search (BFS)
Artificial Intelligence 43
…Breadth-First Search (BFS)
Artificial Intelligence 44
…Breadth-First Search (BFS)
Artificial Intelligence 45
…Breadth-First Search (BFS)
Artificial Intelligence 46
…Breadth-First Search (BFS)
Artificial Intelligence 47
…Breadth-First Search (BFS)
Artificial Intelligence 48
…Breadth-First Search (BFS)
Artificial Intelligence 49
…Breadth-First Search (BFS)
Artificial Intelligence 50
…Breadth-First Search (BFS)
Artificial Intelligence 51
…Breadth-First Search (BFS)
Artificial Intelligence 52
…Breadth-First Search (BFS)
Artificial Intelligence 53
…Breadth-First Search (BFS)
Properties
complete?
• s must be finite if a solution exists, so yes! bm nodes
optimal?
• Only if costs are all 1 (more on costs later)
Artificial Intelligence 54
…Breadth-First Search (BFS)
DFS vs BFS
Artificial Intelligence 55
Iterative-Deepening Search (IDS)
Artificial Intelligence 56
…Iterative-Deepening Search (IDS)
IDS: d = 0
Artificial Intelligence 57
…Iterative-Deepening Search (IDS)
IDS: d = 1
B C
Artificial Intelligence 58
…Iterative-Deepening Search (IDS)
IDS: d = 2
B C
D E F G
Artificial Intelligence 59
…Iterative-Deepening Search (IDS)
IDS: d = 3
B C
D E F G
H I J K L M
GOAL
Artificial Intelligence 60
…Iterative-Deepening Search (IDS)
Artificial Intelligence 61
…Iterative-Deepening Search (IDS)
Properties
Complete
• Có
Time:
• (d+1).b0 + d.b1 + (d-1).b2+ … +bd = O(bd)
Space:
• O(b.d)
Optimal
• Có - nếu chi phí cho mỗi buớc = 1
Artificial Intelligence 62
Uniform Cost Search(UCS)
(Djikstra’s algorithm)
Artificial Intelligence 63
…Uniform Cost Search(UCS)
Artificial Intelligence 64
…Uniform Cost Search(UCS)
Artificial Intelligence 65
…Uniform Cost Search(UCS)
Artificial Intelligence 66
…Uniform Cost Search(UCS)
Artificial Intelligence 67
…Uniform Cost Search(UCS)
Artificial Intelligence 68
…Uniform Cost Search(UCS)
Artificial Intelligence 69
…Uniform Cost Search(UCS)
Artificial Intelligence 70
…Uniform Cost Search(UCS)
Artificial Intelligence 71
…Uniform Cost Search(UCS)
Artificial Intelligence 72
…Uniform Cost Search(UCS)
2 a G
b c
1 8 2
2 e
3 d f
9 2
S h 8
1
1 p q r
15
S 0
d 3 e 9 p 1
b 4 c e 5 h 17 r 11 q 16
11
Cost a 6 a h 13 r 7 p q f
contours
p q f 8 q c G
q 11 c G 10 a
• Contours show equal cost.
a
Artificial Intelligence 73
…Uniform Cost Search(UCS)
Artificial Intelligence 74
…Uniform Cost Search(UCS)
Properties
Artificial Intelligence 75
…Uniform Cost Search(UCS)
Uniform Cost Issues
• The bad:
• Explores options in every “direction”
• No information about goal location
Start Goal
• We’ll fix that soon!
Artificial Intelligence 76
Uninformed Search
Artificial Intelligence 77
CONTENTS
Search Problems
Uninformed Search
Informed Search
Artificial Intelligence 78
CONTENTS
Artificial Intelligence 79
Heuristic Function
A heuristic is
A function that estimates how close a state is to a goal
Designed for a particular search problem
10
5
11.2
Artificial Intelligence 80
…Heuristic Function
Example: Heuristic Function
h(x)
Artificial Intelligence 81
…Heuristic Function
h(u) = 8
= the number of misplaced tiles
(all of the eight tiles are out of position)
• h(u) = 3+1+2+2+2+3+3+2 = 18
= the sum of the distances of the tiles from their goal positions.
Artificial Intelligence 82
…Heuristic Function
Artificial Intelligence 83
Greedy Search
Artificial Intelligence 84
Greedy Search
b
…
A common case: b
…
• Best-first takes you straight to the (wrong) goal
Artificial Intelligence 85
…Greedy Search
h(x)
Artificial Intelligence 86
…Greedy Search
Artificial Intelligence 87
…Greedy Search
Artificial Intelligence 88
…Greedy Search
Artificial Intelligence 90
A* Search
Idea: Combining UCS and Greedy
• Uniform-Cost orders by path cost, or backward cost: g(n)
• Greedy orders by goal proximity, or forward cost: h(n)
• A* Search orders by the sum: f(n) = g(n) + h(n).
UCS Greedy
A*
Artificial Intelligence 91
A* Search
8 g=0
S h=6
g=1
e h=1 a
h=5
1
1 3 2 g=2 g=9
S a d G g=4
h=6 b d e h=1
h=2
h=6 1 h=5
h=2 h=0
1 g=3 g=6 g = 10
c b h=7 c G h=0 d h=2
h=7 h=6
g = 12
G h=0
A* Search orders by the sum: f(n) = g(n) + h(n)
Artificial Intelligence 92
…A* Search
Artificial Intelligence 93
…A* Search
Artificial Intelligence 94
…A* Search
Artificial Intelligence 95
…A* Search
Artificial Intelligence 96
…A* Search
Artificial Intelligence 97
…A* Search
h=2
g h+
2 A 2 S 033
S->A 224
S h=3 h=0 G
S->B 213
2 B 3 S->B->G 5 0 5
h=1
S->A->G 4 0 4
G in queue when B expanded.
No: only stop when we dequeue a goal
Artificial Intelligence 98
…A* Search
Is A* Optimal?
h=6
1 A 3
gh+
S 077
S h=7
G h=0
S->A 1 6 7
S->G 5 0 5
Examples:
15 11.5
Assume:
A is an optimal goal node
…
B is a suboptimal goal node
h is admissible
Claim:
A will exit the fringe before B
Definition of f-
cost
Admissibility of h
h = 0 at a goal
B is suboptimal
h = 0 at a goal
Proof:
…
Imagine B is on the fringe
Some ancestor n of A is on the fringe, too
(maybe A!)
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
All ancestors of A expand before B
A expands before B
A* search is optimal
Properties of A* Search
Uniform-Cost A*
b b
… …
366
15
Actions
Start State Goal State
• What are the states?
• How many states?
• What are the actions?
• How many successors from the start state?
Admissible heuristics?
• What should the costs be?
• With A*: a trade-off between quality of estimate and work per node
• As heuristics get closer to the true cost, you will expand fewer nodes but
usually do more work per node to compute the heuristic itself
A* vs. BFS
Comparison
• Video games
• Pathing / routing problems
• Resource planning problems
• Robot motion planning
• Language analysis
• Machine translation
• Speech recognition
•…
Thank You…!
Artificial Intelligence 118