0% found this document useful (0 votes)
80 views118 pages

AI.02a - Solving Problems by Searching - T

The document discusses problem solving using search algorithms in artificial intelligence. It defines the components of a well-defined problem, including the state space, initial state, actions, transition model, goal test, and path cost. Examples of problems discussed include the 8-queens problem, 8-puzzle, and traveling salesperson problem. Search trees are used to systematically search the state space to find a solution, where each node represents a path and child nodes are generated by applying actions. Fringe data structures like stacks and queues are used to organize nodes for exploration.

Uploaded by

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

AI.02a - Solving Problems by Searching - T

The document discusses problem solving using search algorithms in artificial intelligence. It defines the components of a well-defined problem, including the state space, initial state, actions, transition model, goal test, and path cost. Examples of problems discussed include the 8-queens problem, 8-puzzle, and traveling salesperson problem. Search trees are used to systematically search the state space to find a solution, where each node represents a path and child nodes are generated by applying actions. Fringe data structures like stacks and queues are used to organize nodes for exploration.

Uploaded by

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

Solving Problems

by Searching
CONTENTS

Search Problems

Uninformed Search

Informed Search

Artificial Intelligence 2
Search problems

Artificial Intelligence 3
Problem solving agents

Problem-solving agent in Artificial Intelligence is goal-based


agents that focus on goals, is one embodiment of a group of
algorithms, and techniques to solve a well-defined
problem.

Artificial Intelligence 4
Well-defined problems and solutions

A problem can be defined formally by the following components:


• State space: The set of all states reachable
• Initial state: It is the starting the agent towards the goal
• Actions: A description of the possible actions available to the agent
• Transition model: It describes what each action does
• Goal test: It determines if the given state is a goal state
• Path cost: It assigns a numeric cost to each path that follows the goal. The problem-
solving agent selects a cost function, which reflects its performance measure. An optimal
solution has the lowest path cost among all the solutions
A solution to a problem is an action sequence that leads from the initial
state to a goal state. Solution quality is measured by the path cost function
and an optimal solution has the lowest path cost among all solutions

Artificial Intelligence 5
Example problems

Example: the 8-queens problem


The goal of the 8-queens problem is to place
eight queens on a chessboard such that no
queen attacks any other.
• State space: Any arrangement of 8 queens on the
board
• Initial state: No queens on the board
• Actions: Add a queen to any empty square
• Transition model: Returns the board with a queen
added to the specified square
• Goal test: 8 queens are on the board, none attacked

Artificial Intelligence 6
Example problems

Example: the 8-puzzle

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

Example: the 8-puzzle

Artificial Intelligence 8
Example problems

Example: Traveling in Romania

 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

State Space Graphs vs. Search Trees

State Space Graph Search Tree


Each NODE in in the search
tree is an entire PATH in
the state space graph. S

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

State Space Graphs vs. Search Trees


Thay thế mỗi cạnh vô hướng bằng 2 cạnh có hướng
Loại bỏ các vòng lặp tồn tại trong đồ thị (để tránh không duyệt 2 lần đối với
một nút trong bất kỳ đường đi nào)

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

Fringe: kiểu LIFO/ FIFO

Hàm Expand tạo các nút mới


• Gán giá trị cho các thuộc
tính của nút mới

• Hàm Successor-Fn để tạo các


trạng thái tương ứng
với các nút mới đó

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
sd
d e p se
sp
b c e h r q sdb
sdc
a a h r p q f sde
sdeh
p q f q c G
sder
a sderf
q c G
sderfc
a sderfG

Artificial Intelligence 20
…Search Trees

Thiết kế giải thuật tìm kiếm

2 hướng tiếp cận thiết kế giải thuật tìm kiếm:

Hướng từ dữ liệu (Data-driven Search)


• Suy diễn tiến (forward chaining)

Hướng từ mục tiêu (Goal-driven Search)


• Suy diễn lùi (backward chaining)

Artificial Intelligence 21
…Search Trees

Thiết kế giải thuật tìm kiếm

Hướng từ dữ liệu (Data-driven Search)


Goal
• Việc tìm kiếm đi từ dữ liệu đến mục tiêu

• Thích hơp khi:


• Tất cả/một phần dữ liệu được cho từ đầu
• Có nhiều mục tiêu, nhưng chỉ có một số ít
các phép toán có thể áp dụng cho một
trạng thái bái toán
• Khó đưa ra một mục tiêu/giả thuyết ngay lúc đầu Data

