1 search methodologies. 2 problem solving l missionaries and cannibals l hanoi towers l maze l...

37
1 Search Methodologies

Upload: rosa-harper

Post on 20-Jan-2018

218 views

Category:

Documents


0 download

DESCRIPTION

3 Example (Missionaries and Cannibals) l Start (M, C, Canoe) =(0,0,0) l operators 1.Move one cannibal across the river. 2.Move two cannibals across the river. 3.Move one missionary across the river. 4.Move two missionaries across the river. 5.Move one missionary and one cannibal.

TRANSCRIPT

Page 1: 1 Search Methodologies. 2 Problem Solving l Missionaries and Cannibals l Hanoi Towers l Maze l Puzzles…

1

Search Methodologies

Page 2: 1 Search Methodologies. 2 Problem Solving l Missionaries and Cannibals l Hanoi Towers l Maze l Puzzles…

2

Problem Solving Missionaries and Cannibals Hanoi Towers Maze Puzzles (8-Puzzle, 15-Puzzle) Brix ( 消除方塊 ) Freecell ( 新接龍 ) Magic Cube (Rubik's Cube) Web Spidering Xmas Gift, etc.

Initial State (Start)Actions & costGoal(s) & Dead States?

最古老談論 State Search是春秋時代的 ( )

Page 3: 1 Search Methodologies. 2 Problem Solving l Missionaries and Cannibals l Hanoi Towers l Maze l Puzzles…

3

Example (Missionaries and Cannibals) Start (M, C, Canoe) =(0,0,0) operators

1. Move one cannibal across the river.

2. Move two cannibals across the river.

3. Move one missionary across the river.

4. Move two missionaries across the river.

5. Move one missionary and one cannibal.

Page 4: 1 Search Methodologies. 2 Problem Solving l Missionaries and Cannibals l Hanoi Towers l Maze l Puzzles…

4

Example (Hanoi Tower)

(A,B,C)()()

(B,C)(A)()

(C)(A)(B)

(A,C)()(B)

(C)()(A,B)

(C)()(A,B)

()(C)(A,B)

(B,C)()(A)

(C)(B)(A)

(A,C)(B)()

(C)(A,B)()

(C)(A,B)()

()(A,B)(C)

Page 5: 1 Search Methodologies. 2 Problem Solving l Missionaries and Cannibals l Hanoi Towers l Maze l Puzzles…

5

Example (8-Puzzle)7 6

4 3 1

2 5 8

7 6 1

4 3

2 5 8

7 6

4 3 1

2 5 81 2 3

8 4

7 6 5

Page 6: 1 Search Methodologies. 2 Problem Solving l Missionaries and Cannibals l Hanoi Towers l Maze l Puzzles…

6

Example (Maze)

Page 7: 1 Search Methodologies. 2 Problem Solving l Missionaries and Cannibals l Hanoi Towers l Maze l Puzzles…

7

Example (Maze, dead states)

Infinite Cycle Pitfall

Page 8: 1 Search Methodologies. 2 Problem Solving l Missionaries and Cannibals l Hanoi Towers l Maze l Puzzles…

8

Example (Brix)

(Dead)

Page 9: 1 Search Methodologies. 2 Problem Solving l Missionaries and Cannibals l Hanoi Towers l Maze l Puzzles…

9

Possibly Dead

Example (Freecell)

Page 10: 1 Search Methodologies. 2 Problem Solving l Missionaries and Cannibals l Hanoi Towers l Maze l Puzzles…

10

Contents Brute force search Depth-first search Breadth-first search Depth first iterative

deepening Properties of search

methods Implementations

Heuristics Hill climbing Best first search Beam search Optimal paths A* algorithms Uniform cost search Greedy search

Page 11: 1 Search Methodologies. 2 Problem Solving l Missionaries and Cannibals l Hanoi Towers l Maze l Puzzles…

11

Outline Problem Solving

Data-Driven (forward-chaining) Goal-Driven (back-chaining)

Brute-Force Search (exhaustive Search) Maze, Proof

Blind Search (Generate & Test) Heuristic Search (Informed Method)

DFS BFS Iterative Deepening Search Hill Climbing Best-first S. A* family

A* Alg.

Uniform Cost Search (Branch and Bound, Dijkstra)

Greedy Search

British Museum Procedure(identifying the optimal path)

Not always complete (infinite loop)

e.g.

Beam Search

Evaluation function

Page 12: 1 Search Methodologies. 2 Problem Solving l Missionaries and Cannibals l Hanoi Towers l Maze l Puzzles…

12

break

Page 13: 1 Search Methodologies. 2 Problem Solving l Missionaries and Cannibals l Hanoi Towers l Maze l Puzzles…

13

Brute Force Search Search methods that examine every node in the

search tree – also called exhaustive. Generate and test is the simplest brute force search

method: Generate possible solutions to the problem. Test each one in turn to see if it is a valid solution. Stop when a valid solution is found.

The method used to generate possible solutions must be carefully chosen.

Page 14: 1 Search Methodologies. 2 Problem Solving l Missionaries and Cannibals l Hanoi Towers l Maze l Puzzles…

14

Depth-First Search

An exhaustive search method. Follows each path to its deepest node, before

backtracking to try the next path.

Page 15: 1 Search Methodologies. 2 Problem Solving l Missionaries and Cannibals l Hanoi Towers l Maze l Puzzles…

15

Breadth-First Search

An exhaustive search method. Follows each path to a given depth before

moving on to the next depth.

Page 16: 1 Search Methodologies. 2 Problem Solving l Missionaries and Cannibals l Hanoi Towers l Maze l Puzzles…

16

Comparison of Depth-First and Breadth-First Search

It is important to choose the correct search method for a given problem.

In some situations, for example, depth first search will never find a solution, even though one exists.

e.g. 解的數量及分佈

Page 17: 1 Search Methodologies. 2 Problem Solving l Missionaries and Cannibals l Hanoi Towers l Maze l Puzzles…

17

Depth-First Iterative Deepening An exhaustive search method based on both depth-

first and breadth-first search. Carries out depth-first search to depth of 1, then to

depth of 2, 3, and so on until a goal node is found. Efficient in memory use, and can cope with infinitely

long branches. Not as inefficient in time as it might appear,

particularly for very large trees, in which it only needs to examine the largest row (the last one) once.

DFID, IDS (Iterative Deepening Search): Case 1. Resource 有限 (memory/ 旅遊時間 )Case 2. Game Tree, Min-Max 變化大,限時

一般因為記憶體有限,採用 DFS + Revocation 節省資源,否則 BFS 全記錄即可

Page 18: 1 Search Methodologies. 2 Problem Solving l Missionaries and Cannibals l Hanoi Towers l Maze l Puzzles…

18

Properties of Search Methods Complexity (say, #(states) Problem/Representation)

How much time and memory the method uses. Completeness (infinite loop? Algorithm)

A complete method is one that is guaranteed to find a goal if one exists.

Optimality & Admissibility ( Algorithm) A method is optimal (or admissible) if it is guaranteed to find the best path to the goal.

Irrevocability ( Implementation) Example: DFS to solve 15-puzzle Counter example: DFS to solve Brix

Representation: e.g. 砝碼秤重問題

有些實作的方法可以只記錄 action( 而非 state)再使用 revocation 搜尋

Page 19: 1 Search Methodologies. 2 Problem Solving l Missionaries and Cannibals l Hanoi Towers l Maze l Puzzles…

19

break

Page 20: 1 Search Methodologies. 2 Problem Solving l Missionaries and Cannibals l Hanoi Towers l Maze l Puzzles…

20

Outline Problem Solving

Data-Driven (forward-chaining) Goal-Driven (back-chaining)

Brute-Force Search (exhaustive Search) Maze, Proof

Blind Search (Generate & Test) Heuristic Search (Informed Method)

DFS BFS Iterative Deepening Search Hill Climbing Best-first S. A* family

A* Alg.

Uniform Cost Search (Branch and Bound, Dijkstra)

Greedy Search

British Museum Procedure(identifying the optimal path)

Not always complete

e.g.

Beam Search

Evaluation function

Page 21: 1 Search Methodologies. 2 Problem Solving l Missionaries and Cannibals l Hanoi Towers l Maze l Puzzles…

21

Implementations Depth-first search can be implemented using

a queue to store states But a stack makes more sense And enables us to create a recursive depth-first

search function Breadth-first search implementations are

almost identical to depth-first Except that they place states on the back of the

queue instead of on the front.

Page 22: 1 Search Methodologies. 2 Problem Solving l Missionaries and Cannibals l Hanoi Towers l Maze l Puzzles…

22

Heuristics Heuristic: a rule or other piece of information

that is used to make methods such as search more efficient or effective.

In search, often use a heuristic evaluation function, f(n): f(n) tells you the approximate distance of a node,

n, from a goal node. f(n) may not be 100% accurate, but it should

give better results than pure guesswork.

Page 23: 1 Search Methodologies. 2 Problem Solving l Missionaries and Cannibals l Hanoi Towers l Maze l Puzzles…

23

Heuristics – how informed?

The more informed a heuristic is, the better it will perform.

Heuristic h is more informed than j, if: j(n) h(n) h*(n) for all nodes n.

A search method using h will search more efficiently than one using j.

A heuristic should reduce the number of nodes that need to be examined.8-puzzles: h1(),h2(),h3(); Maze: straight-line distance

如何找 h(n)?Relaxing Problems

h(n) h*(n) 與「保證最佳解」有關,稍後 A* 完整討論

Page 24: 1 Search Methodologies. 2 Problem Solving l Missionaries and Cannibals l Hanoi Towers l Maze l Puzzles…

24

Hill Climbing An informed, irrevocable search method. Easiest to understand when considered as a

method for finding the highest point in a three dimensional search space:

Check the height one foot away from your current location in each direction; North, South, East and West.

As soon as you find a position whose height is higher than your current position, move to that location, and restart the algorithm.

DFS Hill Climbing Best First A*

展開過程中遇到更好的點,就直接走過去,走一步算一步;樹形沒記錄,向前無法回頭,不是實用的解題法。另有一說,Hill Climbing 是展開的點先排好,再 Stack 到 OPEN list 去,便可回溯。

Page 25: 1 Search Methodologies. 2 Problem Solving l Missionaries and Cannibals l Hanoi Towers l Maze l Puzzles…

25

Best-First Search Works rather like hill-climbing: Picks the most likely path (based on

heuristic value) from the partially expanded tree at each stage.

Tends to find a shorter path than depth-first or breadth-first search, but does not guarantee to find the best path.

DFS Hill Climbing Best First A*

讓最有希望的解,在 Queue 上插隊到前面去不保證最佳解的原因是 ? 題示: f = g + h

Page 26: 1 Search Methodologies. 2 Problem Solving l Missionaries and Cannibals l Hanoi Towers l Maze l Puzzles…

26

Beam Search Breadth-first method. Only expands the best few paths at

each level. Thus has the memory advantages of

depth-first search. Not exhaustive, and so may not find the

best solution. May not find a solution at all.

Page 27: 1 Search Methodologies. 2 Problem Solving l Missionaries and Cannibals l Hanoi Towers l Maze l Puzzles…

27

break

Page 28: 1 Search Methodologies. 2 Problem Solving l Missionaries and Cannibals l Hanoi Towers l Maze l Puzzles…

28

Outline Problem Solving

Data-Driven (forward-chaining) Goal-Driven (back-chaining)

Brute-Force Search (exhaustive Search) Maze, Proof

Blind Search (Generate & Test) Heuristic Search (Informed Method)

DFS BFS Iterative Deepening Search Hill Climbing Best-first S. A* family

A* Alg.

Uniform Cost Search (Branch and Bound, Dijkstra)

Greedy Search

British Museum Procedure(identifying the optimal path)

Not always complete

e.g.

Beam Search

Evaluation function

Page 29: 1 Search Methodologies. 2 Problem Solving l Missionaries and Cannibals l Hanoi Towers l Maze l Puzzles…

29

Optimal Paths An optimal path through a tree is one that is

the shortest possible path from root to goal. In other words, an optimal path has the lowest

cost (not necessarily at the shallowest depth, if edges have costs associated with them).

The British Museum procedure finds an optimal path by examining every possible path, and selecting the one with the least cost.

There are more efficient ways.BM: 所有 State 都要能回溯其 path/actions, 找出所有解再選最佳解

Page 30: 1 Search Methodologies. 2 Problem Solving l Missionaries and Cannibals l Hanoi Towers l Maze l Puzzles…

30

A* Algorithms Uses the cost function:

f(node) = g(node) + h(node). g(node) is the cost of the path so far leading

to the node. h(node) is an underestimate of how far node

is from a goal state. f is a path-based evaluation function. An A* algorithm expands paths from nodes

that have the lowest f value.

OPEN List &CLOSED List

Page 31: 1 Search Methodologies. 2 Problem Solving l Missionaries and Cannibals l Hanoi Towers l Maze l Puzzles…

31

Add START to OPEN listwhile OPEN not emptyget node n from OPEN that has the lowest f(n)if n is GOAL then return pathmove n to CLOSEDfor each n’ = CanMove(n , direction)g(n’) = g(n) + cost(n,n’)calculate f(n’)=g(n’)+h(n’)if n’ in OPEN list and new n’ is not better , continueif n’ in CLOSED list and new n’ is not better , continueremove any n’ from OPEN and CLOSEDadd n as n’s parentadd n’ to OPEN … 從 OPEN 開始 end forend whileif we get here , then there is No Solution

replacementor insertion

A

YX

Z

~~

For any X in the opt. path,如果 A…X...Z 才是 Opt. Solution 則 ZY 將被永久放在 OPEN List f(X)≤f*(X)<f*(ZY)=f(ZY)

~~ ~~其實 goal, i.e., Z, 已放在 OPEN而且經評估 h=0, 唯尚未確定其為 lowest 之前都不算找到最佳解

Page 32: 1 Search Methodologies. 2 Problem Solving l Missionaries and Cannibals l Hanoi Towers l Maze l Puzzles…

32

A* Alg (admissible heuristic) A* algorithms are optimal:

They are guaranteed to find the shortest path to a goal node.

Provided h(node) is always an underestimate. (h is an admissible heuristic).

A* methods are also optimally efficient – they expand the fewest possible paths to find the right one.

If h is not admissible, the method is called A, rather than A*.

Page 33: 1 Search Methodologies. 2 Problem Solving l Missionaries and Cannibals l Hanoi Towers l Maze l Puzzles…

33

A* Alg (example)A 8

B 5 C 9

D 3

L 1

E6 F 4

H 3

K 2G 2

M 0

244

513

13

2

52 3

42

8

45

1

7 2

1

0 OPEN A/0+8

CLOSE

1 OPEN B/4+5, E/4+6, C/2+9

CLOSE A/8

2 OPEN D/5+3, G/7+2, E/4+6, C/2+9, (E/9+6)

CLOSE A/8, B/9 E 自消不進 OPEN

3 OPEN G/6+2, (G/7+2), E/4+6, C/2+9

CLOSE A/8, B/9, D/8 G 取代既有 OPEN

4 OPEN H/7+3, E/4+6, C/9+2, M/13+0

CLOSE A/8, B/9, D/8, G/8 M 非最佳解5 OPEN E/4+6, C/9+2, M/12+0, L/11+1

CLOSE A/8, B/9, D/8, G/8, H/10

6 OPEN

CLOSE

還有「取代既有 CLOSED」未舉例Note: (1) Why OPEN list (rather than a tree…?) 不必用 DFS 或 BFS 把每條路徑都找出來比較 (2) Why “underestimate?” 試想如果 E and C 高估 , Step 6 可能已得到非最佳解 (M/12)

Page 34: 1 Search Methodologies. 2 Problem Solving l Missionaries and Cannibals l Hanoi Towers l Maze l Puzzles…

34

Uniform Cost Search Also known as branch and bound. Like A*, but uses:

f(node) = g(node); h( )=0 g(node) is the cost of the path leading up to

node. Note: All Cost >0

or Dijkstra

Recall: 此即以前看過的 Dijkstra Algorithm shortest path searching 從未要求所有連結自始展開

Page 35: 1 Search Methodologies. 2 Problem Solving l Missionaries and Cannibals l Hanoi Towers l Maze l Puzzles…

35

Greedy Search Like A*, but uses:

f(node) = h(node); g( )=0 Hence, always expands the node that

appears to be closest to a goal, regardless of what has gone before.

Not optimal, and not guaranteed to find a solution at all.

Can easily be fooled into taking poor paths.

Greedy Search: f(n)=h(n) 不變,無可能替換親源節點,所以效果和 Best-first Search 相同,差別在Gr- 有 CLOSED list ,剔除重複節點Be- 重複的節點可能再次展開

Page 36: 1 Search Methodologies. 2 Problem Solving l Missionaries and Cannibals l Hanoi Towers l Maze l Puzzles…

36

Outline Problem Solving

Data-Driven (forward-chaining) Goal-Driven (back-chaining)

Brute-Force Search (exhaustive Search) Maze, Proof

Blind Search (Generate & Test) Heuristic Search (Informed Method)

DFS BFS Iterative Deepening Search Hill Climbing Best-first S. A* family

A* Alg.

Uniform Cost Search (Branch and Bound, Dijkstra)

Greedy Search

British Museum Procedure(identifying the optimal path)

Not always complete

e.g.

Beam Search

Evaluation function

Page 37: 1 Search Methodologies. 2 Problem Solving l Missionaries and Cannibals l Hanoi Towers l Maze l Puzzles…

37

TSP using A* A B C D E

A 4 8 9 5B 4 3 5 1C 8 3 6 7D 9 5 6 2E 5 1 7 2

A

C1B1

A

E1D1

C2 E2D2

h(B1)=min(B-CDE)+ min(CDE-A)+min(C-D,D-E,E-C)|2

=1+5+(2+6)=14

h(C2)=min(C-DE)+ min(DE-A)+

min(D-E)|1=6+5+(2)=13

4 8 9 5

4+148+10 9+10 5+13

3 5 1

7+13

FYR