src/work/marci/lp/magic_square.cc
author alpar
Mon, 21 Feb 2005 14:59:12 +0000
changeset 1164 80bb73097736
child 1176 1ba2b4c0c970
permissions -rw-r--r--
A year has passed again.
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@1153
    13
  for n=3,4 , the program is very fast
marci@1153
    14
  for n=5, with glpk, the run takes hours
marci@1153
    15
 */
marci@1153
    16
marci@1153
    17
int main(int, char **) {
marci@1153
    18
  const int n=4;
marci@1153
    19
  const double row_sum=(1.0+n*n)*n/2;
marci@1153
    20
  Timer ts;
marci@1153
    21
  ts.reset();
marci@1153
    22
  typedef LPGLPK LPSolver;
marci@1153
    23
  typedef LPSolver::Col Col;
marci@1153
    24
  LPSolver lp;
marci@1153
    25
  typedef std::map<std::pair<int, int>, Col> Coords;
marci@1153
    26
  Coords x;
marci@1153
    27
  // we create a new variable for each entry 
marci@1153
    28
  // of the magic square
marci@1153
    29
  for (int i=1; i<=n; ++i) {
marci@1153
    30
    for (int j=1; j<=n; ++j) { 
marci@1153
    31
      Col col=lp.addCol();
marci@1153
    32
      x[std::make_pair(i,j)]=col;
marci@1153
    33
      lp.setColLowerBound(col, 1.0);
marci@1153
    34
      lp.setColUpperBound(col, double(n*n));
marci@1153
    35
    }
marci@1153
    36
  }
marci@1153
    37
  LPSolver::Expression expr3, expr4;
marci@1153
    38
  for (int i=1; i<=n; ++i) {
marci@1153
    39
    LPSolver::Expression expr1, expr2;
marci@1153
    40
    for (int j=1; j<=n; ++j) {
marci@1153
    41
      expr1+=x[std::make_pair(i, j)];
marci@1153
    42
      expr2+=x[std::make_pair(j, i)];
marci@1153
    43
    }
marci@1153
    44
    // sum of rows and columns
marci@1153
    45
    lp.addRow(expr1==row_sum);
marci@1153
    46
    lp.addRow(expr2==row_sum);
marci@1153
    47
    expr3+=x[std::make_pair(i, i)];
marci@1153
    48
    expr4+=x[std::make_pair(i, (n+1)-i)];
marci@1153
    49
  }
marci@1153
    50
  // sum of the diagonal entries
marci@1153
    51
  lp.addRow(expr3==row_sum);
marci@1153
    52
  lp.addRow(expr4==row_sum);
marci@1153
    53
  lp.solveSimplex();
marci@1153
    54
  cout << "elapsed time: " << ts << endl;
marci@1153
    55
  for (int i=1; i<=n; ++i) {
marci@1153
    56
    for (int j=1; j<=n; ++j) { 
marci@1153
    57
      cout << "x("<<i<<","<<j<<")="<<lp.getPrimal(x[std::make_pair(i,j)]) 
marci@1153
    58
	   << endl;
marci@1153
    59
    }
marci@1153
    60
  }
marci@1153
    61
  // we make new binary variables for each pair of 
marci@1153
    62
  // entries of the square to achieve that 
marci@1153
    63
  // the values of different entries are different
marci@1153
    64
  lp.setMIP();
marci@1153
    65
  for (Coords::const_iterator it=x.begin(); it!=x.end(); ++it) {
marci@1153
    66
    Coords::const_iterator jt=it; ++jt;
marci@1153
    67
    for(; jt!=x.end(); ++jt) {
marci@1153
    68
      Col col1=(*it).second;
marci@1153
    69
      Col col2=(*jt).second;
marci@1153
    70
      Col col=lp.addCol();
marci@1153
    71
      lp.setColLowerBound(col, 0.0);
marci@1153
    72
      lp.setColUpperBound(col, 1.0);
marci@1153
    73
      lp.addRow(double(-n*n+1.0)<=1.0*col2-1.0*col1-double(n*n)*col<=-1.0);
marci@1153
    74
      lp.setColInt(col);
marci@1153
    75
    }
marci@1153
    76
  }
marci@1153
    77
  cout << "elapsed time: " << ts << endl;
marci@1153
    78
  lp.solveSimplex();
marci@1153
    79
  // let's solve the integer problem
marci@1153
    80
  lp.solveBandB();
marci@1153
    81
  cout << "elapsed time: " << ts << endl;
marci@1153
    82
  for (int i=1; i<=n; ++i) {
marci@1153
    83
    for (int j=1; j<=n; ++j) { 
marci@1153
    84
      cout << "x("<<i<<","<<j<<")="<<lp.getMIPPrimal(x[std::make_pair(i,j)]) 
marci@1153
    85
	   << endl;
marci@1153
    86
    }
marci@1153
    87
  }
marci@1153
    88
}