Copied only so far.
5 #include <lemon/time_measure.h>
6 #include <lp_solver_base.h>
10 using namespace lemon;
13 On an 1537Mhz PC, the run times with
14 glpk are the following.
15 for n=3,4, some secondes
19 int main(int, char **) {
21 const double row_sum=(1.0+n*n)*n/2;
24 typedef LPGLPK LPSolver;
25 typedef LPSolver::Col Col;
27 typedef std::map<std::pair<int, int>, Col> Coords;
29 // we create a new variable for each entry
30 // of the magic square
31 for (int i=1; i<=n; ++i) {
32 for (int j=1; j<=n; ++j) {
34 x[std::make_pair(i,j)]=col;
35 lp.setColLowerBound(col, 1.0);
36 lp.setColUpperBound(col, double(n*n));
39 LPSolver::Expression expr3, expr4;
40 for (int i=1; i<=n; ++i) {
41 LPSolver::Expression expr1, expr2;
42 for (int j=1; j<=n; ++j) {
43 expr1+=x[std::make_pair(i, j)];
44 expr2+=x[std::make_pair(j, i)];
46 // sum of rows and columns
47 lp.addRow(expr1==row_sum);
48 lp.addRow(expr2==row_sum);
49 expr3+=x[std::make_pair(i, i)];
50 expr4+=x[std::make_pair(i, (n+1)-i)];
52 // sum of the diagonal entries
53 lp.addRow(expr3==row_sum);
54 lp.addRow(expr4==row_sum);
56 cout << "elapsed time: " << ts << endl;
57 for (int i=1; i<=n; ++i) {
58 for (int j=1; j<=n; ++j) {
59 cout << "x("<<i<<","<<j<<")="<<lp.getPrimal(x[std::make_pair(i,j)])
63 // we make new binary variables for each pair of
64 // entries of the square to achieve that
65 // the values of different entries are different
67 for (Coords::const_iterator it=x.begin(); it!=x.end(); ++it) {
68 Coords::const_iterator jt=it; ++jt;
69 for(; jt!=x.end(); ++jt) {
70 Col col1=(*it).second;
71 Col col2=(*jt).second;
73 lp.setColLowerBound(col, 0.0);
74 lp.setColUpperBound(col, 1.0);
75 lp.addRow(double(-n*n+1.0)<=1.0*col2-1.0*col1-double(n*n)*col<=-1.0);
79 cout << "elapsed time: " << ts << endl;
81 // let's solve the integer problem
83 cout << "elapsed time: " << ts << endl;
84 for (int i=1; i<=n; ++i) {
85 for (int j=1; j<=n; ++j) {
86 cout << "x("<<i<<","<<j<<")="<<lp.getMIPPrimal(x[std::make_pair(i,j)])