Title: Artificial Intelligence Constraint satisfaction problems
 1Artificial IntelligenceConstraint satisfaction 
problems
- Fall 2008 
- professor Luigi Ceccaroni
2Problem characterization
- A constraint satisfaction problem (or CSP) is a 
 special kind of problem that satisfies some
 additional structural properties beyond the basic
 requirements for problems in general.
- In a CSP, the states are defined by the values of 
 a set of variables and the goal test specifies a
 set of constraints that the values have to obey.
3Problem characterization
- State components 
- Variables 
- Domains (possible values for the variables) 
- (Binary) constraints between variables 
- Goal to find a state (a complete assignment of 
 values to variables), which satisfies the
 constraints
- Examples 
- map coloring 
- crossword puzzles 
- n-queens 
- resource assignment/distribution/location
4Representation
- State  constraint graph 
- variables (n)  node tags 
- domains  node content 
- constraints  directed and tagged arcs between 
 nodes
- Example map coloring
C1
blue, red
?
C2
blue
C3
C1
?
?
C3
C4
C2
C4
blue, red, green
blue, green
?
initial state 
 5Representation
- In the search tree, a variable is assigned at 
 each level.
- Solutions have to be complete assignment, 
 therefore they appear at depth n, the number of
 variable and maximum depth of the tree.
- Depth-first search algorithms are popular in 
 CSPs.
- The simplest class of CSP (map coloring, 
 n-queens) are characterized by
- discrete variables 
- finite domains
6Finite domains
- If the maximum size of the domain of any variable 
 is d, then the number of possible complete
 assignments is O(dn), exponential in the number
 of variables.
- CSPs with finite domain include Boolean CSPs, 
 whose variables can only be true or false.
- In most practical applications, CSP algorithms 
 can solve problems with domains orders of
 magnitude larger than the ones solvable by
 uninformed search algorithms.
7Infinite domains
- With infinite domains (e.g., integers and 
 strings), to describe constraints enumerating all
 legal combinations of values is not possible.
- A constraint language has to be used, for 
 example
- job-start1  5  job-start3 
- There exist algorithms for linear constraints 
 over integer variables.
- No algorithm exists for general non-linear 
 constraints over integer variables.
- In cases, infinite-domain problems can be reduced 
 to a finite domain, just restricting the values
 of all variables (e.g., setting limits to the
 dates in which jobs can start).
8Search-based algorithms
- Incremental formulation 
- Initial state empty assignment  , with all 
 unassigned variables
- Successor function assignment of a value to any 
 variable not yet assigned, provided it does not
 conflict with assigned variables
- Goal test check if the current assignment is 
 complete
- Path cost a constant cost (e.g., 1) for every 
 step
- Complete formulation 
- Each state is a complete assignment, which 
 satisfies or not the constraints.
- Local-search methods work well in this case. 
- Constraint propagation 
- Before the search 
- During the search
9Constraints
- The simplest type is the unary constraint, which 
 constraints the values of just one variable.
- A binary constraint relates two variables. 
- Higher-order constraints involve three or more 
 variables. Cryptarithmetic puzzles are an example
10Cryptarithmetic puzzles
- Variables F, T, U, W, R, O, X1, X2, X3 
- Domains 0,1,2,3,4,5,6,7,8,9 
- Constraints 
- Alldiff (F,T,U,W,R,O) 
- O  O  R  10  X1 
- X1  W  W  U  10  X2 
- X2  T  T  O  10  X3 
- X3  F, T ? 0, F ? 0
11Depth-first search with backtracking
- Standard depth-first search on a CSP wastes time 
 searching when constraints have already been
 violated.
- Because of the way that the operators have been 
 defined, an operator can never redeem a
 constraint that has already been violated.
- A first improvement is 
- To test constraints after each variable 
 assignment
- If all possible values violate some constraint, 
 then the algorithm backtracks to the last valid
 assignment
- Variables are classified as past, current, 
 future.
12Backtracking search algorithm 
 13Backtracking search algorithm
- Set each variable as undefined. Empty stack. All 
 variables are future variables.
- Select a future variable as current variable. 
-  If it exists, delete it from FUTURE and stack it 
 (top  current variable),
-  if not, the assignment is a solution. 
- Select an unused value for the current variable. 
-  If it exists, mark the value as used, 
-  if not, set current variable as undefined, 
-  mark all its values as unused, 
-  unstack the variable and add it to FUTURE, 
-  if stack is empty, there is no solution, 
-  if not, go to 3. 
- Test constraints between past variables and the 
 current one.
