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