1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/test/mip_test.cc Thu Dec 10 17:05:35 2009 +0100
1.3 @@ -0,0 +1,158 @@
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 + buf << "Wrong optimal value: the right optimum is " << exp_opt;
1.57 + check(std::abs(mip.solValue()-exp_opt) < 1e-3, sbuf.str());
1.58 + //+ecvt(exp_opt,2)
1.59 + }
1.60 +}
1.61 +
1.62 +void aTest(MipSolver& mip)
1.63 +{
1.64 + //The following example is very simple
1.65 +
1.66 +
1.67 + typedef MipSolver::Row Row;
1.68 + typedef MipSolver::Col Col;
1.69 +
1.70 +
1.71 + Col x1 = mip.addCol();
1.72 + Col x2 = mip.addCol();
1.73 +
1.74 +
1.75 + //Objective function
1.76 + mip.obj(x1);
1.77 +
1.78 + mip.max();
1.79 +
1.80 + //Unconstrained optimization
1.81 + mip.solve();
1.82 + //Check it out!
1.83 +
1.84 + //Constraints
1.85 + mip.addRow(2 * x1 + x2 <= 2);
1.86 + Row y2 = mip.addRow(x1 - 2 * x2 <= 0);
1.87 +
1.88 + //Nonnegativity of the variable x1
1.89 + mip.colLowerBound(x1, 0);
1.90 +
1.91 +
1.92 + //Maximization of x1
1.93 + //over the triangle with vertices (0,0),(4/5,2/5),(0,2)
1.94 + double expected_opt=4.0/5.0;
1.95 + solveAndCheck(mip, MipSolver::OPTIMAL, expected_opt);
1.96 +
1.97 +
1.98 + //Restrict x2 to integer
1.99 + mip.colType(x2,MipSolver::INTEGER);
1.100 + expected_opt=1.0/2.0;
1.101 + solveAndCheck(mip, MipSolver::OPTIMAL, expected_opt);
1.102 +
1.103 +
1.104 + //Restrict both to integer
1.105 + mip.colType(x1,MipSolver::INTEGER);
1.106 + expected_opt=0;
1.107 + solveAndCheck(mip, MipSolver::OPTIMAL, expected_opt);
1.108 +
1.109 + //Erase a variable
1.110 + mip.erase(x2);
1.111 + mip.rowUpperBound(y2, 8);
1.112 + expected_opt=1;
1.113 + solveAndCheck(mip, MipSolver::OPTIMAL, expected_opt);
1.114 +
1.115 +}
1.116 +
1.117 +
1.118 +template<class MIP>
1.119 +void cloneTest()
1.120 +{
1.121 +
1.122 + MIP* mip = new MIP();
1.123 + MIP* mipnew = mip->newSolver();
1.124 + MIP* mipclone = mip->cloneSolver();
1.125 + delete mip;
1.126 + delete mipnew;
1.127 + delete mipclone;
1.128 +}
1.129 +
1.130 +int main()
1.131 +{
1.132 +
1.133 +#ifdef LEMON_HAVE_GLPK
1.134 + {
1.135 + GlpkMip mip1;
1.136 + aTest(mip1);
1.137 + cloneTest<GlpkMip>();
1.138 + }
1.139 +#endif
1.140 +
1.141 +#ifdef LEMON_HAVE_CPLEX
1.142 + try {
1.143 + CplexMip mip2;
1.144 + aTest(mip2);
1.145 + cloneTest<CplexMip>();
1.146 + } catch (CplexEnv::LicenseError& error) {
1.147 + check(false, error.what());
1.148 + }
1.149 +#endif
1.150 +
1.151 +#ifdef LEMON_HAVE_CBC
1.152 + {
1.153 + CbcMip mip1;
1.154 + aTest(mip1);
1.155 + cloneTest<CbcMip>();
1.156 + }
1.157 +#endif
1.158 +
1.159 + return 0;
1.160 +
1.161 +}