test/mip_test.cc
author alpar
Wed, 09 Aug 2006 12:51:21 +0000
changeset 2170 42fffa713424
parent 2147 63d293ff1bef
child 2213 2c094dfa176d
permissions -rw-r--r--
Do not list the header itself.
athos@2146
     1
#include <lemon/lp.h>
athos@2149
     2
#include "test_tools.h"
athos@2146
     3
athos@2146
     4
using namespace lemon;
athos@2146
     5
athos@2149
     6
void solveAndCheck(Mip& lp, LpSolverBase::SolutionStatus stat, 
athos@2149
     7
		   double exp_opt) {
athos@2149
     8
  using std::string;
athos@2149
     9
  lp.solve();
athos@2149
    10
  //int decimal,sign;
athos@2149
    11
  std::ostringstream buf;
athos@2149
    12
  buf << "Primalstatus should be: " << int(stat)<<" and it is "<<int(lp.primalStatus());
athos@2146
    13
athos@2149
    14
  //  itoa(stat,buf1, 10);
athos@2149
    15
  check(lp.primalStatus()==stat, buf.str());
athos@2149
    16
athos@2149
    17
  if (stat ==  LpSolverBase::OPTIMAL) {
athos@2149
    18
    std::ostringstream buf;
athos@2149
    19
    buf << "Wrong optimal value: the right optimum is " << exp_opt; 
athos@2149
    20
    check(std::abs(lp.primalValue()-exp_opt) < 1e-3, buf.str());
athos@2149
    21
    //+ecvt(exp_opt,2)
athos@2149
    22
  }
athos@2149
    23
}
athos@2149
    24
athos@2149
    25
void aTest(Mip& mip)
athos@2149
    26
{
athos@2149
    27
 //The following example is very simple
athos@2149
    28
athos@2149
    29
  typedef Mip::Row Row;
athos@2149
    30
  typedef Mip::Col Col;
athos@2149
    31
athos@2149
    32
athos@2149
    33
  Col x1 = mip.addCol();
athos@2149
    34
  Col x2 = mip.addCol();
athos@2149
    35
athos@2149
    36
athos@2149
    37
athos@2149
    38
  //Constraints
athos@2149
    39
  mip.addRow(2*x1+x2 <=2);  
athos@2149
    40
  mip.addRow(x1-2*x2 <=0);  
athos@2149
    41
athos@2149
    42
  //Nonnegativity of the variable x1
athos@2149
    43
  mip.colLowerBound(x1, 0);
athos@2149
    44
athos@2149
    45
athos@2149
    46
athos@2149
    47
  //Objective function
athos@2149
    48
  mip.setObj(x1);
athos@2149
    49
athos@2149
    50
  mip.max();
athos@2149
    51
athos@2149
    52
athos@2149
    53
  //Maximization of x1
athos@2149
    54
  //over the triangle with vertices
athos@2149
    55
  double expected_opt=4.0/5.0;
athos@2149
    56
  solveAndCheck(mip, Mip::OPTIMAL, expected_opt);
athos@2149
    57
athos@2149
    58
  //Restrict x2 to integer
athos@2149
    59
  mip.colType(x2,Mip::INTEGER);  
athos@2149
    60
  expected_opt=1.0/2.0;
athos@2149
    61
  solveAndCheck(mip, Mip::OPTIMAL, expected_opt);
athos@2149
    62
athos@2149
    63
athos@2149
    64
  //Restrict both to integer
athos@2149
    65
  mip.colType(x1,Mip::INTEGER);  
athos@2149
    66
  expected_opt=0;
athos@2149
    67
  solveAndCheck(mip, Mip::OPTIMAL, expected_opt);
athos@2149
    68
athos@2149
    69
 
athos@2149
    70
athos@2149
    71
}
athos@2149
    72
athos@2149
    73
athos@2149
    74
int main() 
athos@2149
    75
{
athos@2146
    76
athos@2147
    77
#ifdef HAVE_GLPK
athos@2149
    78
  MipGlpk mip1;
athos@2149
    79
  aTest(mip1);
athos@2147
    80
#endif
athos@2146
    81
athos@2147
    82
  return 0;
athos@2146
    83
athos@2146
    84
}