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