g enerate and t est f ramework nattee niparnan. o ptimization e xample : f inding m ax v alue in an...
TRANSCRIPT
![Page 1: G ENERATE AND T EST F RAMEWORK Nattee Niparnan. O PTIMIZATION E XAMPLE : F INDING M AX V ALUE IN AN A RRAY 2523443490-5870561 There are N possible answers](https://reader036.vdocuments.site/reader036/viewer/2022070410/56649f145503460f94c287b9/html5/thumbnails/1.jpg)
GENERATE AND TEST FRAMEWORKNattee Niparnan
![Page 2: G ENERATE AND T EST F RAMEWORK Nattee Niparnan. O PTIMIZATION E XAMPLE : F INDING M AX V ALUE IN AN A RRAY 2523443490-5870561 There are N possible answers](https://reader036.vdocuments.site/reader036/viewer/2022070410/56649f145503460f94c287b9/html5/thumbnails/2.jpg)
OPTIMIZATION EXAMPLE: FINDING MAX VALUE IN AN ARRAY
25
2 34
43 4 9 0 -5 87
0 5 6 1
There are N possible answersThe first elementThe second element3rd, 4th …
Try all of themRemember the best one
![Page 3: G ENERATE AND T EST F RAMEWORK Nattee Niparnan. O PTIMIZATION E XAMPLE : F INDING M AX V ALUE IN AN A RRAY 2523443490-5870561 There are N possible answers](https://reader036.vdocuments.site/reader036/viewer/2022070410/56649f145503460f94c287b9/html5/thumbnails/3.jpg)
GENERATE AND TEST FRAMEWORK
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
![Page 4: G ENERATE AND T EST F RAMEWORK Nattee Niparnan. O PTIMIZATION E XAMPLE : F INDING M AX V ALUE IN AN A RRAY 2523443490-5870561 There are N possible answers](https://reader036.vdocuments.site/reader036/viewer/2022070410/56649f145503460f94c287b9/html5/thumbnails/4.jpg)
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
![Page 5: G ENERATE AND T EST F RAMEWORK Nattee Niparnan. O PTIMIZATION E XAMPLE : F INDING M AX V ALUE IN AN A RRAY 2523443490-5870561 There are N possible answers](https://reader036.vdocuments.site/reader036/viewer/2022070410/56649f145503460f94c287b9/html5/thumbnails/5.jpg)
GENERATING ALL POSSIBLE ANSWER
![Page 6: G ENERATE AND T EST F RAMEWORK Nattee Niparnan. O PTIMIZATION E XAMPLE : F INDING M AX V ALUE IN AN A RRAY 2523443490-5870561 There are N possible answers](https://reader036.vdocuments.site/reader036/viewer/2022070410/56649f145503460f94c287b9/html5/thumbnails/6.jpg)
COMBINATION
Given N things Generate all possible selections of K things
from N things
Ex. N = 3, k = 2
![Page 7: G ENERATE AND T EST F RAMEWORK Nattee Niparnan. O PTIMIZATION E XAMPLE : F INDING M AX V ALUE IN AN A RRAY 2523443490-5870561 There are N possible answers](https://reader036.vdocuments.site/reader036/viewer/2022070410/56649f145503460f94c287b9/html5/thumbnails/7.jpg)
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
![Page 8: G ENERATE AND T EST F RAMEWORK Nattee Niparnan. O PTIMIZATION E XAMPLE : F INDING M AX V ALUE IN AN A RRAY 2523443490-5870561 There are N possible answers](https://reader036.vdocuments.site/reader036/viewer/2022070410/56649f145503460f94c287b9/html5/thumbnails/8.jpg)
BREAKING THE PADLOCK
![Page 9: G ENERATE AND T EST F RAMEWORK Nattee Niparnan. O PTIMIZATION E XAMPLE : F INDING M AX V ALUE IN AN A RRAY 2523443490-5870561 There are N possible answers](https://reader036.vdocuments.site/reader036/viewer/2022070410/56649f145503460f94c287b9/html5/thumbnails/9.jpg)
BREAKING THE PADLOCK Assuming we have four rings Assuming each ring has following mark
We try
…. Backtracking
Undone the second step,
switch to another value
![Page 10: G ENERATE AND T EST F RAMEWORK Nattee Niparnan. O PTIMIZATION E XAMPLE : F INDING M AX V ALUE IN AN A RRAY 2523443490-5870561 There are N possible answers](https://reader036.vdocuments.site/reader036/viewer/2022070410/56649f145503460f94c287b9/html5/thumbnails/10.jpg)
KEY IDEA
A problem consists of several similar steps Choosing a things from the pool
We need to remember the things we done so far
![Page 11: G ENERATE AND T EST F RAMEWORK Nattee Niparnan. O PTIMIZATION E XAMPLE : F INDING M AX V ALUE IN AN A RRAY 2523443490-5870561 There are N possible answers](https://reader036.vdocuments.site/reader036/viewer/2022070410/56649f145503460f94c287b9/html5/thumbnails/11.jpg)
GENERAL FRAMEWORK
Storage
Step that have been done
Engine
Initial Step
1. Get a step that is not complete
2. Try any possible next step
3. Store each newly generated next step
![Page 12: G ENERATE AND T EST F RAMEWORK Nattee Niparnan. O PTIMIZATION E XAMPLE : F INDING M AX V ALUE IN AN A RRAY 2523443490-5870561 There are N possible answers](https://reader036.vdocuments.site/reader036/viewer/2022070410/56649f145503460f94c287b9/html5/thumbnails/12.jpg)
COMBINATION
Generate all combinations of lock key Represent key by int
=0=1=2=3
Step = sequence of selected symbols
E.g., ’01’ ‘0003’
![Page 13: G ENERATE AND T EST F RAMEWORK Nattee Niparnan. O PTIMIZATION E XAMPLE : F INDING M AX V ALUE IN AN A RRAY 2523443490-5870561 There are N possible answers](https://reader036.vdocuments.site/reader036/viewer/2022070410/56649f145503460f94c287b9/html5/thumbnails/13.jpg)
GENERAL SEARCH Storage s S ‘’ While s is not empty
Curr Storage.get If Curr is the last step
evaluate Else
Generate all next step from Curr Push them to S
For all symbol i New = curr+I Storage.push(new)
If length(curr) == 4
![Page 14: G ENERATE AND T EST F RAMEWORK Nattee Niparnan. O PTIMIZATION E XAMPLE : F INDING M AX V ALUE IN AN A RRAY 2523443490-5870561 There are N possible answers](https://reader036.vdocuments.site/reader036/viewer/2022070410/56649f145503460f94c287b9/html5/thumbnails/14.jpg)
SEARCH SPACE
Set of all admissible solutions E.g., Combination Padlock
Search space = 0000 4444 For a task with distinct steps it is easy to
enumerate the search space with “backtracking”
for most of the case, we can do “backtracking”
![Page 15: G ENERATE AND T EST F RAMEWORK Nattee Niparnan. O PTIMIZATION E XAMPLE : F INDING M AX V ALUE IN AN A RRAY 2523443490-5870561 There are N possible answers](https://reader036.vdocuments.site/reader036/viewer/2022070410/56649f145503460f94c287b9/html5/thumbnails/15.jpg)
BACKTRACKING
A tool to enumerate the search space Usually using the “stack” to implement the
storage Employ the processor stack i.e., using the “recursive” paradigm
![Page 16: G ENERATE AND T EST F RAMEWORK Nattee Niparnan. O PTIMIZATION E XAMPLE : F INDING M AX V ALUE IN AN A RRAY 2523443490-5870561 There are N possible answers](https://reader036.vdocuments.site/reader036/viewer/2022070410/56649f145503460f94c287b9/html5/thumbnails/16.jpg)
COMBINATION EXAMPLE BY BACKTRACKING
The process automatically remember the step
void backtracking(int step,int *sol) { if (step < num_step) { for (int i = 0; i < num_symbol; i++) {
sol[step] = i; backtracking(step + 1,sol);}
} else { check(sol); }}
![Page 17: G ENERATE AND T EST F RAMEWORK Nattee Niparnan. O PTIMIZATION E XAMPLE : F INDING M AX V ALUE IN AN A RRAY 2523443490-5870561 There are N possible answers](https://reader036.vdocuments.site/reader036/viewer/2022070410/56649f145503460f94c287b9/html5/thumbnails/17.jpg)
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
![Page 18: G ENERATE AND T EST F RAMEWORK Nattee Niparnan. O PTIMIZATION E XAMPLE : F INDING M AX V ALUE IN AN A RRAY 2523443490-5870561 There are N possible answers](https://reader036.vdocuments.site/reader036/viewer/2022070410/56649f145503460f94c287b9/html5/thumbnails/18.jpg)
SEARCH TREE
0 1 2 3
01 02 03 04 …
… … … …… … … …… … … …
![Page 19: G ENERATE AND T EST F RAMEWORK Nattee Niparnan. O PTIMIZATION E XAMPLE : F INDING M AX V ALUE IN AN A RRAY 2523443490-5870561 There are N possible answers](https://reader036.vdocuments.site/reader036/viewer/2022070410/56649f145503460f94c287b9/html5/thumbnails/19.jpg)
8-QUEEN
Given a chess board 8 queens
X X X
X X X
X X X X
X X X
X X X
![Page 20: G ENERATE AND T EST F RAMEWORK Nattee Niparnan. O PTIMIZATION E XAMPLE : F INDING M AX V ALUE IN AN A RRAY 2523443490-5870561 There are N possible answers](https://reader036.vdocuments.site/reader036/viewer/2022070410/56649f145503460f94c287b9/html5/thumbnails/20.jpg)
8-QUEENS PROBLEM
Try to place the queens so that they don’t get in the others’ ways
Q
Q
Q
Q
Q
Q
Q
Q
![Page 21: G ENERATE AND T EST F RAMEWORK Nattee Niparnan. O PTIMIZATION E XAMPLE : F INDING M AX V ALUE IN AN A RRAY 2523443490-5870561 There are N possible answers](https://reader036.vdocuments.site/reader036/viewer/2022070410/56649f145503460f94c287b9/html5/thumbnails/21.jpg)
SOLVING THE PROBLEM
Define the search space What is the search space of this problem? How large it is?
Choose an appropriate representation
![Page 22: G ENERATE AND T EST F RAMEWORK Nattee Niparnan. O PTIMIZATION E XAMPLE : F INDING M AX V ALUE IN AN A RRAY 2523443490-5870561 There are N possible answers](https://reader036.vdocuments.site/reader036/viewer/2022070410/56649f145503460f94c287b9/html5/thumbnails/22.jpg)
EXAMPLE
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!!!
![Page 23: G ENERATE AND T EST F RAMEWORK Nattee Niparnan. O PTIMIZATION E XAMPLE : F INDING M AX V ALUE IN AN A RRAY 2523443490-5870561 There are N possible answers](https://reader036.vdocuments.site/reader036/viewer/2022070410/56649f145503460f94c287b9/html5/thumbnails/23.jpg)
EXAMPLE
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
![Page 24: G ENERATE AND T EST F RAMEWORK Nattee Niparnan. O PTIMIZATION E XAMPLE : F INDING M AX V ALUE IN AN A RRAY 2523443490-5870561 There are N possible answers](https://reader036.vdocuments.site/reader036/viewer/2022070410/56649f145503460f94c287b9/html5/thumbnails/24.jpg)
COMBINATION WITHOUT REPLACEMENT
We go over all position For each position, we either “choose” or “skip”
that position for the queen
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
![Page 25: G ENERATE AND T EST F RAMEWORK Nattee Niparnan. O PTIMIZATION E XAMPLE : F INDING M AX V ALUE IN AN A RRAY 2523443490-5870561 There are N possible answers](https://reader036.vdocuments.site/reader036/viewer/2022070410/56649f145503460f94c287b9/html5/thumbnails/25.jpg)
COMBINATION WITHOUT REPLACEMENT
void e_queen(int step,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); }}
Also has to check whether
we mark exactly 8 spots
Mark_on_board is a binary string indicate that whether
the position is selected
![Page 26: G ENERATE AND T EST F RAMEWORK Nattee Niparnan. O PTIMIZATION E XAMPLE : F INDING M AX V ALUE IN AN A RRAY 2523443490-5870561 There are N possible answers](https://reader036.vdocuments.site/reader036/viewer/2022070410/56649f145503460f94c287b9/html5/thumbnails/26.jpg)
COMBINATION WITHOUT REPLACEMENT
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 4
![Page 27: G ENERATE AND T EST F RAMEWORK Nattee Niparnan. O PTIMIZATION E XAMPLE : F INDING M AX V ALUE IN AN A RRAY 2523443490-5870561 There are N possible answers](https://reader036.vdocuments.site/reader036/viewer/2022070410/56649f145503460f94c287b9/html5/thumbnails/27.jpg)
COMBINATION WITHOUT REPLACEMENT
void e_queen(int step,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
![Page 28: G ENERATE AND T EST F RAMEWORK Nattee Niparnan. O PTIMIZATION E XAMPLE : F INDING M AX V ALUE IN AN A RRAY 2523443490-5870561 There are N possible answers](https://reader036.vdocuments.site/reader036/viewer/2022070410/56649f145503460f94c287b9/html5/thumbnails/28.jpg)
EXAMPLE
Any better way? For each row, there should be only one queen
Size: 88
Representation: sequence of columns E.g., (1,2,3,4,5,6,7,8)
![Page 29: G ENERATE AND T EST F RAMEWORK Nattee Niparnan. O PTIMIZATION E XAMPLE : F INDING M AX V ALUE IN AN A RRAY 2523443490-5870561 There are N possible answers](https://reader036.vdocuments.site/reader036/viewer/2022070410/56649f145503460f94c287b9/html5/thumbnails/29.jpg)
USING BACKTRACKING?
The problem consists of 8 step Placing each queen
We never sure whether the queen we place would lead to a solution
Backtracking is an appropriate way
![Page 30: G ENERATE AND T EST F RAMEWORK Nattee Niparnan. O PTIMIZATION E XAMPLE : F INDING M AX V ALUE IN AN A RRAY 2523443490-5870561 There are N possible answers](https://reader036.vdocuments.site/reader036/viewer/2022070410/56649f145503460f94c287b9/html5/thumbnails/30.jpg)
SOLVING THE PROBLEM: ENUMERATING SEARCH SPACE There are eight possible ways in each step There are eight steps Very similar to the combination problem
void e_queen(int step,int *queen_pos) { if (step < 8) { for (int i = 0; i < 8; i++) {
queen_pos[step] = i; e_queen(step + 1, queen_pos);}
} else { check(queen_pos); }}
![Page 31: G ENERATE AND T EST F RAMEWORK Nattee Niparnan. O PTIMIZATION E XAMPLE : F INDING M AX V ALUE IN AN A RRAY 2523443490-5870561 There are N possible answers](https://reader036.vdocuments.site/reader036/viewer/2022070410/56649f145503460f94c287b9/html5/thumbnails/31.jpg)
8-QUEEN BY PERMUTATION
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!!!
![Page 32: G ENERATE AND T EST F RAMEWORK Nattee Niparnan. O PTIMIZATION E XAMPLE : F INDING M AX V ALUE IN AN A RRAY 2523443490-5870561 There are N possible answers](https://reader036.vdocuments.site/reader036/viewer/2022070410/56649f145503460f94c287b9/html5/thumbnails/32.jpg)
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
![Page 33: G ENERATE AND T EST F RAMEWORK Nattee Niparnan. O PTIMIZATION E XAMPLE : F INDING M AX V ALUE IN AN A RRAY 2523443490-5870561 There are N possible answers](https://reader036.vdocuments.site/reader036/viewer/2022070410/56649f145503460f94c287b9/html5/thumbnails/33.jpg)
PERMUTATION BY BACKTRACKING 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
![Page 34: G ENERATE AND T EST F RAMEWORK Nattee Niparnan. O PTIMIZATION E XAMPLE : F INDING M AX V ALUE IN AN A RRAY 2523443490-5870561 There are N possible answers](https://reader036.vdocuments.site/reader036/viewer/2022070410/56649f145503460f94c287b9/html5/thumbnails/34.jpg)
PERMUTATIONvoid backtracking(int step,int *sol) { if (step < num_step) { for (int i = 0; i < num_symbol; i++) { if not_used(sol,i,step) {
sol[step] = i; backtracking(step,sol);
}}
} else { check(sol); }}
Bool not_used(int *sol,int value,int step) { for (int i = 0;i < step; i++) { if (sol[i] == value) return false; } return true;}
![Page 35: G ENERATE AND T EST F RAMEWORK Nattee Niparnan. O PTIMIZATION E XAMPLE : F INDING M AX V ALUE IN AN A RRAY 2523443490-5870561 There are N possible answers](https://reader036.vdocuments.site/reader036/viewer/2022070410/56649f145503460f94c287b9/html5/thumbnails/35.jpg)
PERMUTATION
More proper ways
void backtracking(int step,int *sol,bool *used) { if (step < num_step) { for (int i = 0; i < num_symbol; i++) { if (!used[i]) {
used[i] = true;sol[step] = i;
backtracking(step,sol,used); used[i] = false; }
} } else { check(sol); }}
![Page 36: G ENERATE AND T EST F RAMEWORK Nattee Niparnan. O PTIMIZATION E XAMPLE : F INDING M AX V ALUE IN AN A RRAY 2523443490-5870561 There are N possible answers](https://reader036.vdocuments.site/reader036/viewer/2022070410/56649f145503460f94c287b9/html5/thumbnails/36.jpg)
BACKTRACKING PROBLEMS
Given N Find any sequence of (a1,a2,a3,…) such that
a1 +a2 + a3 +… + ak = N ai > 0 ai <= aj for all i < j ai is an integer
![Page 37: G ENERATE AND T EST F RAMEWORK Nattee Niparnan. O PTIMIZATION E XAMPLE : F INDING M AX V ALUE IN AN A RRAY 2523443490-5870561 There are N possible answers](https://reader036.vdocuments.site/reader036/viewer/2022070410/56649f145503460f94c287b9/html5/thumbnails/37.jpg)
EXAMPLE
N = 4 1 + 1 + 1 + 1 1 + 1 + 2 1 + 3 2 + 2 4
![Page 38: G ENERATE AND T EST F RAMEWORK Nattee Niparnan. O PTIMIZATION E XAMPLE : F INDING M AX V ALUE IN AN A RRAY 2523443490-5870561 There are N possible answers](https://reader036.vdocuments.site/reader036/viewer/2022070410/56649f145503460f94c287b9/html5/thumbnails/38.jpg)
SOLVING WITH BACKTRACKING
Representation Array of ai
Step Choosing the next value for ai
![Page 39: G ENERATE AND T EST F RAMEWORK Nattee Niparnan. O PTIMIZATION E XAMPLE : F INDING M AX V ALUE IN AN A RRAY 2523443490-5870561 There are N possible answers](https://reader036.vdocuments.site/reader036/viewer/2022070410/56649f145503460f94c287b9/html5/thumbnails/39.jpg)
BRANCH & BOUNDTechnique to reduce enumeration
![Page 40: G ENERATE AND T EST F RAMEWORK Nattee Niparnan. O PTIMIZATION E XAMPLE : F INDING M AX V ALUE IN AN A RRAY 2523443490-5870561 There are N possible answers](https://reader036.vdocuments.site/reader036/viewer/2022070410/56649f145503460f94c287b9/html5/thumbnails/40.jpg)
MAIN IDEA
We should not enumerate solution that will never produce a solution
We have done that!!! 8-queens By naïve combination, we will have to do all 648
But, by each improvement, we further reduce what we have to do
![Page 41: G ENERATE AND T EST F RAMEWORK Nattee Niparnan. O PTIMIZATION E XAMPLE : F INDING M AX V ALUE IN AN A RRAY 2523443490-5870561 There are N possible answers](https://reader036.vdocuments.site/reader036/viewer/2022070410/56649f145503460f94c287b9/html5/thumbnails/41.jpg)
PERMUTATION
0 1 2
10 11 12 20 21 2200 01 02
000
001
002
010
011
012
020
021
022
100
101
102
110
111
112
120
121
122
200
201
202
210
211
212
220
221
222
![Page 42: G ENERATE AND T EST F RAMEWORK Nattee Niparnan. O PTIMIZATION E XAMPLE : F INDING M AX V ALUE IN AN A RRAY 2523443490-5870561 There are N possible answers](https://reader036.vdocuments.site/reader036/viewer/2022070410/56649f145503460f94c287b9/html5/thumbnails/42.jpg)
PERMUTATION
0 1 2
10 11 12 20 21 2200 01 02
000
001
002
010
011
012
020
021
022
100
101
102
110
111
112
120
121
122
200
201
202
210
211
212
220
221
222
![Page 43: G ENERATE AND T EST F RAMEWORK Nattee Niparnan. O PTIMIZATION E XAMPLE : F INDING M AX V ALUE IN AN A RRAY 2523443490-5870561 There are N possible answers](https://reader036.vdocuments.site/reader036/viewer/2022070410/56649f145503460f94c287b9/html5/thumbnails/43.jpg)
KEY
If we know, at any step, that the solution is not feasible Then, it is futile to further search along that path
![Page 44: G ENERATE AND T EST F RAMEWORK Nattee Niparnan. O PTIMIZATION E XAMPLE : F INDING M AX V ALUE IN AN A RRAY 2523443490-5870561 There are N possible answers](https://reader036.vdocuments.site/reader036/viewer/2022070410/56649f145503460f94c287b9/html5/thumbnails/44.jpg)
OPTIMIZATION PROBLEM
All previous examples are decision problems Asking whether the solution satisfy the criteria
Now, we consider broader set of problem, the optimization problem
Example For all students, find one with maximum height
![Page 45: G ENERATE AND T EST F RAMEWORK Nattee Niparnan. O PTIMIZATION E XAMPLE : F INDING M AX V ALUE IN AN A RRAY 2523443490-5870561 There are N possible answers](https://reader036.vdocuments.site/reader036/viewer/2022070410/56649f145503460f94c287b9/html5/thumbnails/45.jpg)
EVALUATION
Representation: x = id of student “goodness evaluation” evaluate(x)
For all x in the search space, we have to find one with maximum evaluate(x)
![Page 46: G ENERATE AND T EST F RAMEWORK Nattee Niparnan. O PTIMIZATION E XAMPLE : F INDING M AX V ALUE IN AN A RRAY 2523443490-5870561 There are N possible answers](https://reader036.vdocuments.site/reader036/viewer/2022070410/56649f145503460f94c287b9/html5/thumbnails/46.jpg)
SOLVING OPTIMIZATION PROBLEM
Enumerate all possible solution Calculate its value
Remember the max
void backtracking(int step,int *sol) { if (step < num_step) { for (int i = 0; i < num_symbol; i++) {
sol[step] = i; backtracking(step,sol);}
} else { value = evaluate(sol); if (value > max) remember(value,sol); }}
![Page 47: G ENERATE AND T EST F RAMEWORK Nattee Niparnan. O PTIMIZATION E XAMPLE : F INDING M AX V ALUE IN AN A RRAY 2523443490-5870561 There are N possible answers](https://reader036.vdocuments.site/reader036/viewer/2022070410/56649f145503460f94c287b9/html5/thumbnails/47.jpg)
BRANCH & BOUND IN OPTIMIZATION PROBLEM For many problems, it is possible to assert its
goodness even the solution is not complete If we can predict the best value for the
remaining step, then we can use that value to “bound” our search
![Page 48: G ENERATE AND T EST F RAMEWORK Nattee Niparnan. O PTIMIZATION E XAMPLE : F INDING M AX V ALUE IN AN A RRAY 2523443490-5870561 There are N possible answers](https://reader036.vdocuments.site/reader036/viewer/2022070410/56649f145503460f94c287b9/html5/thumbnails/48.jpg)
EXAMPLE
Assuming that we have 10 steps At step 7, the goodness of the partial solution
is X Assuming that we know that the remaining
step could not produce a solution better than Y If we have found a solution better than X+Y
We can simply “bound” the search
![Page 49: G ENERATE AND T EST F RAMEWORK Nattee Niparnan. O PTIMIZATION E XAMPLE : F INDING M AX V ALUE IN AN A RRAY 2523443490-5870561 There are N possible answers](https://reader036.vdocuments.site/reader036/viewer/2022070410/56649f145503460f94c287b9/html5/thumbnails/49.jpg)
KEYS
We must know the so-called “upper bound” of the remaining step It should be compute easily
![Page 50: G ENERATE AND T EST F RAMEWORK Nattee Niparnan. O PTIMIZATION E XAMPLE : F INDING M AX V ALUE IN AN A RRAY 2523443490-5870561 There are N possible answers](https://reader036.vdocuments.site/reader036/viewer/2022070410/56649f145503460f94c287b9/html5/thumbnails/50.jpg)
EXAMPLE
23 35 2
Let value at this point be 10
If we know that this path never bet higher than 13 (which make
10 + 13 < 35)We can neglect it
![Page 51: G ENERATE AND T EST F RAMEWORK Nattee Niparnan. O PTIMIZATION E XAMPLE : F INDING M AX V ALUE IN AN A RRAY 2523443490-5870561 There are N possible answers](https://reader036.vdocuments.site/reader036/viewer/2022070410/56649f145503460f94c287b9/html5/thumbnails/51.jpg)
KNAPSACK PROBLEM
![Page 52: G ENERATE AND T EST F RAMEWORK Nattee Niparnan. O PTIMIZATION E XAMPLE : F INDING M AX V ALUE IN AN A RRAY 2523443490-5870561 There are N possible answers](https://reader036.vdocuments.site/reader036/viewer/2022070410/56649f145503460f94c287b9/html5/thumbnails/52.jpg)
KNAPSACK PROBLEM
Given a sack, able to hold K kg Given a list of objects
Each has a weight and a value Try to pack the object in the sack so that the
total value is maximized
![Page 53: G ENERATE AND T EST F RAMEWORK Nattee Niparnan. O PTIMIZATION E XAMPLE : F INDING M AX V ALUE IN AN A RRAY 2523443490-5870561 There are N possible answers](https://reader036.vdocuments.site/reader036/viewer/2022070410/56649f145503460f94c287b9/html5/thumbnails/53.jpg)
VARIATION Rational Knapsack
Object is like a gold bar, we can cut it in to piece with the same value/weight
0-1 Knapsack Object cannot be broken, we have to choose to
take (1) or leave (0) the object E.g.
K = 50 Objects = (60,10) (100,20) (120,30) Best solution = choose second and third
![Page 54: G ENERATE AND T EST F RAMEWORK Nattee Niparnan. O PTIMIZATION E XAMPLE : F INDING M AX V ALUE IN AN A RRAY 2523443490-5870561 There are N possible answers](https://reader036.vdocuments.site/reader036/viewer/2022070410/56649f145503460f94c287b9/html5/thumbnails/54.jpg)
RATIONAL KNAPSACK
Can be solved by greedy Sort object according to value/weight ratio Pick objects by that ratio
If object is larger than the remaining capacity, just divide it
![Page 55: G ENERATE AND T EST F RAMEWORK Nattee Niparnan. O PTIMIZATION E XAMPLE : F INDING M AX V ALUE IN AN A RRAY 2523443490-5870561 There are N possible answers](https://reader036.vdocuments.site/reader036/viewer/2022070410/56649f145503460f94c287b9/html5/thumbnails/55.jpg)
0-1 KNAPSACK WITH B&B
0-1 knapsack is very suitable for B&B We can calculate the goodness of the partial
solution Just sum the value of the selected objects
We have fast, good upper bounds (several one) The sum of remaining unselected objects
The sum of remaining unselected object that don’t exceed the capacity
The solution of the “rational knapsack” of the remaining objects with the remaining capacity
![Page 56: G ENERATE AND T EST F RAMEWORK Nattee Niparnan. O PTIMIZATION E XAMPLE : F INDING M AX V ALUE IN AN A RRAY 2523443490-5870561 There are N possible answers](https://reader036.vdocuments.site/reader036/viewer/2022070410/56649f145503460f94c287b9/html5/thumbnails/56.jpg)
TASK
Implement 0-1 Knapsack