test/mip_test.cc
changeset 784 1a7fe3bef514
parent 631 d21b38647e53
child 1113 62dba6c90f35
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/mip_test.cc	Thu Nov 05 15:50:01 2009 +0100
     1.3 @@ -0,0 +1,159 @@
     1.4 +/* -*- mode: C++; indent-tabs-mode: nil; -*-
     1.5 + *
     1.6 + * This file is a part of LEMON, a generic C++ optimization library.
     1.7 + *
     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 + * Permission to use, modify and distribute this software is granted
    1.13 + * provided that this copyright notice appears in all copies. For
    1.14 + * precise terms see the accompanying LICENSE file.
    1.15 + *
    1.16 + * This software is provided "AS IS" with no warranty of any kind,
    1.17 + * express or implied, and with no claim as to its suitability for any
    1.18 + * purpose.
    1.19 + *
    1.20 + */
    1.21 +
    1.22 +#include "test_tools.h"
    1.23 +
    1.24 +#include <lemon/config.h>
    1.25 +
    1.26 +#ifdef LEMON_HAVE_CPLEX
    1.27 +#include <lemon/cplex.h>
    1.28 +#endif
    1.29 +
    1.30 +#ifdef LEMON_HAVE_GLPK
    1.31 +#include <lemon/glpk.h>
    1.32 +#endif
    1.33 +
    1.34 +#ifdef LEMON_HAVE_CBC
    1.35 +#include <lemon/cbc.h>
    1.36 +#endif
    1.37 +
    1.38 +
    1.39 +using namespace lemon;
    1.40 +
    1.41 +void solveAndCheck(MipSolver& mip, MipSolver::ProblemType stat,
    1.42 +                   double exp_opt) {
    1.43 +  using std::string;
    1.44 +
    1.45 +  mip.solve();
    1.46 +  //int decimal,sign;
    1.47 +  std::ostringstream buf;
    1.48 +  buf << "Type should be: " << int(stat)<<" and it is "<<int(mip.type());
    1.49 +
    1.50 +
    1.51 +  //  itoa(stat,buf1, 10);
    1.52 +  check(mip.type()==stat, buf.str());
    1.53 +
    1.54 +  if (stat ==  MipSolver::OPTIMAL) {
    1.55 +    std::ostringstream sbuf;
    1.56 +    sbuf << "Wrong optimal value ("<< mip.solValue()
    1.57 +         <<" instead of " << exp_opt << ")";
    1.58 +    check(std::abs(mip.solValue()-exp_opt) < 1e-3, sbuf.str());
    1.59 +    //+ecvt(exp_opt,2)
    1.60 +  }
    1.61 +}
    1.62 +
    1.63 +void aTest(MipSolver& mip)
    1.64 +{
    1.65 +  //The following example is very simple
    1.66 +
    1.67 +
    1.68 +  typedef MipSolver::Row Row;
    1.69 +  typedef MipSolver::Col Col;
    1.70 +
    1.71 +
    1.72 +  Col x1 = mip.addCol();
    1.73 +  Col x2 = mip.addCol();
    1.74 +
    1.75 +
    1.76 +  //Objective function
    1.77 +  mip.obj(x1);
    1.78 +
    1.79 +  mip.max();
    1.80 +
    1.81 +  //Unconstrained optimization
    1.82 +  mip.solve();
    1.83 +  //Check it out!
    1.84 +
    1.85 +  //Constraints
    1.86 +  mip.addRow(2 * x1 + x2 <= 2);
    1.87 +  Row y2 = mip.addRow(x1 - 2 * x2 <= 0);
    1.88 +
    1.89 +  //Nonnegativity of the variable x1
    1.90 +  mip.colLowerBound(x1, 0);
    1.91 +
    1.92 +
    1.93 +  //Maximization of x1
    1.94 +  //over the triangle with vertices (0,0),(4/5,2/5),(0,2)
    1.95 +  double expected_opt=4.0/5.0;
    1.96 +  solveAndCheck(mip, MipSolver::OPTIMAL, expected_opt);
    1.97 +
    1.98 +
    1.99 +  //Restrict x2 to integer
   1.100 +  mip.colType(x2,MipSolver::INTEGER);
   1.101 +  expected_opt=1.0/2.0;
   1.102 +  solveAndCheck(mip, MipSolver::OPTIMAL, expected_opt);
   1.103 +
   1.104 +
   1.105 +  //Restrict both to integer
   1.106 +  mip.colType(x1,MipSolver::INTEGER);
   1.107 +  expected_opt=0;
   1.108 +  solveAndCheck(mip, MipSolver::OPTIMAL, expected_opt);
   1.109 +
   1.110 +  //Erase a variable
   1.111 +  mip.erase(x2);
   1.112 +  mip.rowUpperBound(y2, 8);
   1.113 +  expected_opt=1;
   1.114 +  solveAndCheck(mip, MipSolver::OPTIMAL, expected_opt);
   1.115 +
   1.116 +}
   1.117 +
   1.118 +
   1.119 +template<class MIP>
   1.120 +void cloneTest()
   1.121 +{
   1.122 +
   1.123 +  MIP* mip = new MIP();
   1.124 +  MIP* mipnew = mip->newSolver();
   1.125 +  MIP* mipclone = mip->cloneSolver();
   1.126 +  delete mip;
   1.127 +  delete mipnew;
   1.128 +  delete mipclone;
   1.129 +}
   1.130 +
   1.131 +int main()
   1.132 +{
   1.133 +
   1.134 +#ifdef LEMON_HAVE_GLPK
   1.135 +  {
   1.136 +    GlpkMip mip1;
   1.137 +    aTest(mip1);
   1.138 +    cloneTest<GlpkMip>();
   1.139 +  }
   1.140 +#endif
   1.141 +
   1.142 +#ifdef LEMON_HAVE_CPLEX
   1.143 +  try {
   1.144 +    CplexMip mip2;
   1.145 +    aTest(mip2);
   1.146 +    cloneTest<CplexMip>();
   1.147 +  } catch (CplexEnv::LicenseError& error) {
   1.148 +    check(false, error.what());
   1.149 +  }
   1.150 +#endif
   1.151 +
   1.152 +#ifdef LEMON_HAVE_CBC
   1.153 +  {
   1.154 +    CbcMip mip1;
   1.155 +    aTest(mip1);
   1.156 +    cloneTest<CbcMip>();
   1.157 +  }
   1.158 +#endif
   1.159 +
   1.160 +  return 0;
   1.161 +
   1.162 +}