test/mip_test.cc
author alpar
Mon, 13 Nov 2006 18:46:19 +0000
changeset 2301 eb378706bd3d
parent 2227 809b18050520
child 2369 6ae1a97055a2
permissions -rw-r--r--
Test the automatic compilation checker 1/2: make a bug
athos@2149
     1
#include "test_tools.h"
athos@2146
     2
athos@2218
     3
athos@2221
     4
#ifdef HAVE_CONFIG_H
athos@2221
     5
#include <config.h>
athos@2221
     6
#endif
athos@2221
     7
deba@2227
     8
#ifdef HAVE_CPLEX
deba@2227
     9
#include <lemon/mip_cplex.h>
deba@2227
    10
#endif
deba@2227
    11
deba@2227
    12
#ifdef HAVE_GLPK
deba@2227
    13
#include <lemon/mip_glpk.h>
deba@2227
    14
#endif
athos@2221
    15
athos@2218
    16
athos@2146
    17
using namespace lemon;
athos@2146
    18
athos@2218
    19
void solveAndCheck(MipSolverBase& lp, MipSolverBase::SolutionStatus stat, 
athos@2149
    20
		   double exp_opt) {
athos@2149
    21
  using std::string;
athos@2218
    22
athos@2149
    23
  lp.solve();
athos@2149
    24
  //int decimal,sign;
athos@2149
    25
  std::ostringstream buf;
athos@2149
    26
  buf << "Primalstatus should be: " << int(stat)<<" and it is "<<int(lp.primalStatus());
athos@2146
    27
athos@2218
    28
athos@2149
    29
  //  itoa(stat,buf1, 10);
athos@2213
    30
  check(lp.mipStatus()==stat, buf.str());
athos@2149
    31
athos@2218
    32
  if (stat ==  MipSolverBase::OPTIMAL) {
athos@2149
    33
    std::ostringstream buf;
athos@2149
    34
    buf << "Wrong optimal value: the right optimum is " << exp_opt; 
athos@2149
    35
    check(std::abs(lp.primalValue()-exp_opt) < 1e-3, buf.str());
athos@2149
    36
    //+ecvt(exp_opt,2)
athos@2149
    37
  }
athos@2149
    38
}
athos@2149
    39
athos@2218
    40
void aTest(MipSolverBase& mip)
athos@2149
    41
{
athos@2149
    42
 //The following example is very simple
athos@2149
    43
athos@2218
    44
athos@2218
    45
  typedef MipSolverBase::Row Row;
athos@2218
    46
  typedef MipSolverBase::Col Col;
athos@2218
    47
athos@2149
    48
athos@2149
    49
athos@2149
    50
  Col x1 = mip.addCol();
athos@2149
    51
  Col x2 = mip.addCol();
athos@2149
    52
athos@2149
    53
athos@2213
    54
  //Objective function
athos@2213
    55
  mip.setObj(x1);
athos@2213
    56
athos@2213
    57
  mip.max();
athos@2213
    58
athos@2213
    59
athos@2213
    60
  //Unconstrained optimization
athos@2213
    61
  mip.solve();
athos@2213
    62
  //Check it out!
athos@2213
    63
athos@2149
    64
  //Constraints
athos@2149
    65
  mip.addRow(2*x1+x2 <=2);  
athos@2149
    66
  mip.addRow(x1-2*x2 <=0);  
athos@2149
    67
athos@2149
    68
  //Nonnegativity of the variable x1
athos@2149
    69
  mip.colLowerBound(x1, 0);
athos@2149
    70
athos@2149
    71
athos@2149
    72
athos@2149
    73
  //Maximization of x1
athos@2221
    74
  //over the triangle with vertices (0,0),(4/5,2/5),(0,2)
athos@2149
    75
  double expected_opt=4.0/5.0;
athos@2218
    76
  solveAndCheck(mip, MipSolverBase::OPTIMAL, expected_opt);
athos@2149
    77
athos@2149
    78
  //Restrict x2 to integer
athos@2267
    79
  mip.colType(x2,MipSolverBase::INT);  
athos@2149
    80
  expected_opt=1.0/2.0;
athos@2218
    81
  solveAndCheck(mip, MipSolverBase::OPTIMAL, expected_opt);
athos@2149
    82
athos@2149
    83
athos@2149
    84
  //Restrict both to integer
athos@2267
    85
  mip.colType(x1,MipSolverBase::INT);  
athos@2149
    86
  expected_opt=0;
athos@2218
    87
  solveAndCheck(mip, MipSolverBase::OPTIMAL, expected_opt);
athos@2149
    88
athos@2149
    89
 
athos@2149
    90
athos@2149
    91
}
athos@2149
    92
athos@2149
    93
athos@2149
    94
int main() 
athos@2149
    95
{
athos@2146
    96
athos@2147
    97
#ifdef HAVE_GLPK
athos@2149
    98
  MipGlpk mip1;
athos@2149
    99
  aTest(mip1);
athos@2147
   100
#endif
athos@2146
   101
athos@2218
   102
#ifdef HAVE_CPLEX
athos@2218
   103
  MipCplex mip2;
athos@2218
   104
  aTest(mip2);
athos@2218
   105
#endif
athos@2218
   106
athos@2147
   107
  return 0;
athos@2146
   108
athos@2146
   109
}