Lecture 5: Constraint Satisfaction Problem (CSP) : Heshaam Faili Hfaili@ut - Ac.ir University of Tehran
Lecture 5: Constraint Satisfaction Problem (CSP) : Heshaam Faili Hfaili@ut - Ac.ir University of Tehran
Many problems can be formulated as finding a solution, possibly optimal, that satisfies a set of constraints.
built a time table for the next semester with the minimum number of conflicts schedule air crew assignments so that pilots dont fly too many hours, have evenly-spaced breaks, find a 0/1 assignment to a propositional formula 8-queen problem, crypto arithmetic
2
State:
a set of variables x1, x2, ,xn variable domains D1, D2 Dn state vector (x1, x2, x3, xn) with instantiated and uninstantiated variables
Start state: vector of uninstantiated variables Goal state: instantiated vector that satisfies all the constraints
3
Time-table schedule
Mon Tue Wed Thu Fri 8 9 10 11
Simplest form: discrete and finite domain For n variables and domain size d the possible assignments are O(dn) Boolean CSP: domains are boolean, like 3SAT , special case of NPcomplete Infinite domain: no exact solution can be found
Continuous domains:
Constraints:
The best-known category is linear programming Constraints are linear inequalities forming a convex region Unary: involve only one variable: can be eliminated by remove any value violate the constraint from the domain of that variable Binary: involves two variables (like 8-queen) Higher-order : like crypt arithmetic
Higher-order Constraints
Can be represented by a constraints hyper-graph every higher-order, finite-domain constraint can be reduced to a set of binary constraints if enough auxiliary variables are introduced, prove it!
Backtracking in CSP
Commutativity: if the order of application of any given set of actions has no effect on the outcome. CSPs are commutative
Branching factor for level 1 is nd, level 2 is (n-1)d, level 3 is (n-2)d, Tree has n!dn leaves while there are only dn possible assignments
all CSP search algorithms generate successors by considering possible assignments for only a single variable at each node in the search tree In root node of Australia region coloring: we choice between SA = red, SA = green, and SA = blue, and we never choose between SA = red and WA = blue. The number of leaves are dn
Search strategy
1. pick an uninstantiated variable, pick a value from its domain, check if all constraints are satisfied 2. if constraints are satisfied, continue search recursively 3. else, backtrack: go back to previous decision and make another choice
bd
Backtracking Search
10
11
M=1
N=2
S32D
E=3
Special case of a search problem Domains can be discrete or continuous The order in which the constraints are satisfied does not matter. The order in which variables and their values are picked makes a big difference!
13
The goal test is decomposed into a set of constraints on variables, rather than a single black box When sets of variables are independent (no constraints between them) the problem is decomposable and subproblems can be solved independently. At each step we must check for consistency. We need constraint propagation methods!
14
Which variable should be assigned next, and in what order should its values be tried? What are the implications of the current variable assignments for the other unassigned variables? When a path failsthat is, a state is reached in which a variable has no legal values can the search avoid repeating this failure in subsequent paths?
15
16
Variable ordering
Pick values for the minimum remaining values (MRV) or most constrained variables or first fail principle
choosing the variable with the fewest "legal" value those with smaller domains and which appear in more constraints. The rationale: those leave fewer choices and focus the search
Improve the search from 3 til 3000 times better than simple
17
Variable ordering
Degree heuristic: attempts to reduce the BF in feature by selecting the variables that is involved in the largest number of constraints on other unassigned variables
18
Value ordering
least-constraining-value: prefers the value that rules out the fewest choices for the neighboring variables in the constraint In the previous coloring problem:
if we are trying to find all the solutions to a problem, not just the first one, then the ordering does not matter because we have to consider every value anyway.
19
Forward checking: delete values from domains as the assignments are made. When a domain becomes empty, abort.
20
21
Arc-consistency: a state is arc-consistent if every variable has a value in its domain that is consistent with each of the constraints of that variable. This is a form of constraint propagation. Arc-consistency provides a fast method of constraint propagation that is substantially stronger than forward checking. arc-consistency is directed arc in constraint graph (SA, NSW) is consistent while (NSW,SA) is not consistent 22 in the third row of Figure 5.6
Arc-Consistency
AC-3 can not find harder inconsistency (WA=red, NSW=red) Need more concepts
23
Arc-consistency can be applied after every assignment during search: MAC Complexity for binary CSP
Binary CSP has at most O(n2) arcs Each arc (Xi,Xk) can be inserted on the agenda only d times: Xi has at most d values to delete Checking consistency of an arc can be done in O(d2) Total O(n2d3)
24
CSP terminology
Label <xi, a> Compound Label { <xi, a> } K-Constraint Modeling 8-Queen problem:
25
Important Concepts
K-Satisfiable K-Consistency
K=1: node consistency 1-Consistent: node consistent K>1: for each K-1 satisfiable variable set can add 2-consistent: arc-consistent the Kth variable with consistent value and no 3-consistent: path consistent change in the previous K-1 variable values
K-strongly consistent
26
AllDiff constraints says that all variables should be different If n variables with m values and n>m, the constraints should not be satisfied
First, remove any variable in the constraint that has a singleton domain, and delete that variable's value from the domains of the remaining variables. Repeat as long as there are singleton variables. If at any point an empty domain is produced or there are more variables than domain values left, then an inconsistency has been detected.
By using the above method, the inconsistency (WA=red, NSW=red) can be detected. Because all SA, NT and Q has domain {green, blue}.
27
For example, let's suppose there are two flights, 271 and 272, for which the planes have capacities 165 and 385, respectively. The initial domains for the numbers of passengers on each flight are then
Checking the sum of minimum values of current domains (in each step of procedure)
Now suppose we have the additional constraint that the two flights together must carry 420 people: Flight271 + Flight272 [420,420]. Propagating bounds constraints, we reduce the domains to
28
M=1
C1 = 1 C2 + S + 1 = O
N=2 E=3
C2 + S + 1 = 1 C3 + E + 0 = N C4 + N + R = E
29
U
30
conflict set for variable X is the set of previously assigned variables that are connected to X by constraints.
Backjumping occurs when every value in a domain is in conflict with the current assignment; but forward checking detects this event and prevents the search from ever reaching such a node! every branch pruned by backjumping is also pruned by forward checking.
31
Min-conflicts heuristics
Select the value that results in the minimum number of conflicts with other variables Surprisingly effective for many CSPs
Independent of problem size Roughly speaking, n-queens is easy for local search because solutions are densely distributed throughout the state space.
32
Min-conflicts heuristics
33
34
Graph Order Node width Graph Order width Graph Width Value ordering for CSP search in the minimum Graph order width If the level of strong consistency is greater than the width of constraint graph, the search for solution tuple is backtrack free
35
Connected component in constraint graph is same to independence sub problem For subproblem CSPi
c variables there are n/c problems Each takes dc works Total: O(dcn/c) E.g; boolean CSP with n=80, c=20 decrease the time from lifetime of world to less than a second Only one path between any two variables tree-structured CSP can be solved in time linear in the number of variables.
36
1. Choose any variable as the root of the tree, and order the variables from the root to the leaves in such a way that every node's parent in the tree precedes it in the ordering.
2. For j from n down to 2, apply arc consistency to the arc (Xi,Xj), where Xi is the parent of Xj, removing values from DOMAIN[Xj] as necessary. 3. For j from 1 to n, assign any value for Xj consistent with the value assigned for Xi, where Xj is the parent of Xj.
Label the variables X1,..., Xn in order. Now, every variable except the root has exactly one parent variable.
Complexity : O(nd2)
37
tree-structured CSP
38
39
40
1. Choose a subset S from VARIABLES[csp] such that the constraint graph becomes a tree after removal of S. S is called a cycle cutset. 2. For each possible assignment to the variables in S, that satisfies all constraints on S
(a) remove from the domains of the remaining variables any values that are inconsistent with the assignment for S (b) If the remaining CSP has a solution, return it together with the assignment for S.
41
If the graph is "nearly a tree" then c will be small and the savings over straight backtracking will be huge. In the worst case, c =(n 2)
Finding the smallest cycle cutset is NP-hard but several efficient approximation algorithms are known for this task.
42
Second approach on changing more general CG into tree (based on collapsing nodes )
Every variable in the original problem appears in at least one of the sub-problems. If two variables are connected by a constraint in the original problem, they must appear together (along with the constraint) in at least one of the sub-problems. If a variable appears in two sub-problems in the tree, it must appear in every sub-problem along the path connecting those sub-problems. (SA in following example)
43
Tree decomposition
44
Using the concept of mega-variables Each sub-problem can be a megavariable Tree width: size of the largest subproblem For graph with w width: problem can be solved in O(ndw+1)
45
Large body of literature in different areas: AI, CS, Operations Research, Mathematics,
46
?
47