| 1 | /* -*- mode: C++; indent-tabs-mode: nil; -*- |
|---|
| 2 | * |
|---|
| 3 | * This file is a part of LEMON, a generic C++ optimization library. |
|---|
| 4 | * |
|---|
| 5 | * Copyright (C) 2003-2009 |
|---|
| 6 | * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
|---|
| 7 | * (Egervary Research Group on Combinatorial Optimization, EGRES). |
|---|
| 8 | * |
|---|
| 9 | * Permission to use, modify and distribute this software is granted |
|---|
| 10 | * provided that this copyright notice appears in all copies. For |
|---|
| 11 | * precise terms see the accompanying LICENSE file. |
|---|
| 12 | * |
|---|
| 13 | * This software is provided "AS IS" with no warranty of any kind, |
|---|
| 14 | * express or implied, and with no claim as to its suitability for any |
|---|
| 15 | * purpose. |
|---|
| 16 | * |
|---|
| 17 | */ |
|---|
| 18 | |
|---|
| 19 | #include "test_tools.h" |
|---|
| 20 | |
|---|
| 21 | #include <lemon/config.h> |
|---|
| 22 | |
|---|
| 23 | #ifdef LEMON_HAVE_CPLEX |
|---|
| 24 | #include <lemon/cplex.h> |
|---|
| 25 | #endif |
|---|
| 26 | |
|---|
| 27 | #ifdef LEMON_HAVE_GLPK |
|---|
| 28 | #include <lemon/glpk.h> |
|---|
| 29 | #endif |
|---|
| 30 | |
|---|
| 31 | #ifdef LEMON_HAVE_CBC |
|---|
| 32 | #include <lemon/cbc.h> |
|---|
| 33 | #endif |
|---|
| 34 | |
|---|
| 35 | #ifdef LEMON_HAVE_MIP |
|---|
| 36 | #include <lemon/lp.h> |
|---|
| 37 | #endif |
|---|
| 38 | |
|---|
| 39 | |
|---|
| 40 | using namespace lemon; |
|---|
| 41 | |
|---|
| 42 | void solveAndCheck(MipSolver& mip, MipSolver::ProblemType stat, |
|---|
| 43 | double exp_opt) { |
|---|
| 44 | using std::string; |
|---|
| 45 | |
|---|
| 46 | mip.solve(); |
|---|
| 47 | //int decimal,sign; |
|---|
| 48 | std::ostringstream buf; |
|---|
| 49 | buf << "Type should be: " << int(stat)<<" and it is "<<int(mip.type()); |
|---|
| 50 | |
|---|
| 51 | |
|---|
| 52 | // itoa(stat,buf1, 10); |
|---|
| 53 | check(mip.type()==stat, buf.str()); |
|---|
| 54 | |
|---|
| 55 | if (stat == MipSolver::OPTIMAL) { |
|---|
| 56 | std::ostringstream sbuf; |
|---|
| 57 | sbuf << "Wrong optimal value ("<< mip.solValue() |
|---|
| 58 | <<" instead of " << exp_opt << ")"; |
|---|
| 59 | check(std::abs(mip.solValue()-exp_opt) < 1e-3, sbuf.str()); |
|---|
| 60 | //+ecvt(exp_opt,2) |
|---|
| 61 | } |
|---|
| 62 | } |
|---|
| 63 | |
|---|
| 64 | void aTest(MipSolver& mip) |
|---|
| 65 | { |
|---|
| 66 | //The following example is very simple |
|---|
| 67 | |
|---|
| 68 | |
|---|
| 69 | typedef MipSolver::Row Row; |
|---|
| 70 | typedef MipSolver::Col Col; |
|---|
| 71 | |
|---|
| 72 | |
|---|
| 73 | Col x1 = mip.addCol(); |
|---|
| 74 | Col x2 = mip.addCol(); |
|---|
| 75 | |
|---|
| 76 | |
|---|
| 77 | //Objective function |
|---|
| 78 | mip.obj(x1); |
|---|
| 79 | |
|---|
| 80 | mip.max(); |
|---|
| 81 | |
|---|
| 82 | //Unconstrained optimization |
|---|
| 83 | mip.solve(); |
|---|
| 84 | //Check it out! |
|---|
| 85 | |
|---|
| 86 | //Constraints |
|---|
| 87 | mip.addRow(2 * x1 + x2 <= 2); |
|---|
| 88 | Row y2 = mip.addRow(x1 - 2 * x2 <= 0); |
|---|
| 89 | |
|---|
| 90 | //Nonnegativity of the variable x1 |
|---|
| 91 | mip.colLowerBound(x1, 0); |
|---|
| 92 | |
|---|
| 93 | |
|---|
| 94 | //Maximization of x1 |
|---|
| 95 | //over the triangle with vertices (0,0),(4/5,2/5),(0,2) |
|---|
| 96 | double expected_opt=4.0/5.0; |
|---|
| 97 | solveAndCheck(mip, MipSolver::OPTIMAL, expected_opt); |
|---|
| 98 | |
|---|
| 99 | |
|---|
| 100 | //Restrict x2 to integer |
|---|
| 101 | mip.colType(x2,MipSolver::INTEGER); |
|---|
| 102 | expected_opt=1.0/2.0; |
|---|
| 103 | solveAndCheck(mip, MipSolver::OPTIMAL, expected_opt); |
|---|
| 104 | |
|---|
| 105 | |
|---|
| 106 | //Restrict both to integer |
|---|
| 107 | mip.colType(x1,MipSolver::INTEGER); |
|---|
| 108 | expected_opt=0; |
|---|
| 109 | solveAndCheck(mip, MipSolver::OPTIMAL, expected_opt); |
|---|
| 110 | |
|---|
| 111 | //Erase a variable |
|---|
| 112 | mip.erase(x2); |
|---|
| 113 | mip.rowUpperBound(y2, 8); |
|---|
| 114 | expected_opt=1; |
|---|
| 115 | solveAndCheck(mip, MipSolver::OPTIMAL, expected_opt); |
|---|
| 116 | |
|---|
| 117 | } |
|---|
| 118 | |
|---|
| 119 | |
|---|
| 120 | template<class MIP> |
|---|
| 121 | void cloneTest() |
|---|
| 122 | { |
|---|
| 123 | |
|---|
| 124 | MIP* mip = new MIP(); |
|---|
| 125 | MIP* mipnew = mip->newSolver(); |
|---|
| 126 | MIP* mipclone = mip->cloneSolver(); |
|---|
| 127 | delete mip; |
|---|
| 128 | delete mipnew; |
|---|
| 129 | delete mipclone; |
|---|
| 130 | } |
|---|
| 131 | |
|---|
| 132 | int main() |
|---|
| 133 | { |
|---|
| 134 | |
|---|
| 135 | #ifdef LEMON_HAVE_MIP |
|---|
| 136 | { |
|---|
| 137 | Mip mip1; |
|---|
| 138 | aTest(mip1); |
|---|
| 139 | cloneTest<Mip>(); |
|---|
| 140 | } |
|---|
| 141 | #endif |
|---|
| 142 | |
|---|
| 143 | #ifdef LEMON_HAVE_GLPK |
|---|
| 144 | { |
|---|
| 145 | GlpkMip mip1; |
|---|
| 146 | aTest(mip1); |
|---|
| 147 | cloneTest<GlpkMip>(); |
|---|
| 148 | } |
|---|
| 149 | #endif |
|---|
| 150 | |
|---|
| 151 | #ifdef LEMON_HAVE_CPLEX |
|---|
| 152 | try { |
|---|
| 153 | CplexMip mip2; |
|---|
| 154 | aTest(mip2); |
|---|
| 155 | cloneTest<CplexMip>(); |
|---|
| 156 | } catch (CplexEnv::LicenseError& error) { |
|---|
| 157 | check(false, error.what()); |
|---|
| 158 | } |
|---|
| 159 | #endif |
|---|
| 160 | |
|---|
| 161 | #ifdef LEMON_HAVE_CBC |
|---|
| 162 | { |
|---|
| 163 | CbcMip mip1; |
|---|
| 164 | aTest(mip1); |
|---|
| 165 | cloneTest<CbcMip>(); |
|---|
| 166 | } |
|---|
| 167 | #endif |
|---|
| 168 | |
|---|
| 169 | return 0; |
|---|
| 170 | |
|---|
| 171 | } |
|---|