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