Artificial Intelligence 22
…Search Trees

Thiết kế giải thuật tìm kiếm

Hướng từ mục tiêu (Goal-driven Search)


• Việc tìm kiếm đi từ mục tiêu về dữ liệu Goal
Thích hơp khi:
• Có thể đưa ra mục tiêu/giả thuyết ngay lúc đầu

• Có nhiều phép toán có thể áp dụng trên 1 trạng


thái bái toán
 sự bùng nổ số lượng các trạng thái

• Các dữ liệu của bài toán không được cho trước,


Data
hệ thống phải đạt được trong quá trình tìm kiếm

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?

 Độ phức tạp về thời gian và bộ nhớ được đánh giá bởi:


• b: Hệ số phân nhánh tối đa của cây tìm kiếm
• d: Độ sâu của lời giải có chi phí thấp nhất
• m: Độ sâu tối đa của không gian trạng thái (độ sâu của cây), có thể vô hạn
Artificial Intelligence 24
CONTENTS

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)

• Strategy: expand a deepest node first


• Implementation: Fringe is a LIFO stack

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)

• Strategy: expand a shallowest node first


• Implementation: Fringe is a FIFO queue

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

What nodes does BFS expand?


• Processes all nodes above shallowest solution b
1 node
• Let depth of shallowest solution be s … b nodes
s tiers
b2 nodes
Time: takes time O(bs)
Space: has roughly the last tier, so O(bs) bs nodes

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

DFS: need to get to the bottom. Memory constrained.


BFS: solutions not too far down.

Artificial Intelligence 55
Iterative-Deepening Search (IDS)

Idea: get DFS’s space advantage with


BFS’s time / shallow-solution advantages b

• Run a DFS with depth limit 1. If no solution…
• Run a DFS with depth limit 2. If no solution…
• Run a DFS with depth limit 3. …..

Isn’t that wastefully redundant?


• Generally most work happens in the lowest level
searched, so not so bad!

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)

• Like breadth first, but with costs.


Label with cumulative cost.

Artificial Intelligence 63
…Uniform Cost Search(UCS)

Strategy: expand a cheapest node first:


Fringe is a priority queue (priority: cumulative cost)

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

• What nodes does UCS expand?


• Processes all nodes with cost less than cheapest solution!
• If that solution costs C* and arcs cost at least  , then the “effective depth”
is roughly C*/
• Time: O(bC*/) (exponential in effective depth)
• Space: has roughly the last tier, so O(bC*/) b c1

• complete? c2
C*/ “tiers”
c3
• Assuming best solution has a finite cost
and minimum arc cost is positive, yes!
• optimal?
• Yes! (Proof next lecture via A*)

Artificial Intelligence 75
…Uniform Cost Search(UCS)
Uniform Cost Issues

• UCS explores increasing cost contours … c1


c2
c3
• The good: UCS is complete and optimal!

• The bad:
• Explores options in every “direction”
• No information about goal location

Start Goal
• We’ll fix that soon!

Artificial Intelligence 76
Uninformed Search

Phương pháp tìm kiếm sâu dần (IDS):


• Chi phí về thời gian chỉ nhiều hơn một chút so với các phương pháp khác
• Chi phí về không gian bộ nhớ ở mức hàm tuyến tính

Artificial Intelligence 77
CONTENTS

Search Problems

Uninformed Search

Informed Search

Artificial Intelligence 78
CONTENTS

Informed search (Heuristic search)


• Heuristic Function
• Greedy Search
A* Search

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

 Ex: Manhattan distance, Euclidean distance for pathing

10

5
11.2

Artificial Intelligence 80
…Heuristic Function
Example: Heuristic Function

h(x)
Artificial Intelligence 81
…Heuristic Function

Example: 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

Tree Search Pseudo-Code

Artificial Intelligence 83
Greedy Search

Artificial Intelligence 84
Greedy Search

b

Strategy: expand a node that (you think) is


closest to a goal state
• Heuristic: estimate of distance to nearest goal
for each state

A common case: b

• Best-first takes you straight to the (wrong) goal

Worst-case: heuristic wrong!!!

Artificial Intelligence 85
…Greedy Search

h(x)
Artificial Intelligence 86
…Greedy Search

Artificial Intelligence 87
…Greedy Search

Artificial Intelligence 88
…Greedy Search

