test/mip_test.cc
changeset 783 ef88c0a30f85
parent 631 d21b38647e53
child 1113 62dba6c90f35
     1.1 --- a/test/mip_test.cc	Mon Jan 12 23:11:39 2009 +0100
     1.2 +++ b/test/mip_test.cc	Thu Nov 05 15:48:01 2009 +0100
     1.3 @@ -2,7 +2,7 @@
     1.4   *
     1.5   * This file is a part of LEMON, a generic C++ optimization library.
     1.6   *
     1.7 - * Copyright (C) 2003-2008
     1.8 + * Copyright (C) 2003-2009
     1.9   * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
    1.10   * (Egervary Research Group on Combinatorial Optimization, EGRES).
    1.11   *
    1.12 @@ -18,19 +18,20 @@
    1.13  
    1.14  #include "test_tools.h"
    1.15  
    1.16 +#include <lemon/config.h>
    1.17  
    1.18 -#ifdef HAVE_CONFIG_H
    1.19 -#include <lemon/config.h>
    1.20 -#endif
    1.21 -
    1.22 -#ifdef HAVE_CPLEX
    1.23 +#ifdef LEMON_HAVE_CPLEX
    1.24  #include <lemon/cplex.h>
    1.25  #endif
    1.26  
    1.27 -#ifdef HAVE_GLPK
    1.28 +#ifdef LEMON_HAVE_GLPK
    1.29  #include <lemon/glpk.h>
    1.30  #endif
    1.31  
    1.32 +#ifdef LEMON_HAVE_CBC
    1.33 +#include <lemon/cbc.h>
    1.34 +#endif
    1.35 +
    1.36  
    1.37  using namespace lemon;
    1.38  
    1.39 @@ -49,7 +50,8 @@
    1.40  
    1.41    if (stat ==  MipSolver::OPTIMAL) {
    1.42      std::ostringstream sbuf;
    1.43 -    buf << "Wrong optimal value: the right optimum is " << exp_opt;
    1.44 +    sbuf << "Wrong optimal value ("<< mip.solValue()
    1.45 +         <<" instead of " << exp_opt << ")";
    1.46      check(std::abs(mip.solValue()-exp_opt) < 1e-3, sbuf.str());
    1.47      //+ecvt(exp_opt,2)
    1.48    }
    1.49 @@ -57,14 +59,13 @@
    1.50  
    1.51  void aTest(MipSolver& mip)
    1.52  {
    1.53 - //The following example is very simple
    1.54 +  //The following example is very simple
    1.55  
    1.56  
    1.57    typedef MipSolver::Row Row;
    1.58    typedef MipSolver::Col Col;
    1.59  
    1.60  
    1.61 -
    1.62    Col x1 = mip.addCol();
    1.63    Col x2 = mip.addCol();
    1.64  
    1.65 @@ -74,23 +75,24 @@
    1.66  
    1.67    mip.max();
    1.68  
    1.69 -
    1.70    //Unconstrained optimization
    1.71    mip.solve();
    1.72    //Check it out!
    1.73  
    1.74    //Constraints
    1.75 -  mip.addRow(2*x1+x2 <=2);
    1.76 -  mip.addRow(x1-2*x2 <=0);
    1.77 +  mip.addRow(2 * x1 + x2 <= 2);
    1.78 +  Row y2 = mip.addRow(x1 - 2 * x2 <= 0);
    1.79  
    1.80    //Nonnegativity of the variable x1
    1.81    mip.colLowerBound(x1, 0);
    1.82  
    1.83 +
    1.84    //Maximization of x1
    1.85    //over the triangle with vertices (0,0),(4/5,2/5),(0,2)
    1.86    double expected_opt=4.0/5.0;
    1.87    solveAndCheck(mip, MipSolver::OPTIMAL, expected_opt);
    1.88  
    1.89 +
    1.90    //Restrict x2 to integer
    1.91    mip.colType(x2,MipSolver::INTEGER);
    1.92    expected_opt=1.0/2.0;
    1.93 @@ -102,32 +104,53 @@
    1.94    expected_opt=0;
    1.95    solveAndCheck(mip, MipSolver::OPTIMAL, expected_opt);
    1.96  
    1.97 -
    1.98 +  //Erase a variable
    1.99 +  mip.erase(x2);
   1.100 +  mip.rowUpperBound(y2, 8);
   1.101 +  expected_opt=1;
   1.102 +  solveAndCheck(mip, MipSolver::OPTIMAL, expected_opt);
   1.103  
   1.104  }
   1.105  
   1.106  
   1.107 +template<class MIP>
   1.108 +void cloneTest()
   1.109 +{
   1.110 +
   1.111 +  MIP* mip = new MIP();
   1.112 +  MIP* mipnew = mip->newSolver();
   1.113 +  MIP* mipclone = mip->cloneSolver();
   1.114 +  delete mip;
   1.115 +  delete mipnew;
   1.116 +  delete mipclone;
   1.117 +}
   1.118 +
   1.119  int main()
   1.120  {
   1.121  
   1.122 -#ifdef HAVE_GLPK
   1.123 +#ifdef LEMON_HAVE_GLPK
   1.124    {
   1.125      GlpkMip mip1;
   1.126      aTest(mip1);
   1.127 +    cloneTest<GlpkMip>();
   1.128    }
   1.129  #endif
   1.130  
   1.131 -#ifdef HAVE_CPLEX
   1.132 +#ifdef LEMON_HAVE_CPLEX
   1.133    try {
   1.134      CplexMip mip2;
   1.135      aTest(mip2);
   1.136 +    cloneTest<CplexMip>();
   1.137    } catch (CplexEnv::LicenseError& error) {
   1.138 -#ifdef LEMON_FORCE_CPLEX_CHECK
   1.139      check(false, error.what());
   1.140 -#else
   1.141 -    std::cerr << error.what() << std::endl;
   1.142 -    std::cerr << "Cplex license check failed, lp check skipped" << std::endl;
   1.143 +  }
   1.144  #endif
   1.145 +
   1.146 +#ifdef LEMON_HAVE_CBC
   1.147 +  {
   1.148 +    CbcMip mip1;
   1.149 +    aTest(mip1);
   1.150 +    cloneTest<CbcMip>();
   1.151    }
   1.152  #endif
   1.153