19.01.2015 Views

Constraint Logic Programming Using ECLiPSe

Constraint Logic Programming Using ECLiPSe

Constraint Logic Programming Using ECLiPSe

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

6.4 <strong>Constraint</strong> satisfaction problems: examples 97<br />

where x ∈ {S, E, N, D, M, O, R, Y }. In turn, the all different constraint<br />

is expressed as the set of ten constraints<br />

∑<br />

is x,d ≤ 1,<br />

x∈{S,E,N,D,M,O,R,Y }<br />

where d ∈ {0, 1, . . ., 9}. The 0/1 variables is x,d relate back to the original<br />

variables used in the equality constraint(s) via eight constraints<br />

9∑<br />

d · is x,d = x,<br />

d=0<br />

where x ∈ {S, E, N, D, M, O, R, Y }.<br />

The disadvantage of this approach is of course that we need to introduce<br />

80 new variables.<br />

The problem under consideration has a unique solution depicted by the<br />

following sum:<br />

That is, the assignment<br />

9567<br />

+ 1085<br />

10652<br />

{(S, 9), (E, 5), (N, 6), (D, 7), (M, 1), (O, 0), (R, 8), (Y, 2)}<br />

is a unique solution of the original CSP and each other discussed representation<br />

of the problem has a unique solution, as well.<br />

6.4.3 The n-queens problem<br />

This is probably the most well-known CSP. The problem is to place n queens<br />

on the n×n chess board, where n ≥ 3, so that they do not attack each other.<br />

We discuss two representations of this problem as a CSP.<br />

Representation using 0/1 variables This representation uses n 2 0/1<br />

variables x i,j , where i ∈ [1..n] and j ∈ [1..n], each of them representing one<br />

field of the chess board. The appropriate constraints can then be written<br />

using sums of relevant variables.<br />

The following constraints formalise the problem:<br />

• ∑ n<br />

j=1 x i,j = 1 for i ∈ [1..n] (exactly one queen per row),<br />

• ∑ n<br />

j=1 x j,i = 1 for i ∈ [1..n] (exactly one queen per column),

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!