n
queens on a n×n
boardSuggestion (important):
To design a model, always start from the variables
Suggestion (important):
To design a model, always start from the variables
Variables, first hypothesis:
xi,j∈{0,1},∀i,j=0..n−1
xi,j=1
iff there is a queen in position i,j
Variables, second hypothesis:
xj∈{0..n−1},∀j=0..n−1
xj=i
iff the queen on column j
is on row i
Suggestion:
Suggestion:
Constraint: "No two queens on the same column"
Constraint: "No two queens on the same row"
xi≠xj,∀i<j
Suggestion:
Constraint: "No two queens on the same lo-left/up-right diagonal"
xj−xi≠j−i,∀i<j
xj−xi≠−(j−i),∀i<j
xj−xi≠(j−i)
?
A first alternative:
x−y≠v
But what about similar cases?
A first alternative:
x−y≠v
But what about similar cases?
x+y≠v
x−y=v
x+y=v
A second alternative:
x+y≠v
z=x+y
z≠v
This is much more versatile!
Thanks to this approach:
For example:
ax2+b=0⟷{y=xxz=ayw=z+bw=0
Do we have to post the individual constraints separately?
Let's add some constraints to our library!
z=x+y
sum constraintz=xy
multiplication constraintz=|x|
absolute value constraintz=min
minimum constraintwhere:
z
is automatically introduced when parsing the expressionz
is either a fresh variable or an expression objectx
and y
can be variables, expression objects, or constantsConstraints:
z = x / y\quad
integer division constraintz = x - y\quad
difference constraintz = \max(x, y)\quad
maximum constraintConstraints:
z = x / y\quad
integer division constraintz = x - y\quad
difference constraintz = \max(x, y)\quad
maximum constraintAre often available, but can be implemented internally as:
y\,z = x\quad
(typical limitation: y
must be positive)z = x + (-y)
z = -\min((-x), (-y))
So we have:
z = x + y\quad
sum constraintz = x - y\quad
difference constraintz = x\,y\quad
multiplication constraintz = x / y\quad
integer division constraintz = |x|\quad
absolute value constraintz = \min(x, y)\quad
minimum constraintz = \max(x, y)\quad
maximum constraintAnd the filtering algorithms?
We can now model the problem:
x_i \in \{0..n-1\}, \quad i = 0..n-1
x_i \neq x_j, \quad \forall i < j
x_j - x_i \neq (j-i), \forall i < j
x_j - x_i \neq -(j-1), \forall i < j
If we want the situation to stay manageable...
We will start from the simplest case, i.e. binary constraints
=
and \neq
<
, \leq
, >
, and \geq
Equality constraint, x = y
:
x \in \{0..2\}, y \in \{1..3\}
x \in \{1,2\}, y \in \{1,2\}
Disequality constraint x \neq y
x \in \{2\}, y \in \{1..3\}
x \in \{2\}, y \in \{1,3\}
Inequality constraint x \leq y
:
x \in \{1..3\}, y \in \{0..2\}
x \in \{1..2\}, y \in \{1..3\}
Informally, we remove a value v
from domain D(x)
:
c_j
...D(y)
.Informally, we remove a value v
from domain D(x)
:
c_j
...D(y)
.Formally, for a binary constraint c
:
v \in D(x)
has a support iff there exists a value w \in D(y)
such that (v,w) \in c
The definition for w \in D(y)
is of course analogous.
Arc Consistency
c
Formally:
c
with X(c) = \{x, y\}
is arc consistent iff every v \in D(x)
and every w \in D(y)
has a support
Constraint Graph
Let's see an example on our map-coloring problem:
Constraint Graph
Let's see an example on our map-coloring problem:
Arc Consistency is important for several reasons:
c
Let's consider this state of our example PLS:
Let's consider this state of our example PLS:
1
to x_{0,1}
...Let's consider this state of our example PLS:
Can we enforce global consistency on a problem?
And what is the complexity of enforcing AC for single constraints?
The complexity of a filtering algorithm is very important
Consequence: strong impact on the performance
Let's see the complexity of enforcing AC on our constraints!
The complexity of a filtering algorithm is very important
Consequence: strong impact on the performance
Let's see the complexity of enforcing AC on our constraints!
Assumptions:
=
, \neq
)Equality (x = y
): v \in D(x), v \notin D(y) \longrightarrow v \notin D(x)
=
, \neq
)Equality (x = y
): v \in D(x), v \notin D(y) \longrightarrow v \notin D(x)
D(x)
v
, check whether v \in D(y)
and possibly prunex
and y
: O(|D(x)| + |D(y)|)
=
, \neq
)Equality (x = y
): v \in D(x), v \notin D(y) \longrightarrow v \notin D(x)
D(x)
v
, check whether v \in D(y)
and possibly prunex
and y
: O(|D(x)| + |D(y)|)
Disequality (x \neq y
): D(x) = \{v\} \longrightarrow v \notin D(y)
=
, \neq
)Equality (x = y
): v \in D(x), v \notin D(y) \longrightarrow v \notin D(x)
D(x)
v
, check whether v \in D(y)
and possibly prunex
and y
: O(|D(x)| + |D(y)|)
Disequality (x \neq y
): D(x) = \{v\} \longrightarrow v \notin D(y)
D(x)
x
and y
: O(1)
<
, \leq
, >
, \geq
)What about inequalities? E.g. x \leq y
Two naive filtering rules (non-symmetric for x
and y
):
v \in D(x) \wedge v > \max(D(y)) \longrightarrow v \notin D(x)
v \in D(y) \wedge v < \min(D(x)) \longrightarrow v \notin D(y)
<
, \leq
, >
, \geq
)What about inequalities? E.g. x \leq y
Two naive filtering rules (non-symmetric for x
and y
):
v \in D(x) \wedge v > \max(D(y)) \longrightarrow v \notin D(x)
\max(D(y))
D(x)
, check and possibly prunev \in D(y) \wedge v < \min(D(x)) \longrightarrow v \notin D(y)
\min(D(y))
D(y)
, check and possibly prunex
and y
): O(|D(x)| + |D(y)|)
Let's consider an example:
x \leq y, \quad x \in \{400\,000..1\,000\,000\}\, y \in \{0..500\,000\}
We try to apply the rule: v \in D(x) \wedge v > \max(D(y)) \longrightarrow v \notin D(x)
\max(D(y))
has already been computed...600\,000
times...D(x)
should be the set \{400\,000..500\,000\}
Let's consider an example:
x \leq y, \quad x \in \{400\,000..1\,000\,000\}\, y \in \{0..500\,000\}
We try to apply the rule: v \in D(x) \wedge v > \max(D(y)) \longrightarrow v \notin D(x)
\max(D(y))
has already been computed...600\,000
times...D(x)
should be the set \{400\,000..500\,000\}
But we could reason in another way!
x
is \max(D(y))
, i.e. 500\,000
v \in D(x)
s.t. v > 500\,000
lacks a support\underline{x}, \overline{x}
...\overline{x} = 500\,000
, which takes only O(1)
We can formalize this idea:
x
), we store the domain min/max\underline{x} \leftrightarrow \min(D(x))
, \overline{x} \leftrightarrow \max(D(x))
\underline{x}
and \overline{x}
, we compute the min/max for y
lb_y
= lower bound, ub_y
= upper bound\underline{y} = lb_y
and \overline{y} = ub_y
We can formalize this idea:
x
), we store the domain min/max\underline{x} \leftrightarrow \min(D(x))
, \overline{x} \leftrightarrow \max(D(x))
\underline{x}
and \overline{x}
, we compute the min/max for y
lb_y
= lower bound, ub_y
= upper bound\underline{y} = \max(\underline{y}, lb_y)
and \overline{y} = \min(\overline{y}, ub_y)
By doing so, we enforce Bound Consistency. Formally:
c
on x
and y
is bound consistent iff \underline{x}
and \overline{x}
have a support in \{\underline{y}...\overline{y}\}
, and vice-versa
By computing ub_x, lb_x
based on \underline{y}, \overline{y}
we pretend that D(y) = \{\underline{y}..\overline{y}\}
As an example, we consider again:
x \leq y, \quad x \in \{400\,000..1\,000\,000\}\, y \in \{0..500\,000\}
\underline{x}, \overline{x}, \underline{y}, \overline{y}
explicitlylb_x = -\infty
, ub_x = \underline{y}
lb_y = \overline{x}
, ub_y = +\infty
D(x) = \{400\,000..500\,000\}
D(y) = \{400\,000..500\,000\}
\underline{x}, \overline{x}, \underline{y}, \overline{y}
, hence in constant timeBC is usually more efficient than AC, but also weaker
I.e. BC
x = y, \quad x \in \{0, 2, 4, 6\}\, y \in \{2, 3, 4\}
The bounds are:
lb_x = \underline{y} = 2
, ub_x = \overline{y} = 4
lb_y = \underline{x} = 0
, ub_y = \overline{x} = 6
We prune by updating the bounds:
D(x) = \{2, 4\}
, i.e. we keep the values between the boundsD(y) = \{2, 3, 4\}
, even if the value 3
is infeasible!In general, be careful when there are "holes" in the domains
So, overall we have that:
Both AC and BC are incomplete: we still need search
It is a trade-off!
\leq
, =
or (next slides) arithmetic constraintsAnd what about the arithmetic constraints? E.g.
z = x + y
Can we derive filtering rules by relying on (e.g.) AC?
And what about the arithmetic constraints? E.g.
z = x + y
Can we derive filtering rules by relying on (e.g.) AC?
Yes, but we have a problem:
We need another generalization
c
be a constraint with scope X(c) = \{x_0, x_1, \ldots x_{m-1}\}
v \in D(x_i)
has a (generalized) support...c
Formally:
v_i \in D(x_i)
has a support iff, \forall x_j \in X(c) \setminus \{x_i\}
, there exists a value v_j \in D(x_j)
such that (v_0, v_1, \ldots v_{m-1}) \in c
c
is specified as a list of tuplesAnd then we just repeat the same old story:
c
c
is generalized arc consistent iff\forall x_i \in X(c)
every v \in D(x_i)
has a support
x_i = v
can be extended to feasible assignment for c
Let us apply the definition to (e.g.) the sum constraint
z = x + y
Basic idea (for z
, the other rules are similar):
v
in D(z)
w,u
in D(x), D(y)
such that v = w + u
Let us apply the definition to (e.g.) the sum constraint
z = x + y
Basic idea (for z
, the other rules are similar):
v
in D(z)
w,u
in D(x), D(y)
such that v = w + u
v \in D(z)
:w \in D(x)
:v - w \in D(y)
: v
from D(z)
O(|D(z)|\,|D(x)|)
So, our optimized complexity is:
O(|D(z)|\,|D(x)|)
If the domains are large we are in big trouble. E.g.:
z = x + y, \quad x, y \in \{0..1\,000\}, z \in \{0..10\,000\}
x \in \{0..1,000\}, y \in \{0..1,000\}, z \in \{0..2,000\}
12\,000\,000
steps!The main idea is the same as in binary constraints:
\underline{x}_j, \overline{x}_j
values, we compute bounds\underline{x}_i, \overline{x}_i
Formally, for a constraint c with X(c) = \{x_0, x_1, \ldots x_{m-1}\}
:
v_i \in D(x_i)
has a (Generalized) BC support iff,\forall x_j \in X(c) \setminus \{x_i\}
, there exists a value v_j \in \{\underline{x}_j..\overline{x}_j\}
(v_0, v_1, \ldots v_{m-1}) \in c
\forall x_i \in X(c)
the values \underline{x}_i, \overline{x}_i
have a BC support
As an example, let us consider the sum constraint
z = x + y
The bounds are:
lb_z = \underline{x} + \underline{y}
, ub_z = \overline{x} + \overline{y}
lb_x = \underline{z} - \overline{y}
, ub_x = \overline{z} - \underline{y}
(based on the fact that x = z - y
)lb_y = \underline{z} - \overline{x}
, ub_y = \overline{z} - \underline{x}
The whole computation is done in O(1)
(constant time!)
As a second example, we consider the minimum constraint
z = \min(x, y)
The bounds are:
lb_z = \min(\underline{x}, \underline{y})
ub_z = \min(\overline{x}, \overline{y})
lb_x = \underline{z}
(the same reasoning holds for lb_y
)ub_x = \overline{z} \text{ if } (\underline{y} > \overline{x} \vee \underline{y} > \overline{z}) \text{ else } +\infty
(the same holds for ub_y
)y
cannot be the minimumExamples (filtering on z
):
z \in \{-3..3\}, x \in \{-1..4\}, y \in \{0..4\} \longrightarrow
z \in \{-1..3\}
z \in \{0..3\}, x \in \{-1..2\}, y \in \{0..4\} \longrightarrow
z \in \{0..2\}
As a second example, we consider the minimum constraint
z = \min(x, y)
The bounds are:
lb_z = \min(\underline{x}, \underline{y})
ub_z = \min(\overline{x}, \overline{y})
lb_x = \underline{z}
(the same reasoning holds for lb_y
)ub_x = \overline{z} \text{ if } (\underline{y} > \overline{x} \vee \underline{y} > \overline{z}) \text{ else } +\infty
(the same holds for ub_y
)y
cannot be the minimumExamples (filtering on x
):
z \in \{0..2\}, x \in \{0..5\}, y \in \{3..4\} \longrightarrow
x \in \{0..2\}
z \in \{0..2\}, x \in \{-1..3\}, y \in \{-2..4\} \longrightarrow
x \in \{0..3\}
A Constraint Optimization Problem (COP) is defined as:
COP = \langle X, D, C, f \rangle
where:
X, D, C
are the same as in CSPsf
is an expression, called objective functionf
(maximization by f
with -f
)Long story short:
Let us consider our old map coloring example:
Variables (and domains):
x_i \in \{0..n-1\},\quad \forall i = 0..n-1
n
is the number of regionsn
is an upper bound on the number of colors!Variables (and domains):
x_i \in \{0..n-1\},\quad \forall i = 0..n-1
n
is the number of regionsn
is an upper bound on the number of colors!Constraints:
x_i \neq x_j, \forall i,j \in 0..n-1
if region i
touches region j
Variables (and domains):
x_i \in \{0..n-1\},\quad \forall i = 0..n-1
n
is the number of regionsn
is an upper bound on the number of colors!Constraints:
x_i \neq x_j, \forall i,j \in 0..n-1
if region i
touches region j
Objective function:
\displaystyle f(x) = \max_{i=0..n-1} (x_i)\quad
(can be implemented via binary \max
)How do we solve a COP?
Branch and Bound:
A single, simple, trick:
f^*
...f(x) < f^*
I.e. the next solution must improve over f^*
x_0
)x_0 = 1
and propagatex_1
)x_1 = 2
and propagatef(x) = \max_{i=0..7}(x_i)
is also updatedf(x)
is 4f(x) < 4
f(x) < 4
f(x)
and x_6
x_7
)x_7 = 1
and propagatef(x) = 3
The solution is optimal, but we don't know it (yet)!
Main idea:
Some observations: