41
Outline: Gii quyết bài toán và tìm kiếm (Problem solving and search, problem solving agents) Hình thành bài toán (Problem formulation) Xác định các thành phnca bài toán (problem formulation) Lược đồ chung để gii bài toán (general-solving procedure) Đánh giá mt gii thut tìm kiếm Uninformed (blink) search (Tìm không có thông tin phnhi, hoc tìm kiếm mù) Search strategies: depth-first, breadth-first (Các chiếnlưc tìm kiếm: theo chiu sâu, theo chiurng) Informed search (Tìm kiếmda 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 search Greedy search Greedy search A* search (This time) 1 (This time)

Outline: Giải quyết bài toán và tìm kiếm ((g p …ccs1.hnue.edu.vn/hoanpt/AI/informed_search.pdfBest-first search Idea: Sử dụng hàm đánh giá trong quá trình phát

  • Upload
    others

  • View
    3

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Outline: Giải quyết bài toán và tìm kiếm ((g p …ccs1.hnue.edu.vn/hoanpt/AI/informed_search.pdfBest-first search Idea: Sử dụng hàm đánh giá trong quá trình phát

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)

Page 2: Outline: Giải quyết bài toán và tìm kiếm ((g p …ccs1.hnue.edu.vn/hoanpt/AI/informed_search.pdfBest-first search Idea: Sử dụng hàm đánh giá trong quá trình phát

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?

Page 3: Outline: Giải quyết bài toán và tìm kiếm ((g p …ccs1.hnue.edu.vn/hoanpt/AI/informed_search.pdfBest-first search Idea: Sử dụng hàm đánh giá trong quá trình phát

Hàm đánh giá – heuristicg

3

Page 4: Outline: Giải quyết bài toán và tìm kiếm ((g p …ccs1.hnue.edu.vn/hoanpt/AI/informed_search.pdfBest-first search Idea: Sử dụng hàm đánh giá trong quá trình phát

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

Page 5: Outline: Giải quyết bài toán và tìm kiếm ((g p …ccs1.hnue.edu.vn/hoanpt/AI/informed_search.pdfBest-first search Idea: Sử dụng hàm đánh giá trong quá trình phát

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

Page 6: Outline: Giải quyết bài toán và tìm kiếm ((g p …ccs1.hnue.edu.vn/hoanpt/AI/informed_search.pdfBest-first search Idea: Sử dụng hàm đánh giá trong quá trình phát

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

Page 7: Outline: Giải quyết bài toán và tìm kiếm ((g p …ccs1.hnue.edu.vn/hoanpt/AI/informed_search.pdfBest-first search Idea: Sử dụng hàm đánh giá trong quá trình phát

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

Page 8: Outline: Giải quyết bài toán và tìm kiếm ((g p …ccs1.hnue.edu.vn/hoanpt/AI/informed_search.pdfBest-first search Idea: Sử dụng hàm đánh giá trong quá trình phát

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

Page 9: Outline: Giải quyết bài toán và tìm kiếm ((g p …ccs1.hnue.edu.vn/hoanpt/AI/informed_search.pdfBest-first search Idea: Sử dụng hàm đánh giá trong quá trình phát

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

Page 10: Outline: Giải quyết bài toán và tìm kiếm ((g p …ccs1.hnue.edu.vn/hoanpt/AI/informed_search.pdfBest-first search Idea: Sử dụng hàm đánh giá trong quá trình phát

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

Page 11: Outline: Giải quyết bài toán và tìm kiếm ((g p …ccs1.hnue.edu.vn/hoanpt/AI/informed_search.pdfBest-first search Idea: Sử dụng hàm đánh giá trong quá trình phát

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

Page 12: Outline: Giải quyết bài toán và tìm kiếm ((g p …ccs1.hnue.edu.vn/hoanpt/AI/informed_search.pdfBest-first search Idea: Sử dụng hàm đánh giá trong quá trình phát

Ví dụ: Romania with step costs in kmụ p

CS 561, Lectures 3-5 12

Page 13: Outline: Giải quyết bài toán và tìm kiếm ((g p …ccs1.hnue.edu.vn/hoanpt/AI/informed_search.pdfBest-first search Idea: Sử dụng hàm đánh giá trong quá trình phát

Uniform-cost search

CS 561, Lectures 3-5 13

Page 14: Outline: Giải quyết bài toán và tìm kiếm ((g p …ccs1.hnue.edu.vn/hoanpt/AI/informed_search.pdfBest-first search Idea: Sử dụng hàm đánh giá trong quá trình phát

Uniform-cost search

CS 561, Lectures 3-5 14

Page 15: Outline: Giải quyết bài toán và tìm kiếm ((g p …ccs1.hnue.edu.vn/hoanpt/AI/informed_search.pdfBest-first search Idea: Sử dụng hàm đánh giá trong quá trình phát

Uniform-cost search

CS 561, Lectures 3-5 15

Page 16: Outline: Giải quyết bài toán và tìm kiếm ((g p …ccs1.hnue.edu.vn/hoanpt/AI/informed_search.pdfBest-first search Idea: Sử dụng hàm đánh giá trong quá trình phát

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

Page 17: Outline: Giải quyết bài toán và tìm kiếm ((g p …ccs1.hnue.edu.vn/hoanpt/AI/informed_search.pdfBest-first search Idea: Sử dụng hàm đánh giá trong quá trình phát

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

Page 18: Outline: Giải quyết bài toán và tìm kiếm ((g p …ccs1.hnue.edu.vn/hoanpt/AI/informed_search.pdfBest-first search Idea: Sử dụng hàm đánh giá trong quá trình phát

Romania with step costs in kmp

374374

253

329

18

Page 19: Outline: Giải quyết bài toán và tìm kiếm ((g p …ccs1.hnue.edu.vn/hoanpt/AI/informed_search.pdfBest-first search Idea: Sử dụng hàm đánh giá trong quá trình phát

CS 561, Lectures 3-5 19

Page 20: Outline: Giải quyết bài toán và tìm kiếm ((g p …ccs1.hnue.edu.vn/hoanpt/AI/informed_search.pdfBest-first search Idea: Sử dụng hàm đánh giá trong quá trình phát

CS 561, Lectures 3-5 20

Page 21: Outline: Giải quyết bài toán và tìm kiếm ((g p …ccs1.hnue.edu.vn/hoanpt/AI/informed_search.pdfBest-first search Idea: Sử dụng hàm đánh giá trong quá trình phát

CS 561, Lectures 3-5 21

Page 22: Outline: Giải quyết bài toán và tìm kiếm ((g p …ccs1.hnue.edu.vn/hoanpt/AI/informed_search.pdfBest-first search Idea: Sử dụng hàm đánh giá trong quá trình phát

CS 561, Lectures 3-5 22

Page 23: Outline: Giải quyết bài toán và tìm kiếm ((g p …ccs1.hnue.edu.vn/hoanpt/AI/informed_search.pdfBest-first search Idea: Sử dụng hàm đánh giá trong quá trình phát

Properties of Greedy Searchp y

Complete?

Time?

Space?

Optimal?Optimal?

23

Page 24: Outline: Giải quyết bài toán và tìm kiếm ((g p …ccs1.hnue.edu.vn/hoanpt/AI/informed_search.pdfBest-first search Idea: Sử dụng hàm đánh giá trong quá trình phát

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

Page 25: Outline: Giải quyết bài toán và tìm kiếm ((g p …ccs1.hnue.edu.vn/hoanpt/AI/informed_search.pdfBest-first search Idea: Sử dụng hàm đánh giá trong quá trình phát

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

Page 26: Outline: Giải quyết bài toán và tìm kiếm ((g p …ccs1.hnue.edu.vn/hoanpt/AI/informed_search.pdfBest-first search Idea: Sử dụng hàm đánh giá trong quá trình phát

CS 561, Lectures 3-5 26

Page 27: Outline: Giải quyết bài toán và tìm kiếm ((g p …ccs1.hnue.edu.vn/hoanpt/AI/informed_search.pdfBest-first search Idea: Sử dụng hàm đánh giá trong quá trình phát

CS 561, Lectures 3-5 27

Page 28: Outline: Giải quyết bài toán và tìm kiếm ((g p …ccs1.hnue.edu.vn/hoanpt/AI/informed_search.pdfBest-first search Idea: Sử dụng hàm đánh giá trong quá trình phát

CS 561, Lectures 3-5 28

Page 29: Outline: Giải quyết bài toán và tìm kiếm ((g p …ccs1.hnue.edu.vn/hoanpt/AI/informed_search.pdfBest-first search Idea: Sử dụng hàm đánh giá trong quá trình phát

CS 561, Lectures 3-5 29

Page 30: Outline: Giải quyết bài toán và tìm kiếm ((g p …ccs1.hnue.edu.vn/hoanpt/AI/informed_search.pdfBest-first search Idea: Sử dụng hàm đánh giá trong quá trình phát

CS 561, Lectures 3-5 30

Page 31: Outline: Giải quyết bài toán và tìm kiếm ((g p …ccs1.hnue.edu.vn/hoanpt/AI/informed_search.pdfBest-first search Idea: Sử dụng hàm đánh giá trong quá trình phát

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

Page 32: Outline: Giải quyết bài toán và tìm kiếm ((g p …ccs1.hnue.edu.vn/hoanpt/AI/informed_search.pdfBest-first search Idea: Sử dụng hàm đánh giá trong quá trình phát

Optimality of A* (more useful proof)p y ( p )

CS 561, Lectures 3-5 32

Page 33: Outline: Giải quyết bài toán và tìm kiếm ((g p …ccs1.hnue.edu.vn/hoanpt/AI/informed_search.pdfBest-first search Idea: Sử dụng hàm đánh giá trong quá trình phát

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

Page 34: Outline: Giải quyết bài toán và tìm kiếm ((g p …ccs1.hnue.edu.vn/hoanpt/AI/informed_search.pdfBest-first search Idea: Sử dụng hàm đánh giá trong quá trình phát

Properties of A*p

Complete?

Time?Time?

Space?

Optimal?

34

Page 35: Outline: Giải quyết bài toán và tìm kiếm ((g p …ccs1.hnue.edu.vn/hoanpt/AI/informed_search.pdfBest-first search Idea: Sử dụng hàm đánh giá trong quá trình phát

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

Page 36: Outline: Giải quyết bài toán và tìm kiếm ((g p …ccs1.hnue.edu.vn/hoanpt/AI/informed_search.pdfBest-first search Idea: Sử dụng hàm đánh giá trong quá trình phát

Admissible heuristics

36

Page 37: Outline: Giải quyết bài toán và tìm kiếm ((g p …ccs1.hnue.edu.vn/hoanpt/AI/informed_search.pdfBest-first search Idea: Sử dụng hàm đánh giá trong quá trình phát

Admissible heuristics

37

Page 38: Outline: Giải quyết bài toán và tìm kiếm ((g p …ccs1.hnue.edu.vn/hoanpt/AI/informed_search.pdfBest-first search Idea: Sử dụng hàm đánh giá trong quá trình phát

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

Page 39: Outline: Giải quyết bài toán và tìm kiếm ((g p …ccs1.hnue.edu.vn/hoanpt/AI/informed_search.pdfBest-first search Idea: Sử dụng hàm đánh giá trong quá trình phát

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

Page 40: Outline: Giải quyết bài toán và tìm kiếm ((g p …ccs1.hnue.edu.vn/hoanpt/AI/informed_search.pdfBest-first search Idea: Sử dụng hàm đánh giá trong quá trình phát

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

Page 41: Outline: Giải quyết bài toán và tìm kiếm ((g p …ccs1.hnue.edu.vn/hoanpt/AI/informed_search.pdfBest-first search Idea: Sử dụng hàm đánh giá trong quá trình phát

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.