optimal?: No. Resulting path


to Bucharest is not the shortest!

What can go wrong?


Greedy: 140 + 99 + 211 = 450
Better: 140+80+97+101 = 418
Artificial Intelligence 89
A* 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

Uniform-cost orders by path cost, or backward cost g(n)


Greedy orders by goal proximity, or forward cost h(n)

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

Should we stop when we enqueue a goal?

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

What went wrong?


• Actual bad goal cost < estimated good goal cost
We need estimates to be less than actual costs!
• Lower bounds for actual costs.
Artificial Intelligence 99
…A* Search
Admissible Heuristics

Inadmissible (pessimistic) heuristics Admissible (optimistic) heuristics


break optimality by trapping slow down bad plans but
good plans on the fringe never outweigh true costs
Artificial Intelligence 100
…A* Search
Admissible Heuristics

A heuristic h is admissible (optimistic) if:

where is the true cost to a nearest goal

Examples:

15 11.5

Coming up with admissible heuristics is most of what’s involved in


using A* in practice.
Artificial Intelligence 101
…A* Search
Optimality of A* Tree Search

Assume:
A is an optimal goal node

B is a suboptimal goal node
h is admissible

Claim:
A will exit the fringe before B

Artificial Intelligence 102


…A* Search
Optimality of A* Tree Search
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)

Definition of f-
cost
Admissibility of h
h = 0 at a goal

Artificial Intelligence 103


…A* Search
Optimality of A* Tree Search
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)

B is suboptimal
h = 0 at a goal

Artificial Intelligence 104


…A* Search
Optimality of A* Tree Search

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

Artificial Intelligence 105


…A* Search

Properties of A* Search

Uniform-Cost A*

b b
… …

UCS ragged based on cost.


A* narrows when near goal, heuristic
Artificial Intelligence 106
…A* Search
Creating Admissible Heuristics
• Most of the work in solving hard search problems optimally is in coming up
with admissible heuristics

• Often, admissible heuristics are solutions to relaxed problems, where new


actions are available

366

15

• Inadmissible heuristics are often useful too


Artificial Intelligence 107
…A* Search
Example: 8 Puzzle

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?

Artificial Intelligence 108


…A* Search
Example: 8 Puzzle

• Heuristic: Number of tiles misplaced


• Why is it admissible?
• h(start) = 8
• This is a relaxed-problem heuristic Start State Goal State

Average nodes expanded


when the optimal path has…
…4 steps …8 steps …12 steps
UCS 112 6,300 3.6 x 106
TILES 13 39 227
Statistics from Andrew Moore

Artificial Intelligence 109


…A* Search
Example: 8 Puzzle

• Tile could slide any direction at any


time, ignoring other tiles?

• Total Manhattan distance Start State Goal State

Average nodes expanded


• Why is it admissible? when the optimal path has…
…4 steps …8 steps …12 steps
• h(start) = 3 + 1 + 2 + … = 18 TILES 13 39 227
MANHATTAN 12 25 73
Statistics from Andrew Moore

Artificial Intelligence 110


…A* Search
Example: 8 Puzzle

• How about using the actual cost as a heuristic?


• Would it be admissible?
• Would we save on nodes expanded?
• What’s wrong with it?

• 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

Artificial Intelligence 111


…A* Search

A* vs. BFS

Comparison of the search costs for 8-puzzle


problems using breadth-first search, A* with:
• h1(misplaced tiles)
• h2 (Manhattan distance).

Data are averaged over 100 puzzles for


each solution length d from 6 to 28.

Artificial Intelligence 112


…A* Search
A* vs. UCS UCS

UCS expands equally in all “directions”

A*: expands mainly toward the goal,


but does hedge its bets to ensure optimality A*

Artificial Intelligence 113


…A* Search

Comparison

Uniform Cost Greedy A*

Artificial Intelligence 114


…A* Search
A* Applications

• Video games
• Pathing / routing problems
• Resource planning problems
• Robot motion planning
• Language analysis
• Machine translation
• Speech recognition
•…

Artificial Intelligence 115


…A* Search
Summary

• A* uses both backward costs and (estimates of) forward costs

• A* is optimal with admissible / consistent heuristics

• Heuristic design is key: often use relaxed problems

Artificial Intelligence 116


Solving Problems by Searching

Artificial Intelligence 117


Artificial Intelligence

Thank You…!
Artificial Intelligence 118

You might also like