Artificial Intelligence (Trí tuệ nhân tạo) : Solving Problems by searching (Giải quyết vấn đề bằng tìm kiếm)
Artificial Intelligence (Trí tuệ nhân tạo) : Solving Problems by searching (Giải quyết vấn đề bằng tìm kiếm)
• 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
0 5 0
3 0 0
3 2 0
0 0 3
3 0 2
3 0 3
3 5 2
0 0 6 3 0 7 goal
3 0 6
0 3 6
3 3 6
1 5 6
0 5 7 goal
20
Search problem components
• State :
• Actions :
• Transition model:
• Goal test:
• Path cost:
24
Example: 8-puzzle
26
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
• Ta có thể thể hiện quá trình tìm kiếm thông qua cây tìm kiếm
A
B C D
C E B
29
Cây tìm kiếm
A A
D
C D
C
E
B E C
B
B B
30
Cây tìm kiếm
• “What if” tree of sequences of actions and
Starting
outcomes
state
• The root node corresponds to the starting state
• The children of a node correspond to the Action
successor states of that node’s state
Successor
• A path through the tree corresponds to a state
…
sequence of actions
• A solution is a path ending in the goal
state
• Nodes vs. states
• A state is a representation of the world,
………
while a node is a data structure that is
part of the search tree Goal state
• Node has to keep pointer to parent, path cost,
possibly other info
Tree Search Algorithm Outline
32
Problem-Solving
• Problem solving:
• Goal formulation
• Problem formulation (states, operators)
• Search for solution
• Problem formulation:
• Initial state
• Actions
• Goal test
• Path cost
33
Finding a solution
Solution: is ???
34
Finding a solution
35
Finding a solution
36
Example: Traveling from Arad To Bucharest
37
Tree search example
38
Tree search example
39
Tree search example
40
Tree search example
41
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
49
Implementation of search algorithms
50
Encapsulating state information in nodes
51
Evaluation of search strategies (Các tiêu chí đánh giá
các chiến lược tìm kiếm)
52
Binary Tree Example
Depth = 0
root
Depth = 1
N1 N2
Depth = 2 N3 N4 N5 N6
54
Complexity: Tower of Hanoi
55
Complexity:
Tower of Hanoi
"THÁP HÀ NỘI
Trò chơi trí tuệ của An nam
Trò chơi được đem về từ Bắc Kì
Bởi giáo sư N. CLAUS (của SIAM)
Trường Cao đẳng Quan Li-Sou-Stian!
----
….
56
Complexity: Tower of Hanoi
57
Complexity: Tower of Hanoi
58
Complexity: Tower of Hanoi
59
Complexity: Tower of Hanoi
The world's fastest computer (getting close to petaflopspeed) is IBM's Blue Gene with a
top speed of 360 trillion operations a second. 60
Complexity
61
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
62
Big-Oh Notation Example
Algorithm Analysis 63
Big-Oh Notation Example
10,000
Example: 3n
2n 10 cn n
(c 2) n 10 100
n 10(c 2)
Pick c 3 and n0 10 10
1
1 10 100 1,000
n
Algorithm Analysis 64
Big-Oh Notation Example (cont.)
1,000,000
n^2
Example: the function 100,000 100n
1
1 10 100 1,000
n
Algorithm Analysis 65
Big-Oh and Growth Rate
Algorithm Analysis 68
Seven Important Functions
Algorithm Analysis 69
Seven Important Functions
Algorithm Analysis 70
Asymptotic Analysis
Algorithm Analysis 71
SEE YOU IN THE NEXT WEEK !
72
Remember: Implementation of search algorithms
73
Các loại chiến lược tìm kiếm
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
Tìm kiếm theo chiều rộng (BFS) - Ý tưởng
• 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
BFS – Ý tưởng
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
Ví dụ: Đi từ Arad tới Bucharest
79
Ví dụ
80
Ví dụ
81
Ví dụ
82
Mô tả thuật toán BFS
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
Tính chất của thuật toán BFS
• 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
Tính chất của thuật toán BFS
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
Ví dụ về độ phức tạp thời gian và không gian của
BFS
• 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
Tìm kiếm theo chiều sâu (Depth First Search) - Ý
tưởng
• 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
DFS – Ý tưởng
C E
D F
90
Ví dụ: Du lịch Romania với giá (cost) tính bằng km
91
Ví dụ
92
Ví dụ
93
Ví dụ
94
Mô tả thuật toán DFS
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
Tính chất của thuật toán DFS
Remember:
b = branching factor
m = max depth of search tree
96
Độ phức tạp về thời gian
m
b
...
99
Bài tập (1)
100
Bài tập (2)
102
Tìm kiếm theo chều sâu lặp (Iterative deepening
search)
104
105
106
107
108
109
110
111
112
Ví dụ
• 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
Ví dụ
Ví dụ
• Expansion order:
(S,p,d,b,e,a,r,f,e,G)
Một ví dụ khác
Source: Wikipedia
Tính chất của tìm kiếm chi phí đều
• 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
Độ tối ưu của tìm kiếm chi phí đều
• 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*/ ε)
Bài tập 3 – Tìm kiếm chi phí đều
Các Biên giới: tập các đối tượng Mở Đã xét: tập các nút
bước (nút và chi phí) rộng đã xét
1 {(A,0)} A ∅
? ? ? ?
121
So sánh các thuật toán tìm kiếm mù
123