Constraint Networks
( slides courtesy of Natalia Flerova,based on slides courtesy of Rina Dechter)
Reading
Russell and Norvig
Chapter 6 (3rd ed.); Chapter 5 (2nd ed.)
For next week:
Chapter 7 (for Tuesday)
Chapter 8 (for Thursday)
3
Good source of advanced information
Rina Dechter,
Constraint Processing,Morgan Kaufmann
4
Outline
Constraint Satisfaction Problems (CSPs): Definition, and simple modeling examples
Representing constraints Consistency algorithms (arc-, path- and i-consistency) General search strategy
Look-ahead methods
A Bred greenred blackgreen redgreen blackblack greenblack red
Constraint Satisfaction
Example: map coloring Variables - countries (A,B,C,etc.)
Values - colors (e.g., red, green, black)
Constraints: A≠B, A≠D, D≠E , etc .
C
A
B
D
E
F
G
6
Constraint Network; DefinitionA constraint network is: R=(X,D,C)X variables
D domains
C constraints
R expresses allowed tuples over scopes
A solution is an assignment to all variables that satisfies all constraints (join of all relations).
Tasks: consistency?, one or all solutions, counting, optimization
X={X1 , .. . ,X n}
D={D1 ,. . . , Dn}, Di={v1 , . .. vk }
C={C1 ,. .. C t },,, C i= S i , Ri
Example The 4-queen problem
Q
Q QQ
Place 4 Queens on a chess board of 4x4 such that no two queens reside in the same row, column or diagonal.
Standard CSP formulation of the problem:• Variables: each row is a variable.
X1
X 4
X 3
X 2
1 2 3 4
• Domains: Di={1,2,3,4}.
• Constraints: There are = 6 constraints involved:42( )
R12={1,31,4 2,4 3,1 4,1 4,2 }R13={1,2 1,4 2,12,3 3,2 3,4 4,1 4,3}R14={1,2 1,3 2,12,3 2,4 3,1 3,2 3,4 4,2 4,3 }R23={1,31,4 2,4 3,1 4,1 4,2 }R24={1,2 1,4 2,1 2,3 3,2 3,4 4,1 4,3 }R34={1,3 1,4 2,4 3,1 4,1 4,2 }
• Constraint Graph :
X1
X 2 X 4
X 3
Spring 2009 10
Example: configuration and design
Spring 2009 11
Configuration and design Want to build: recreation area, apartments, houses, cemetery,
dump
Recreation area near lakeSteep slopes avoided except for recreation areaPoor soil avoided for developmentsHighway far from apartments, houses and recreationDump not visible from apartments, houses and lakeLots 3 and 4 have poor soilLots 3, 4, 7, 8 are on steep slopesLots 2, 3, 4 are near lakeLots 1, 2 are near highway
12
Outline
CSP: Definition, and simple modeling examples Representing constraints Consistency algorithms (arc-, path- and i-consistency) General search strategy
Look-ahead methods
Spring 2009 13
Constraint’s representations
Relation: allowed tuples
Algebraic expression:
Propositional formula:X Y 2≤ 10 , X ≠Y
a∨b ¬c
X Y Z1 3 22 1 3
14
Operations with relations Intersection Union Difference Selection Projection Join Composition
Spring 2009 15
Figure 1.8: Example of set operations intersection, union, and difference applied to relations.
Spring 2009 16
selection, projection, and join operations on relations.
Spring 2009 17
Constraint Graphs: Primal, Dual and Hypergraphs
A (primal) constraint graph: a node per variable arcs connect constrained variables. A dual constraint graph: a node per constraint’s scope, an
arc connect nodes sharing variables =hypergraph
18
Outline
CSP: Definition, and simple modeling examples Representing constraints Consistency algorithms (arc-, path- and i-consistency) General search strategy
Look-ahead methods
19
Consistency methods Constraint propagation – inferring new
constraints Can get such an explicit network that the search
will find the solution without dead-ends. Approximation of inference:
Arc, path and i-consistency Methods that transform the original network
into a tighter and tighter representations
20
Arc-consistency
32,1,
32,1, 32,1,
1 X, Y, Z, T 3X YY = ZT ZX T
X Y
T Z
32,1,
=
- infer constraints based on pairs of variables
Insures that every legal value in the domain of a single variable hasa legal match In the domain of any other selected variable
21
1 X, Y, Z, T 3X YY = ZT ZX T
X Y
T Z
=
1 3
2 3
Arc-consistency
22
Arc-consistency
23
Revise for arc-consistency
Di Di∩π i Rij⊗ D j
24
AC-1
O enk 3 Complexity (Mackworth and Freuder, 1986): e = number of arcs, n variables, k values (ek^2, each loop, nk number of loops), best-case = ek, Arc-consistency is:
ek 2
25
AC-3
O ek 3 Complexity: Best case O(ek), since each arc may be processed in O(2k) e = number of arcs, n variables, k values
30
Sudoku –Constraint Satisfaction
Each row, column and major block must be alldifferent
“Well posed” if it has unique solution: 27 constraints
2 34 62
•Variables: empty slots
•Domains = {1,2,3,4,5,6,7,8,9}
•Constraints: 27 all-different
•Constraint •Propagation
•Inference
31
Path-consistency
32
Path-consistency
33
Revise-3
Complexity: O(k^3) Best-case: O(t) Worst-case O(tk) e = number of arcs, n variables, k values, t = tightness: number of tuples in the
constraint relation
34
PC-1
Complexity: e = number of arcs, n variables, k values, t = tightness: number of tuples in the
constraint relation
O n 5 k 5
35
PC-2
O n 3 k 5 Complexity: Optimal PC-4: (each pair deleted may add: 2n-1 triplets, number of pairs: O(n^2 k^2) size of Q is
O(n^3 k^2), processing is O(k^3)) e = number of arcs, n variables, k values, t = tightness: number of tuples in the
constraint relation
O n 3 k 3
36
Example: before and after path-consistency
PC-1 requires 2 processings of each arc while PC-2 may not Can we do path-consistency distributedly?
37
I-consistency
38
Outline
CSP: Definition, and simple modeling examples Representing constraints Consistency algorithms (arc-, path- and i-consistency) General search strategy
Look-ahead methods
39
Backtracking algorithm
No matter how much we reason about the problem – we are still left with choices → need to search the space of possible choices.
Backtracking search – assign values to each variable in turn, making sure each assigned value is consistent with values assigned thus far.
Encounter variable with no domain value consistent with previous assignment – dead-end → backtrack – change the value of previous variables.
Backtracking
The search space
A tree of all partial solutions A partial solution: (a1,…,aj) satisfying all
relevant constraints The size of the underlying search space depends
on: Variable ordering Level of consistency possessed by the problem
Search space and the effect of ordering
20 legal states1 dead-end leaf
48 legal states18 dead-end leaves
Z evenly divides x,y,l
Dependency on consistency level
After arc-consistency z=5 and l=5 are removed
After path-consistency R’_zx R’_zy R’_zl R’_xy R’_xl R’_yl
Backtrack-free network
Backtracking
Complexity of extending a partial solution: Complexity of consistent O(e log
t), t bounds tuples, e constraints Complexity of selectvalue O(e k
log t)
51
Outline
CSP: Definition, and simple modeling examples Representing constraints Consistency algorithms (arc-, path- and i-consistency) General search strategy
Look-ahead methods
Look-ahead: value orderings
Intuition: Choose value least likely to yield a dead-end Approach: apply propagation at each node in the search tree
Forward-checking (check each unassigned variable separately)
Maintaining arc-consistency (MAC) (apply full arc-consistency)
Full look-ahead One pass of arc-consistency (AC-1)
Partial look-ahead directional-arc-consistency
Forward-checking
The most limited form of constraint propagation during search
Propagates the effect of a tentative value selection to each future variable separately
If domain of one of the future variables becomes empty – value is not selected → try next value.
Forward-checking example
Forward-checking
O ek 2 Complexity of selectValue-forward-checking at each node:
Arc-consistency look-ahead(Gashnig, 1977)
Applies full arc-consistency on all un-instantiated variables following each value assignment to the current variable.
Complexity: If optimal arc-consistency is used:
SelectValue-Arc-Consistency: The repeat...until loop – essentially AC-1 with some variables instantiated.
Maintaining-Arc-Consistency (MAC): Performs full arc-consistency after each domain value is rejected.
O ek 3
Arc-consistency look-ahead:(maintaining arc-consistency MAC)
Full and partial look-ahead
Full looking ahead: Make one full pass through future variables
(remove repeat-until from selectValue-arc-consistency)
Partial look-ahead: Applies (similar-to) directional arc-consistency to
future variables. - future variables are only compared with those variables following them.
Complexity: also More efficient than MAC
O ek 3
Example of partial look-ahead