 Download Download Presentation Search

# Search

Télécharger la présentation ## Search

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
##### Presentation Transcript

1. Search

2. Problem State Space • What is Problem State Space? • What is valid move within the space? • Rule of the Game • Characteristic of the State Space • How to find the Solution Path in the State Space? • Search Methods

3. What is Problem State Space? • 8-Puzzel 3 1 2 3 2 1 8 8 4 4 6 5 7 6 5 7 2 3 8 3 2 2 3 1 1 4 8 4 8 1 4 7 7 6 5 5 7 6 5 6 a c b

4. State Space of 8-Puzzle(1) • List Representation (2 0 3 1 8 4 7 6 5) : Initial State (1 2 3 8 0 4 7 6 5) : Goal State (0 2 3 1 8 4 7 6 5) : Intermediate States (2 8 3 1 0 4 7 6 5) …..

5. State Space of 8-Puzzle(2) • Position Function Pos(1, 1) = 2 Pos(1, 2) = 8 …. Or Predicate? Pos(1, 1, 2) = True Empty(3, 2) = True

6. Valid Moves of 8-Puzzle • List (2 0 3 1 8 4 7 6 5)  (0 2 3 1 8 4 7 6 5) (2 0 3 1 8 4 7 6 5)  (2 8 3 1 0 4 7 6 5) (2 0 3 1 8 4 7 6 5)  (2 3 0 1 8 4 7 6 5) • Predicate (empty 1 2) (pos 1 1 x)  (empty 1 1) (pos 1 2 x)

7. Water Jug Problem • 2 water jugs : 4 liter, 3 liter Need to get exactly 2 liters of water • State space? (0, 0) : initial (2, 0) : Goal • Valid move? • (4,0) (1, 3)  (0, 1)(4, 1) (2,3) (2,0 ) • (0,3)  (3, 0)  (3, 3)  (4, 2)  (0, 2)

8. Examples • Missionaries & Cannibals Problem • Blocks world • Tower of Hamoi

9. Missionaries & Cannibals Problem • 3명의 선교사와 3명의 식인괴물(cannibal)이 강 한 쪽편에 있어요. • 보트를 이용해 강을 건너야 하는데, 보트는 1개이고 1번 건너갈 때 최대 2명을 태울수 있어요 • 그러니까 1명은 노를 저어야하죠...... • 만약 식인괴물 수가 선교사보다 많으면(같으면 괜찮아요) 선교사가 괴물한테 잡아 먹힙니다. 선교사를 무사히 살려서 6명 모두 강을 건너는 퀴즈.

