| [2149] | 1 | #include "test_tools.h" |
|---|
| [2146] | 2 | |
|---|
| [2218] | 3 | |
|---|
| [2221] | 4 | #ifdef HAVE_CONFIG_H |
|---|
| 5 | #include <config.h> |
|---|
| 6 | #endif |
|---|
| 7 | |
|---|
| [2227] | 8 | #ifdef HAVE_CPLEX |
|---|
| 9 | #include <lemon/mip_cplex.h> |
|---|
| 10 | #endif |
|---|
| 11 | |
|---|
| 12 | #ifdef HAVE_GLPK |
|---|
| 13 | #include <lemon/mip_glpk.h> |
|---|
| 14 | #endif |
|---|
| [2221] | 15 | |
|---|
| [2218] | 16 | |
|---|
| [2146] | 17 | using namespace lemon; |
|---|
| 18 | |
|---|
| [2218] | 19 | void solveAndCheck(MipSolverBase& lp, MipSolverBase::SolutionStatus stat, |
|---|
| [2149] | 20 | double exp_opt) { |
|---|
| 21 | using std::string; |
|---|
| [2218] | 22 | |
|---|
| [2149] | 23 | lp.solve(); |
|---|
| 24 | //int decimal,sign; |
|---|
| 25 | std::ostringstream buf; |
|---|
| 26 | buf << "Primalstatus should be: " << int(stat)<<" and it is "<<int(lp.primalStatus()); |
|---|
| [2146] | 27 | |
|---|
| [2218] | 28 | |
|---|
| [2149] | 29 | // itoa(stat,buf1, 10); |
|---|
| [2213] | 30 | check(lp.mipStatus()==stat, buf.str()); |
|---|
| [2149] | 31 | |
|---|
| [2218] | 32 | if (stat == MipSolverBase::OPTIMAL) { |
|---|
| [2149] | 33 | std::ostringstream buf; |
|---|
| 34 | buf << "Wrong optimal value: the right optimum is " << exp_opt; |
|---|
| 35 | check(std::abs(lp.primalValue()-exp_opt) < 1e-3, buf.str()); |
|---|
| 36 | //+ecvt(exp_opt,2) |
|---|
| 37 | } |
|---|
| 38 | } |
|---|
| 39 | |
|---|
| [2218] | 40 | void aTest(MipSolverBase& mip) |
|---|
| [2149] | 41 | { |
|---|
| 42 | //The following example is very simple |
|---|
| 43 | |
|---|
| [2218] | 44 | |
|---|
| 45 | typedef MipSolverBase::Row Row; |
|---|
| 46 | typedef MipSolverBase::Col Col; |
|---|
| 47 | |
|---|
| [2149] | 48 | |
|---|
| 49 | |
|---|
| 50 | Col x1 = mip.addCol(); |
|---|
| 51 | Col x2 = mip.addCol(); |
|---|
| 52 | |
|---|
| 53 | |
|---|
| [2213] | 54 | //Objective function |
|---|
| 55 | mip.setObj(x1); |
|---|
| 56 | |
|---|
| 57 | mip.max(); |
|---|
| 58 | |
|---|
| 59 | |
|---|
| 60 | //Unconstrained optimization |
|---|
| 61 | mip.solve(); |
|---|
| 62 | //Check it out! |
|---|
| 63 | |
|---|
| [2149] | 64 | //Constraints |
|---|
| 65 | mip.addRow(2*x1+x2 <=2); |
|---|
| 66 | mip.addRow(x1-2*x2 <=0); |
|---|
| 67 | |
|---|
| 68 | //Nonnegativity of the variable x1 |
|---|
| 69 | mip.colLowerBound(x1, 0); |
|---|
| 70 | |
|---|
| 71 | |
|---|
| 72 | |
|---|
| 73 | //Maximization of x1 |
|---|
| [2221] | 74 | //over the triangle with vertices (0,0),(4/5,2/5),(0,2) |
|---|
| [2149] | 75 | double expected_opt=4.0/5.0; |
|---|
| [2218] | 76 | solveAndCheck(mip, MipSolverBase::OPTIMAL, expected_opt); |
|---|
| [2149] | 77 | |
|---|
| 78 | //Restrict x2 to integer |
|---|
| [2267] | 79 | mip.colType(x2,MipSolverBase::INT); |
|---|
| [2149] | 80 | expected_opt=1.0/2.0; |
|---|
| [2218] | 81 | solveAndCheck(mip, MipSolverBase::OPTIMAL, expected_opt); |
|---|
| [2149] | 82 | |
|---|
| 83 | |
|---|
| 84 | //Restrict both to integer |
|---|
| [2267] | 85 | mip.colType(x1,MipSolverBase::INT); |
|---|
| [2149] | 86 | expected_opt=0; |
|---|
| [2218] | 87 | solveAndCheck(mip, MipSolverBase::OPTIMAL, expected_opt); |
|---|
| [2149] | 88 | |
|---|
| 89 | |
|---|
| 90 | |
|---|
| 91 | } |
|---|
| 92 | |
|---|
| 93 | |
|---|
| 94 | int main() |
|---|
| 95 | { |
|---|
| [2146] | 96 | |
|---|
| [2147] | 97 | #ifdef HAVE_GLPK |
|---|
| [2149] | 98 | MipGlpk mip1; |
|---|
| 99 | aTest(mip1); |
|---|
| [2147] | 100 | #endif |
|---|
| [2146] | 101 | |
|---|
| [2218] | 102 | #ifdef HAVE_CPLEX |
|---|
| 103 | MipCplex mip2; |
|---|
| 104 | aTest(mip2); |
|---|
| 105 | #endif |
|---|
| 106 | |
|---|
| [2147] | 107 | return 0; |
|---|
| [2146] | 108 | |
|---|
| 109 | } |
|---|