test/mip_test.cc
changeset 458 7afc121e0689
child 459 ed54c0d13df0
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/mip_test.cc	Tue Dec 02 21:40:33 2008 +0100
     1.3 @@ -0,0 +1,126 @@
     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-2008
     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 +
    1.25 +#ifdef HAVE_CONFIG_H
    1.26 +#include <lemon/config.h>
    1.27 +#endif
    1.28 +
    1.29 +#ifdef HAVE_CPLEX
    1.30 +#include <lemon/mip_cplex.h>
    1.31 +#endif
    1.32 +
    1.33 +#ifdef HAVE_GLPK
    1.34 +#include <lemon/mip_glpk.h>
    1.35 +#endif
    1.36 +
    1.37 +
    1.38 +using namespace lemon;
    1.39 +
    1.40 +void solveAndCheck(MipSolverBase& lp, MipSolverBase::SolutionStatus stat,
    1.41 +                   double exp_opt) {
    1.42 +  using std::string;
    1.43 +
    1.44 +  lp.solve();
    1.45 +  //int decimal,sign;
    1.46 +  std::ostringstream buf;
    1.47 +  buf << "Primalstatus should be: " << int(stat)
    1.48 +      <<" and it is "<<int(lp.mipStatus());
    1.49 +
    1.50 +
    1.51 +  //  itoa(stat,buf1, 10);
    1.52 +  check(lp.mipStatus()==stat, buf.str());
    1.53 +
    1.54 +  if (stat ==  MipSolverBase::OPTIMAL) {
    1.55 +    std::ostringstream sbuf;
    1.56 +    buf << "Wrong optimal value: the right optimum is " << exp_opt;
    1.57 +    check(std::abs(lp.primalValue()-exp_opt) < 1e-3, sbuf.str());
    1.58 +    //+ecvt(exp_opt,2)
    1.59 +  }
    1.60 +}
    1.61 +
    1.62 +void aTest(MipSolverBase& mip)
    1.63 +{
    1.64 + //The following example is very simple
    1.65 +
    1.66 +
    1.67 +  typedef MipSolverBase::Row Row;
    1.68 +  typedef MipSolverBase::Col Col;
    1.69 +
    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 +
    1.82 +  //Unconstrained optimization
    1.83 +  mip.solve();
    1.84 +  //Check it out!
    1.85 +
    1.86 +  //Constraints
    1.87 +  mip.addRow(2*x1+x2 <=2);
    1.88 +  mip.addRow(x1-2*x2 <=0);
    1.89 +
    1.90 +  //Nonnegativity of the variable x1
    1.91 +  mip.colLowerBound(x1, 0);
    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, MipSolverBase::OPTIMAL, expected_opt);
    1.97 +
    1.98 +  //Restrict x2 to integer
    1.99 +  mip.colType(x2,MipSolverBase::INT);
   1.100 +  expected_opt=1.0/2.0;
   1.101 +  solveAndCheck(mip, MipSolverBase::OPTIMAL, expected_opt);
   1.102 +
   1.103 +
   1.104 +  //Restrict both to integer
   1.105 +  mip.colType(x1,MipSolverBase::INT);
   1.106 +  expected_opt=0;
   1.107 +  solveAndCheck(mip, MipSolverBase::OPTIMAL, expected_opt);
   1.108 +
   1.109 +
   1.110 +
   1.111 +}
   1.112 +
   1.113 +
   1.114 +int main()
   1.115 +{
   1.116 +
   1.117 +#ifdef HAVE_GLPK
   1.118 +  MipGlpk mip1;
   1.119 +  aTest(mip1);
   1.120 +#endif
   1.121 +
   1.122 +#ifdef HAVE_CPLEX
   1.123 +  MipCplex mip2;
   1.124 +  aTest(mip2);
   1.125 +#endif
   1.126 +
   1.127 +  return 0;
   1.128 +
   1.129 +}