src/work/athos/lp/magic_square.cc
author athos
Tue, 22 Mar 2005 16:49:30 +0000
changeset 1243 41caee260bd4
parent 1241 dadc9987c537
permissions -rw-r--r--
Ami f?lig van k?sz, ma m?r f?lig marad...
marci@1153
     1
// -*- c++ -*-
marci@1153
     2
#include <iostream>
marci@1153
     3
#include <fstream>
marci@1153
     4
marci@1153
     5
#include <lemon/time_measure.h>
athos@1241
     6
#include <lp_solver_glpk.h>
marci@1153
     7
marci@1153
     8
using std::cout;
marci@1153
     9
using std::endl;
marci@1153
    10
using namespace lemon;
marci@1153
    11
marci@1153
    12
/*
marci@1176
    13
  On an 1537Mhz PC, the run times with 
marci@1176
    14
  glpk are the following.
marci@1176
    15
  for n=3,4, some secondes
marci@1176
    16
  for n=5, 25 hours
marci@1153
    17
 */
marci@1153
    18
marci@1153
    19
int main(int, char **) {
athos@1243
    20
  const int n=3;
marci@1153
    21
  const double row_sum=(1.0+n*n)*n/2;
marci@1153
    22
  Timer ts;
marci@1153
    23
  ts.reset();
athos@1241
    24
  typedef LpGlpk LPSolver;
marci@1153
    25
  typedef LPSolver::Col Col;
marci@1153
    26
  LPSolver lp;
marci@1153
    27
  typedef std::map<std::pair<int, int>, Col> Coords;
marci@1153
    28
  Coords x;
marci@1153
    29
  // we create a new variable for each entry 
marci@1153
    30
  // of the magic square
marci@1153
    31
  for (int i=1; i<=n; ++i) {
marci@1153
    32
    for (int j=1; j<=n; ++j) { 
marci@1153
    33
      Col col=lp.addCol();
marci@1153
    34
      x[std::make_pair(i,j)]=col;
marci@1153
    35
      lp.setColLowerBound(col, 1.0);
marci@1153
    36
      lp.setColUpperBound(col, double(n*n));
marci@1153
    37
    }
marci@1153
    38
  }
marci@1153
    39
  LPSolver::Expression expr3, expr4;
marci@1153
    40
  for (int i=1; i<=n; ++i) {
marci@1153
    41
    LPSolver::Expression expr1, expr2;
marci@1153
    42
    for (int j=1; j<=n; ++j) {
marci@1153
    43
      expr1+=x[std::make_pair(i, j)];
marci@1153
    44
      expr2+=x[std::make_pair(j, i)];
marci@1153
    45
    }
athos@1243
    46
marci@1153
    47
    // sum of rows and columns
marci@1153
    48
    lp.addRow(expr1==row_sum);
marci@1153
    49
    lp.addRow(expr2==row_sum);
athos@1243
    50
      cout <<"Expr1: "<<expr1<<endl;
athos@1243
    51
      cout <<"Expr2: "<<expr2<<endl;
athos@1243
    52
marci@1153
    53
    expr3+=x[std::make_pair(i, i)];
marci@1153
    54
    expr4+=x[std::make_pair(i, (n+1)-i)];
marci@1153
    55
  }
athos@1243
    56
  cout <<"Expr3: "<<expr3<<endl;
athos@1243
    57
  cout <<"Expr4: "<<expr4<<endl;
marci@1153
    58
  // sum of the diagonal entries
marci@1153
    59
  lp.addRow(expr3==row_sum);
marci@1153
    60
  lp.addRow(expr4==row_sum);
marci@1153
    61
  lp.solveSimplex();
marci@1153
    62
  cout << "elapsed time: " << ts << endl;
marci@1153
    63
  for (int i=1; i<=n; ++i) {
marci@1153
    64
    for (int j=1; j<=n; ++j) { 
marci@1153
    65
      cout << "x("<<i<<","<<j<<")="<<lp.getPrimal(x[std::make_pair(i,j)]) 
marci@1153
    66
	   << endl;
marci@1153
    67
    }
marci@1153
    68
  }
athos@1243
    69
athos@1243
    70
athos@1243
    71
athos@1243
    72
//   // we make new binary variables for each pair of 
athos@1243
    73
//   // entries of the square to achieve that 
athos@1243
    74
//   // the values of different entries are different
athos@1243
    75
//   lp.setMIP();
athos@1243
    76
//   for (Coords::const_iterator it=x.begin(); it!=x.end(); ++it) {
athos@1243
    77
//     Coords::const_iterator jt=it; ++jt;
athos@1243
    78
//     for(; jt!=x.end(); ++jt) {
athos@1243
    79
//       Col col1=(*it).second;
athos@1243
    80
//       Col col2=(*jt).second;
athos@1243
    81
//       Col col=lp.addCol();
athos@1243
    82
//       lp.setColLowerBound(col, 0.0);
athos@1243
    83
//       lp.setColUpperBound(col, 1.0);
athos@1243
    84
//       lp.addRow(double(-n*n+1.0)<=1.0*col2-1.0*col1-double(n*n)*col<=-1.0);
athos@1243
    85
//       lp.setColInt(col);
athos@1243
    86
//     }
athos@1243
    87
//   }
athos@1243
    88
//   cout << "elapsed time: " << ts << endl;
athos@1243
    89
//   lp.solveSimplex();
athos@1243
    90
//   // let's solve the integer problem
athos@1243
    91
//   lp.solveBandB();
athos@1243
    92
//   cout << "elapsed time: " << ts << endl;
athos@1243
    93
//   for (int i=1; i<=n; ++i) {
athos@1243
    94
//     for (int j=1; j<=n; ++j) { 
athos@1243
    95
//       cout << "x("<<i<<","<<j<<")="<<lp.getMIPPrimal(x[std::make_pair(i,j)]) 
athos@1243
    96
// 	   << endl;
athos@1243
    97
//     }
athos@1243
    98
//   }
marci@1153
    99
}