Finite Constraint Domains
Finite Constraint Domains
1
Finite Constraint Domains
Constraint satisfaction problems (CSP)
A backtracking solver
Node and arc consistency
Bounds consistency
Generalized consistency
Optimization for arithmetic CSPs
2
Finite Constraint Domains
An important class of constraint domains
Use to model constraint problems involving
choice: e.G. Scheduling, routing and
timetabling
The greatest industrial impact of constraint
programming has been on these problems
3
Constraint Satisfaction
Problems
A constraint satisfaction problem (CSP)
consists of:
A constraint C over variables x1,..., Xn
A domain D which maps each variable xi to a
set of possible values d(xi)
It is understood as the constraint
C x1 D( x1) xn D( xn)
4
Map Colouring
A classic CSP is the problem of coloring a map so that
no adjacent regions have the same color
Can the map of Australia be
colored with 3 colors ?
NT Q
WA NT WA SA NT SA
NT Q SA Q SA NSW WA
SA
SA V Q NSW NSW V NSW
7
Smugglers Knapsack
Smuggler with knapsack with capacity 9, who
needs to choose items to smuggle to make
profit at least 30
object profit size
whiskey 15 4
perfume 10 3
cigarretes 7 2
4W 3 P 2C 9 15W 10 P 7C 30
Backtracking (BT)
Generate & Test (GT)
exploring
Z
individual assignments
Y
X9
Generate & Test
probably the most general problem solving method
Algorithm:
generate labelling
test satisfaction
Drawbacks: Improvements:
blind generator smart generator
--> local search
late discovery of testing within generator
inconsistencies --> backtracking
10
Backtracking (BT)
incrementally extends a partial solution towards a complete
solution
Algorithm:
assign value to variable A1
check consistency
until all variables labelled B 2
1
Drawbacks:
thrashing C 2
1 1
redundant work
late detection of conflict
D
1 2 1 2 1
A = D, B D, A+C < 4
11
GT & BT - Example
Problem:
X::{1,2}, Y::{1,2}, Z::{1,2}
X = Y, X Z, Y > Z
X Y Z test X Y Z test
1 1 1 fail 1 1 1 fail
1 1 2 fail 2 fail
1 2 1 fail 2 fail
1 2 2 fail 2 1 fail
2 1 1 fail 2 1 passed
2 1 2 fail
2 2 1 passed
generate & test backtracking
12
Simple Backtracking Solver
The simplest way to solve CSPs is to enumerate
the possible solutions
The backtracking solver:
Enumerates values for one variable at a time
Checks that no prim. Constraint is false at each stage
Assume satisfiable(c) returns false when
primitive constraint c with no variables is
unsatisfiable
13
Partial Satisfiable
Check whether a constraint is unsatisfiable
because of a prim. Constraint with no vars
Partial_satisfiable(c)
Partial_satisfiable
For each primitive constraint c in C
If vars(c) is empty
Return true
14
Backtrack Solve
Back_solve(c,d)
Back_solve
If vars(c) is empty return partial_satisfiable(c)
partial_satisfiable
Choose x in vars(c)
For each value d in d(x)
Let C1 be C with x replaced by d
If partial_satisfiable(c1)
partial_satisfiable( then
If back_solve(c1,d)
back_solve then return true
Return false
15
Backtracking Solve
X Y Y Z D( X ) D(Y ) D( Z ) {1,2}
X Y Y Z
X 1 X 2
Choose
Variablevar
Choose XZ
var X
Y
Y
domain
domain {1,2}
domain {1,2}
{1,2} 1 Y Y Z 2 Y Y Z
Y 1 Y2 2 1 1 Z 2 22 Z
1 1 1 Z 1 22 Z
Z 1 No variables,
Z 2
partial_satisfiable
No variables, false and false
and false 1 22 1 1 22 2 16
Consistency Techniques
removing inconsistent values from variables’ domains
graph representation of the CSP
binary and unary constraints only (no problem!)
nodes = variables
edges = constraints A>5
node consistency (NC)
A
arc consistency (AC) A<
A
path consistency (PC) B C
(strong) k-consistency C
B B=
C
17
Node and Arc Consistency
Basic idea: find an equivalent CSP to the
original one with smaller domains of vars
Key: examine 1 prim.Constraint c at a time
Node consistency: (vars(c)={x}) remove any
values from domain of x that falsify c
Arc consistency: (vars(c)={x,y}) remove any
values from d(x) for which there is no value in
d(y) that satisfies c and vice versa
18
Node Consistency
Primitive constraint c is node consistent
with domain D if |vars(c)| /=1 or
If vars(c) = {x} then for each d in d(x)
X assigned d is a solution of c
A CSP is node consistent if each prim.
Constraint in it is node consistent
19
Node Consistency Examples
Example CSP is not node consistent (see Z)
X Y Y Z Z 2
D( X ) D(Y ) D( Z ) {1,2,3,4}
24
Achieving Arc Consistency
Arc_consistent(c,d)
Arc_consistent
Repeat
W := d
D := arc_consistent_primitive(c,d)
arc_consistent_primitive
Until W = D
Return D
A very naive version (there are much better)
25
Using Node and Arc Cons.
We can build constraint solvers using the
consistency methods
Two important kinds of domain
False domain: some variable has empty domain
Valuation domain: each variable has a
singleton domain
Extend satisfiable to CSP with val. Domain
26
Node and Arc Cons. Solver
D := node_consistent(C,D)
node_consistent
D := arc_consistent(C,D)
arc_consistent
if D is a false domain
return false
if D is a valuation domain
return satisfiable(C,D)
return unknown
27
Node and Arc Solver Example
Colouring Australia: with constraints
WA red NT yellow
WA NT SA Q NSW V T
Node
consistency
WA NT WA SA NT SA
NT Q SA Q SA NSW
SA V Q NSW NSW V
28
Node and Arc Solver Example
Colouring Australia: with constraints
WA red NT yellow
WA NT SA Q NSW V T
Arc
consistency
WA NT WA SA NT SA
NT Q SA Q SA NSW
SA V Q NSW NSW V 29
Node and Arc Solver Example
Colouring Australia: with constraints
WA red NT yellow
WA NT SA Q NSW V T
Arc
consistency
WA NT WA SA NT SA
NT Q SA Q SA NSW
SA V Q NSW NSW V 30
Node and Arc Solver Example
Colouring Australia: with constraints
WA red NT yellow
WA NT SA Q NSW V T
Arc
consistency
Answer:
WA NT WA SA NT SA
unknown NT Q SA Q SA NSW
SA V Q NSW NSW V 31
Backtracking Cons. Solver
We can combine consistency with the
backtracking solver
Apply node and arc consistency before
starting the backtracking solver and after
each variable is given a value
32
Back. Cons Solver Example
Q1 Q2 Q3 Q4
1
Therefore,
No value
There is no
wecan
need
be to
2
possible
choose to
assigned
value for
another value
Q3 in this 3
variable Q3!
for Q2.
case!
4
33
Back. Cons Solver Example
Q1 Q2 Q3 Q4
1
backtracking,
Backtracking…
backtracking,
We cannot
find any 2
Find another
Find another
possible
value of value
Q1?
value
value for
of
for Q4 inQ3? 3
Q2?
No!
this case!
Yes, Q1 =2
4
34
Back. Cons Solver Example
Q1 Q2 Q3 Q4
35
Back. Cons Solver Example
Q1 Q2 Q3 Q4
36
Node and Arc Solver Example
Colouring Australia: with constraints
WA red NT yellow
WA NT SA Q NSW V T
Backtracking
enumeration
1 2
Y
Z
1 2
38
Path Consistency (PC)
V2 V4
V3 V5
V0 V1
???
39
Path Consistency (PC)
V2 V4
V3 V5
V0 V1
???
41
Is k-consistency enough ?
If all the domains have cardinality 1=> solution
If any domain is empty => no solution
Otherwise ?
42
Consistency Completeness
strongly N-consistent constraint graph with N nodes =>
solution
strongly K-consistent constraint graph with N nodes
(K<N) => ???
A D
{1,2,3} {1,2,3}
path consistent
but no solution
B C
{1,2,3 {1,2,3}
Special graph structures }
tree structured graph => (D)AC is enough
43
hyper-arc consistency
A primitive constraint c is hyper-arc
consistent with domain D if
for each variable x in c and for each d in d(x) the valuation
x-->d can be extended to a solution of c
A CSP is hyper-arc consistent if each prim.
Constraint in it is hyper-arc consistent
NC hyper-arc consistency for constraint with 1 var
AC hyper-arc consistency for constraint with 2 var
44
Non binary constraints
What about prim. constraints with more
than 2 variables?
Each CSP can be transformed into an
equivalent binary CSP (dual encoding)
k-consitncy
hyper-arc consistency
45
Dual encoding
Each CSP can be transformed into an
equivalent binary CSP by ``dual encoding’’:
k-ary constraint c is converted to a dual variable vc with
the domain consisting of compatible tuples
for each pair of constraint c,c’ sharing some variable there
is a binary constraint between vc and vc’ restricting the
dual variables to tuples in which the original shared
variables take the same value
46
Hyper-arc consistency
hyper-arc consistency: extending arc
consistency to arbitrary number of variables
(better than binary representation)
Unfortunately determining hyper-arc
consistency is NP-hard (as expensive as
determinining if a CSP is satisfiable).
Solution?
47
Bounds Consistency
arithmetic CSP: domains = integers
range: [l..u] represents the set of integers {l,
l+1, ..., u}
idea use real number consistency and only
examine the endpoints (upper and lower
bounds) of the domain of each variable
Define min(D,x) as minimum element in
domain of x, similarly for max(D,x)
48
Bounds Consistency
A prim. constraint c is bounds consistent with
domain D if for each var x in vars(c)
exist real numbers d1, ..., dk for remaining vars
x1, ..., xk such that min(D,xj),= dj<= max(D,xj) and
{x min( D, x ), x1 d 1, xk is adk }
solution of c
and similarly for
{x max( D, x )}
An arithmetic CSP is bounds consistent if all its
primitive constraints are
49
Bounds Consistency Examples
X 3Y 5Z
D( X ) [2..7], D(Y ) [0..2], D( Z ) [ 1..2]
51
Achieving Bounds
Consistency
Consider the primitive constraint X = Y + Z which
is equivalent to the three forms
X Y Z Y X Z Z X Y
55
Multiplication X Y Z
If all variables are positive its simple enough
X min( D, Y ) min( D, Z ) X max( D, Y ) max( D, Z )
Y min( D, X ) / max( D, Z ) Y max( D, X ) / min( D, Z )
Z min( D, X ) / max( D, Y ) Z max( D, X ) / min( D, Y )
58
Bounds Consistency Algm
Repeatedly apply the propagation rules for
each primitive constraint until there is no
change in the domain
We do not need to examine a primitive
constraint until the domains of the variables
involve are modified
59
Bounds Consistency Example
61
Back. Bounds Cons. Solver
Apply bounds consistency before starting
the backtracking solver and after each
variable is given a value
62
Back. Bounds Solver Example
Smugglers knapsack problem (whiskey available)
capacity profit
4W 3 P 2C 9 15W 10 P 7C 30
Current domain:
D W)) [[00....9
D((W 20],],D P)) [[0
D((P ..3
1..19],
], D C)) [[3
D((C 9]]]
00....344
Backtrack
Backtrack Initial bounds consistency
W=0 W=1 W=2
P=1 P=2 P=3 (1,1,1) (2,0,0)
(0,1,3) false (0,3,0) No more solutions64
Generalized Consistency
Can use any consistency method with any
other communicating through the domain,
node consistency : prim constraints with 1 var
arc consistency: prim constraints with 2 vars
bounds consistency: other prim. constraints
Sometimes we can get more information by
using complex constraints and special
consistency methods
65
Alldifferent
alldifferent({V1,...,Vn}) holds when each
variable V1,..,Vn takes a different value
alldifferent({X, Y, Z}) is equivalent to
X Y X Z Y Z
Arc consistent with domain
D( X ) {1,2}, D(Y ) {1,2}, D( Z ) {1,2}
BUT there is no solution! specialized
consistency for alldifferent can find it
66
Alldifferent Consistency
let c be of the form alldifferent(V)
while exists v in V where D(v) = {d}
V := V - {v}
for each v’ in V
D(v’) := D(v’) - {d}
1 2 3 4 5
68
Other Complex Constraints
cumulative([ S1 , , Sn ],[ D1 , , Dn ],[ R1 , , Rn ], L)
schedule n tasks with start times Si and
durations Di needing resources Ri where L
resources are available at each moment
element ( I ,[V1 , ,Vn ], X )
array access if I = i, then X = Vi and if X !=
Vi then I != i
69
Optimization for CSPs
Because domains are finite can use a solver
to build a straightforward optimizer
retry_int_opt(C,
retry_int_opt D, f, best)
D2 := int_solv(C,D)
int_solv
if D2 is a false domain then return best
let sol be the solution corresponding to D2
return retry_int_opt(C
retry_int_opt /\ f < sol(f), D, f, sol)
70
Retry Optimization Example
Smugglers knapsack problem (optimize profit)
minimize 15
minimize 15WW10 10PP77CCsubject
subjectto to
capacity
capacity profit
profit
W33PP22CC99 15
44W 15W W10 10PP77CC30 30
15WD(W10
) P[0..715 D
9C],W 10)
( 31
P
P [015 9],D
W
7..C 10 CP)
(31 7[ 0C..9]32
D W))[[00....99],],D
D((W D((PP))[[00....99],],D
D((CC))[[00....99]]
First
Next
No next
solution
solution
solution!
found: (W) )[0[1....01],],D
found: DD(W D((PP)) [[11....11],],D C)) [[31..1
D((C 3]
Corresponding
Return sol{W
solution sol
best solution {W 01
, ,PP 11, C
, C 31}}
sol ( f ) 32
31 71
Backtracking Optimization
Since the solver may use backtrack search
anyway combine it with the optimization
At each step in backtracking search, if best
is the best solution so far add the constraint
f < best(f)
72
Back. Optimization Example
Smugglers knapsack problem (whiskey available)
capacity
Smugglers profitavailable)
knapsack problem (whiskey
3 P 2C 9
4W capacity 15W 10 P 7C 30
profit
4W 3 P 2C15
W9 10 W
P15 7C10P
31 7C 30
Current domain:
D W)) [[00....9
D((W 20],],D P)) [[0
D((P ..3
1..19],
], D C)) [[3
D((C 9]]]
00....344
xcurrent
(Cthe
use d best (C to
, f )solution x constrain
d , f )prob.
75
Branch and Bound Example
Smugglers knapsack problem
{W
211 / 4, P 0, CW 03}
{W P 20, P 1 / 3, CP 10} false
W 0 2, P 0C, C
{C 6 1/ 4, P W1,
1 1 / 2{}W W C 2 0}
{W 2,(2,0,0)
Solution P 0=, C30 0} {W P 1,1P P5 / 32, Cfalse
0}
{W 17 / 4, P W0, C2 Solution
{}
1W 1,(1,1,1)
P 1= , C32 1}
C 21, P C
{W 3 5 false
0, C / 2} Worse than best sol
false W 03 / 4, W
{W P 10, C 3}
false false 76
Finite Constraint Domains
Summary
CSPs form an important class of problems
Solving of CSPs is essentially based on
backtracking search
Reduce the search using consistency methods
node, arc, bound, generalized
Optimization is based on repeated solving or
using a real optimizer to guide the search
77