solving problems that can be expressed as a number of constraints over some variables
some problems can be formulated as:
each constraint restrict the values of some variables
only those of the kind:
find some values for some variables so that some constraints are satisfied
place eight queens in a chessboard so that they cannot take each other
how to encode as variables and constraints:
not the only way to formulate the problem!
same problem (eight queens)
given: propositional formula in 3CNF
find: truth assignment satisfying it
each clause ⇒ a constraint
clause:
x1 ∨ ¬x4 ∨ x5
constraint: all Boolean values but 010
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
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)
…
often: on finite domains
theory: triple 〈X,D,C〉
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
each constraint:
example: C3 = 〈〈x2,x4〉, {〈2,3〉, 〈4,6〉, 〈1,5〉}〉
a contraint tells: these variables can only take these values
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
color this graph with two colors only:
|
|
|
|
formulate the “two queens” problem on a 3x3 chessboard
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:
|
|
|
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
express this propositional formula as variables, values and constraints
{x1 ∨ ¬x2, ¬x2 ∨ ¬x3, x3}
| x1 ∨ ¬x2 | = |
|
clause is satisfied by all pairs of values but 01 |
| ¬x2 ∨ ¬x3 | = |
|
clause is satisfied by all pair of values but 11 |
| ¬x3 | = |
|
clause is satisfied only by the value 1
one variable = unary constraint |
trivia: may a problem have the same constraint on variables x1,x2 and on variables x3,x4?
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
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
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!
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
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)
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
solution = assignment from variables to values
all constraint satisfied
function f : V → ∪ D
from variables to elements of the domains
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
more constraints may have the same variables
|
|
can be merged in a single constraint
only tuples in both
| x1 | x2 |
|---|---|
| 3 | 4 |
formally: if the constraint with scope S are C1 = 〈S,R1〉, C2 = 〈S,R2〉 … Cm = 〈S,Rm〉 their combination is:
Cc = 〈S, R1 ∩ R2 ∩ … ∩ Rm〉
this single constraint is equivalent to the original ones
merge all constraints with the same scope
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
how to find a solution:
the two appoaches can be combined
example: generate new constraint by inference, then use backtracking
backtracking may work better with more constraints
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
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:
C1 =
〈<x1,x2>,R1〉
C2 =
〈<x2,x3>,R2〉
C3 =
〈<x3,x1>,R2〉
C4 =
〈<x1,x4>,R2〉
the graph is therefore not a representation of the problem
only one aspect of its (the way the variables are linked)
also non-binary problems
notes:
example: problems with a single constraint, with scope
〈x1,x2,x3〉,
problem with three constraints, with scopes
〈x1,x2〉,
〈x2,x3〉, and
〈x3,x1〉
same primal graph:
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〉:
hyphergraph for the constraints with scope 〈x1,x2〉, 〈x2,x3〉, and 〈x3,x1〉:
write a program that generates the constraints for a Sudoku puzzle
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