CSC5240 - Modeling with FD Constraints Controlling Search Where we examine how modeling and controlling search interact with finite domain constraints CSC5240 - Modeling with FD Constraints
CSC5240 - Modeling with FD Constraints Domains and Labeling New requirement: need to define the domains of variables arithmetic X :: [1,2,3,4] or X :: [1..4] non-arithmetic X :: [red, blue, yellow] multiple variables [X,Y,Z] :: [0..10] If no domain is given a default is used (say [-10000..10000] CSC5240 - Modeling with FD Constraints
CSC5240 - Modeling with FD Constraints Domains Example - 1 Goal for the smugglers knapsack problem: [W,P,C] :: [0..9], 4*W + 3*P + 2*C <= 9, 15*W + 10*P + 7*C >= 30. The constraint solver returns unknown. But how do we find a solution? Invoke a complete (backtracking) solver CSC5240 - Modeling with FD Constraints
CSC5240 - Modeling with FD Constraints Domains Example - 2 Complete solvers for FD are expensive and should not be invoked every time a new constraint is added to the constraint store Thus, most constraint solver provide only an incomplete solver When necessary, however, the complete backtracking solver for FD can be invoked explicitly by the programmer, usually in the form of built-in labeling CSC5240 - Modeling with FD Constraints
CSC5240 - Modeling with FD Constraints Labeling - 1 Built-in predicate labeling invokes the complete constraint solver labeling(Vs) takes a list of finite domain variables Vs and finds a solution [W,P,C] :: [0..9], 4*W + 3*P + 2*C <= 9, 15*W + 10*P + 7*C >= 30, labeling([W,P,C]). has solutions: CSC5240 - Modeling with FD Constraints
Constrain-and-Generate Typical form of a finite domain program variables and domains are defined constraints modeling problem are given labeling is added to invoke complete solver Minimization can be applied on labeling [W,P,C] :: [0..9], 4*W + 3*P + 2*C <= 9, 15*W + 10*P + 7*C >= 30, minimize(labeling([W,P,C]), -15*W-10*P-7*C). CSC5240 - Modeling with FD Constraints
Send+More=Money Example - 1 Cryptarithmetic problem: each digit is different and the equation holds [S,E,N,D,M,O,R,Y] :: [0..9], S != 0, M != 0, alldifferent_neq([S,E,N,D,M,O,R,Y]), 1000*S + 100*E + 10*N + D + 1000*M + 100*O + 10*R + E = 10000*M + 1000*O + 100*R + 10*E + Y, labeling([S,E,N,D,M,O,R,Y]). CSC5240 - Modeling with FD Constraints
Send+More=Money Example - 2 After domain declarations: After then alldifferent_neq adds disequations (no change) final equation: (one propagation rule) With the current domain: CSC5240 - Modeling with FD Constraints
Send+More=Money Example - 3 Hence D(M) = [1..1] Propagation continues arriving at Note: 3 variables fixed and all domains reduced Making use of the current domains of variables, labeling tries S=9. Then E=4 (this fails eventually), then E=5 which gives CSC5240 - Modeling with FD Constraints
CSC5240 - Modeling with FD Constraints Generate-and-Test Methodology without constraints: first generate a valuation and then test if it is a solution [S,E,N,D,M,O,R,Y] :: [0..9], labeling([S,E,N,D,M,O,R,Y]), S != 0, M != 0, alldifferent_neq([S,E,N,D,M,O,R,Y]), 1000*S + 100*E + 10*N + D + 1000*M + 100*O + 10*R + E = 10000*M + 1000*O + 100*R + 10*E + Y. This program requires 95671082 choices before finding the solution, and the previous required 2 CSC5240 - Modeling with FD Constraints
CSC5240 - Modeling with FD Constraints Labeling - 3 There are two choices made in labeling choice of which variable to label first choice of which value to try first Default labeling try variables in order of the given list try value in order min to max (returned by dom) We can program different strategies CSC5240 - Modeling with FD Constraints
CSC5240 - Modeling with FD Constraints Choice of Variable Variable choice effects the size of derivation tree Choose variable with smallest current domain (hence smallest possible answers) Example Labeling first tries S, M, O (need do nothing), then either E or N. Much better than Y first CSC5240 - Modeling with FD Constraints
First-Fail Labeling - 2 Application of the first-fail principle “To succeed, try first where you are most likely to fail” CSC5240 - Modeling with FD Constraints
CSC5240 - Modeling with FD Constraints First-Fail Labeling - 3 It is reasonable to assume that a variable involved in many constraints is more likely to cause failure when it is set to a value Thus another application of the first-fail principle is to label first a variable that is involved in the most number of constraints CSC5240 - Modeling with FD Constraints
CSC5240 - Modeling with FD Constraints Choice of Domain Value Value choice only effects order in which branches are explored Problem specific knowledge may lead to finding a solution faster Also important for optimization Good solutions found earlier reduce search later CSC5240 - Modeling with FD Constraints
CSC5240 - Modeling with FD Constraints N-Queens Example Q1 Q2 Q3 Q4 1 2 3 4 Problem of placing N queens on an NN chessboard so that none can take another. For large N solutions are more likely with values in the middle of variable domains CSC5240 - Modeling with FD Constraints
CSC5240 - Modeling with FD Constraints Labeling Efficiency We can use both variable and value ordering together Different number of choices for N-queens in different ranges CSC5240 - Modeling with FD Constraints
CSC5240 - Modeling with FD Constraints Domain Splitting - 1 Labeling doesn’t have to set a variable to a value---principle of least commitment Simply has to reduce the variable domains Domain splitting splits the current domain of chosen variable in half Advantage: more search space is pruned when failure is detected Disadvantage: less propagation CSC5240 - Modeling with FD Constraints
CSC5240 - Modeling with FD Constraints Domain Splitting - 3 The idea is to split recursively the domains of each variable in half until all domains are singletons. Domain splitting is better than regular labeling when the variable domains are large, and the constraints involving the variables to be labeled can gain substantial consistency information from the split domain This way, half of the variable’s domain may be eliminated CSC5240 - Modeling with FD Constraints
CSC5240 - Modeling with FD Constraints Search in MiniZinc Search order defined using the variables that appear in the output statement Search annotations allow you to specify search order There are basic search annotations and advanced ones can be defined too Read Chapter 6 of the MiniZinc tutorial CSC5240 - Modeling with FD Constraints