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