The constraints of my model are that the sum of the elements on the lines must be equal to a constant (var constant), that the sum of the elements on the columns must be equal to the same constant and that also the sum on the diagonals must be equal to the same constant . These constraints are r, c, d1 and d2.
I have also inserted a constraint to find this constant because there is the property that multiplying the order of the matrix by the magic constant yields the sum of the values from 1 to k^2.
The problem I have with this model is that it works for every value of k, except for k = 2. The result gives me 5, but it should not be correct because the constraints r, c, d1 and d2 with k = 2 cannot be satisfied! (and furthermore there is no solution for 2x2 matrices).
What am I doing wrong?
### PARAMETER ###
param k;
param firstKnumber = ((k*k)/2)*(k*k+1);
### VARIABLE ###
var x{1..k,1..k} >= 1 <= k*k integer;
var constant;
### CONSTRAINT ###
subject to r{t in 1..k}: sum{i in 1..k} x[t,i] = constant;
subject to c{t in 1..k}: sum{i in 1..k} x[i,t] = constant;
subject to d1: sum{i in 1..k} x[i,i] = constant;
subject to d2: sum{i in 1..k} x[i,k-i+1] = constant;
subject to q1: firstKnumber = constant*k;
### OBJECTIVE ###
minimize Magic_Constant: constant;
data;
param k:= 2;
option solver gurobi;
solve;
display Magic_Constant;