I'm solving the magic square problem without using the alldiff operator.
To insert different values in the matrix, I initialized a variable:
var y {1..n, 1..n, 1..n * n} binary;
where y[i,j,k] will equal 1 if and only if entry i, j in the matrix equals k. Then each cell contains exactly one number is given by:
s.t. OneEach {i in 1..n, j in 1..n}: sum {k in 1..n * n} y[i,j,k] = 1;
and the constraint that all entries are different is given by:
s.t. AllDifferent {k in 1..n * n}: sum {i in 1..n, j in 1..n} y[i,j,k] <= 1;
My problem now is to formalize the constraint that the sum on the rows, columns and on the diagonals is equal to the magic constant. I tried with
s.t. RowsSum {k in 1..n * n}: sum {i in 1..n, j in 1..n} k * y [i, j, k] = constant;
s.t. ColsSum {k in 1..n * n}: sum {i in 1..n, j in 1..n} k * y [j, i, k] = constant;
An additional constraint is used to calculate the value of the constant:
s.t. q1: ((k*k)/2)*(k*k+1) = constant*k;
But when I load the model, he tells me:
presolve: constraint ColsSum [1] cannot hold: body> = 15 cannot be <= 9; difference = 6
presolve: constraint RowsSum [1] cannot hold: body> = 15 cannot be <= 9; difference = 6
Without the constraints on the sum of rows and columns, the result is this:
Magic_Constant = 15
1 2 3
4 5 6
7 8 9
The value of the magic constant is right, the square works because there are all the numbers from 1 to n^2, and they also appear only once. But now I must add that the sums on rows and columns must be equal to the magic constant.
Tips?