Upload
others
View
3
Download
0
Embed Size (px)
Citation preview
Outline: Giải quyết bài toán và tìm kiếm(Problem solving and search, problem solving agents)( g p g g )
Hình thành bài toán (Problem formulation)• Xác định các thành phần của bài toán (problem formulation)• Lược đồ chung để giải bài toán (general-solving procedure)• Đánh giá một giải thuật tìm kiếm
Uninformed (blink) search (Tìm không có thông tin phản hồi, hoặctìm kiếm mù)• Search strategies: depth-first, breadth-first (Các chiến lược tìm kiếm: theo chiềug p , ( ợ
sâu, theo chiều rộng)
Informed search (Tìm kiếm dựa trên các hàm đánh giá, heuristics)• Search strategies: best first search• Search strategies: best-first search• Heuristic functions (Các hàm heuristic)
Uniform searchGreedy searchGreedy searchA* search
(This time)
1
(This time)
Xem lại giải thuật uninformed searchg
Function General-Search(problem, Queue/Stack) returns a solution, or failureQueue/Stack make-queue(make-node(initial-state[problem]));Queue/Stack make-queue(make-node(initial-state[problem]));father(initial-state[problem]) = empty;while (1)
if Queue/Stack is empty then return failure;if Queue/Stack is empty then return failure;node = pop(Queue/Stack) ;if test(node,Goal[problem]) then return path(node,father);expand-nodes adjacent-nodes(node, Operators[problem]);p j ( , p [p ]);push(Queue/Stack, expand-nodes );foreach ex-node in expand-nodes
father(ex-node) = node;end
Có thể sử dụng cấu trúc dữ liệu khác Queue/Stack tốt hơn không?
Hàm đánh giá – heuristicg
3
Sử dụng thêm thông tin – hàm đánh giá h2ụ g g g 2
5 4
6 1 86 1 8
7 3 2
5 4 5 4 8
18
1 2 3
Goal5 4
6 1 8
7 3 2
5 4 8
6 1
7 3 217 19
8 4
7 6 5
CS 561, Lectures 3-5 4
Sử dụng thêm thông tin – hàm đánh giá h2ụ g g g 2
5 4
6 1 86 1 8
7 3 2
5 4 5 4 8
18
1 2 3
Goal5 4
6 1 8
7 3 2
5 4 8
6 1
7 3 217 19
8 4
7 6 55 4
6 1 8
7 3 2
5 1 4
6 8
7 3 2 167 3 2 7 3 216
16
CS 561, Lectures 3-5 5
Sử dụng thêm thông tin – hàm đánh giá h2ụ g g g 2
5 4
6 1 86 1 8
7 3 2
5 4 5 4 8
18
1 2 3
Goal5 4
6 1 8
7 3 2
5 4 8
6 1
7 3 217 19
8 4
7 6 55 4
6 1 8
7 3 2
5 1 4
6 8
7 3 2 167 3 2 7 3 216
16
6 5 4
1 8
7 3 2 17
CS 561, Lectures 3-5 6
Sử dụng thêm thông tin – hàm đánh giá h2ụ g g g 2
5 4
6 1 86 1 8
7 3 2
5 4 5 4 8
18
1 2 3
Goal5 4
6 1 8
7 3 2
5 4 8
6 1
7 3 217 19
8 4
7 6 55 4
6 1 8
7 3 2
5 1 4
6 8
7 3 2 167 3 2 7 3 216
16
6 5 45 1 4
6 85 1 4
6 8
5 1 4
6 3 81 8
7 3 2 17 7 3 26 8
7 3 2 7 2
15 15 15
CS 561, Lectures 3-5 7
Sử dụng thêm thông tin – hàm đánh giá h2ụ g g g 2
5 4
6 1 86 1 8
7 3 2
5 4 5 4 8
18
1 2 3
Goal5 4
6 1 8
7 3 2
5 4 8
6 1
7 3 217 19
8 4
7 6 55 4
6 1 8
7 3 2
5 1 4
6 8
7 3 2 167 3 2 7 3 216
16
6 5 45 1 4
6 85 1 4
6 8
5 1 4
6 3 81 8
7 3 2 17 7 3 26 8
7 3 2 7 2
15 15 15
CS 561, Lectures 3-5 8
Best-first search
Idea:Sử dụng hàm đánh giá trong quá trình phát triển cây tìm kiếm, hàm đánh giá ước lượng “độ gần” của mỗi đỉnh
á ớ á í ứ à ỉ ể â ìtrạng thái với trạng thái đích. Tức là chỉ triển khai cây tìm kiếm theo nhánh có triển vọng đi đến đích nhanh nhất.
Implementation:Thay vì dùng Queue/Stack, dùng mảng có sắp xếp theo hàm đ h iđánh giá
9
Cài đặt chi tiết giải thuật Best-first search
Function General-Search(problem, ordered-array,h) returns a solution, or failureordered-array make-queue(make-node(initial-state[problem]));ordered-array make-queue(make-node(initial-state[problem]));father(initial-state[problem]) = empty;while (1)
if ordered-array is empty then return failure;if ordered array is empty then return failure;node = pop(ordered-array) ; // node with max/min hif test(node,Goal[problem]) then return path(node,father);expand-nodes adjacent-nodes(node, Operators[problem]);p j ( , p [p ]);push(ordered-array, expand-nodes ,h);foreach ex-node in expand-nodes
father(ex-node) = node;Lấy các đỉnh kề với node, nhưng chưa xuất hiện trong
end
Function push(ordered-array, expand-nodes ,h);
ordered-array
Chèn các nodes trong expand-nodes vào ordered-array sao cho mảng ordered-array sắp theo thứ tự tăng/giảmtheo hàm h
Các loại hàm đánh giá - hạ g
Hàm h(n) – chi phí từ trạng thái đầu đến n uniform search
Hàm h(n) – Ước lượng chi phí từ n đến trạng thái đích greedy search
Hàm h(n) = f(n) + g(n)Trong đó:
f(n) – chi phí từ trạng thái đầu đến ng(n) – Ước lượng chi phí từ n đến trạng thái đích
và g(n) ≤ g*(n), g*(n) là chi phí thực sự từ n đến trạng thái đích
A* search…
CS 561, Lectures 3-5 11
Ví dụ: Romania with step costs in kmụ p
CS 561, Lectures 3-5 12
Uniform-cost search
CS 561, Lectures 3-5 13
Uniform-cost search
CS 561, Lectures 3-5 14
Uniform-cost search
CS 561, Lectures 3-5 15
Properties of uniform-cost searchp
• Completeness: Yes, if step cost ≥ ε >0• Time complexity: # nodes with g ≤ cost of optimal solution, ≤ O(b d)• Space complexity: # nodes with g ≤ cost of optimal solution, ≤ O(b d)• Optimality: Yes as long as path cost never decreases• Optimality: Yes, as long as path cost never decreases
g(n) is the path cost to node nRemember:
b = branching factorb = branching factord = depth of least-cost solution
CS 561, Lectures 3-5 16
Greedy searchy
Estimation function:
h(n) = estimate of cost from n to goal (heuristic)
For example:For example:
hSLD(n) = straight-line distance from n to Bucharest
Greedy search expands first the node that appears to be closest to the goal, according to h(n).
CS 561, Lectures 3-5 17
Romania with step costs in kmp
374374
253
329
18
CS 561, Lectures 3-5 19
CS 561, Lectures 3-5 20
CS 561, Lectures 3-5 21
CS 561, Lectures 3-5 22
Properties of Greedy Searchp y
Complete?
Time?
Space?
Optimal?Optimal?
23
Properties of Greedy Searchp y
Complete? No – can get stuck in loopse.g., Iasi > Neamt > Iasi > Neamt > …Complete in finite space with repeated-state checking.
Time? O(b^m) but a good heuristic can givedramatic improvementp
Space? O(b^m) – keeps all nodes in memory
Optimal? NoOptimal? No.
24
A* search
Idea: avoid expanding paths that are already expensive
evaluation function: h(n) = f(n) + g(n), with:f(n) – cost so far to reach ng(n) – estimated cost to goal from nh(n) – estimated total cost of path through n to goal
A* search uses an admissible heuristic, that is,
h(n) ≤ h*(n) where h*(n) is the true cost from n.For example: hSLD(n) never overestimates actual road distance.
Theorem: A* search is optimal
25
CS 561, Lectures 3-5 26
CS 561, Lectures 3-5 27
CS 561, Lectures 3-5 28
CS 561, Lectures 3-5 29
CS 561, Lectures 3-5 30
Optimality of A* (standard proof)p y ( p )
Suppose some suboptimal goal G2 has been generated and is in the L b d d d h hqueue. Let n be an unexpanded node on a shortest path to an
optimal goal G1.
1
CS 561, Lectures 3-5 31
Optimality of A* (more useful proof)p y ( p )
CS 561, Lectures 3-5 32
f-contoursf contours
Ho do the conto s look like hen h(n) 0?How do the contours look like when h(n) =0?
CS 561, Lectures 3-5 33
Properties of A*p
Complete?
Time?Time?
Space?
Optimal?
34
Properties of A*p
Complete? Yes, unless infinitely many nodes with f ≤ f(G)
Time? Exponential in [(relative error in h) x (length of solution)]Time? Exponential in [(relative error in h) x (length of solution)]
Space? Keeps all nodes in memory
Optimal? Yes – cannot expand fi+1 until fi is finished
35
Admissible heuristics
36
Admissible heuristics
37
Relaxed Problem
Admissible heuristics can be derived from the exact solution cost of l d i f h bla relaxed version of the problem.
If the rules of the 8-puzzle are relaxed so that a tile can move h th h ( ) i th h t t l tianywhere, then h1(n) gives the shortest solution.
If the rules are relaxed so that a tile can move to any adjacent th h ( ) i th h t t l tisquare, then h2(n) gives the shortest solution.
38
Iterative improvementp
In many optimization problems, path is irrelevant;
the goal state itself is the solution.
Then, state space = space of “complete” configurations.e , state space space o co p ete co gu at o s
Algorithm goal:- find optimal configuration (e.g., TSP), or,g g- find configuration satisfying constraints
(e.g., n-queens)
In such cases, can use iterative improvement algorithms: keep a single “current” state, and try to improve it.
39
Iterative improvement example: n-queensp p q
Goal: Put n chess-game queens on an n x n board, with no two h l di lqueens on the same row, column, or diagonal.
fHere, goal state is initially unknown but is specified by constraints that it must satisfy.
40
Summaryy
Best-first search = general search, where the minimum-cost nodes ( o ding to ome me e) e e p nded fi t(according to some measure) are expanded first.
Uniform search = best-first with the cost from initial state as a heuristic
Greedy search = best-first with the estimated cost to reach the goal as a heuristic measure.
generally faster than uninformed searchgenerally faster than uninformed searchnot optimalnot complete.
A* search = best-first with measure = path cost so far + estimated path cost to goal.
bi d t f if t d d hcombines advantages of uniform-cost and greedy searchescomplete, optimal and optimally efficientspace complexity still exponential
41Time complexity of heuristic algorithms depend on quality of heuristic function.