[458] | 1 | /* -*- mode: C++; indent-tabs-mode: nil; -*- |
---|
| 2 | * |
---|
| 3 | * This file is a part of LEMON, a generic C++ optimization library. |
---|
| 4 | * |
---|
[540] | 5 | * Copyright (C) 2003-2009 |
---|
[458] | 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 | #ifdef HAVE_CONFIG_H |
---|
| 22 | #include <lemon/config.h> |
---|
| 23 | #endif |
---|
| 24 | |
---|
[622] | 25 | #ifdef LEMON_HAVE_CPLEX |
---|
[461] | 26 | #include <lemon/cplex.h> |
---|
[458] | 27 | #endif |
---|
| 28 | |
---|
[622] | 29 | #ifdef LEMON_HAVE_GLPK |
---|
[461] | 30 | #include <lemon/glpk.h> |
---|
[458] | 31 | #endif |
---|
| 32 | |
---|
[622] | 33 | #ifdef LEMON_HAVE_CBC |
---|
[559] | 34 | #include <lemon/cbc.h> |
---|
| 35 | #endif |
---|
| 36 | |
---|
[458] | 37 | |
---|
| 38 | using namespace lemon; |
---|
| 39 | |
---|
[459] | 40 | void solveAndCheck(MipSolver& mip, MipSolver::ProblemType stat, |
---|
[458] | 41 | double exp_opt) { |
---|
| 42 | using std::string; |
---|
| 43 | |
---|
[459] | 44 | mip.solve(); |
---|
[458] | 45 | //int decimal,sign; |
---|
| 46 | std::ostringstream buf; |
---|
[459] | 47 | buf << "Type should be: " << int(stat)<<" and it is "<<int(mip.type()); |
---|
[458] | 48 | |
---|
| 49 | |
---|
| 50 | // itoa(stat,buf1, 10); |
---|
[459] | 51 | check(mip.type()==stat, buf.str()); |
---|
[458] | 52 | |
---|
[459] | 53 | if (stat == MipSolver::OPTIMAL) { |
---|
[458] | 54 | std::ostringstream sbuf; |
---|
| 55 | buf << "Wrong optimal value: the right optimum is " << exp_opt; |
---|
[459] | 56 | check(std::abs(mip.solValue()-exp_opt) < 1e-3, sbuf.str()); |
---|
[458] | 57 | //+ecvt(exp_opt,2) |
---|
| 58 | } |
---|
| 59 | } |
---|
| 60 | |
---|
[459] | 61 | void aTest(MipSolver& mip) |
---|
[458] | 62 | { |
---|
[559] | 63 | //The following example is very simple |
---|
[458] | 64 | |
---|
| 65 | |
---|
[459] | 66 | typedef MipSolver::Row Row; |
---|
| 67 | typedef MipSolver::Col Col; |
---|
[458] | 68 | |
---|
| 69 | |
---|
| 70 | Col x1 = mip.addCol(); |
---|
| 71 | Col x2 = mip.addCol(); |
---|
| 72 | |
---|
| 73 | |
---|
| 74 | //Objective function |
---|
| 75 | mip.obj(x1); |
---|
| 76 | |
---|
| 77 | mip.max(); |
---|
| 78 | |
---|
| 79 | //Unconstrained optimization |
---|
| 80 | mip.solve(); |
---|
| 81 | //Check it out! |
---|
| 82 | |
---|
| 83 | //Constraints |
---|
[559] | 84 | mip.addRow(2 * x1 + x2 <= 2); |
---|
| 85 | Row y2 = mip.addRow(x1 - 2 * x2 <= 0); |
---|
[458] | 86 | |
---|
| 87 | //Nonnegativity of the variable x1 |
---|
| 88 | mip.colLowerBound(x1, 0); |
---|
| 89 | |
---|
[559] | 90 | |
---|
[458] | 91 | //Maximization of x1 |
---|
| 92 | //over the triangle with vertices (0,0),(4/5,2/5),(0,2) |
---|
| 93 | double expected_opt=4.0/5.0; |
---|
[459] | 94 | solveAndCheck(mip, MipSolver::OPTIMAL, expected_opt); |
---|
[458] | 95 | |
---|
[559] | 96 | |
---|
[458] | 97 | //Restrict x2 to integer |
---|
[459] | 98 | mip.colType(x2,MipSolver::INTEGER); |
---|
[458] | 99 | expected_opt=1.0/2.0; |
---|
[459] | 100 | solveAndCheck(mip, MipSolver::OPTIMAL, expected_opt); |
---|
[458] | 101 | |
---|
| 102 | |
---|
| 103 | //Restrict both to integer |
---|
[459] | 104 | mip.colType(x1,MipSolver::INTEGER); |
---|
[458] | 105 | expected_opt=0; |
---|
[459] | 106 | solveAndCheck(mip, MipSolver::OPTIMAL, expected_opt); |
---|
[458] | 107 | |
---|
[559] | 108 | //Erase a variable |
---|
| 109 | mip.erase(x2); |
---|
| 110 | mip.rowUpperBound(y2, 8); |
---|
| 111 | expected_opt=1; |
---|
| 112 | solveAndCheck(mip, MipSolver::OPTIMAL, expected_opt); |
---|
[458] | 113 | |
---|
| 114 | } |
---|
| 115 | |
---|
[559] | 116 | |
---|
[528] | 117 | template<class MIP> |
---|
| 118 | void cloneTest() |
---|
| 119 | { |
---|
[540] | 120 | |
---|
[528] | 121 | MIP* mip = new MIP(); |
---|
| 122 | MIP* mipnew = mip->newSolver(); |
---|
| 123 | MIP* mipclone = mip->cloneSolver(); |
---|
| 124 | delete mip; |
---|
| 125 | delete mipnew; |
---|
| 126 | delete mipclone; |
---|
| 127 | } |
---|
[458] | 128 | |
---|
| 129 | int main() |
---|
| 130 | { |
---|
| 131 | |
---|
[622] | 132 | #ifdef LEMON_HAVE_GLPK |
---|
[459] | 133 | { |
---|
[462] | 134 | GlpkMip mip1; |
---|
[459] | 135 | aTest(mip1); |
---|
[528] | 136 | cloneTest<GlpkMip>(); |
---|
[459] | 137 | } |
---|
[458] | 138 | #endif |
---|
| 139 | |
---|
[622] | 140 | #ifdef LEMON_HAVE_CPLEX |
---|
[459] | 141 | try { |
---|
[462] | 142 | CplexMip mip2; |
---|
[459] | 143 | aTest(mip2); |
---|
[540] | 144 | cloneTest<CplexMip>(); |
---|
[459] | 145 | } catch (CplexEnv::LicenseError& error) { |
---|
| 146 | check(false, error.what()); |
---|
| 147 | } |
---|
[458] | 148 | #endif |
---|
| 149 | |
---|
[622] | 150 | #ifdef LEMON_HAVE_CBC |
---|
[559] | 151 | { |
---|
| 152 | CbcMip mip1; |
---|
| 153 | aTest(mip1); |
---|
| 154 | cloneTest<CbcMip>(); |
---|
| 155 | } |
---|
| 156 | #endif |
---|
| 157 | |
---|
[458] | 158 | return 0; |
---|
| 159 | |
---|
| 160 | } |
---|