what is constraint programming?

solving problems that can be expressed as a number of constraints over some variables


why?

some problems can be formulated as:

  1. some variables
  2. some constraints on them

each constraint restrict the values of some variables


which problems?

only those of the kind:

find some values for some variables so that some constraints are satisfied


example: eight queens

place eight queens in a chessboard so that they cannot take each other

how to encode as variables and constraints:

variables
two for each queen: x1,y1, x2,y2, …
constraints

not the only way to formulate the problem!


another formulation

same problem (eight queens)

variables
each queen on its column: variables only for rows: r1,...,r8
constraints

example: 3SAT

given: propositional formula in 3CNF
find: truth assignment satisfying it

each clause ⇒ a constraint

clause: x1 ∨ ¬x4 ∨ x5
constraint: all Boolean values but 010


example: map coloring

color the areas of a map (e.g., nations)
no touching areas of the same color

variables: color for each area
constraints: available colors
constraints: no same color for touching areas


others...

find a schedule (e.g., allocation of time slots for lessons)

find a plan (sequence of action to reach a goal)
(only with some restrictions)


constraint satisfaction problem

often: on finite domains

theory: triple ⟨X,D,C⟩


variables and domains

X = {x1,…,xn}
D = {D1,…,Dn}

each xi is a variable

each Di is a set
contains: all possible values for xi

sometimes: same domain for all variables


constraint

each constraint:

example: C3 = ⟨⟨x2,x4⟩, {⟨2,3⟩, ⟨4,6⟩, ⟨1,5⟩}⟩

variables:
x2 and x4
values:
all other values are disallowed

a contraint tells: these variables can only take these values


tabular representation of a constraint

C3 = ⟨⟨x2,x4⟩, {⟨2,3⟩, ⟨4,6⟩, ⟨1,5⟩}⟩

in tabular form:

x2 x4
2 3
4 6
1 5

table header: which variables
table rows: which values these variables can take


example

color this graph with two colors only:

[graph.fig]


exercise

formulate the “two queens” problem on a 3x3 chessboard


solution

both coordinates for each queen
other formulation do not work (no queen may be in column 1, etc.)

two variables for each queen: x1,y1 e x2,y2

same domain for all: {1,2,3}

constraints:

x1 x2
1 2
1 3
2 1
2 3
3 1
3 2
y1 y2
1 2
1 3
2 1
2 3
3 1
3 2
x1 y1 x2 y2
1 1 1 2
1 1 1 3
1 1 2 1
1 1 2 3
...

first constraint: not the same row

second constraint: not the same column

third constraint: not same diagonal
note: 1112 satisfies this constraint but not the first


exercise

express this propositional formula as variables, values and constraints

{x1 ∨ ¬x2, ¬x2 ∨ ¬x3, x3}

solution

variables
one for each Boolean variables
domains
same for all variables: {0,1}
constraints
each clause = a constraint
variables of the constraint = variables of the clause
rows of the constraint = values allowed by the clause
(next slide)

solution: constraints

x1 ∨ ¬x2 =
x1 x2
0 0
1 0
1 1
clause is satisfied by all pairs of values but 01
¬x2 ∨ ¬x3 =
x2 x3
0 0
0 1
1 0
clause is satisfied by all pair of values but 11
¬x3 =
x3
0
clause is satisfied only by the value 1
one variable = unary constraint

names

domain of the variable x1:
the set Di of the values it can take
scope of a constraint:
its variables (the header of the table)
relation of a constraint
the set of its tuples of values (the rows of the table)

same constraint?

trivia: may a problem have the same constraint on variables x1,x2 and on variables x3,x4?


same constraint? no

constraint = list of variables and allowed values

two constraints may have the same relation (allowed values) on different variables

still, two different constraint (different variables)

same relation, not same constraint


common relations

the relation of a constraint can be expressed as:

second is easier; but requires the relation to be predefined in the solver;
example: the relation “all-different” is often available

in the theory of CSP, usually only the first


unary constraints

a constraint on a single variable

