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