400 likes | 539 Views
Exhaustion. Tam Siu Lung, Alan 96397999 Tam@SiuLung.com 99967891. Prerequisites. Basic knowledge of Recursion Elementary Graph Theory Experience in solving Exhaustion problems. When can we use Exhaustion?. We can list out ALL possible states
E N D
Exhaustion Tam Siu Lung, Alan 96397999 Tam@SiuLung.com 99967891
Prerequisites • Basic knowledge of Recursion • Elementary Graph Theory • Experience in solving Exhaustion problems
When can we use Exhaustion? • We can list out ALL possible states • We can justify whether any given state is a goal • List out all possible states • Solve for a single goal state • Solve for the optimal goal state • Know how to order two states • Solve as good as you can for a state • Know how to evaluate a state
Constraint SatisfactionWhy some states are not goal? • There are n variables (a1..an) • Each variable takes one of the m values (domain of values) (v1..vm) • There are p constraints (c1..cp) • A state is a partial assignment • Initial state is a state without assignments • A goal state is a state with all variables assigned with values • Operation: assign a value to a variable
Example 1: 8 Queen Problem • Variable: 8 Queens • Values: 64 Locations • Constraints: No two Queens attack • Initial state: no queen • Goal state: 8 Queens • Solution: Any goal state
Solve for: YYZ ZX + X XYZ ONE TWO FIVE NINE ELEVEN TWELVE + FIFTY NINETY Variables: X, Y, Z Values: 0~9 Constraints: Z+2X=Z+10C0 Y+Z+C0=Y+10C1 Y+C1=X X ≠ Y Y ≠ Z X ≠ Z Example 2: Crypto-arithmetic
NT Q WA SA NSW V T Example 3: Map Coloring • To color the map of Australia ... • Variables: “States” • Values: Colors • Constraints: Neighbor “States” have Different Colors
Variables: Position of A, B, C, D Values: 1, 2, 3, 4 Constraints: Positions of A, B, C, D are all different Variables: 4 Positions Values: A, B, C, D Constraints: Positions of A, B, C, D are all different Example 4: PermutationsFind all permutations of ABCD
Algorithm 1 • Given a state • If it is a goal state, ... • For each of the unassigned variables • For each of the values in the domain • Assign the value to the variable • Test the constraints • Test if this “new” state works • Undo the assignment • When all the assignment failed, declare failed
ABC CBA CBA ABC Search Tree ??? No variables assigned A?? ?A? ??A B?? ?B? ??B C?? ?C? ??C 1 variable assigned AB? ?BA CB? ?BC 2 variables assigned 3 variables assigned
Search Tree ??? No variables assigned 1 variable assigned A?? B?? C?? AB? AC? 2 variables assigned ABC ACB 3 variables assigned
Algorithm 2 • Given a state • If it is a goal state, ... • Pick one variable • For each of the values in the domain • Assign the value to the variable • Test the constraints • Test if this “new” state works • Undo the assignment • When all the assignment failed, declare failed
What to do for goal state? • If you want all goals • Print it out • If you want any goal • Print it out and terminate • If you want the best goal • Update the best goal till now if the new one is better
How to check the constraints? • Constraints checked after every assignment • In each assignment, only one value affected • Associate a list of constraints to the variables • Reduce constraints to binary constraints
Order Analysis • Search depth is n • Search breadth is m • Order = Size of Tree = O(nm) • In general CSP’s are NP-hard
Doing better • How to pick a good variable, i.e. what is a good ordering of variables? • How to know earlier that a branch is doomed? • When we backtrack from a doomed branch, how do we avoid the same mistake by modifying an irrelevant variable? • How to decompose a large problem into more manageable ones?
NT Q WA SA NSW V T Search Tree WA=R WA=G WA=B WA=R NT=G WA=R NT=B WA=R NT=G SA=B WA=R NT=B SA=G
Minimum Remaining Values(MRV) • Choosing the variable with MRV • So we won’t try meaningless moves (reduced the tree size) • Which node to try at the very beginning? • If two variables have the same MRV, choose the one with the largest degree
NT Q WA SA NSW V T Search Tree WA=R WA=G WA=B WA=R NT=G WA=R NT=B WA=R NT=G Q=R WA=R NT=G Q=B
Least Constraining Value(LCV) • When choosing a value, choose the LCV • The LCV makes a branch dooms faster • Why not aim at reducing the tree size this time?
Forward CheckingData Structures • Forward checking done to store the number of RV for each node • Stored what values are impossible • Effectively reduced branching factor
NT Q WA SA NSW V T Search Tree WA=R WA=G WA=B WA=R NSW=R WA=R NSW=G WA=R NSW=B WA=R NSW=R T=R WA=R NSW=R T=G WA=R NSW=R T=B
Back Jumping • Don’t try another value when the conflict is not caused by the current node • Define conflict set be the set of variables that however assigned will cause conflict • When failure, return the conflict set • If the current variable is not in the conflict set, declare failure immediately with the same conflict set • When all values exhausts, adds itself to the union all conflict sets of its branches
Back JumpingData Structures • Store a set for each variable, initially empty • Whenever assignment of variable a1 constrains another variable a2, add a1 to the conflict set of a2 • When a branch fails, the conflict set is there already
Tree CSP A E B D C F A B C D E F
Decomposition (Optional) • Split a problem into several smaller problems • If the problem is Tree CSP, then it runs in linear time
Removing Nodes (Optional) • Find a set of variables so that when it is removed the graph becomes a tree (Cycle Cutset) • How to remove? • O(kn) where k is the size of the Cycle Cutset NT Q WA NSW V T
Tree Decomposition • Sub-problems form a tree • Each sub-problem is a variable in the original problem • Two values conflict if their common “variables” are different NT Q SA NT WA SA Q SA NSW NSW SA V T
Solving for Optimality • How about if we are asked to find the minimum number of colors that we have to use? • Algorithm 1 • Try them one by one upwards • Slow? • Algorithm 2 • No fix amount of available variables
Example 5: Coins • SEARCC 2001 HK TFT • Given a set of coin values • 100, 30, 8, 5, 1 • Find the minimum number of coins required to pay for a given amount, e.g. 112 • Variables, Values, Conflicts?
Example 5: Coins • Variables: the coins picked • Values: values of coins • Cost: the amount not yet paid • Operation: Pick a coin
Search Tree 100 30 8 100+8 100+1 30+30 100+5 30+8 100+8+1 100+5+5 100+5+1 100+1+1 30+30 +30 100+8 +1+1 100+5 +5+1 100+5 +1+1 100+1 +1+1 30+30 +30+8 100+8 +1+1+1 100+5 +5+1+1 100+5 +1+1+1 100+1 +1+1+1 30+30 +30+8 +1 30+30 +30+30 +8+1 100+8 +1+1 +1+1 100+5 +1+1+1 +1 100+1 +1+1+1 +1
Example 5: Coins • Why can we cut the nodes to early? • What is the prerequisite?
Local SearchHill Climbing • Don’t try to search all. Only store one (Local) state and search from it. • A state is NOT a partial assignment, but an full assignment with constraints violated • Use an objective function to guide our search, e.g. the number of conflicts present • Every step tries to modify the Local state, aiming at performing an upward move • The objective is to move to the global maximum
Example 1: 8-Queen Problem • State: A full assignment of all 8 queens, say one in a column • Operation: Modify one/some queens • Initial state: random • Cost function: number of pair of queens attacking with each other
Difficulties • Local Maxima: every single move will worsen, but another move may give rise to somewhere even higher • We will solve this later • Plateau: everywhere is the same • Allow some sideway moves until you are confirmed enough it is local maxima
Random Restart • When we have reached somewhere that we cannot make more improvements • Remember the best solution • Restart from another random initial state
Simulated Annealing • Instead of just choose a random next-state, move on a probability depending on how much it worsen our solution • But we want randomness to decreases with time • With high enough “temperature”, one can make a bad move to a certain extent • Assign a schedule of temperature decrease
Local Beam Search • Keep more than one local states • Many simultaneous local searches with communication • States which are bad will die soon • But we want diversity ...
Stochastic Beam Search • Don’t always use the best states • Allow some not so good states given a probability