10. ;;;; The Missionaries and Cannibals Domain (defun goal-test (state) "The goal is to have no missionaries or cannibals left on the first side." (= 0 (m1 state) (c1 state))) (defun successors (state) "Return a list of (action . state) pairs. An action is a triple of the form (delta-m delta-c delta-b), where a positive delta means to move from side 1 to side 2; negative is the opposite. For example, the action (1 0 1) means move one missionary and 1 boat from side 1 to side 2." (let ((pairs nil)) (loop for action in '((+1 0 +1) (0 +1 +1) (+2 0 +1) (0 +2 +1) (+1 +1 +1) (-1 0 -1) (0 -1 -1) (-2 0 -1) (0 -2 -1) (-1 -1 -1)) do (let ((new-state (take-the-boat state action))) (when (and new-state (not (cannibals-can-eat? new-state))) (push (cons action new-state) pairs)))) pairs)) (defun main () (print-actions (solve)) 'done) (defun solve () (labels ((iter (state path) (if (goal-test state) (reverse path) (let ((ss (remove-cyclic (successors state) path))) (and ss (some #'(lambda (s) (iter (cdr s) (cons s path))) (suffle ss))))))) (iter (make-cannibal-state) (list (cons 'Begin (make-cannibal-state)))))) (defun remove-cyclic (succs path) (remove-if #'(lambda (s) (member s path :test #'(lambda (a b) (equal (cdr a) (cdr b))))) succs)) (defun print-actions (path) (format t "~&~10A ~{~4S~}" "Action" '(m1 c1 b1 m2 c2 b2)) (mapc #'(lambda (p) (format t "~&~10A~{~4d~}" (car p) (cdr p))) path)) (defun suffle (lst) (sort (copy-list lst) #'> :key #'(lambda (x) (declare (ignore x)) (random 1.0))))

11. (defstruct (cannibal-state (:conc-name nil) (:type list)) "The state says how many missionaries, cannibals, and boats on each side. The components m1,c1,b1 stand for the number of missionaries, cannibals and boats, respectively, on the first side of the river. The components m2,c2,b2 are for the other side of the river." ;; We need to represent both sides (rather than just one as on [p 68]) ;; because we have generalized from 3+3 people to M+C. Incidently, we ;; also generalized from 1 boat to B boats. (m1 3) (c1 3) (b1 1) (m2 0) (c2 0) (b2 0)) (defun take-the-boat (state action) "Move a certain number of missionaries, cannibals, and boats (if possible)." (destructuring-bind (delta-m delta-c delta-b) action (if (or (and (= delta-b +1) (> (b1 state) 0)) (and (= delta-b -1) (> (b2 state) 0))) (let ((new (copy-cannibal-state state))) (decf (m1 new) delta-m) (incf (m2 new) delta-m) (decf (c1 new) delta-c) (incf (c2 new) delta-c) (decf (b1 new) delta-b) (incf (b2 new) delta-b) (if (and (>= (m1 new) 0) (>= (m2 new) 0) (>= (c1 new) 0) (>= (c2 new) 0)) new nil)) nil))) (defun cannibals-can-eat? (state) "The cannibals feast if they outnumber the missionaries on either side." (or (> (c1 state) (m1 state) 0) (> (c2 state) (m2 state) 0))) CL-USER 1 > (main) Action M1 C1 B1 M2 C2 B2 BEGIN 3 3 1 0 0 0 (0 2 1) 3 1 0 0 2 1 (0 -1 -1) 3 2 1 0 1 0 (0 2 1) 3 0 0 0 3 1 (0 -1 -1) 3 1 1 0 2 0 (2 0 1) 1 1 0 2 2 1 (-1 -1 -1) 2 2 1 1 1 0 (2 0 1) 0 2 0 3 1 1 (0 -1 -1) 0 3 1 3 0 0 (0 2 1) 0 1 0 3 2 1 (-1 0 -1) 1 1 1 2 2 0 (1 1 1) 0 0 0 3 3 1 DONE CL-USER 2 >

12. C A B Tower of Hamoi • 문제는 막대 A에 쌓여있는 원판 3개를 막대 C로 옮기는 것이다. 단 다음의 조건을 지켜야 한다. • 한 번에 하나의 원판만 이동할 수 있다 • 맨 위에 있는 원판만 이동할 수 있다 • 크기가 작은 원판위에 큰 원판이 쌓일 수 없다. • 중간의 막대를 임시적으로 이용할 수 있으나 앞의 조건들을 지켜야 한다.

13. C C B B A A C C A B B A C C A B B A C C B B A A 3개의 원판인 경우의 해답 • 3개중 위 2개의 원판을 A->B • A의제일 큰 원판을 A->C • B 에있는 2개의 원판을 B->C

14. 문제의 일반화 n-1개의 원판을 A에서 B로 옮기고 n번째 원판을 A에서 C로 옮긴 다음, n-1개의 원판을 B에서 C로 옮긴다. 필요한 변수 원판의 개수 : n 어디에서 어디로? : from  to 임시막대 1개 : tmp 따라서, 함수의 prototype n-1개의 원판 1개의 원판 C B A C B A C B A C B A n개의 원판인 경우 • void hanoi_tower(int n, char from, chartmp, char to)

15. void hanoi_tower(int n, char from, chartmp, char to) ? • if(n==1) 1. 1번 원판을 fromto 즉, AC로옮긴다. • else n>=1 이면{ 2. from의맨아래 원판을 제외한 (n-1)개의 원판을 fromtmp, 즉AB로 옮긴다. 3. 2번에서 옮기지 않은 from의 1번원판을 fromto, AC로 옮긴다. 4. 임시막대인 tmp의 원판들을 to로 옮긴다. } 2. hanoi_tower(n-1, from, to, tmp); 4. hanoi_tower(n-1, tmp, from, to);

16. 하노이탑 실습 • #include <stdio.h> • voidhanoi_tower(int n, char from, chartmp, char to) • { • if( n==1 ) printf("원판 1을 %c 에서 %c으로 옮긴다.\n",from,to); • else { • hanoi_tower(n-1, from, to, tmp); • printf("원판 %d을 %c에서 %c으로 옮긴다.\n",n, from, to); • hanoi_tower(n-1, tmp, from, to); • } • } • intmain(void) • { • hanoi_tower(4, 'A', 'B', 'C'); • }

17. Characteristics of State Space • Decomposable? goal -- subgoals • Blind vs Informed (heuristic) Search • Revocable vs Irrevocable backtracking • Predictable vs Stochastic • Optimal vs Plausible Solution

18. Search Algorithm • Guarantee to find a solution ? • Always terminate ? • Guarantee the solution to be optimal -- admissible • Complexity - time and space • How can the complexity be reduced ? • Characteristics of the space? State Space Search

19. Strategies of SS Search • Data-Driven vs. Goal Driven • Forward chaining / Backward chaining • Symptom  diagnosis : Forward eg. Fever, cough  flu • Goal/Sub-goal : Backward eg. Good Husband? Rich? Or Handsome & Edu

20. Forward vs Backward • When is forward (backward) better? • Bi-Directional • Factors deciding Forward/Backward - Size of the known states: initial, goal small to big - Branching Factor : converging - computational simplicity eg. Water jug problem

21. Weak Methods • General Purpose Control Strategies • Simplicity is the KEY of Weak Methods • Usually - Not Optimal - No Backtracking - No Guarantee to the Solution • Generate & Test • Hill Climbing • Best First Search • Means-Ends Analysis • Etc.

22. Heuristic Function • Heuristics : Knowledge obtained from human experience • Heuristic function h : SS  number a preference measure for each position of the state space • What is the heuristic function for 8-puzzle? • Necessary for Informed Search (cf. Blind)

23. Generate & Test • British Museum Algorithm • Guess a Solution & Test if it is right • DENDRAL : molecular structure analysis plan-generate-test (constraint) • AM (Automated Mathematician) Number Theory – generate a conjecture • Generate & Test is especially useful when combined with other strategy

24. Hill Climbing • Greed Method • Iterative Improvement Algorithm • Select the Best Looking Candidate Use heuristic function • Stop when no more better looking candidates  Assume it is the Goal! • No Backtracking : Simplicity!!

25. Hill Climbing Algorithm • Hill-climbing: Attempt to maximize Eval(X) by moving to the highest configuration in our moveset. 1. Let X := initial config 2. Let E := Eval(X) 3. Let N = moveset_size(X) 4. For ( i = 0 ; i<N ; i := i+1) Let Ei := Eval(move(X,i)) 5. If all Ei’s are ≤ E, terminate, return X 6. Else let i* = argmaxi Ei 7. X := move(X,i*) 8. E := Ei* 9. Goto 3

26. Hill Climbing Issues • Low Memory Requirement -- No backtracking • Moveset (next move) design is critical • Evaluation Function design is critical • If the number of move is too big -- Inefficient • Too small? -- Easily stuck (plateau)

27. Problems of Hill Climbing • Local Maxima (단기최적화) • Plateau(고원) • Ridge(산등성이)

28. Avoiding Problems in H.C. • Many Starting Points • Random Jump : Mutation (Genetic Algorithm) • Simulated Annealing Stochastic Method

29. Simulated Annealing Algorithm 1. Let X := initial config 2. Let E := Eval(X) 3. Let i = random move from the moveset 4. Let Ei := Eval(move(X,i)) 5. If E < Ei then X := move(X,i) E := Ei Else with some probability, accept the move even though things get worse: X := move(X,i) E := Ei 6. Goto 3 unless bored.

30. Simulated Annealing If Ei >= E then definitely accept the change. If Ei < E then accept the change with probability exp (-(E - Ei)/Ti) (called the Boltzman distribution) …where Ti is a “temperature” parameter that gradually decreases. • High temp: accept all moves (Random Walk) Low temp: Stochastic Hill-Climbing • When enough iterations have passed without improvement, terminate.

31. Means Ends Analysis(1) • Bi-Directional Method • GPS(General Problem Solver) – Newell • Operator, Pre-condition, Result Table • Choose the operator that reduce the difference most • Recursively reduce the first and rest parts with other operators

32. Means Ends Analysis(2) GPS(I, G, O); I:initial, G: goal, O: op table select o from O which reduce the difference GPS(I, pre-condition(o), O) print(o) GPS(result(o), G, O) end • Going to Central Park, N.Y GPS(Konkuk, C.Park, O-Table) • Operation Table op pre result ------------------------------------------------ AirPlane Inchon J.F.K AirBus 버스터미널 Inchon Taxi Konkuk Shinchon Subway J.F.K C.Park

33. Implementing Search(1) • Blind Search -- Search does not depend on the nature of the positional value -- Systematic Search Method * Breadth-First Search * Depth-First Search * Iterative Deepening Search ** Lowest Cost First Search • Heuristic Search

34. Implementing Search(2) • Blind Search • Heuristic Search * Hill Climbing * Best First Search * A* Algorithm

35. Blind Search • Abstract Data Type - Tree • (defun make-TREE(label value children) • (list ‘tree label value children)) • (defun TREE-label (tree) (second tree)) • (defun TREE-value(tree) (third tree)) • (defun TREE-children(tree)(fourth tree)) • (defun TREE-print(tree) • (princ(TREE-label tree)))

36. Depth First Search • (defun dfs(nodes goalp next) • (cond ((null nodes) nil) • ((funcall goalp (first nodes)) (first nodes)) • (t (dfs (append (funcall next (first nodes)) • (rest nodes)) • goalp • next))))

37. Function DFS • > (setq tree • (make-TREE ‘a 6 • (list (make-TREE ‘b 3 • (list(make-TREE ‘d 5 nil) • (make-TREE ‘e 4 nil)))))) • //더 추가해서 만들어 보도록 하세요 • > (dfs (list tree) • #’(lambda(x) (TREE-print x) • (eq ‘g (TREE-label x))) • #’TREE-children)

38. 1 3 2 4 5 9 6 8 7 Depth-First Search Example

39. Breadth First Search • (defun bfs(nodes goalp next) • (cond ((null nodes) nil) • ((funcall goalp (first nodes)) (first nodes)) • (t (bfs (append (rest nodes) • (funcall next (first nodes))) • goalp • next))))

40. 1 3 2 4 5 9 6 8 7 Breadth-First Search Example

41. Comparison of BFS and DFS • BFS always terminate if goal exist cf. DFS on infinite tree • BFS Guarantee shortest path to the goal (admissible) eg. Multiple Goals • Space requirement • BFS - Exponential • DFS - Linear Which is better ? BFS or DFS ?

42. Iterative Deepening Search • Compromise of BFS and DFS • Branch and Bound • Set the Limit then DFS • If Fail, extend the limit (one step) • Save on Storage, guarantee shortest path

43. Function ids • (defun ids (start goalp next depth) • (or (dfs-fd start goalp next 0 depth) • (ids start goalp next (1+ depth)))) • (defun dfs-fd(node goalp next depth max) • (cond((funcall goalp node) node) • ((= depth max) nil) • (t (some #’(lambda (n) • (dfs-fd n goalp next (1+ depth) max)) • (funcall next node)))))

44. Heuristic Search • Blind search assumes no information about the domain – Too expensive • Heuristic Information can be helpful • Heuristic Function f(n) = g(n) + h(n) f: total cost g: cost from start to node n (real value) h: cost from node n to goal (usually unknown)

45. Lowest Cost First Search • Borderline between Blind and Heuristic Search • Cost value is assigned to each arc • Moveset (candidate list) is a priority queue ordered by path cost Eg. Dijkstra’s algorithm • When Arc cost is unit value, BFS

46. Best First Search • Moveset (candidate list) is a priority queue ordered by heuristic cost h(n) • Does not guarantee the Shortest Path • Does not always terminate even though there is a solution • Performance depends on the h function

47. Best First Search - Program • (defun best(nodes goalp next comparep) • (cond ((null nodes) nil) • ((funcall goalp (first nodes)) (first nodes)) • (t (best (sort (append • (funcall next (first nodes)) • (rest nodes)) • comparep) • goalp • next • comparep))))

48. A* Algorithm • Best First Search only considers h • Lowest Cost First only considers g • A* algorithm takes f (note. f = g + h) • A* is optimal if h never overestimates the cost (optimistic) - h is admissible • See the extra slide about A* algorithm

49. And/Or Graph • Divide a problem into sub-problems and solve them individually • divide and conquer • And/Or Graph • Node: sub-problems • Links: And Link, Or Link • And : connect parent and sub-problems • Or : represents alternatives

50. Searching And/Or Graph(1) • Objective of Search • To show whether start node is Solvable? • or Unsolvable ? • Definition of Solvable • Terminal node is solvable • A non-terminal OR node is solvable if at least one of its successor is solvable • A non-terminal AND node is solvable iff all of its successors are solvable