restricts the possible values of its variable

common changes involving unary constraints:

one can assume that the domain is the same for all variables
OR
one can assume that no constraint is unary

not both!


node consistency

a variable is node-consistent if every value of its domain satisfies all unary constraintson that variable

a CSP is node consistent if all its variables are


ensuring node consistency

remove values not satisfying a unary constraints from the domain

Di' = Di ∩ { v | ∀ C=⟨xi,R⟩   ⟨v⟩ ∈ R }

a value v is kept only if it in the relation of every unary constraint fot the variable

same as removing all values not satisfying one or more constraints:

Di' = Di \ { v | ∃ C=⟨xi,R)   ⟨v⟩ ∉ R }

this transformation makes all unary constraints unnecessary
(always satisfied)


constraint propagation

ensuring a problem is consistent is a way to simplify the problem without changing its solutions

is a form of constraint propagation:
from ⟨v⟩ ∉ R to the removal of v to the domain

exploit the effects of some constraints

other propagations of constraints are possible


solutions

solution = assignment from variables to values
all constraint satisfied

function f : V → ∪ D
from variables to elements of the domains


equivalence

equivalent problems = same solutions

example: constraint propagation
changes the problem without changing the solution
new problem is equivalent to the old

other such transformations exist


normalization

more constraints may have the same variables

x1x2
12
34
24
x1x2
11
34

can be merged in a single constraint
only tuples in both

x1x2
34

formally: if the constraint with scope S are C1 = ⟨S,R1, C2 = ⟨S,R2Cm = ⟨S,Rm their combination is:

Cc = ⟨S, R1 ∩ R2 ∩ … ∩ Rm

this single constraint is equivalent to the original ones


normal form

merge all constraints with the same scope


composition

new constraint, consequence of others

two binary constraints with a shared variable:

⟨⟨x1,x2⟩,R1
⟨⟨x2,x3>,R2

composition:

⟨⟨x1,x3⟩,R3
where ⟨a,b⟩ ∈ R3 ↔ ∃ c . ⟨a,c⟩ ∈ R1 and ⟨c,b⟩ ∈ R2

combine constraints, remove the shared variable

generalize to an arbitrary number of constraints
possibly not binary
arbitrary set of variables in the composition


solving

how to find a solution:


the two appoaches, combined

the two appoaches can be combined

example: generate new constraint by inference, then use backtracking

backtracking may work better with more constraints


backtracking


partial consistency

partical assigment: only some variables have a value;
example: x1 = 1, x2 = unassigned, x3 = 5

some constraints may already be violated;
example: a constraint on x1 and x3 that does not have the tuple ⟨1,5⟩

allows for backtracking before choosing a value for x2


backtracking variants

backmarking
avoid some checks of consistency
look-ahead
check in advance what happens with a value for a variable
backjumping
skip recursive calls if they are the same as others already done
constraint learning
store information discovered during search as new constraints

other topics


binary problems

common restriction: only constraints with two variables

same as: at most two variables each constraint,
since unary constraints can be removed

cam be visualized as graphs:


example

C1 = ⟨<x1,x2>,R1
C2 = ⟨<x2,x3>,R2
C3 = ⟨<x3,x1>,R2
C4 = ⟨<x1,x4>,R2

[graph-binary.fig]

the graph is therefore not a representation of the problem
only one aspect of its (the way the variables are linked)


primal graph

also non-binary problems

notes:


hyphergraph

alternative representation for non-binary problems

hyphergraph = nodes + sets of nodes (hypheredges)
graph: special case where all hypheredges are sets of two nodes

hyphergraph for the constraint with scope ⟨x1,x2,x3:

[one.fig]

hyphergraph for the constraints with scope ⟨x1,x2, ⟨x2,x3, and ⟨x3,x1:

[three.fig]


exercise

write a program that generates the constraints for a Sudoku puzzle


other kind of constraints

problems of the first kind are solved by elimination algorithms or the symplex method

problems of the second kind are solved by an extended unification algorithm