1.09k likes | 1.22k Vues
Learn to solve optimization problems efficiently through state space search and algorithms. Explore examples and methods for finding maximum or minimum values in various scenarios.
E N D
Problem Problem • Description of valid input • Description of desired output Instance 1 Instance 2 … Instance 3 Instance N
Algorithm Instance X Algorithm Solution for X
Optimization Problem • Most problems can be described as an Optimization Problem (OP) • An instance of a OP is described by a function F(S) • A domain of S must also be describe • Called D, the set of possible solutions • The solution of the instance is • S such that F(S) is maximized (or minimized)
Optimization Problem Instance Instance X Input = Ix D: Set of possible solution Fx(S) Evaluation value of S
How to solve OP? • Simple • Iterate every possible S • Calculate F(S) for each S • Remember S that maximize F(S)
THE METHOD for OP • Try everything!!! • Pick the best one!!!!
Is this efficient? • Depends on the size of possible solution • In many problems, we have a better approach
Example • Find Max in the previous lab • Shortest Path • Find min = find max of the negative • i.e., Minimize F(S) Maximize -F(S)
More Subtle Example • Sorting • Define inversion • Inversion = number of pairs that is out of order • Ex • (3,1,2) has two inversions (a pair of 3,1 and 3,2) • Find permutation that minimize inversion
Wait • “Most problems can be described as an Optimization Problem (OP)” • Is this true? • What is a “problem”? • Description of valid input • Description of desired output • We know how to “ check” whether the output is desired. • Hence, we can use that as F(S)
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
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
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
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
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
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
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
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
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
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
Combination • Given N things • Generate all possible selections of K things from N things • Ex. N = 3, k = 2
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
Breaking the Padlock • Assuming we have four rings • Assuming each ring has following mark • • We try • • • …. • • Undone the second step, switch to another value
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
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
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)
Search Space • Set of all admissible solutions • E.g., Combination Padlock • Search space = 0000 3333
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
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’
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)
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
Search Tree 0 1 2 3 … 00 01 02 03 … … … … … … … … … … … …
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
8-queen problem • Given a chess board with 8 queens
8-queen problem • Try to place the queens so that they don’t get in the others’ ways
8-queen problem • Input: • None! • Output: • Every possible placement of 8-queens that does not jeopardize each other
Solving the Problem • Define the search space • What is the search space of this problem? • How large it is? • Choose an appropriate representation
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!!!
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
2nd attemp (first implementation) • We go over all positions • For each position, we either “choose” or “skip” that position for the queen
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
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
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);
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);