src/work/marci/lp/magic_square.cc
author hegyi
Fri, 01 Apr 2005 09:43:52 +0000
changeset 1288 6cc7b573b7b5
parent 1153 4b0468de3a31
permissions -rw-r--r--
Graph displayer is now displaying nodes. Edges remain still undisplayed yet.
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>
marci@1153
     6
#include <lp_solver_base.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 **) {
marci@1153
    20
  const int n=4;
marci@1153
    21
  const double row_sum=(1.0+n*n)*n/2;
marci@1153
    22
  Timer ts;
marci@1153
    23
  ts.reset();
marci@1153
    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
    }
marci@1153
    46
    // sum of rows and columns
marci@1153
    47
    lp.addRow(expr1==row_sum);
marci@1153
    48
    lp.addRow(expr2==row_sum);
marci@1153
    49
    expr3+=x[std::make_pair(i, i)];
marci@1153
    50
    expr4+=x[std::make_pair(i, (n+1)-i)];
marci@1153
    51
  }
marci@1153
    52
  // sum of the diagonal entries
marci@1153
    53
  lp.addRow(expr3==row_sum);
marci@1153
    54
  lp.addRow(expr4==row_sum);
marci@1153
    55
  lp.solveSimplex();
marci@1153
    56
  cout << "elapsed time: " << ts << endl;
marci@1153
    57
  for (int i=1; i<=n; ++i) {
marci@1153
    58
    for (int j=1; j<=n; ++j) { 
marci@1153
    59
      cout << "x("<<i<<","<<j<<")="<<lp.getPrimal(x[std::make_pair(i,j)]) 
marci@1153
    60
	   << endl;
marci@1153
    61
    }
marci@1153
    62
  }
marci@1153
    63
  // we make new binary variables for each pair of 
marci@1153
    64
  // entries of the square to achieve that 
marci@1153
    65
  // the values of different entries are different
marci@1153
    66
  lp.setMIP();
marci@1153
    67
  for (Coords::const_iterator it=x.begin(); it!=x.end(); ++it) {
marci@1153
    68
    Coords::const_iterator jt=it; ++jt;
marci@1153
    69
    for(; jt!=x.end(); ++jt) {
marci@1153
    70
      Col col1=(*it).second;
marci@1153
    71
      Col col2=(*jt).second;
marci@1153
    72
      Col col=lp.addCol();
marci@1153
    73
      lp.setColLowerBound(col, 0.0);
marci@1153
    74
      lp.setColUpperBound(col, 1.0);
marci@1153
    75
      lp.addRow(double(-n*n+1.0)<=1.0*col2-1.0*col1-double(n*n)*col<=-1.0);
marci@1153
    76
      lp.setColInt(col);
marci@1153
    77
    }
marci@1153
    78
  }
marci@1153
    79
  cout << "elapsed time: " << ts << endl;
marci@1153
    80
  lp.solveSimplex();
marci@1153
    81
  // let's solve the integer problem
marci@1153
    82
  lp.solveBandB();
marci@1153
    83
  cout << "elapsed time: " << ts << endl;
marci@1153
    84
  for (int i=1; i<=n; ++i) {
marci@1153
    85
    for (int j=1; j<=n; ++j) { 
marci@1153
    86
      cout << "x("<<i<<","<<j<<")="<<lp.getMIPPrimal(x[std::make_pair(i,j)]) 
marci@1153
    87
	   << endl;
marci@1153
    88
    }
marci@1153
    89
  }
marci@1153
    90
}