c={1,1,1,1,1,1,1,1,1};
m={
{1,1,1,0,0,0,0,0,0},
{0,0,0,1,1,1,0,0,0},
{0,0,0,0,0,0,1,1,1},
{1,0,0,1,0,0,1,0,0},
{0,1,0,0,1,0,0,1,0},
{0,0,1,0,0,1,0,0,1},
{1,0,0,0,1,0,0,0,1},
{0,0,1,0,1,0,1,0,0},
{-1,-1,-1,0,0,0,0,0,0},
{0,0,0,-1,-1,-1,0,0,0},
{0,0,0,0,0,0,-1,-1,-1},
{-1,0,0,-1,0,0,-1,0,0},
{0,-1,0,0,-1,0,0,-1,0},
{0,0,-1,0,0,-1,0,0,-1},
{-1,0,0,0,-1,0,0,0,-1},
{0,0,-1,0,-1,0,-1,0,0},
{1,0,0,0,0,0,0,0,0},
{0,1,0,0,0,0,0,0,0},
{0,0,1,0,0,0,0,0,0},
{0,0,0,1,0,0,0,0,0},
{0,0,0,0,1,0,0,0,0},
{0,0,0,0,0,1,0,0,0},
{0,0,0,0,0,0,1,0,0},
{0,0,0,0,0,0,0,1,0},
{0,0,0,0,0,0,0,0,1},
{-1,0,0,0,0,0,0,0,0},
{0,-1,0,0,0,0,0,0,0},
{0,0,-1,0,0,0,0,0,0},
{0,0,0,-1,0,0,0,0,0},
{0,0,0,0,-1,0,0,0,0},
{0,0,0,0,0,-1,0,0,0},
{0,0,0,0,0,0,-1,0,0},
{0,0,0,0,0,0,0,-1,0},
{0,0,0,0,0,0,0,0,-1}
};
b={15,15,15,15,15,15,15,15,-15,-15,-15,-15,-15,-15,-15,-15,9,9,9,9,9,9,9,9,9,-1,-1,-6,-1,-1,-1,-1,-1,-1};
LinearProgramming[-c,-m,-b]
{2, 7, 6, 9, 5, 1, 4, 3, 8}
Mathematica does find a solution in less than a second. An interesting ( Mathematica ) programming exercise would be to generate the code for solving this integer programming problem for magic squares of size n. With increasing n the number of constraints increase fast. This could become an interesting benchmark. - For a 3 by 3 magic square 34 constraints are used above ( although 32 would have sufficed, probably even less ) but these constraints can be systematically generated.
Tidak ada komentar:
Posting Komentar