Download
state space search n.
Skip this Video
Loading SlideShow in 5 Seconds..
State Space Search PowerPoint Presentation
Download Presentation
State Space Search

State Space Search

195 Vues Download Presentation
Télécharger la présentation

State Space Search

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

  1. State Space Search Nattee Niparnan

  2. Optimization Example: Finding Max Value in an Array • There are N possible answers • The first element • The second element • 3rd, 4th … • Try all of them • Remember the best one

  3. State Space Search Framework VERY IMPORTANT!! • Define the set of admissible solution • Generate all of them (generating) • For each generated solution • Test whether it is the one we want • By the “evaluation” function (testing) • for optimization: remember the best one so far • For decision: report when we found the “correct” one

  4. Solving Problem by State Space Search • Define the set of admissible solution (the search space) • What is it? • How large? • How to represent the solution • Determine how to generate all solutions • Determine how to check each solution

  5. Generating in Steps • In most problem, admissible solutions can be generated iteratively • Step-by-step • Example • Maximum Sum of Subsequence • Minimal Spanning Tree • Pachinko Problem

  6. Search: Maximum sum of Subsequence • Search Space • Every possible sequence • Described by a pair of starting,ending element • Generating all solutions • Step 1: select the position of the first element • Step 2: select the position of the last element • Checking each solution • Sum and test whether it is maximum

  7. Search: Minimal Spanning Tree • Search Space • Every subset of edges • Described by a set of edges • Generating all solutions • For every edge • Either include or exclude it from the set • Checking each solution • Sum the cost in the set • and test whether it is maximum • Test whether it is a tree • Test whether it is connected

  8. Search: Minimal Spanning Tree 2nd attemp • Search Space • Every subset of edges of size |V|-1 • Described by a set of edges • Generating all solutions • For every edge • Either include or exclude it from the set • Do not select more than |V|-1 edges • Checking each solution • Sum the cost in the set • and test whether it is maximum • Test whether it is a tree

  9. Search: Minimal Spanning Tree 3rd attemp • Search Space • Every subgraph of size |V|-1 edge that is a tree • Described by a set of edges • Generating all solutions • For every edge in X in “cut property” • Either include or exclude it from the set • Update the tree • Do not select more than |V|-1 edges • Checking each solution • Sum the cost in the set • and test whether it is maximum

  10. Search: Pachinko • Search Space • Every path from the top pin to the bottom pin • Described by a sequence of direction • (left,left,right,left) • Generating all solutions • For every level • Choose either the left side or right side • Checking each solution • Sum the cost in the path • and test whether it is maximum

  11. Generating all possible answers

  12. Combination and Permutation • In many case, the set of the admissible solutions is a set of “combination” or “permutation” of something • We need to knows how to generate all permutations and combinations

  13. Combination • Given N things • Generate all possible selections of K things from N things • Ex. N = 3, k = 2

  14. Combination with replacement • Given N things • Generate all possible selections of K things from N things • When something is selected, we are permit to select that things again (we replace the selected thing in the pool) • Ex. N = 3, k = 2

  15. Breaking the Padlock

  16. Breaking the Padlock • Assuming we have four rings • Assuming each ring has following mark •    • We try •  •  • …. •   •  Undone the second step, switch to another value

  17. Key Idea • A problem consists of several similar steps • Choosing a things from the pool • We need to remember the things we’ve done so far

  18. General Framework Initial Step 1. Get a step that is not complete Engine Storage Gemerated (partial) solution 2. Try all possible choice in next step 3. Store each newly generated next step

  19. Solution Generation • Storage s • s  ‘’ • While s is not empty • Curr  s.get • If Curr is the last step • evaluate • Else • Generate all next step from Curr • put them into S If length(curr) == 4 For all symbol i New = curr+I Storage.push(new)

  20. Search Space • Set of all admissible solutions • E.g., Combination Padlock • Search space = 0000  3333

  21. Search Space Enumeration • Key Idea: generate step-by-step, • Undo previous step when necessary • Usually using some data structure to implement the storage • E.g., using stack, either explicitly or implicitly from the processor stack • i.e., using the “recursive” paradigm • Queue is a possible choice

  22. Example: Padlock • Generate all combinations of lock key • Represent key by int • =0 • =1 • =2 • =3 • Step = sequence of selected symbols • E.g., • ’01’    • ‘0003’     

  23. Example: Padlock void search(intstep,int *sol) { if (step < num_step) { for (int i =0; i < num_symbol; i++){ sol[step]=i; search(step + 1,sol); } } else { check(sol); } } • The process automatically remember the step (by sol array) and by stack (int i)

  24. Search Tree • A tree representing every step in the seach • Similar to Recursion Tree • Actually, it is related • Node: • Each step in solution generation • Edge: • Connects two nodes such that one node is generated from applying one step to the other node

  25. Search Tree 0 1 2 3 … 00 01 02 03 … … … … … … … … … … … …

  26. Search Tree • Sols in each step • 0 • 00 • 000 • 0000  check • 000 • 0001  check • 000 • 0002  check • 000 • 0003  check • 000 • 00 • 001 • 0010  check

  27. 8-Queen Problem

  28. 8-queen problem • Given a chess board with 8 queens

  29. 8-queen problem • Try to place the queens so that they don’t get in the others’ ways

  30. 8-queen problem • Input: • None! • Output: • Every possible placement of 8-queens that does not jeopardize each other

  31. Solving the Problem • Define the search space • What is the search space of this problem? • How large it is? • Choose an appropriate representation

  32. 1st attemp • Every possible placement of queens • Size: 648 • Representation: a set of queens position • E.g., (1,1) (1,2) (2,5) (4,1) (1,2) (3,4) (8,8) (7,6) • This includes overlapping placement!!!

  33. 2nd attemp • Another representation • Try to exclude overlapping • Use combination without replacement • This is a combination • Selecting 8 positions out of 64 positions • Size: (64)! / (64 – 8)! * 8! • Implementation: in the “generating next step”, check for overlapping

  34. 2nd attemp (first implementation) • We go over all positions • For each position, we either “choose” or “skip” that position for the queen

  35. Combination without replacement Mark_on_board is a binary array to indicate whether the position is selected void e_queen(intstep,int *mark_on_board) { if (step < 64) { mark_on_board[step] = 0; e_queen(step + 1,mark_on_board); mark_on_board[step] = 1; e_queen(step + 1,mark_on_board); } else { check(mark_on_board); } } * Check if OK * Also has to check whether we mark exactly 8 queens

  36. 2nd attemp Issue • The generated mark_on_board includes • 000000000000  select no position (obviously not the answer) • 111111000000  select 6 positions (obviously not the answer) • We must limit our selection to be exactly 8

  37. 2nd attemp (second implementation) void e_queen(intstep,int *mark_on_board,int chosen) { if (step < 64) { if ((64 – 8) – (step – chosen) > 0) { mark_on_board[step] = 0; e_queen(step + 1,mark_on_board,chosen); } if (8 - chosen > 0) { mark_on_board[step] = 1; e_queen(step + 1,mark_on_board,chosen+1); } } else { check(mark_on_board); } } Number of possible 0 Number of possible 1 e_queen(0,mark,0);

  38. 2nd attemp (second implementation) rev 2.0 void e_queen(int step,int *mark_on_board,int one,int zero) { if (step < 64) { if (zero > 0) { mark_on_board[step] = 0; e_queen(step + 1,mark_on_board,one,zero - 1); } if (one > 0) { mark_on_board[step] = 1; e_queen(step + 1,mark_on_board,one – 1,zero); } } else { check(mark_on_board); } } Number of possible 1 Number of possible 0 e_queen(0,mark,8,64 - 8);

  39. 3rd attemp • Any better way? • For each row, there should be only one queen • The problem consists of 8 step • Placing each queen • Size: 88 • Representation: sequence of columns • E.g., (1,2,3,4,5,6,7,8)

  40. 3rd attemp Implementation • There are eight possible ways in each step • There are eight steps • Very similar to the combination problem void e_queen(intstep,int *queen_pos) { if (step < 8) { for (inti = 0; i < 8; i++) { queen_pos[step] = i; e_queen(step + 1, queen_pos); } } else { check(queen_pos); } }

  41. 4th attemp • Queen should not be in the same column • The solution should never have any column repeated • E.g., • (1,2,3,4,5,6,7,1) is bad (column collision • (1,1,3,4,5,6,7,5) is bad as well…. • (1,2,3,4,5,6,7,8) is good • There should be no duplicate column index!!!

  42. Permutation • Given N symbols • A permutation is the element arrange in any order • E.g., 1 2 3 4 • Shows • 1 2 3 4 • 1 2 4 3 • 1 3 2 4 • 1 3 4 2 • … • 4 3 2 1 • For each step, we have to known which one is used

  43. Permutation • The problem consists of several similar steps • Special condition • Symbols never repeat • How to do? • Easy way: • Generate all combination (as done before) • Check for ones that symbols do not repeat • Better way: • Remember what symbols are used

  44. Permutation void search(intstep,int *sol) { if (step < num_step) { for (inti = 0; i < num_symbol; i++) { if not_used(sol,i,step) { sol[step] = i; search(step,sol); } } } else { check(sol); } } Boolnot_used(int *sol,intvalue,int step) { for (inti = 0;i < step; i++) { if (sol[i] == value) return false; } return true; }

  45. Permutation • More proper ways • void search(intstep,int*sol,bool *used) • { • if (step < num_step) { • for (inti= 0; i < num_symbol; i++) { • if (!used[i]) { • used[i] = true; • sol[step]= i; • search(step,sol,used); • used[i] = false; • } • } • } else { • check(sol); • } • }

  46. Incresing Sequence • Given N • Find any sequence of (a1,a2,a3,…) such that • a1 +a2 + a3 +… + ak = N • ai > 0 • ai<= aj for all i < j • aiis an integer

  47. Example • N = 4 • 1 + 1 + 1 + 1 • 1 + 1 + 2 • 1 + 3 • 2 + 2 • 4

  48. DFS and BFS Algorithm

  49. Breadth First Search and Depth First Search • Can we draw a search tree from this pseudo-code? • Assume that we know how to generate next step? • Do we know the growth (in term of structure, not size) of the search tree? • Storage s • s  ‘’ • While s is not empty • Curr  s.get • If Curr is the last step • evaluate • Else • Generate all next step from Curr • put them into S

  50. Breadth First Search and Depth First Search • Search Tree grows according to S • and how to generate all next step as well, • If S is “stack”, the algorithm is called DFS • If S is “queue”, the algorithm is called BFS • Storage s • s  ‘’ • While s is not empty • Curr  s.get • If Curr is the last step • evaluate • Else • Generate all next step from Curr • put them into S