2022 Slide3 UninformSearch Eng
2022 Slide3 UninformSearch Eng
• Informed search
• Search strategies: best-first, A*
• Heuristic functions
3
Example: Measuring problem!
9l
3l 5l
4
Example: Measuring problem!
a b c 9l
0 0 0 start 3l 5l
3 0 0
0 0 3 a b c
3 0 3
0 0 6
3 0 6
0 3 6
3 3 6
1 5 6
0 5 7 goal
5
Example: Measuring problem!
a b c 9l
0 0 0 start 3l 5l
3 0 0
0 0 3 a b c
3 0 3
0 0 6
3 0 6
0 3 6
3 3 6
1 5 6
0 5 7 goal
6
Example: Measuring problem!
a b c 9l
0 0 0 start 3l 5l
3 0 0
0 0 3 a b c
3 0 3
0 0 6
3 0 6
0 3 6
3 3 6
1 5 6
0 5 7 goal
7
Example: Measuring problem!
a b c 9l
0 0 0 start 3l 5l
3 0 0
0 0 3 a b c
3 0 3
0 0 6
3 0 6
0 3 6
3 3 6
1 5 6
0 5 7 goal
8
Example: Measuring problem!
a b c 9l
0 0 0 start 3l 5l
3 0 0
0 0 3 a b c
3 0 3
0 0 6
3 0 6
0 3 6
3 3 6
1 5 6
0 5 7 goal
9
Example: Measuring problem!
a b c 9l
0 0 0 start 3l 5l
3 0 0
0 0 3 a b c
3 0 3
0 0 6
3 0 6
0 3 6
3 3 6
1 5 6
0 5 7 goal
10
Example: Measuring problem!
a b c 9l
0 0 0 start 3l 5l
3 0 0
0 0 3 a b c
3 0 3
0 0 6
3 0 6
0 3 6
3 3 6
1 5 6
0 5 7 goal
11
Example: Measuring problem!
a b c 9l
0 0 0 start 3l 5l
3 0 0
0 0 3 a b c
3 0 3
0 0 6
3 0 6
0 3 6
3 3 6
1 5 6
0 5 7 goal
12
Example: Measuring problem!
a b c 9l
0 0 0 start 3l 5l
3 0 0
0 0 3 a b c
3 0 3
0 0 6
3 0 6
0 3 6
3 3 6
1 5 6
0 5 7 goal
13
Example: Measuring problem!
a b c 9l
0 0 0 start 3l 5l
3 0 0
0 0 3 a b c
3 0 3
0 0 6
3 0 6
0 3 6
3 3 6
1 5 6
0 5 7 goal
14
Example: Measuring problem!
• Another Solution:
a b c 9l
0 0 0 start 3l 5l
0 5 0
0 0 3 a b c
3 0 3
0 0 6
3 0 6
0 3 6
3 3 6
1 5 6
0 5 7 goal
15
Example: Measuring problem!
• Another Solution:
a b c 9l
0 0 0 start 3l 5l
0 5 0
3 2 0 a b c
0 0 3
3 0 3
0 0 6
3 0 6
0 3 6
3 3 6
1 5 6
0 5 7 goal
16
Example: Measuring problem!
• Another Solution:
a b c 9l
0 0 0 start 3l 5l
0 5 0
3 2 0 a b c
3 0 2
3 0 3
0 0 6
3 0 6
0 3 6
3 3 6
1 5 6
0 5 7 goal
17
Example: Measuring problem!
• Another Solution:
a b c 9l
0 0 0 start 3l 5l
0 5 0
3 2 0 a b c
3 0 2
3 5 2
0 0 6
3 0 6
0 3 6
3 3 6
1 5 6
0 5 7 goal
18
Example: Measuring problem!
• Another Solution:
a b c 9l
0 0 0 start 3l 5l
0 5 0
3 2 0 a b c
3 0 2
3 5 2
3 0 7 goal
3 0 6
0 3 6
3 3 6
1 5 6
0 5 7 goal
19
Which solution do we prefer?
• Solution 1: • Solution 2:
a b c a b c
0 0 0 start 0 0 0 start
3 0 0 0 5 0
0 0 3 3 2 0
3 0 3 3 0 2
0 0 6 3 5 2
3 0 6 3 0 7 goal
0 3 6
3 3 6
1 5 6
0 5 7 goal
20
Other Example:
21
Search Problems
22
Search
• We will consider the problem of designing goal-
based agents in fully observable,
deterministic, discrete, known environments
• The agent must find a sequence of actions that reaches
the goal
• The performance measure is defined by (a) reaching
the goal and (b) how “expensive” the path to the goal is
Search problem components
Performance measure
-1 per step; +10 food; +500 win; -500 die;
+200 hit scared ghost
Environment
Pacman dynamics (incl ghost behavior)
Actuators
North, South, East, West, (Stop)
Sensors
Entire state is visible
Environment Types – Quiz?
Pacman Taxi
Fully or partially observable
Single agent or multi-agent
Deterministic or stochastic
Static or dynamic
Discrete or continuous
State space
• The initial state, actions, and transition model define
the state space of the problem
• The set of all states reachable from initial state by any sequence of
actions
• Can be represented as a directed graph where the nodes are states
and links between nodes are actions
• What is the state space for the Romania problem?
State Space Graphs
Oradea
71
Neamt
Zerind 87
151
75
Iasi
Arad
140
92
Sibiu Fagaras
99
118
Vaslui
80
Rimnicu Vilcea
Timisoara
142
111 Pitesti 211
Lugoj 97
70 98
85 Hirsova
Mehadia 146 101 Urziceni
75 138 86
Bucharest
Drobeta 120
90
Craiova Eforie
Giurgiu
More Examples
R
L R
L
S S
R R
L R L R
L L
S S
S S
R
L R
S S
Example: 8-puzzle
32
Example: 8-puzzle
33
Example: 8-puzzle
• World state:
• Agent positions: 120
• Food count: 30
• Ghost positions: 12
• Agent facing: NSEW
• How many
• World states?
120x(230)x(122)x4
• States for pathing?
120
• States for eat-all-dots?
120x(230)
Search
• Given:
• Initial state
• Actions
• Transition model
• Goal state
• Path cost
• How do we find the optimal solution?
• How about building the state space and then using Dijkstra’s
shortest path algorithm?
• Complexity of Dijkstra’s is O(E + V log V), where V is the size of the state
space
• The state space may be huge!
Search: Basic idea
Consider this 4-state graph: How big is its search tree (from S)?
S
a
a b
S G
b G G a
b G a b G
∞
Important: Lots of repeated structure in the search tree!
Tree Search Algorithm Outline
40
Problem-Solving
• Problem solving:
• Goal formulation
• Problem formulation (states, operators)
• Search for solution
• Problem formulation:
• Initial state
• Actions
• Goal test
• Path cost
41
Finding a solution
Solution: is ???
42
Finding a solution
43
Finding a solution
44
Example: Traveling from Arad To Bucharest
45
Tree search example
46
Tree search example
47
Tree search example
48
Tree search example
49
Tree search example
Start: Arad
Goal: Bucharest
Tree search example
Start: Arad
Goal: Bucharest
Tree search example
Start: Arad
Goal: Bucharest
Tree search example
Start: Arad
Goal: Bucharest
Tree search example
Start: Arad
Goal: Bucharest
Tree search example
Start: Arad
Goal: Bucharest
Tree search example
Start: Arad
Goal: Bucharest
Handling repeated states
57
Implementation of search algorithms
58
Function TREE_SEARCH(problem) returns a solution, or failure
initialize the frontier as a specific work list (stack, queue, priority queue)
add initial state of problem to frontier
loop do
if the frontier is empty then
return failure
choose a node and remove it from the frontier
if the node contains a goal state then
return the corresponding solution
61
Evaluation of search strategies (Các tiêu chí đánh giá các chiến lược tìm
kiếm)
• A search strategy is defined by picking the order of node expansion (Một chiến lược tìm
kiếm được định nghĩa bằng chọn thứ tự của việc mở rộng nút)
• Completeness (độ hoàn chỉnh): Có phải lúc nào cũng tìm ra nghiệm nếu có không?
• Time complexity (độ phức tạp về thời gian): số lượng các trạng thái đã đến
• Space complexity (độ phức tạp về không gian): số lượng các trạng thái tối đa phải lưu trong
bộ nhớ
• Optimality (độ tối ưu): có phải lúc nào cũng tìm ra nghiệm có chi phí nhỏ nhất không?
• Các tham số để tính độ phức tạp về không gian và thời gian:
• b: maximum branching factor of the search tree (số trạng thái kề)
• d: depth of the least-cost solution (độ sâu của trạng thái kết thúc)
• m: maximum depth of the state space (may be ∞)(độ sâu lớn nhất)
62
Binary Tree Example
Depth = 0
root
Depth = 1
N1 N2
Depth = 2 N3 N4 N5 N6
because a problem may be solvable in principle but may take too long to
solve in practice
64
Complexity: Tower of Hanoi
65
Complexity:
Tower of Hanoi
66
Complexity: Tower of Hanoi
67
Complexity: Tower of Hanoi
68
Complexity: Tower of Hanoi
69
Complexity: Tower of Hanoi
70
Complexity
71
Complexity of Algorithms
• T(n) is O(f(n)) means there exists n0, k such that for all n >n0 T(n) <= kf(n):
• N = input size
• T(n) = total number of step of the algorithm
• Independent of the implementation, compiler, …
• Asymtotic analysis: For large n, an O(n) algorithm is better than an O(n2) algorithm.
• O() abstract over constant factors:
• T(100n +1000) is better than T(n2 + 1) only for n > 110
• O() notation is a good compromise between precision and easy of analysis
72
Remember: Implementation of search algorithms
73
Types of search strategies
• Uninformed/blind search
• Không hiểu biết gì về đối tượng
• Duyệt qua để tìm được đối tượng cần tìm
• Informed/heuristic search:
• Dựa vào kinh nghiệm và sự hiểu biết của chúng ta để xây dựng hàm
đánh giá hướng dẫn tìm kiếm
• Các chiến lược tìm kiếm tối ưu
• Các phương pháp tìm kiếm có đối thủ
74
Uninformed/Blind search
strategies (Các chiến lược
tìm kiếm mù)
75
Uninformed/Blind search strategies (Các chiến
lược tìm kiếm mù)
76
Breadth-first search (Tìm kiếm theo chiều rộng)- Idea
• Tại mỗi bước, chọn trạng thái để phát triển là trạng thái được sinh ra trước các trạng thái
chờ phát triển khác
• Nói một cách khác, chúng ta sẽ quét cây tìm kiếm theo từng lớp tính từ gốc trở đi
• Đỉnh mới được sinh ra sẽ được lưu vào một danh sách tổ chức theo kiểu “hàng đợi”
(queue FIFO)
77
Breadth-first search
A D Move downwards,
level by level,
until goal is
B D A E reached.
C E E B B F
D F B F C E A C G
G C G F
G 78
Example: Traveling from Arad To Bucharest
79
Breadth-first search
80
Breadth-first search
81
Breadth-first search
82
Breadth-first search
1. Khởi tạo hàng đợi L chỉ chứa trạng thái ban đầu
2. Loop do
2.1 If (L rỗng) then
{thông báo tìm kiếm thất bại; kết thúc}
2.2 Loại trạng thái u ở đầu hàng đợi L;
2.3 If (u là trạng thái kết thúc) then
{thông báo tìm kiếm thành công; kết thúc}
2.4 For (mỗi trạng thái v kề u) do
{Đặt v vào cuối hàng đợi L;}
83
Properties of breadth-first search
• Completeness:
• Time complexity:
• Space complexity:
• Optimality:
• Search algorithms are commonly evaluated according to the following four criteria:
• Completeness: does it always find a solution if one exists?
• Time complexity: how long does it take as function of num. of nodes?
• Space complexity: how much memory does it require?
• Optimality: does it guarantee the least-cost solution?
84
Properties of breadth-first search
85
Time complexity of breadth-first search
• If a goal node is found on depth d of the tree, all nodes up till that
depth are created.
d
m
b G
• Thus: O(bd)
86
Space complexity of breadth-first
d
m
b G
87
Examples
• Giả sử b=10, kiểm tra 1000 trạng thái cần 1 giây, lưu một trạng
thái cần 100 byte
88
Depth First Search (Tìm kiếm theo chiều sâu)- Idea
• Tại mỗi bước, chọn trạng thái để phát triển là trạng thái được sinh ra sau cùng trong các
trạng thái chờ phát triển
• Nói một cách khác, chúng ta sẽ quét cây tìm kiếm theo từng nhánh tính từ gốc trở đi
• Tương tự như thuật toán tìm kiếm chiều rộng chỉ khác là sử dụng một danh sách theo kiểu
“ngăn xếp” (stack LIFO) chứ không phải theo kiểu “hàng đợi”.
89
Depth First Search
C E
D F
90
Romania with step costs in km
91
Depth-first search
92
Depth-first search
93
Depth-first search
94
Depth-first search
1. Khởi tạo ngăn xếp L chỉ chứa trạng thái ban đầu
2. Loop do
2.1 If (L rỗng) then
{thông báo tìm kiếm thất bại; kết thúc}
2.2 Loại trạng thái u ở đầu ngăn xếp L;
2.3 If (u là trạng thái kết thúc) then
{thông báo tìm kiếm thành công; kết thúc}
2.4 For (mỗi trạng thái v kề u) do
{Đặt v vào đầu ngăn xếp L;}
95
Properties of depth-first search
Remember:
b = branching factor
m = max depth of search tree
96
Time complexity of depth-first: details
m
b
...
• Trong cây tìm kiếm có thể chứa nhiều đỉnh ứng với cùng một
trạng thái – các trạng thái này được gọi là trạng thái lặp
• Các thuật toán tìm kiếm sẽ lãng phí rất nhiều thời gian để phát
triển lại các trạng thái mà ta đã gặp và đã phát triển
• Cách giải quyết: đánh dấu các trạng thái đã được sinh ra, không
sinh ra lại những trạng thái đã được sinh ra
99
Exercise
100
Exercise (2)
101
Breadth-First Search
Strategy: expand a a G
shallowest node first b c
Implementation: e
d f
Frontier is a FIFO queue 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
Depth-First Search
Strategy: a G
b c
expand a e
d f
deepest node S h
first p q r
Implementation S
: Frontier is a d e p
LIFO stack b c e h r q
a a h r p q f
p q f q c G
q c G a
a
Depth-limited search
104
Iterative Deepening
107
108
109
110
111
112
113
114
115
Iterative deepening search
Iterative deepening complexity
117
Uniform-cost search
• For each frontier node (queue), save the total cost of the path from the
initial state to that node
• Expand the frontier node with the lowest path cost
• Implementation: frontier is a priority queue ordered by path cost
• Equivalent to breadth-first if step costs all equal
• Equivalent to Dijkstra’s algorithm in general
Function UNIFORM-COST-SEARCH(problem) returns a solution, or failure
initialize the explored set to be empty
initialize the frontier as a priority queue using node path_cost as the priority
add initial state of problem to frontier with path_cost = 0
loop do
if the frontier is empty then
return failure
choose a node and remove it from the frontier
if the node contains a goal state then
return the corresponding solution
add the node state to the explored set
for each resulting child from node
if the child state is not already in the frontier or explored set then
add child to the frontier
else if the child is already in the frontier with higher path_cost then
replace that frontier node with child
Uniform-cost search example
Uniform-cost search example
• Expansion order:
(S,p,d,b,e,a,r,f,e,G)
Another example of uniform-cost search
Source: Wikipedia
Properties of uniform-cost search
• Complete?
Yes, if step cost is greater than some positive
constant ε (we don’t want infinite sequences of
steps that have a finite total cost)
• Optimal?
Yes
Optimality of uniform-cost search
• Complete?
Yes, if step cost is greater than some positive constant ε
(we don’t want infinite sequences of steps that have a
finite total cost)
• Optimal?
Yes – nodes expanded in increasing order of path cost
• Time?
Number of nodes with path cost ≤ cost of optimal solution
(C*), O(bC*/ ε)
This can be greater than O(bd): the search can explore
long paths consisting of small steps before exploring
shorter paths consisting of larger steps
• Space?
O(bC*/ ε)
Comparing uninformed search strategies
• Problem formulation usually requires abstracting away real-world details to define a state
space that can be explored using computer algorithms.
• Once problem is formulated in abstract form, complexity analysis helps us picking out best
algorithm to solve problem.
• Variety of uninformed search strategies; difference lies in method used to pick node that
will be further expanded.
127
References
• AIMA textbook
• Slides of CMU AI course
• Slides of UC Berkeley AI course
128