A sample file.
5 #include <lemon/time_measure.h>
6 #include <lp_solver_glpk.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)];
47 // sum of rows and columns
48 lp.addRow(expr1==row_sum);
49 lp.addRow(expr2==row_sum);
50 cout <<"Expr1: "<<expr1<<endl;
51 cout <<"Expr2: "<<expr2<<endl;
53 expr3+=x[std::make_pair(i, i)];
54 expr4+=x[std::make_pair(i, (n+1)-i)];
56 cout <<"Expr3: "<<expr3<<endl;
57 cout <<"Expr4: "<<expr4<<endl;
58 // sum of the diagonal entries
59 lp.addRow(expr3==row_sum);
60 lp.addRow(expr4==row_sum);
62 cout << "elapsed time: " << ts << endl;
63 for (int i=1; i<=n; ++i) {
64 for (int j=1; j<=n; ++j) {
65 cout << "x("<<i<<","<<j<<")="<<lp.getPrimal(x[std::make_pair(i,j)])
72 // // we make new binary variables for each pair of
73 // // entries of the square to achieve that
74 // // the values of different entries are different
76 // for (Coords::const_iterator it=x.begin(); it!=x.end(); ++it) {
77 // Coords::const_iterator jt=it; ++jt;
78 // for(; jt!=x.end(); ++jt) {
79 // Col col1=(*it).second;
80 // Col col2=(*jt).second;
81 // Col col=lp.addCol();
82 // lp.setColLowerBound(col, 0.0);
83 // lp.setColUpperBound(col, 1.0);
84 // lp.addRow(double(-n*n+1.0)<=1.0*col2-1.0*col1-double(n*n)*col<=-1.0);
88 // cout << "elapsed time: " << ts << endl;
90 // // let's solve the integer problem
92 // cout << "elapsed time: " << ts << endl;
93 // for (int i=1; i<=n; ++i) {
94 // for (int j=1; j<=n; ++j) {
95 // cout << "x("<<i<<","<<j<<")="<<lp.getMIPPrimal(x[std::make_pair(i,j)])