Magic square for n=2. It should have no solution!

52 views
Skip to first unread message

Ricardo

unread,
Oct 23, 2019, 7:30:04 AM10/23/19
to AMPL Modeling Language
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;

AMPL Google Group

unread,
Oct 23, 2019, 5:43:06 PM10/23/19
to AMPL Modeling Language
This model gives the following results for k = 2:

ampl: option solver gurobi;
ampl: solve;
Gurobi 8.1.0: infeasible
No basis.
No primal variables returned.

ampl: display constant;
constant = 5

So Gurobi is saying that there is no feasible solution. In this situation, Gurobi returns the last infeasible solution that it found, but Gurobi is not claiming that it found a feasible solution with constant = 5.


--
Robert Fourer
am...@googlegroups.com
{#HS:987668507-58474#}
Reply all
Reply to author
Forward
0 new messages