2

I want to do a simple constraint over all values of a list, I want each index of each row of an array to have the following (ic) constraint:

500 #= 2^X1 + 2^X2 + 2^X3 + ... + 2^X9

I try to do the code below. Array is a 9x9 matrix and for every row I want the above constraint to be satisfied. However, this does not seem to work, the program does not find any possible values that satisfy the constraint.

model(Array) :- 
Array :: 1..9,
(for(I,1,9), param(Array) 
 do
      X1 is Array[I,1],
      X2 is Array(I,2],
      X3 is Array[I,3],
      X4 is Array[I,4],
      X5 is Array[I,5],
      X6 is Array[I,6],
      X7 is Array[I,7],
      X8 is Array[I,8],
      X9 is Array[I,9],
      500 #= 2^X1 + 2^X2 + 2^X3 + 2^X4 + 2^X5 + 2x^X6 + 2^X7 + 2^X8 + 2^X9

),
term_variables(Array,L),
labeling(L),
printBoard(Array).
false
  • 10,264
  • 13
  • 101
  • 209

1 Answers1

2

This seems to be a bug in the implementation of integer constraints involving exponentiation expressions. Since your variables are all integral anyway, you can replace the #= with $= and it will work (the # constraints are supposed to impose integrality on the variables, while the $ constraints don't do this).

By the way, you could shorten your code this way:

    ...,
    ( foreacharg(Row,Array) do
        ( foreacharg(X,Row), foreach(2^X,Powers) do true ),
        500 $= sum(Powers)
    ),
    ...
jschimpf
  • 4,904
  • 11
  • 24