Download Constraint Satisfaction Problems: A Comprehensive Guide and more Study notes Artificial Intelligence in PDF only on Docsity!
Artificial Intelligence
Constraint Satisfaction Problems Dr. Bilgin Avenoğlu
Constraint Satisfaction Problems
- Up to now, we explored the idea that problems can be solved by searching the state space
- Each state is atomic, or indivisible— a black box with no internal structure.
Constraint Satisfaction Problems
- Now, we break open the black box by using a factored representation for each state: - a set of variables, each of which has a value.
- A problem is solved when each variable has a value that satisfies all the constraints on the variable.
- A problem described this way is called a constraint satisfaction problem - CSP.
Constraint Satisfaction Problems
- CSP search algorithms use general rather than domain-specific heuristics to enable the solution of complex problems. - The main idea is to eliminate large portions of the search space all at once by identifying variable/value combinations that violate the constraints.
- CSPs have the additional advantage that the actions and transition model can be deduced from the problem description.
Defining Constraint Satisfaction Problems
- CSPs deal with assignments of values to variables,
- {Xi = vi , Xj = vj ,.. .}.
- An assignment that does not violate any constraints is called a consistent or legal assignment.
- A complete assignment is one in which every variable is assigned a value,
- A solution to a CSP is a consistent, complete assignment.
- A partial assignment is one that leaves some variables unassigned, and a partial solution is a partial assignment that is consistent.
Example problem: Map coloring
- Coloring each region either red, green, or blue
- no two neighboring regions have the same color.
- X ={WA,NT,Q,NSW,V,SA,T}.
- The domain of every variable is the set D i = {red, green, blue}.
- The constraints require neighboring regions to have distinct colors.
- C = {SA ≠ WA,SA ≠ NT,SA ≠ Q,SA ≠ NSW,SA ≠ V,WA ≠ NT,NT ≠ Q,Q ≠ NSW,NSW ≠ V}.
- SA ≠ WA is a shortcut for ⟨(SA,WA),SA ≠ WA⟩, where SA ≠ WA can be fully enumerated in turn as - {(red, green), (red, blue), (green, red), (green, blue), (blue, red), (blue, green)}.
- There are many possible solutions to this problem, such as
- {WA=red, NT=green, Q=red, NSW=green, V=red, SA=blue, T=red }. 8 Western Australia Northern Territory South Australia Queensland New South Wales Victoria Tasmania (a) Figure 5.1 (a) The principal states and territories of viewed as a constraint satisfaction problem (CSP). T gion so that no neighboring regions have the same represented as a constraint graph. 5.1.2 Example problem: Job-shop scheduli Factories have the problem of scheduling a day’s wort In practice, many of these problems are solved with C scheduling the assembly of a car. The whole job is com task as a variable, where the value of each variable is as an integer number of minutes. Constraints can another—for example, a wheel must be installed befo so many tasks can go on at once. Constraints can amount of time to complete. We consider a small part of the car assembly, co and back), affix all four wheels (right and left, front
Why formulate a problem as a CSP?
- It is often easy to formulate a problem as a CSP
- CSP solvers fast and efficient.
- A CSP solver can quickly prune large swathes of the search space that an atomic state-space searcher cannot.
- In atomic state-space search we can only ask: is this specific state a goal? No? What about this one? - With CSPs, once we find out that a partial assignment violates a constraint, we can immediately discard further refinements of the partial assignment.
Types of variables
- CSP involves variables that have discrete, finite domains.
- Map-coloring problem is of this kind.
- The 8 - queens problem can also be viewed as a finite-domain CSP,
- the variables Q1 ,... , Q8 correspond to the queens in columns 1 to 8,
- the domain of each variable specifies the possible row numbers for the queen in that column, Di = {1, 2, 3, 4, 5, 6, 7, 8}.
- The constraints say that no two queens can be in the same row or diagonal.
Types of constraints
- A constraint involving an arbitrary number of variables is called a global constraint.
- A common global constraints: Alldiff
- All of the variables involved in the constraint must have different values.
- In Sudoku problems, all variables in a row, column, or 3× 3 box must satisfy an Alldiff constraint.
- Every finite-domain constraint can be reduced to a set of binary constraints if enough auxiliary variables are introduced.
- We can transform any CSP into one with only binary constraints
- certainly makes the life of the algorithm designer simpler. 3 2 6 9 3 5 1 1 8 6 4 8 1 2 9 7 8 6 7 8 2 2 6 9 5 8 2 3 9 5 1 3 3 2 6 9 3 5 1 1 8 6 4 8 1 2 9 7 8 6 7 8 2 2 6 9 5 8 2 3 9 5 1 3 4 8 9 1 5 7 6 7 4 8 2 2 5 7 9 3 5 4 3 7 6 2 9 5 6 4 1 3 1 3 9 4 5 3 7 8 1 4 1 4 5 7 6 6 9 4 7 8 2 1 2 3 4 5 6 7 8 9 A B C D E F G H I A B C D E F G H I 1 2 3 4 5 6 7 8 9 (a) (b) Figure 5.4 (a) A Sudoku puzzle and (b) its solution. The Sudoku puzzles that appear in newspapers and puzzle books have the property that there is exactly one solution. Although some can be tricky to solve by hand, taking tens of minutes, a CSP solver can handle thousands of puzzles per second. A Sudoku puzzle can be considered a CSP with 81 variables, one for each square. We use the variable names A1 through A9 for the top row (left to right), down to I1 through I9 for the bottom row. The empty squares have the domain { 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 } and the pre-filled squares have a domain consisting of a single value. In addition, there are 27 different Alldiff constraints, one for each unit (row, column, and box of 9 squares): Alldiff (A 1 , A 2 , A 3 , A 4 , A 5 , A 6 , A 7 , A 8 , A 9 ) Alldiff (B 1 , B 2 , B 3 , B 4 , B 5 , B 6 , B 7 , B 8 , B 9 ) · · · Alldiff (A 1 , B 1 ,C 1 , D 1 , E 1 , F 1 , G 1 , H 1 , I 1 ) Alldiff (A 2 , B 2 ,C 2 , D 2 , E 2 , F 2 , G 2 , H 2 , I 2 ) · · · Alldiff (A 1 , A 2 , A 3 , B 1 , B 2 , B 3 ,C 1 ,C 2 ,C 3 ) Alldiff (A 4 , A 5 , A 6 , B 4 , B 5 , B 6 ,C 4 ,C 5 ,C 6 ) · · · Let us see how far arc consistency can take us. Assume that the Alldiff constraints have been expanded into binary constraints (such as A1 6 = A2) so that we can apply the AC-3 algorithm directly. Consider variable E6 from Figure 5.4(a)—the empty square between the 2 and the 8 in the middle box. From the constraints in the box, we can remove 1, 2, 7, and 8 from E6’s domain. From the constraints in its column, we can eliminate 5, 6, 2, 8, 9, and 3 (although 2 and 8 were already removed). That leaves E6 with a domain of { 4 }; in other words, we know the answer for E6. Now consider variable I6—the square in the bottom middle box surrounded by 1, 3, and 3. Applying arc consistency in its column, we eliminate 5, 6, 2, 4 (since we now know E6 must be 4), 8, 9, and 3. We eliminate 1 by arc consistency with I5,
Constraint Propagation: Inference in CSPs
- Node consistency
- A single variable (a node in the CSP graph) is node-consistent if all the values in the variable’s domain satisfy the variable’s unary constraints. - South Australians dislike green, the variable SA starts with domain {red,green,blue} , and we can make it node consistent by eliminating green, leaving SA with the reduced domain {red,blue}.
- We say that a graph is node-consistent if every variable in the graph is node- consistent.
Arc consistency example
& % Examples of Arc-consistency
- For x 2 [2... 6], y 2 [3... 7] the constraint x < y is arc-consistent.
- For example
- if x = 2 then there is a solution
- if x = 6 then y must be 7
- if y = 3 then x must be 2. Constraints Course - Justin Pearson - Consistency ' A Non Arc-Consistent Constraint
- For x 2 [2..7] and y 2 [3..7] the constraint: x < y is not arc consistent.
- If x = 7 (allowed by the domains) then there is no value for y satisfying the constraint.
AC-3 algorithm
- AC- 3 maintains a queue of arcs to consider.
- Initially, the queue contains all the arcs in the CSP.
- Each binary constraint becomes two arcs, one in each direction.
- AC-3 then pops off an arbitrary arc ( Xi , Xj ) from the queue and makes Xi arc-consistent with respect to Xj. - If this leaves Di unchanged, the algorithm just moves on to the next arc. - If this revises Di (makes the domain smaller), then we add to the queue all arcs ( Xk , Xi ) where Xk is a neighbor of Xi. - The change in Di might enable further reductions in Dk , even if we have previously considered Xk. - If Di is revised down to nothing, then we know the whole CSP has no consistent solution.
- Otherwise, we keep checking, trying to remove values from the domains of variables until no more arcs are in the queue.
- We are left with a CSP that is equivalent to the original CSP
- they both have the same solutions, but the arc-consistent CSP will be faster to search because its variables have smaller domains.
- In some cases, it solves the problem completely (by reducing every domain to size 1) and in others it proves that no solution exists (by reducing some domain to size 0). 17 Section 5.2 Constraint Propagation: Inference i function AC-3(csp) returns false if an inconsistency is found and true otherwise queue a queue of arcs, initially all the arcs in csp while queue is not empty do (Xi, Xj) POP(queue) if REVISE(csp, Xi, Xj) then if size of Di = 0 then return false for each Xk in Xi.NEIGHBORS - {Xj} do add (Xk, Xi) to queue return true function REVISE(csp, Xi, Xj) returns true iff we revise the domain of Xi revised false for each x in Di do if no value y in D (^) j allows (x,y) to satisfy the constraint between Xi and Xj then delete x from Di revised true return revised Figure 5.3 The arc-consistency algorithm AC-3. After applying AC-3, either every arc arc-consistent, or some variable has an empty domain, indicating that the CSP cannot solved. The name “AC-3” was used by the algorithm’s inventor (Mackworth, 1977) beca it was the third version developed in the paper. and makes Xi arc-consistent with respect to Xj. If this leaves Di unchanged, the alg just moves on to the next arc. But if this revises Di (makes the domain smaller), then
Complexity of AC- 3
- Assume a CSP with n variables, each with domain size at most d , and with c binary constraints (arcs).
- Each arc (X k
,X
i ) can be inserted in the queue only d times because X i has at most d values to delete.
- For each value in one domain, REVISE method examines all the values of the second domain. - So the complexity of the REVISE method would be d 2 .
- REVISE is called at maximum c + cd times.
- The complexity of the whole algorithm is O((c+cd)d 2 ) which is O(cd 3 ).
Path Consistency
20 Beyond Arc Consistency Path Consistency Summary
Beyond Arc Consistency: Path Consistency
idea of arc consistency: For every assignment to a variable u there must be a suitable assignment to every other variable v. If not: remove values of u for which no suitable “partner” assignment to v exists. tighter unary constraint on u This idea can be extended to three variables (path consistency): For every joint assignment to variables u, v there must be a suitable assignment to every third variable w. If not: remove pairs of values of u and v for which no suitable “partner” assignment to w exists. tighter binary constraint on u and v German: Pfadkonsistenz