element
element |
Purpose
This constraint states that a variable z is the x
th element of an ordered list of integer V, in its ternary form it states that z is the [x,y]-th element of a matrix of integers M
Synopsis
element(x+I) = C
element(V,x{,I}) = C
element(V,x{,I}) = z with x,z cpvar and I integer
z = element(V,x{,I}) with x,z cpvar and I integer
element(M,x,y) = z with x,y,z cpvar
z = element(M,x,y) with x,y,z cpvar
Arguments
|
C
|
a constant integer value
|
|
z
|
the value variable
|
|
x
|
first index variable
|
|
y
|
second index variable
|
|
I
|
optional constant offset for index
|
|
V
|
a one-dimensional array of integer values
|
|
M
|
a matrix (two-dimensional array) of integers
|
Return value
An element constraint over z, x and y in the ternary form, over x and z in the binary form
Example
The following example shows how to use the element constraint:
model "Element"
uses "kalis"
declarations
RY = 43..52
RX = 1..2
D: array(RY) of integer
D2: array(RX,RY) of integer
x,y,d_of_y,d_of_x_y: cpvar
end-declarations
D :: (43..52)[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
D2:: (1..2,43..52)[10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
20, 21, 22, 23, 24, 25, 26, 27, 28, 29]
setname(x, "x")
setname(y, "y")
setname(d_of_y, "d_of_y")
setname(d_of_x_y, "d_of_x_y")
writeln("Original domains: ", x, y, d_of_y, d_of_x_y)
element(D,y) = d_of_y
element(D2,x,y) = d_of_x_y
writeln("After propagation: ", x, y, d_of_y, d_of_x_y)
! Solve the problem
while (cp_find_next_sol) do
nbSolutions += 1
writeln("Solution ", nbSolutions, ": x:", getsol(x),
" y:", getsol(y), " d_of_y:", getsol(d_of_y),
" d_of_x_y:", getsol(d_of_x_y))
end-do
writeln("done!")
end-model
