test/mip_test.cc
changeset 2149 b437bdee6fd0
parent 2147 63d293ff1bef
child 2213 2c094dfa176d
     1.1 --- a/test/mip_test.cc	Mon Jul 17 11:56:17 2006 +0000
     1.2 +++ b/test/mip_test.cc	Tue Jul 18 11:11:54 2006 +0000
     1.3 @@ -1,12 +1,82 @@
     1.4  #include <lemon/lp.h>
     1.5 +#include "test_tools.h"
     1.6  
     1.7  using namespace lemon;
     1.8  
     1.9 -int main(){
    1.10 +void solveAndCheck(Mip& lp, LpSolverBase::SolutionStatus stat, 
    1.11 +		   double exp_opt) {
    1.12 +  using std::string;
    1.13 +  lp.solve();
    1.14 +  //int decimal,sign;
    1.15 +  std::ostringstream buf;
    1.16 +  buf << "Primalstatus should be: " << int(stat)<<" and it is "<<int(lp.primalStatus());
    1.17  
    1.18 +  //  itoa(stat,buf1, 10);
    1.19 +  check(lp.primalStatus()==stat, buf.str());
    1.20 +
    1.21 +  if (stat ==  LpSolverBase::OPTIMAL) {
    1.22 +    std::ostringstream buf;
    1.23 +    buf << "Wrong optimal value: the right optimum is " << exp_opt; 
    1.24 +    check(std::abs(lp.primalValue()-exp_opt) < 1e-3, buf.str());
    1.25 +    //+ecvt(exp_opt,2)
    1.26 +  }
    1.27 +}
    1.28 +
    1.29 +void aTest(Mip& mip)
    1.30 +{
    1.31 + //The following example is very simple
    1.32 +
    1.33 +  typedef Mip::Row Row;
    1.34 +  typedef Mip::Col Col;
    1.35 +
    1.36 +
    1.37 +  Col x1 = mip.addCol();
    1.38 +  Col x2 = mip.addCol();
    1.39 +
    1.40 +
    1.41 +
    1.42 +  //Constraints
    1.43 +  mip.addRow(2*x1+x2 <=2);  
    1.44 +  mip.addRow(x1-2*x2 <=0);  
    1.45 +
    1.46 +  //Nonnegativity of the variable x1
    1.47 +  mip.colLowerBound(x1, 0);
    1.48 +
    1.49 +
    1.50 +
    1.51 +  //Objective function
    1.52 +  mip.setObj(x1);
    1.53 +
    1.54 +  mip.max();
    1.55 +
    1.56 +
    1.57 +  //Maximization of x1
    1.58 +  //over the triangle with vertices
    1.59 +  double expected_opt=4.0/5.0;
    1.60 +  solveAndCheck(mip, Mip::OPTIMAL, expected_opt);
    1.61 +
    1.62 +  //Restrict x2 to integer
    1.63 +  mip.colType(x2,Mip::INTEGER);  
    1.64 +  expected_opt=1.0/2.0;
    1.65 +  solveAndCheck(mip, Mip::OPTIMAL, expected_opt);
    1.66 +
    1.67 +
    1.68 +  //Restrict both to integer
    1.69 +  mip.colType(x1,Mip::INTEGER);  
    1.70 +  expected_opt=0;
    1.71 +  solveAndCheck(mip, Mip::OPTIMAL, expected_opt);
    1.72 +
    1.73 + 
    1.74 +
    1.75 +}
    1.76 +
    1.77 +
    1.78 +int main() 
    1.79 +{
    1.80  
    1.81  #ifdef HAVE_GLPK
    1.82 -  //This needs some thinking
    1.83 +  MipGlpk mip1;
    1.84 +  aTest(mip1);
    1.85  #endif
    1.86  
    1.87    return 0;