320 likes | 415 Views
Constraint Satisfaction Problems. Tam Siu Lung, Alan HKOI 2005 (2005-03-05). 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
E N D
ConstraintSatisfactionProblems Tam Siu Lung, Alan HKOI 2005 (2005-03-05)
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
Runtime Analysis • Search depth is n • Search breadth is m • Order = Size of Tree = O(mn) • 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
A B C D E F Tree CSP A E B D C 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(km) 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 values