1 | #include "test_tools.h" |
---|
2 | |
---|
3 | |
---|
4 | #ifdef HAVE_CONFIG_H |
---|
5 | #include <config.h> |
---|
6 | #endif |
---|
7 | |
---|
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 |
---|
15 | |
---|
16 | |
---|
17 | using namespace lemon; |
---|
18 | |
---|
19 | void solveAndCheck(MipSolverBase& lp, MipSolverBase::SolutionStatus stat, |
---|
20 | double exp_opt) { |
---|
21 | using std::string; |
---|
22 | |
---|
23 | lp.solve(); |
---|
24 | //int decimal,sign; |
---|
25 | std::ostringstream buf; |
---|
26 | buf << "Primalstatus should be: " << int(stat)<<" and it is "<<int(lp.primalStatus()); |
---|
27 | |
---|
28 | |
---|
29 | // itoa(stat,buf1, 10); |
---|
30 | check(lp.mipStatus()==stat, buf.str()); |
---|
31 | |
---|
32 | if (stat == MipSolverBase::OPTIMAL) { |
---|
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 | |
---|
40 | void aTest(MipSolverBase& mip) |
---|
41 | { |
---|
42 | //The following example is very simple |
---|
43 | |
---|
44 | |
---|
45 | typedef MipSolverBase::Row Row; |
---|
46 | typedef MipSolverBase::Col Col; |
---|
47 | |
---|
48 | |
---|
49 | |
---|
50 | Col x1 = mip.addCol(); |
---|
51 | Col x2 = mip.addCol(); |
---|
52 | |
---|
53 | |
---|
54 | //Objective function |
---|
55 | mip.setObj(x1); |
---|
56 | |
---|
57 | mip.max(); |
---|
58 | |
---|
59 | |
---|
60 | //Unconstrained optimization |
---|
61 | mip.solve(); |
---|
62 | //Check it out! |
---|
63 | |
---|
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 |
---|
74 | //over the triangle with vertices (0,0),(4/5,2/5),(0,2) |
---|
75 | double expected_opt=4.0/5.0; |
---|
76 | solveAndCheck(mip, MipSolverBase::OPTIMAL, expected_opt); |
---|
77 | |
---|
78 | //Restrict x2 to integer |
---|
79 | mip.colType(x2,MipSolverBase::LEMON_INTEGER); |
---|
80 | expected_opt=1.0/2.0; |
---|
81 | solveAndCheck(mip, MipSolverBase::OPTIMAL, expected_opt); |
---|
82 | |
---|
83 | |
---|
84 | //Restrict both to integer |
---|
85 | mip.colType(x1,MipSolverBase::LEMON_INTEGER); |
---|
86 | expected_opt=0; |
---|
87 | solveAndCheck(mip, MipSolverBase::OPTIMAL, expected_opt); |
---|
88 | |
---|
89 | |
---|
90 | |
---|
91 | } |
---|
92 | |
---|
93 | |
---|
94 | int main() |
---|
95 | { |
---|
96 | |
---|
97 | #ifdef HAVE_GLPK |
---|
98 | MipGlpk mip1; |
---|
99 | aTest(mip1); |
---|
100 | #endif |
---|
101 | |
---|
102 | #ifdef HAVE_CPLEX |
---|
103 | MipCplex mip2; |
---|
104 | aTest(mip2); |
---|
105 | #endif |
---|
106 | |
---|
107 | return 0; |
---|
108 | |
---|
109 | } |
---|