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