deba@458
|
1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*-
|
deba@458
|
2 |
*
|
deba@458
|
3 |
* This file is a part of LEMON, a generic C++ optimization library.
|
deba@458
|
4 |
*
|
deba@551
|
5 |
* Copyright (C) 2003-2009
|
deba@458
|
6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
|
deba@458
|
7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES).
|
deba@458
|
8 |
*
|
deba@458
|
9 |
* Permission to use, modify and distribute this software is granted
|
deba@458
|
10 |
* provided that this copyright notice appears in all copies. For
|
deba@458
|
11 |
* precise terms see the accompanying LICENSE file.
|
deba@458
|
12 |
*
|
deba@458
|
13 |
* This software is provided "AS IS" with no warranty of any kind,
|
deba@458
|
14 |
* express or implied, and with no claim as to its suitability for any
|
deba@458
|
15 |
* purpose.
|
deba@458
|
16 |
*
|
deba@458
|
17 |
*/
|
deba@458
|
18 |
|
deba@458
|
19 |
#include "test_tools.h"
|
deba@458
|
20 |
|
deba@458
|
21 |
#include <lemon/config.h>
|
deba@458
|
22 |
|
ladanyi@627
|
23 |
#ifdef LEMON_HAVE_CPLEX
|
alpar@461
|
24 |
#include <lemon/cplex.h>
|
deba@458
|
25 |
#endif
|
deba@458
|
26 |
|
ladanyi@627
|
27 |
#ifdef LEMON_HAVE_GLPK
|
alpar@461
|
28 |
#include <lemon/glpk.h>
|
deba@458
|
29 |
#endif
|
deba@458
|
30 |
|
ladanyi@627
|
31 |
#ifdef LEMON_HAVE_CBC
|
deba@567
|
32 |
#include <lemon/cbc.h>
|
deba@567
|
33 |
#endif
|
deba@567
|
34 |
|
alpar@1113
|
35 |
#ifdef LEMON_HAVE_MIP
|
alpar@1113
|
36 |
#include <lemon/lp.h>
|
alpar@1113
|
37 |
#endif
|
alpar@1113
|
38 |
|
deba@458
|
39 |
|
deba@458
|
40 |
using namespace lemon;
|
deba@458
|
41 |
|
deba@459
|
42 |
void solveAndCheck(MipSolver& mip, MipSolver::ProblemType stat,
|
deba@458
|
43 |
double exp_opt) {
|
deba@458
|
44 |
using std::string;
|
deba@458
|
45 |
|
deba@459
|
46 |
mip.solve();
|
deba@458
|
47 |
//int decimal,sign;
|
deba@458
|
48 |
std::ostringstream buf;
|
deba@459
|
49 |
buf << "Type should be: " << int(stat)<<" and it is "<<int(mip.type());
|
deba@458
|
50 |
|
deba@458
|
51 |
|
deba@458
|
52 |
// itoa(stat,buf1, 10);
|
deba@459
|
53 |
check(mip.type()==stat, buf.str());
|
deba@458
|
54 |
|
deba@459
|
55 |
if (stat == MipSolver::OPTIMAL) {
|
deba@458
|
56 |
std::ostringstream sbuf;
|
alpar@748
|
57 |
sbuf << "Wrong optimal value ("<< mip.solValue()
|
alpar@748
|
58 |
<<" instead of " << exp_opt << ")";
|
deba@459
|
59 |
check(std::abs(mip.solValue()-exp_opt) < 1e-3, sbuf.str());
|
deba@458
|
60 |
//+ecvt(exp_opt,2)
|
deba@458
|
61 |
}
|
deba@458
|
62 |
}
|
deba@458
|
63 |
|
deba@459
|
64 |
void aTest(MipSolver& mip)
|
deba@458
|
65 |
{
|
deba@567
|
66 |
//The following example is very simple
|
deba@458
|
67 |
|
deba@458
|
68 |
|
deba@459
|
69 |
typedef MipSolver::Row Row;
|
deba@459
|
70 |
typedef MipSolver::Col Col;
|
deba@458
|
71 |
|
deba@458
|
72 |
|
deba@458
|
73 |
Col x1 = mip.addCol();
|
deba@458
|
74 |
Col x2 = mip.addCol();
|
deba@458
|
75 |
|
deba@458
|
76 |
|
deba@458
|
77 |
//Objective function
|
deba@458
|
78 |
mip.obj(x1);
|
deba@458
|
79 |
|
deba@458
|
80 |
mip.max();
|
deba@458
|
81 |
|
deba@458
|
82 |
//Unconstrained optimization
|
deba@458
|
83 |
mip.solve();
|
deba@458
|
84 |
//Check it out!
|
deba@458
|
85 |
|
deba@458
|
86 |
//Constraints
|
deba@567
|
87 |
mip.addRow(2 * x1 + x2 <= 2);
|
deba@567
|
88 |
Row y2 = mip.addRow(x1 - 2 * x2 <= 0);
|
deba@458
|
89 |
|
deba@458
|
90 |
//Nonnegativity of the variable x1
|
deba@458
|
91 |
mip.colLowerBound(x1, 0);
|
deba@458
|
92 |
|
deba@567
|
93 |
|
deba@458
|
94 |
//Maximization of x1
|
deba@458
|
95 |
//over the triangle with vertices (0,0),(4/5,2/5),(0,2)
|
deba@458
|
96 |
double expected_opt=4.0/5.0;
|
deba@459
|
97 |
solveAndCheck(mip, MipSolver::OPTIMAL, expected_opt);
|
deba@458
|
98 |
|
deba@567
|
99 |
|
deba@458
|
100 |
//Restrict x2 to integer
|
deba@459
|
101 |
mip.colType(x2,MipSolver::INTEGER);
|
deba@458
|
102 |
expected_opt=1.0/2.0;
|
deba@459
|
103 |
solveAndCheck(mip, MipSolver::OPTIMAL, expected_opt);
|
deba@458
|
104 |
|
deba@458
|
105 |
|
deba@458
|
106 |
//Restrict both to integer
|
deba@459
|
107 |
mip.colType(x1,MipSolver::INTEGER);
|
deba@458
|
108 |
expected_opt=0;
|
deba@459
|
109 |
solveAndCheck(mip, MipSolver::OPTIMAL, expected_opt);
|
deba@458
|
110 |
|
deba@567
|
111 |
//Erase a variable
|
deba@567
|
112 |
mip.erase(x2);
|
deba@567
|
113 |
mip.rowUpperBound(y2, 8);
|
deba@567
|
114 |
expected_opt=1;
|
deba@567
|
115 |
solveAndCheck(mip, MipSolver::OPTIMAL, expected_opt);
|
deba@458
|
116 |
|
deba@458
|
117 |
}
|
deba@458
|
118 |
|
deba@567
|
119 |
|
alpar@540
|
120 |
template<class MIP>
|
alpar@540
|
121 |
void cloneTest()
|
alpar@540
|
122 |
{
|
deba@551
|
123 |
|
alpar@540
|
124 |
MIP* mip = new MIP();
|
alpar@540
|
125 |
MIP* mipnew = mip->newSolver();
|
alpar@540
|
126 |
MIP* mipclone = mip->cloneSolver();
|
alpar@540
|
127 |
delete mip;
|
alpar@540
|
128 |
delete mipnew;
|
alpar@540
|
129 |
delete mipclone;
|
alpar@540
|
130 |
}
|
deba@458
|
131 |
|
deba@458
|
132 |
int main()
|
deba@458
|
133 |
{
|
deba@458
|
134 |
|
alpar@1113
|
135 |
#ifdef LEMON_HAVE_MIP
|
alpar@1113
|
136 |
{
|
alpar@1113
|
137 |
Mip mip1;
|
alpar@1113
|
138 |
aTest(mip1);
|
alpar@1113
|
139 |
cloneTest<Mip>();
|
alpar@1113
|
140 |
}
|
alpar@1113
|
141 |
#endif
|
alpar@1113
|
142 |
|
ladanyi@627
|
143 |
#ifdef LEMON_HAVE_GLPK
|
deba@459
|
144 |
{
|
alpar@462
|
145 |
GlpkMip mip1;
|
deba@459
|
146 |
aTest(mip1);
|
alpar@540
|
147 |
cloneTest<GlpkMip>();
|
deba@459
|
148 |
}
|
deba@458
|
149 |
#endif
|
deba@458
|
150 |
|
ladanyi@627
|
151 |
#ifdef LEMON_HAVE_CPLEX
|
deba@459
|
152 |
try {
|
alpar@462
|
153 |
CplexMip mip2;
|
deba@459
|
154 |
aTest(mip2);
|
deba@551
|
155 |
cloneTest<CplexMip>();
|
deba@459
|
156 |
} catch (CplexEnv::LicenseError& error) {
|
deba@459
|
157 |
check(false, error.what());
|
deba@459
|
158 |
}
|
deba@458
|
159 |
#endif
|
deba@458
|
160 |
|
ladanyi@627
|
161 |
#ifdef LEMON_HAVE_CBC
|
deba@567
|
162 |
{
|
deba@567
|
163 |
CbcMip mip1;
|
deba@567
|
164 |
aTest(mip1);
|
deba@567
|
165 |
cloneTest<CbcMip>();
|
deba@567
|
166 |
}
|
deba@567
|
167 |
#endif
|
deba@567
|
168 |
|
deba@458
|
169 |
return 0;
|
deba@458
|
170 |
|
deba@458
|
171 |
}
|