-  If they are satisfied, go to 2, 
-  if not, go to 3. 
- (It is possible to use heuristics to select 
 variables (2.) and values (3.).
14Backtracking search algorithm 
 15Example 4-queens
- Place 4 queens, one per row, so that they do not 
 attack each others
- Variables R1  R4 (queens) 
- Domains 1  4 for each Ri (columns) 
- Constraints Ri does not attack Rj 
not attacking
Ri
Rj
1 .. 4
1 .. 4 
 16R11
R12
R21 NO R22 NO R23
R24
R21 NO R22 NO R23 NO R24
R31 NO R32 NO R33 NO R34 NO
R31 NO R32
R31
R41 NO R42 NO R43 NO R44 NO
R41 NO R42 NO R43
Backtracking R2
Backtracking R3, R2, R1 
 17Propagating information through constraints
- So far the algorithm considers the constraints on 
 a variable only at the time that the variable is
 chosen (e.g., by Select-Unassigned-Variable).
- By looking at some of the constraints earlier in 
 the search, or even before the search has
 started, the search space can be drastically
 reduced.
18Forward checking
- A way to make better use of constraints during 
 search.
- Whenever a variable X is assigned 
- the forward checking process looks at each 
 unassigned variable Y that is connected to X by a
 constraint and
- deletes from Ys domain any value that is 
 inconsistent with the value chosen for X.
19Forward checking algorithm 
 20Forward checking example 
 21Forward checking example 
 22Forward checking example 
 23Forward checking example
- Idea 
- Keep track of remaining legal values for 
 unassigned variables
- Terminate search when any variable has no legal 
 values
24Forward checking example
- Idea 
- Keep track of remaining legal values for 
 unassigned variables
- Terminate search when any variable has no legal 
 values
25Forward checking example
- Idea 
- Keep track of remaining legal values for 
 unassigned variables
- Terminate search when any variable has no legal 
 values
26Forward checking example
- Idea 
- Keep track of remaining legal values for 
 unassigned variables
- Terminate search when any variable has no legal 
 values
27Forward checking 4-queens example
- R11? propagation R23,4 R32,4 R42,3 ? 
 R11
- R23? propagation  R3? 
- R24? propagation  R32 R43 ? 
 R24
- R32? propagation  R4 ? 
- No other value for R3. Backtracking to R2 
- No other value for R2. Backtracking to R1 
- R12? propagation  R24 R31,3 R41,3,4 ? 
 R12
- R24? propagation  R31 R41,3 ? 
 R24
- R31? propagation  R43 
 ? R31
- R43? No propagations ? R43
28Constraint propagation
- Forward checking propagates information from 
 assigned to unassigned variables, but doesn't
 provide early detection for all failures
- NT and SA cannot both be blue! 
- Constraint propagation repeatedly enforces 
 constraints locally
29Constraint propagation
- Forward checking does not detect the blue 
 inconsistency, because it does not look far
 enough ahead.
- Constraint propagation is the general term for 
 propagating the implications of a constraint on
 one variable onto other variables.
- The idea of arc consistency provides a fast 
 method of constraint propagation that is
 substantially stronger than forward checking.
30Arc consistency
- Simplest form of propagation makes each arc 
 consistent
- X ?Y is consistent iff 
- for every value x of X there is some allowed y
31Arc consistency
- Simplest form of propagation makes each arc 
 consistent
- X ?Y is consistent iff 
- for every value x of X there is some allowed y
32Arc consistency
- Simplest form of propagation makes each arc 
 consistent
- X ?Y is consistent iff 
- for every value x of X there is some allowed y 
- If X loses a value, neighbors of X need to be 
 rechecked.
 
33Arc consistency
- Simplest form of propagation makes each arc 
 consistent
- X ?Y is consistent iff 
- for every value x of X there is some allowed y 
- If X loses a value, neighbors of X need to be 
 rechecked
- Arc consistency detects failure earlier than 
 forward checking
- Can be run as a preprocess or after each 
 assignment
-  
34Algorithm for arc consistency 
 35Algorithm for arc consistency AC-3
- It uses a queue to keep track of the arcs that 
 need to be checked for inconsistency.
- Each arc (Xi, Xj) in turn is removed from the 
 agenda and checked.
- If any values need to be deleted from the domain 
 of Xi, then every arc (Xk, Xj) pointing to Xi
 must be reinserted on the queue for checking.
36Algorithm for arc consistency AC-3
- (C1,C2) eliminate AZUL 
- (C2,C1) ok 
- (C2,C3) ok 
- (C3,C2) eliminate AZUL 
- (C2,C4) ok 
- (C4,C2) eliminate AZUL 
- (C3,C4) eliminate VERDE 
-  add (C2,C3) 
- (C4,C3) ok 
- (C2,C3) ok
C1
AZUL, ROJO
?
C2
AZUL
?
?
C3
C4
AZUL, ROJO, VERDE
AZUL, VERDE
?
Initial list (C1,C2), (C2,C1), (C2,C3), (C3,C2), 
 (C2,C4), (C4,C2), (C3,C4), (C4,C3)