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