Constraint Satisfaction Problems
• Objectives
– Constraint satisfaction problems – Backtracking
– Iterative improvement – Constraint propagation
• Reference
– Russell & Norvig: Chapter 5.
Y. Xiang, Constraint Satisfaction Problems 1
Constraints
• Constraints are encountered regularly in daily live.
– Ex Seats in the car.
– Ex Travel to a holiday resort.
– Ex Register for courses.
• Problems involving constraints may be complex.
– Ex University course timetabling.
– As the complexity of the problem grows, we turn to intelligent agents for finding a solution.
– In general, problems involving constraints are
NP-hard. 2
Y. Xiang, Constraint Satisfaction Problems
University Course Timetablling
• Each semester, hundreds of courses are offered by hundreds of instructors.
• Room-slot: No two lectures can be offered in the same room at the same time slot.
• Instructor: Lectures by the same instructor cannot be scheduled at the same time slot.
• Course: Lectures of the same course cannot be offered at the same time slot.
• Course group: If two courses are to be taken in the same semester, then their lectures cannot be scheduled at the same time slots.
Y. Xiang, Constraint Satisfaction Problems 3
Constraint Networks
• To study agents for solving these problems, we model their environments as constraint networks.
• A constraint network has two components:
1. a set V of variables with associated domains, and
2. a set C of constraints.
• V is a finite set of discrete variablesand is denoted as V={x1,…,xn}.
– Each variable xi in V is associated with a domain Di ={xi1,…,xik} of possible values.
Y. Xiang, Constraint Satisfaction Problems 4
Constraint Networks (CN)
• An assignmentover a subset X V is a combination of values for variables in X.
– An assignment over X is completeif X = V.
• C is a set of constraintsdenoted C={C1,…,Cm}.
– Each constraint Ciis relative to a subset X V and is a set of acceptable assignments over X.
– Subset X is called the scope of constraint Ci.
• An assignment that satisfies all relevant constraints is called a consistentor legalassignment.
• A solutionis a consistent, complete assignment.
• A CN is inconsistentif it has no solution.
5
Constraint Satisfaction Problems (CSPs)
• Tasks of determining whether a solution exists for a CN and finding solutions if they exist are referred to as constraint satisfaction problems (CSP).
• Focus: whether a solution exists & find oneif exists.
– Ex Map coloring.
– Ex The n-queens.
–Other CSPs.
• Arityof a constraint is the cardinality of its scope.
– Unary constraint and binary constraint.
– Binary constraint network has only unary and binary constraints.
Y. Xiang, Constraint Satisfaction Problems 6
Ex Map Coloring
• Color each region with one of {r,g,b}
s.t. no adjacent regions have the same color.
• Constraint network
– What is V and what are variable domains?
– What is an assignment over X = {W,N,S}?
– What is the constraint b/w W and N?
– How many constraints are in C and what are their scopes?
– Is assignment {N=r,Q=g,S=b,E=g} consistent?
– What is a solution?
N W
S Q
R E
Ex The n-queesn
• Place n queens on an nn chessboard s.t. no queen attacks another.
• Constraint network for 4-queen
– What is V if we use one variable for each row?
– What is the domain of x2 ?
– What is the constraint b/w x1and x3?
When the order of variables is clear, we write an assignment as a tuple.
– How many constraints are in C?
– What is a solution?
Other CSPs
• Crossword puzzles.
• 3SAT.
• Job shop scheduling.
• Radio link frequency assignment.
• Space telescope scheduling.
• 3D interpretation of 2D drawing.
• Hospital nurse scheduling.
• Airline flight scheduling.
• Floor plan layout.
• Automobile transmission design.
Y. Xiang, Constraint Satisfaction Problems 9
Constraint Graphs
• Structure of CNs can be expressed as constraint graphs, and they are useful in solving CSPs.
• There are three common forms of constraint graphs.
• In a primal graph, each node represents a variable and each link connects two constrained variables.
– Ex Map coloring.
• In a dual graph, each node (drawn as a cluster) is labeled by the scope of a constraint. Each link connects two clusters that share some variables and is labeled by the intersection.
• Ex Map coloring.
Y. Xiang, Constraint Satisfaction Problems 10
Constraint Graphs (Cont)
• In a hypergraph, each node represents a variable.
Each hyperlink represents a constraint and connects all variables in its scope. It is drawn by a link from a box to each variable in the scope.
• Ex Map coloring.
• Ex Crypt-arithmetic Puzzle.
Y. Xiang, Constraint Satisfaction Problems 11
Ex Crypt-arithmetic Puzzle
• Substitute each letter by a distinct digit without leading zero s.t. the sum is arithmetically correct.
• Constraint network and constraint graph – What are the variables?
– How to represent distinct digit?
– How to represent no leading zero?
– How to represent arithmetic sum?
– What is the constraint hypergraph?
12
T W O + T W O --- F O U R
Y. Xiang, Constraint Satisfaction Problems
Solve CSPs by Search
• Tree search such as BFS, DFS, etc are applicable to CSPs.
– How?
– How many leaf nodes are at level n+1?
– How many complete assignments are there?
• Observation
– Unlike problems such as 8-puzzle, neither the order of variable assignment, nor the search path matters in CSPs.
– Hence, search can focus on a single order only.
Y. Xiang, Constraint Satisfaction Problems 13
Chronological Backtracking
• Idea: Depth-first search that assigns one variable each time (forwards) and backtracks when a variable has no legal values to assign.
• An recursive algorithm.
– Ex The 4-queen.
• Chronological: Always backtrack to the most recent value assignment.
Y. Xiang, Constraint Satisfaction Problems 14
backtracking(config[]) { // assume access to a CN
if config is complete, return config;
v = getVariable(config);
vu[] = orderDomainValue(v, config);
for each u in vu,
if u is consistent with config, add {v=u} to config;
result = backtracking(config);
if result != null, return result;
remove {v=u} from config;
return null;
}
Complexity of Backtracking
• Suppose |V|=n and |Di|≤ k.
• In the worst case, the full search tree is explored.
• What is the total number of nodes in full search tree?
– How many levels are in the search tree?
– What is the branching factor?
• Sum of geometric series
– Sum k0 + k1 +… + knis (kn+1-1)/(k-1).
• Time complexity of backtracking.
Variable Ordering
• Does the order of variables make any difference to the efficiency of chronological backtracking?
– Ex Map coloring: D=(r,g,b) except DQ=(b,g,r).
Order 1: (W,N,Q,E,R,S).
Order 2: (W,N,S,Q,E,R).
• Styles of variable ordering – Fixed
– Dynamic
Y. Xiang, Constraint Satisfaction Problems 17
Minimum Remaining Values (MRV) Heuristic
• Choose the next variable x that has the fewestlegal values.
– Ex Map coloring.
• Rational behind MRV
– If x has no legal value, the search tree is pruned immediately.
– Otherwise, x is most likely to cause failure soon, thereby pruning the search tree.
• Overhead: At each forward step, for each
remaining variable x, each constraint that involves x must be checked.
Y. Xiang, Constraint Satisfaction Problems 18
Maximum Degree (MDeg) Heuristic
• Choose the next variable x that involves the largest number of constraints with unassigned variables.
– Ex Map coloring.
• Rational behind
– Reduce the branching factor on future choices.
• Overhead: At each forward step, for each remaining variable x, count its current degree.
• MRV and MDeg can be combined in chronological backtracking, with MRV as the primary heuristic and MDeg as the tie-breaker.
Y. Xiang, Constraint Satisfaction Problems 19
Value Selection
• Given variable order, does the order in which values are assigned make any difference to efficiency?
– Ex Map coloring with variable ordering (W,N,Q,E,R,S) and value order (r,g,b).
• Styles of value ordering – Fixed
– Dynamic
Y. Xiang, Constraint Satisfaction Problems 20
Forward Checking
• To assign variable x with value u, for each unassigned variable y connected to x by a constraint Ci, delete from Dyeach value inconsistent with x=u by Ci.
• If for any y, Dybecomes empty, consider another value u’ of x.
• If for each y, Dyis non-empty, assign x=u.
• Ex Map coloring with variable order (W,Q,R,N,S,E).
• Benefit: Avoid a value that will cause failure before it is assigned and hence prune search space.
Y. Xiang, Constraint Satisfaction Problems 21
Looking Back in Search
• When a branch of the search fails, chronological backtracking backs up to the preceding variable.
– This is often inefficient.
– Ex A coloring problem.
• It is more efficient to go all the way back to the source of failure.
• However, jumping too far back may skip potential solutions.
– Ex Jumping back to x2.
• Challenge: What is the adequate jump back point?
22
x1{r,b,g}
x5{b,g}
x3{r,b,g}
x7{r,b}
x6{r,g,y}
x4{r,b}
x2{b,g}
Y. Xiang, Constraint Satisfaction Problems
Dead-end and Conflict Set
• If a consistent assignment ui=(u1,…,ui) for x1,…,xi is inconsistent with every possible value of xi+1, then (u1,…,ui) is a dead-end state, and xi+1is a dead-end variable.
• Let ui=(u1,…,ui) for x1,…,xi be a consistent assignment and x be an unassigned variable. If there is no value u in the domain of x s.t. (ui, x=u) is consistent, then uiis a conflict set of x.
– If uidoes not contain a subtuple that is in conflict with x, then uiis a minimalconflict set of x.
Culprit Variable
• Let ui=(u1,…,ui) be a tuple. Then uk=(u1,…,uk), where k ≤ i, is a prefixtuple of ui.
• Let ui=(u1,…,ui) for x1,…,xi be a dead-end state.
The culprit variable relative to uiis xkwhere k = min{j | j≤i and ujis a conflict set of xi+1}.
– Ex What is the culprit variable wrt (x1=r, x2=b, x3=b, x4=b, x5=g, x6=r)?
• The culprit variable is the adequate jump back point.
– Why?
Backjumping Algorithm
backjump() { // assume access to a CN i =1; Di’ = Di ; latesti=0; // latest: a pointer while 1≤i≤n,
xi= selectValue(i);
if xi== null, i = latesti; else i++; Di’ = Di ; latesti=0;
end while;
if i== 0, return “no solution”;
else return assignment over {x1,…,xn};
}
• Ex The coloring problem.
Y. Xiang, Constraint Satisfaction Problems 25
selectValue(i) { while Di’ ≠ {},
select u Di’ and remove u from Di’ ; consistent = true; k = 1;
while k<i and consistent, if k > latesti, latesti=k;
if (uk, xi=u) is inconsistent, consistent = false;
else k++;
if consistent, return u;
return null;
• Does backjump() jumps back to the culprit variable?
Y. Xiang, Constraint Satisfaction Problems 26
Local Search
• Backtracking builds up a solution by assigning one variable at a time consistently.
• Local search iteratively improves a complete but inconsistent assignment, one variable at a time.
• Challenges
– Which variable to improve next?
– Which value of the variable to select?
Y. Xiang, Constraint Satisfaction Problems 27
Time Bounded Min-Conflicts Algorithm
minConflictTB(c[], maxSteps) { // c: constraints in CN current = a complete assignment;
for i=1 to maxSteps,
if current is legal, return current;
v = randomly selected variable violating c;
u = value of v minimizing # constraint violations;
set v = u in current;
return failure;
}
Y. Xiang, Constraint Satisfaction Problems 28
Example and Properties
• Ex Solving 8-queens.
• Can agent avoid repeating the same assignments?
• An algorithm for CSPs is completeif it always finds a solution when one exists, or terminates when no solution exists.
• Is the algorithm complete?
Y. Xiang, Constraint Satisfaction Problems 29
Nogood
• An intelligent CSP agent should use learning to avoid regenerating a conflict set.
• Given a CN, any partial assignment that does not appear in any solution is a nogood.
– Is every conflict set a nogood?
– Is every nogood a conflict set?
Y. Xiang, Constraint Satisfaction Problems 30
Min-Conflicts Algorithm With Nogood Learning minConflictNL(c[]) { // c[]: constraints from CN
parSol[] = ;
varLeft[] = a complete assignment;
return minConflictNL(parSol, varLeft, c);
}
• Each element of parSol and varLeft is a pair in the form (variable, value).
• parSol and varLeft have no variables in common.
• The union of parSol and varLeft is a complete assignment.
minConflictNL(parSol[], varLeft[], c[]) { nogood[] = ;
loop
if varLeftparSol is legal, return varLeftparSol;
(v,u) = element in varLeft violating c;
vu[] = values of v consistent with parSol wrt c & nogood;
if vu is emtpy,
if parSol is empty, return no-solution;
add parSol to nogood;
move most recently added element of parSol to varLeft;
else
u’ = element of vu minimizing # constraint violations with varLeft wrt c;
remove (v,u) from varLeft and add (v,u’) to parSol;
end loop }
Example and Properties
• Ex The 4-queens.
• Is the algorithm complete?
– Does it always find a solution when one exist?
– Does it terminate when there is no solution?
Y. Xiang, Constraint Satisfaction Problems 33
Relative Arc Consistency
• Forward checkingpropagates the implications of a constraint from variable x to y.
• Constraint propagation is the general concept to propagate implications of a constraint on one variable to others, and arc consistency is an effective method of constraint propagation.
• Let Cxybe a constraint of scope {x,y}. Variable x is arc-consistent relative toy iff for every value uDx there exists a value wDys.t. (u,w)Cxy.
• Ex Constraint Cxy: x < y with Dx= Dy={1,2,3}.
– Is x arc-consistent relative to y?
Y. Xiang, Constraint Satisfaction Problems 34
Enforce Relative Arc-Consistency
• If x is not arc-consistent relative to y, consistency can be enforced by executing algorithm revise():
revise(x, y, Cxy) { for each uDx,
if there is no wDys.t. (u,w)Cxy, Dx= Dx/{u};
}
• Ex Given Cxy: x < y with Dx= Dy={1,2,3}, how to make x arc-consistent relative to y?
– Is y arc-consistent relative to x?
– How to make both arcs consistent?
• What is the complexity of revise()?
Y. Xiang, Constraint Satisfaction Problems 35
Arc Consistency
• Let Cxybe a constraint of scope {x,y}. Variables x and y are arc-consistent iff x is arc-consistent relative to y and y is arc-consistent relative to x.
• A CN is arc-consistent iff every pair of variables constrained are arc-consistent.
• Arc consistency in a CN can be enforced by the algorithm arcConsistency().
• Ex Map coloring with order (W,Q,R,N,S,E).
– After assigning W=r, is the CN arc consistent?
– After Q=g, what happens if arcConsistency() is run?
Y. Xiang, Constraint Satisfaction Problems 36
Enforce Arc-Consistency
arcConsistency() { agenda = {};
for each pair {x, y} of variables adjacent in CN, agenda = agenda{(x,y),(y,x)};
while agenda ≠ {},
remove (x,y) from agenda and revise(x, y, Cxy);
if Dxis revised,
for each z adjacent to x in CN and z≠ y , agenda = agenda{(z,x)};
}
• What is the complexity of arcConsistency()?
37
The k-consistency
• Can an arc-consistent CN contain inconsistency?
– Could an arc-consistent CN have no solution?
– Ex A triangle-structured coloring CSP.
• A CN is k-consistent if for every set X of k-1 variables and for every consistent assignment over X, a consistent value can be assigned to any k’th variable.
– 1-consisency = node consisency – 2-consisency = arc consisency – 3-consisency = path consisency
– Ex Is CN of 4-queens k-consistent for k=1,2,3?
Y. Xiang, Constraint Satisfaction Problems 38
Strong k-consistency
• A CN is strongly k-consistent if it is i-consistent for all i≤ k.
• If a CN with |V|=n is strongly n-consistent, what would happen if backtracking() is applied to it?
• For a general CN, what is the complexity to establish strong n-consistency?
– In general, CSPs are NP-hard.
• Between strong n-consistency and arc-consistency, a range of middle ground exists to trade efficiency of search with efficiency of consistency checking.
Solving Tree-Structured CSPs
• Structure info in constraint graphs can provide guidance for limited consistency checking and efficient search.
• Let the constraint graph of a CN be a tree T. The CN can be solved using solveTreeCN().
– After 1stfor loop, every parent is arc-consistent relative to its child.
– The 2ndfor loop is backtracking free.
– Ex A coloring problem with domain {r,g} for each variable.
• Complexity of solveTreeCN().
solveTreeCN() {
pick a node as root of T;
define an order r for nodes s.t. pi(x) precedes x in r;
denote r = (x1,x2,…,xn);
for i=n to 2, revise(pi(xi), xi);
if Dpi(xi)={}, return null;
for i=1 to n, assign xi a value consistent with the value assigned to pi(xi);
return complete assignment;
}
Y. Xiang, Constraint Satisfaction Problems 41
Cutset Conditioning
• What if the CN is not tree-structured?
– Can solveTreeCN() be extended to solve more general CNs?
• A cycle cutsetof a graph is a subset of nodes whose removal renders the graph a tree.
• Solving binary CNs using cutsetCondition().
– Ex Map coloring.
• Complexity of cutsetCondition().
Y. Xiang, Constraint Satisfaction Problems 42
cutsetCondition() {
choose a cycle cutset X and denote Z=V/X;
let A(X) be the set of consistent assignments of X;
if A(X)={}, return null;
for each xA(X), for each yZ,
remove each uDyinconsistent with x ; apply solveTreeCN() to CN over Z;
if solution z is found, return xz ; return null;
}
Y. Xiang, Constraint Satisfaction Problems 43
Cluster Tree Decomposition
• Idea: Decompose CN into a set of small subCNs;
enforce consistency in each subCN; and search the solution for CN efficiently.
solveCNByClusterTree() {
decomposeCN into a cluster tree T;
for each cluster Q in T,
find the set S(Q) of consistent assignments of Q;
if S(Q)={}, return null;
set Tinto a binary CN;
apply solveTreeCN() to T;
}
44
Decompose CN into Cluster Tree
• Divide V into overlapping clusters s.t. each xV is contained in at least one cluster.
• For every constraint in C, all variables in its scope must be contained in at least one cluster.
• Organize clusters into a tree T s.t. every two adjacent clusters in T have at least one common variable.
• Any variable contained by two clusters in T must be contained by all clusters along the path.
• Ex Map coloring.
Y. Xiang, Constraint Satisfaction Problems 45
Set Cluster Tree into Binary CN
• For each cluster Q and its set S(Q) of consistent assignments, create a mega-variable q of domain Dq=S(Q).
• For each two adjacent clusters Q and R, denote Z=QR and corresponding mega-variables q and r.
• Constraint over {q,r} is that assignment of q and assignment of r must agree on the value of variables in Z.
• Ex Map coloring.
Y. Xiang, Constraint Satisfaction Problems 46
How to Decompose CN into Cluster Tree
• Conditions of the cluster tree have been presented.
– Algorithms to generate such cluster trees from CNs are needed.
• The decomposition steps –Triangulateconstraint graph.
–Identifyclusters.
–Organizingclusters into cluster tree.
Triangulating Constraint Graph
triangulateCG(G) { G’ = G; fillin = {};
for i=1 to n,
select node xiin G with adjacent node set adj(xi);
if nodes in adj(xi) are not pairwise linked, add links in G to make adj(xi) pairwise linked;
record links added to fillin;
remove xiand its incident links from G;
add links in fillinto G’;
return G’;
Identify Clusters
• Idea: Bookkeeping during triangulateCG(G).
• During triangulateCG(G):
– Record Qi= adj(xi) {xi} before each xiis removed from G.
• After triangulateCG(G):
identifyCluster({Q1, Q2,…, Qn}) { Clus = {Q1, Q2,…, Qn};
for i=n to 1,
if Qiis a subset of Qk(k<i), remove Qifrom Clus;
return Clus;
}
49
Construct Cluster Tree
buildClusterTree(Clus) {
init cluster tree T with no clusters;
pick QClus;
add Q to T;
remove Q from Clus;
while Clus≠{},
select QClusand Q’ from T s.t. |QQ’| is maximal;
add Q to T;
connect Q to Q’;
remove Q from Clus;
return T;
}
Y. Xiang, Constraint Satisfaction Problems 50
Triangulation Revisited
• How to select node xiin each iteration?
• Ex Map coloring problem.
– What are the clusters produced if triangulation is performed in order (N,W,U,E,S,R)?
– What is the consequence to computation in solveCNByClusterTree()?
• Conclusion: The less number of fillins, the better.
• Finding triangulations with minimum # fillins is NP- hard.
• Heuristic: Select xiwith minimum # of fillins.
Y. Xiang, Constraint Satisfaction Problems 51
Solving CSPs by Cluster Tree:
Complexity
• What is the complexity of finding set S(Q) for cluster Q?
• The tree widthof a cluster tree decomposition of a constraint graph is one less than the size of the largest subproblem.
– What is the above complexity in terms of tree width?
• What is the complexity of solveCNByClusterTree()?
Y. Xiang, Constraint Satisfaction Problems 52