deba@481
|
1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*-
|
deba@481
|
2 |
*
|
deba@481
|
3 |
* This file is a part of LEMON, a generic C++ optimization library.
|
deba@481
|
4 |
*
|
alpar@1270
|
5 |
* Copyright (C) 2003-2013
|
deba@481
|
6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
|
deba@481
|
7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES).
|
deba@481
|
8 |
*
|
deba@481
|
9 |
* Permission to use, modify and distribute this software is granted
|
deba@481
|
10 |
* provided that this copyright notice appears in all copies. For
|
deba@481
|
11 |
* precise terms see the accompanying LICENSE file.
|
deba@481
|
12 |
*
|
deba@481
|
13 |
* This software is provided "AS IS" with no warranty of any kind,
|
deba@481
|
14 |
* express or implied, and with no claim as to its suitability for any
|
deba@481
|
15 |
* purpose.
|
deba@481
|
16 |
*
|
deba@481
|
17 |
*/
|
deba@481
|
18 |
|
deba@481
|
19 |
#include <sstream>
|
deba@481
|
20 |
#include <lemon/lp_skeleton.h>
|
deba@481
|
21 |
#include "test_tools.h"
|
deba@481
|
22 |
#include <lemon/tolerance.h>
|
alpar@1337
|
23 |
#include <lemon/concept_check.h>
|
deba@481
|
24 |
#include <lemon/config.h>
|
deba@481
|
25 |
|
ladanyi@674
|
26 |
#ifdef LEMON_HAVE_GLPK
|
alpar@484
|
27 |
#include <lemon/glpk.h>
|
deba@481
|
28 |
#endif
|
deba@481
|
29 |
|
ladanyi@674
|
30 |
#ifdef LEMON_HAVE_CPLEX
|
alpar@484
|
31 |
#include <lemon/cplex.h>
|
deba@481
|
32 |
#endif
|
deba@481
|
33 |
|
ladanyi@674
|
34 |
#ifdef LEMON_HAVE_SOPLEX
|
alpar@484
|
35 |
#include <lemon/soplex.h>
|
deba@481
|
36 |
#endif
|
deba@481
|
37 |
|
ladanyi@674
|
38 |
#ifdef LEMON_HAVE_CLP
|
alpar@484
|
39 |
#include <lemon/clp.h>
|
deba@482
|
40 |
#endif
|
deba@482
|
41 |
|
alpar@1300
|
42 |
#ifdef LEMON_HAVE_LP
|
alpar@1300
|
43 |
#include <lemon/lp.h>
|
alpar@1300
|
44 |
#endif
|
deba@481
|
45 |
using namespace lemon;
|
deba@481
|
46 |
|
alpar@1140
|
47 |
int countCols(LpBase & lp) {
|
alpar@1140
|
48 |
int count=0;
|
alpar@1140
|
49 |
for (LpBase::ColIt c(lp); c!=INVALID; ++c) ++count;
|
alpar@1337
|
50 |
#ifdef LEMON_CXX11
|
alpar@1337
|
51 |
int cnt = 0;
|
alpar@1337
|
52 |
for(auto c: lp.cols()) { cnt++; ::lemon::ignore_unused_variable_warning(c); }
|
alpar@1337
|
53 |
check(count == cnt, "Wrong STL iterator");
|
alpar@1337
|
54 |
#endif
|
alpar@1140
|
55 |
return count;
|
alpar@1140
|
56 |
}
|
alpar@1140
|
57 |
|
alpar@1140
|
58 |
int countRows(LpBase & lp) {
|
alpar@1140
|
59 |
int count=0;
|
alpar@1140
|
60 |
for (LpBase::RowIt r(lp); r!=INVALID; ++r) ++count;
|
alpar@1337
|
61 |
#ifdef LEMON_CXX11
|
alpar@1337
|
62 |
int cnt = 0;
|
alpar@1337
|
63 |
for(auto r: lp.rows()) { cnt++; ::lemon::ignore_unused_variable_warning(r); }
|
alpar@1337
|
64 |
check(count == cnt, "Wrong STL iterator");
|
alpar@1337
|
65 |
#endif
|
alpar@1140
|
66 |
return count;
|
alpar@1140
|
67 |
}
|
alpar@1140
|
68 |
|
alpar@1140
|
69 |
|
deba@482
|
70 |
void lpTest(LpSolver& lp)
|
deba@481
|
71 |
{
|
deba@481
|
72 |
|
deba@482
|
73 |
typedef LpSolver LP;
|
deba@481
|
74 |
|
alpar@1140
|
75 |
// Test LpBase::clear()
|
alpar@1140
|
76 |
check(countRows(lp)==0, "Wrong number of rows");
|
alpar@1140
|
77 |
check(countCols(lp)==0, "Wrong number of cols");
|
alpar@1140
|
78 |
lp.addCol(); lp.addRow(); lp.addRow();
|
alpar@1140
|
79 |
check(countRows(lp)==2, "Wrong number of rows");
|
alpar@1140
|
80 |
check(countCols(lp)==1, "Wrong number of cols");
|
alpar@1140
|
81 |
lp.clear();
|
alpar@1140
|
82 |
check(countRows(lp)==0, "Wrong number of rows");
|
alpar@1140
|
83 |
check(countCols(lp)==0, "Wrong number of cols");
|
alpar@1140
|
84 |
lp.addCol(); lp.addCol(); lp.addCol(); lp.addRow();
|
alpar@1140
|
85 |
check(countRows(lp)==1, "Wrong number of rows");
|
alpar@1140
|
86 |
check(countCols(lp)==3, "Wrong number of cols");
|
alpar@1140
|
87 |
lp.clear();
|
alpar@1140
|
88 |
|
deba@481
|
89 |
std::vector<LP::Col> x(10);
|
deba@481
|
90 |
// for(int i=0;i<10;i++) x.push_back(lp.addCol());
|
deba@481
|
91 |
lp.addColSet(x);
|
deba@481
|
92 |
lp.colLowerBound(x,1);
|
deba@481
|
93 |
lp.colUpperBound(x,1);
|
deba@481
|
94 |
lp.colBounds(x,1,2);
|
deba@481
|
95 |
|
deba@481
|
96 |
std::vector<LP::Col> y(10);
|
deba@481
|
97 |
lp.addColSet(y);
|
deba@481
|
98 |
|
deba@481
|
99 |
lp.colLowerBound(y,1);
|
deba@481
|
100 |
lp.colUpperBound(y,1);
|
deba@481
|
101 |
lp.colBounds(y,1,2);
|
deba@481
|
102 |
|
deba@481
|
103 |
std::map<int,LP::Col> z;
|
deba@481
|
104 |
|
deba@481
|
105 |
z.insert(std::make_pair(12,INVALID));
|
deba@481
|
106 |
z.insert(std::make_pair(2,INVALID));
|
deba@481
|
107 |
z.insert(std::make_pair(7,INVALID));
|
deba@481
|
108 |
z.insert(std::make_pair(5,INVALID));
|
deba@481
|
109 |
|
deba@481
|
110 |
lp.addColSet(z);
|
deba@481
|
111 |
|
deba@481
|
112 |
lp.colLowerBound(z,1);
|
deba@481
|
113 |
lp.colUpperBound(z,1);
|
deba@481
|
114 |
lp.colBounds(z,1,2);
|
deba@481
|
115 |
|
deba@481
|
116 |
{
|
deba@481
|
117 |
LP::Expr e,f,g;
|
deba@481
|
118 |
LP::Col p1,p2,p3,p4,p5;
|
deba@481
|
119 |
LP::Constr c;
|
deba@481
|
120 |
|
deba@481
|
121 |
p1=lp.addCol();
|
deba@481
|
122 |
p2=lp.addCol();
|
deba@481
|
123 |
p3=lp.addCol();
|
deba@481
|
124 |
p4=lp.addCol();
|
deba@481
|
125 |
p5=lp.addCol();
|
deba@481
|
126 |
|
deba@481
|
127 |
e[p1]=2;
|
deba@482
|
128 |
*e=12;
|
deba@481
|
129 |
e[p1]+=2;
|
deba@482
|
130 |
*e+=12;
|
deba@481
|
131 |
e[p1]-=2;
|
deba@482
|
132 |
*e-=12;
|
deba@481
|
133 |
|
deba@481
|
134 |
e=2;
|
deba@481
|
135 |
e=2.2;
|
deba@481
|
136 |
e=p1;
|
deba@481
|
137 |
e=f;
|
deba@481
|
138 |
|
deba@481
|
139 |
e+=2;
|
deba@481
|
140 |
e+=2.2;
|
deba@481
|
141 |
e+=p1;
|
deba@481
|
142 |
e+=f;
|
deba@481
|
143 |
|
deba@481
|
144 |
e-=2;
|
deba@481
|
145 |
e-=2.2;
|
deba@481
|
146 |
e-=p1;
|
deba@481
|
147 |
e-=f;
|
deba@481
|
148 |
|
deba@481
|
149 |
e*=2;
|
deba@481
|
150 |
e*=2.2;
|
deba@481
|
151 |
e/=2;
|
deba@481
|
152 |
e/=2.2;
|
deba@481
|
153 |
|
deba@481
|
154 |
e=((p1+p2)+(p1-p2)+(p1+12)+(12+p1)+(p1-12)+(12-p1)+
|
deba@481
|
155 |
(f+12)+(12+f)+(p1+f)+(f+p1)+(f+g)+
|
deba@481
|
156 |
(f-12)+(12-f)+(p1-f)+(f-p1)+(f-g)+
|
deba@481
|
157 |
2.2*f+f*2.2+f/2.2+
|
deba@481
|
158 |
2*f+f*2+f/2+
|
deba@481
|
159 |
2.2*p1+p1*2.2+p1/2.2+
|
deba@481
|
160 |
2*p1+p1*2+p1/2
|
deba@481
|
161 |
);
|
deba@481
|
162 |
|
deba@481
|
163 |
|
deba@481
|
164 |
c = (e <= f );
|
deba@481
|
165 |
c = (e <= 2.2);
|
deba@481
|
166 |
c = (e <= 2 );
|
deba@481
|
167 |
c = (e <= p1 );
|
deba@481
|
168 |
c = (2.2<= f );
|
deba@481
|
169 |
c = (2 <= f );
|
deba@481
|
170 |
c = (p1 <= f );
|
deba@481
|
171 |
c = (p1 <= p2 );
|
deba@481
|
172 |
c = (p1 <= 2.2);
|
deba@481
|
173 |
c = (p1 <= 2 );
|
deba@481
|
174 |
c = (2.2<= p2 );
|
deba@481
|
175 |
c = (2 <= p2 );
|
deba@481
|
176 |
|
deba@481
|
177 |
c = (e >= f );
|
deba@481
|
178 |
c = (e >= 2.2);
|
deba@481
|
179 |
c = (e >= 2 );
|
deba@481
|
180 |
c = (e >= p1 );
|
deba@481
|
181 |
c = (2.2>= f );
|
deba@481
|
182 |
c = (2 >= f );
|
deba@481
|
183 |
c = (p1 >= f );
|
deba@481
|
184 |
c = (p1 >= p2 );
|
deba@481
|
185 |
c = (p1 >= 2.2);
|
deba@481
|
186 |
c = (p1 >= 2 );
|
deba@481
|
187 |
c = (2.2>= p2 );
|
deba@481
|
188 |
c = (2 >= p2 );
|
deba@481
|
189 |
|
deba@481
|
190 |
c = (e == f );
|
deba@481
|
191 |
c = (e == 2.2);
|
deba@481
|
192 |
c = (e == 2 );
|
deba@481
|
193 |
c = (e == p1 );
|
deba@481
|
194 |
c = (2.2== f );
|
deba@481
|
195 |
c = (2 == f );
|
deba@481
|
196 |
c = (p1 == f );
|
deba@481
|
197 |
//c = (p1 == p2 );
|
deba@481
|
198 |
c = (p1 == 2.2);
|
deba@481
|
199 |
c = (p1 == 2 );
|
deba@481
|
200 |
c = (2.2== p2 );
|
deba@481
|
201 |
c = (2 == p2 );
|
deba@481
|
202 |
|
alpar@483
|
203 |
c = ((2 <= e) <= 3);
|
alpar@483
|
204 |
c = ((2 <= p1) <= 3);
|
deba@481
|
205 |
|
alpar@483
|
206 |
c = ((2 >= e) >= 3);
|
alpar@483
|
207 |
c = ((2 >= p1) >= 3);
|
deba@481
|
208 |
|
retvari@1092
|
209 |
{ //Tests for #430
|
retvari@1092
|
210 |
LP::Col v=lp.addCol();
|
retvari@1092
|
211 |
LP::Constr c = v >= -3;
|
retvari@1092
|
212 |
c = c <= 4;
|
retvari@1092
|
213 |
LP::Constr c2;
|
alpar@1247
|
214 |
#if ( __GNUC__ == 4 ) && ( __GNUC_MINOR__ == 3 )
|
alpar@1247
|
215 |
c2 = ( -3 <= v ) <= 4;
|
alpar@1247
|
216 |
#else
|
retvari@1092
|
217 |
c2 = -3 <= v <= 4;
|
alpar@1247
|
218 |
#endif
|
alpar@1247
|
219 |
|
retvari@1092
|
220 |
}
|
retvari@1092
|
221 |
|
deba@481
|
222 |
e[x[3]]=2;
|
deba@481
|
223 |
e[x[3]]=4;
|
deba@481
|
224 |
e[x[3]]=1;
|
deba@482
|
225 |
*e=12;
|
deba@481
|
226 |
|
deba@482
|
227 |
lp.addRow(-LP::INF,e,23);
|
deba@482
|
228 |
lp.addRow(-LP::INF,3.0*(x[1]+x[2]/2)-x[3],23);
|
deba@482
|
229 |
lp.addRow(-LP::INF,3.0*(x[1]+x[2]*2-5*x[3]+12-x[4]/3)+2*x[4]-4,23);
|
deba@481
|
230 |
|
deba@481
|
231 |
lp.addRow(x[1]+x[3]<=x[5]-3);
|
alpar@483
|
232 |
lp.addRow((-7<=x[1]+x[3]-12)<=3);
|
deba@481
|
233 |
lp.addRow(x[1]<=x[5]);
|
deba@481
|
234 |
|
deba@481
|
235 |
std::ostringstream buf;
|
deba@481
|
236 |
|
deba@481
|
237 |
|
deba@481
|
238 |
e=((p1+p2)+(p1-0.99*p2));
|
deba@481
|
239 |
//e.prettyPrint(std::cout);
|
deba@481
|
240 |
//(e<=2).prettyPrint(std::cout);
|
deba@481
|
241 |
double tolerance=0.001;
|
deba@481
|
242 |
e.simplify(tolerance);
|
deba@481
|
243 |
buf << "Coeff. of p2 should be 0.01";
|
deba@481
|
244 |
check(e[p2]>0, buf.str());
|
deba@481
|
245 |
|
deba@481
|
246 |
tolerance=0.02;
|
deba@481
|
247 |
e.simplify(tolerance);
|
deba@481
|
248 |
buf << "Coeff. of p2 should be 0";
|
deba@482
|
249 |
check(const_cast<const LpSolver::Expr&>(e)[p2]==0, buf.str());
|
deba@481
|
250 |
|
alpar@587
|
251 |
//Test for clone/new
|
alpar@587
|
252 |
LP* lpnew = lp.newSolver();
|
alpar@587
|
253 |
LP* lpclone = lp.cloneSolver();
|
alpar@587
|
254 |
delete lpnew;
|
alpar@587
|
255 |
delete lpclone;
|
deba@481
|
256 |
|
deba@481
|
257 |
}
|
deba@481
|
258 |
|
deba@481
|
259 |
{
|
deba@481
|
260 |
LP::DualExpr e,f,g;
|
alpar@1232
|
261 |
LP::Row p1 = INVALID, p2 = INVALID;
|
deba@481
|
262 |
|
deba@481
|
263 |
e[p1]=2;
|
deba@481
|
264 |
e[p1]+=2;
|
deba@481
|
265 |
e[p1]-=2;
|
deba@481
|
266 |
|
deba@481
|
267 |
e=p1;
|
deba@481
|
268 |
e=f;
|
deba@481
|
269 |
|
deba@481
|
270 |
e+=p1;
|
deba@481
|
271 |
e+=f;
|
deba@481
|
272 |
|
deba@481
|
273 |
e-=p1;
|
deba@481
|
274 |
e-=f;
|
deba@481
|
275 |
|
deba@481
|
276 |
e*=2;
|
deba@481
|
277 |
e*=2.2;
|
deba@481
|
278 |
e/=2;
|
deba@481
|
279 |
e/=2.2;
|
deba@481
|
280 |
|
deba@481
|
281 |
e=((p1+p2)+(p1-p2)+
|
deba@481
|
282 |
(p1+f)+(f+p1)+(f+g)+
|
deba@481
|
283 |
(p1-f)+(f-p1)+(f-g)+
|
deba@481
|
284 |
2.2*f+f*2.2+f/2.2+
|
deba@481
|
285 |
2*f+f*2+f/2+
|
deba@481
|
286 |
2.2*p1+p1*2.2+p1/2.2+
|
deba@481
|
287 |
2*p1+p1*2+p1/2
|
deba@481
|
288 |
);
|
deba@481
|
289 |
}
|
deba@481
|
290 |
|
deba@481
|
291 |
}
|
deba@481
|
292 |
|
deba@482
|
293 |
void solveAndCheck(LpSolver& lp, LpSolver::ProblemType stat,
|
deba@481
|
294 |
double exp_opt) {
|
deba@481
|
295 |
using std::string;
|
deba@481
|
296 |
lp.solve();
|
deba@482
|
297 |
|
deba@481
|
298 |
std::ostringstream buf;
|
deba@482
|
299 |
buf << "PrimalType should be: " << int(stat) << int(lp.primalType());
|
deba@481
|
300 |
|
deba@482
|
301 |
check(lp.primalType()==stat, buf.str());
|
deba@481
|
302 |
|
deba@482
|
303 |
if (stat == LpSolver::OPTIMAL) {
|
deba@481
|
304 |
std::ostringstream sbuf;
|
alpar@587
|
305 |
sbuf << "Wrong optimal value (" << lp.primal() <<") with "
|
alpar@587
|
306 |
<< lp.solverName() <<"\n the right optimum is " << exp_opt;
|
deba@482
|
307 |
check(std::abs(lp.primal()-exp_opt) < 1e-3, sbuf.str());
|
deba@481
|
308 |
}
|
deba@481
|
309 |
}
|
deba@481
|
310 |
|
deba@482
|
311 |
void aTest(LpSolver & lp)
|
deba@481
|
312 |
{
|
deba@482
|
313 |
typedef LpSolver LP;
|
deba@481
|
314 |
|
deba@481
|
315 |
//The following example is very simple
|
deba@481
|
316 |
|
deba@482
|
317 |
typedef LpSolver::Row Row;
|
deba@482
|
318 |
typedef LpSolver::Col Col;
|
deba@481
|
319 |
|
deba@481
|
320 |
|
deba@481
|
321 |
Col x1 = lp.addCol();
|
deba@481
|
322 |
Col x2 = lp.addCol();
|
deba@481
|
323 |
|
deba@481
|
324 |
|
deba@481
|
325 |
//Constraints
|
deba@482
|
326 |
Row upright=lp.addRow(x1+2*x2 <=1);
|
deba@481
|
327 |
lp.addRow(x1+x2 >=-1);
|
deba@481
|
328 |
lp.addRow(x1-x2 <=1);
|
deba@481
|
329 |
lp.addRow(x1-x2 >=-1);
|
deba@481
|
330 |
//Nonnegativity of the variables
|
deba@481
|
331 |
lp.colLowerBound(x1, 0);
|
deba@481
|
332 |
lp.colLowerBound(x2, 0);
|
deba@481
|
333 |
//Objective function
|
deba@481
|
334 |
lp.obj(x1+x2);
|
deba@481
|
335 |
|
deba@482
|
336 |
lp.sense(lp.MAX);
|
deba@481
|
337 |
|
deba@481
|
338 |
//Testing the problem retrieving routines
|
deba@481
|
339 |
check(lp.objCoeff(x1)==1,"First term should be 1 in the obj function!");
|
deba@482
|
340 |
check(lp.sense() == lp.MAX,"This is a maximization!");
|
deba@481
|
341 |
check(lp.coeff(upright,x1)==1,"The coefficient in question is 1!");
|
alpar@1427
|
342 |
check(lp.coeff(upright,x2)==2,"The coefficient in question is 1!");
|
deba@482
|
343 |
check(lp.colLowerBound(x1)==0,
|
deba@482
|
344 |
"The lower bound for variable x1 should be 0.");
|
deba@482
|
345 |
check(lp.colUpperBound(x1)==LpSolver::INF,
|
deba@482
|
346 |
"The upper bound for variable x1 should be infty.");
|
deba@482
|
347 |
check(lp.rowLowerBound(upright) == -LpSolver::INF,
|
deba@482
|
348 |
"The lower bound for the first row should be -infty.");
|
deba@482
|
349 |
check(lp.rowUpperBound(upright)==1,
|
deba@482
|
350 |
"The upper bound for the first row should be 1.");
|
deba@482
|
351 |
LpSolver::Expr e = lp.row(upright);
|
deba@482
|
352 |
check(e[x1] == 1, "The first coefficient should 1.");
|
deba@482
|
353 |
check(e[x2] == 2, "The second coefficient should 1.");
|
deba@481
|
354 |
|
deba@482
|
355 |
lp.row(upright, x1+x2 <=1);
|
deba@482
|
356 |
e = lp.row(upright);
|
deba@482
|
357 |
check(e[x1] == 1, "The first coefficient should 1.");
|
deba@482
|
358 |
check(e[x2] == 1, "The second coefficient should 1.");
|
deba@482
|
359 |
|
deba@482
|
360 |
LpSolver::DualExpr de = lp.col(x1);
|
deba@481
|
361 |
check( de[upright] == 1, "The first coefficient should 1.");
|
deba@481
|
362 |
|
deba@482
|
363 |
LpSolver* clp = lp.cloneSolver();
|
deba@481
|
364 |
|
deba@481
|
365 |
//Testing the problem retrieving routines
|
deba@481
|
366 |
check(clp->objCoeff(x1)==1,"First term should be 1 in the obj function!");
|
deba@482
|
367 |
check(clp->sense() == clp->MAX,"This is a maximization!");
|
deba@481
|
368 |
check(clp->coeff(upright,x1)==1,"The coefficient in question is 1!");
|
deba@481
|
369 |
// std::cout<<lp.colLowerBound(x1)<<std::endl;
|
deba@482
|
370 |
check(clp->colLowerBound(x1)==0,
|
deba@482
|
371 |
"The lower bound for variable x1 should be 0.");
|
deba@482
|
372 |
check(clp->colUpperBound(x1)==LpSolver::INF,
|
deba@482
|
373 |
"The upper bound for variable x1 should be infty.");
|
deba@481
|
374 |
|
deba@482
|
375 |
check(lp.rowLowerBound(upright)==-LpSolver::INF,
|
deba@482
|
376 |
"The lower bound for the first row should be -infty.");
|
deba@482
|
377 |
check(lp.rowUpperBound(upright)==1,
|
deba@482
|
378 |
"The upper bound for the first row should be 1.");
|
deba@481
|
379 |
e = clp->row(upright);
|
deba@482
|
380 |
check(e[x1] == 1, "The first coefficient should 1.");
|
deba@482
|
381 |
check(e[x2] == 1, "The second coefficient should 1.");
|
deba@481
|
382 |
|
deba@481
|
383 |
de = clp->col(x1);
|
deba@482
|
384 |
check(de[upright] == 1, "The first coefficient should 1.");
|
deba@481
|
385 |
|
deba@481
|
386 |
delete clp;
|
deba@481
|
387 |
|
deba@481
|
388 |
//Maximization of x1+x2
|
deba@481
|
389 |
//over the triangle with vertices (0,0) (0,1) (1,0)
|
deba@481
|
390 |
double expected_opt=1;
|
deba@482
|
391 |
solveAndCheck(lp, LpSolver::OPTIMAL, expected_opt);
|
deba@481
|
392 |
|
deba@481
|
393 |
//Minimization
|
deba@482
|
394 |
lp.sense(lp.MIN);
|
deba@481
|
395 |
expected_opt=0;
|
deba@482
|
396 |
solveAndCheck(lp, LpSolver::OPTIMAL, expected_opt);
|
deba@481
|
397 |
|
deba@481
|
398 |
//Vertex (-1,0) instead of (0,0)
|
deba@482
|
399 |
lp.colLowerBound(x1, -LpSolver::INF);
|
deba@481
|
400 |
expected_opt=-1;
|
deba@482
|
401 |
solveAndCheck(lp, LpSolver::OPTIMAL, expected_opt);
|
deba@481
|
402 |
|
deba@481
|
403 |
//Erase one constraint and return to maximization
|
deba@482
|
404 |
lp.erase(upright);
|
deba@482
|
405 |
lp.sense(lp.MAX);
|
deba@482
|
406 |
expected_opt=LpSolver::INF;
|
deba@482
|
407 |
solveAndCheck(lp, LpSolver::UNBOUNDED, expected_opt);
|
deba@481
|
408 |
|
deba@481
|
409 |
//Infeasibilty
|
deba@481
|
410 |
lp.addRow(x1+x2 <=-2);
|
deba@482
|
411 |
solveAndCheck(lp, LpSolver::INFEASIBLE, expected_opt);
|
deba@481
|
412 |
|
deba@481
|
413 |
}
|
deba@481
|
414 |
|
alpar@587
|
415 |
template<class LP>
|
alpar@587
|
416 |
void cloneTest()
|
alpar@587
|
417 |
{
|
alpar@587
|
418 |
//Test for clone/new
|
deba@598
|
419 |
|
alpar@587
|
420 |
LP* lp = new LP();
|
alpar@587
|
421 |
LP* lpnew = lp->newSolver();
|
alpar@587
|
422 |
LP* lpclone = lp->cloneSolver();
|
alpar@587
|
423 |
delete lp;
|
alpar@587
|
424 |
delete lpnew;
|
alpar@587
|
425 |
delete lpclone;
|
alpar@587
|
426 |
}
|
alpar@587
|
427 |
|
alpar@1427
|
428 |
template<class LP>
|
alpar@1427
|
429 |
void rangeConstraintTest()
|
alpar@1427
|
430 |
{
|
alpar@1427
|
431 |
LP lp;
|
alpar@1427
|
432 |
// Add two columns (variables) to the problem
|
alpar@1427
|
433 |
typename LP::Col x1 = lp.addCol();
|
alpar@1427
|
434 |
typename LP::Col x2 = lp.addCol();
|
alpar@1427
|
435 |
// Add rows (constraints) to the problem
|
alpar@1427
|
436 |
lp.addRow(x1 - 5 <= x2);
|
alpar@1427
|
437 |
lp.addRow(0 <= 2 * x1 + x2 <= 25);
|
alpar@1427
|
438 |
|
alpar@1427
|
439 |
// Set lower and upper bounds for the columns (variables)
|
alpar@1427
|
440 |
lp.colLowerBound(x1, 0);
|
alpar@1427
|
441 |
lp.colUpperBound(x2, 10);
|
alpar@1427
|
442 |
|
alpar@1427
|
443 |
// Specify the objective function
|
alpar@1427
|
444 |
lp.max();
|
alpar@1427
|
445 |
lp.obj(5 * x1 + 3 * x2);
|
alpar@1427
|
446 |
|
alpar@1427
|
447 |
// Solve the problem using the underlying LP solver
|
alpar@1427
|
448 |
lp.solve();
|
alpar@1427
|
449 |
// Print the results
|
alpar@1427
|
450 |
check(lp.primalType() == LP::OPTIMAL, "Optimal solution is not found");
|
alpar@1427
|
451 |
check(lp.primal() <= 67.501 && lp.primal() >= 67.499, "Wrong objective value");
|
alpar@1427
|
452 |
check(lp.primal(x1) <= 7.501 && lp.primal(x1) >= 7.499, "Wrong value for x1");
|
alpar@1427
|
453 |
check(lp.primal(x2) <= 10.001 && lp.primal(x2) >= 9.999, "Wrong value for x2");
|
alpar@1427
|
454 |
}
|
alpar@1427
|
455 |
|
deba@481
|
456 |
int main()
|
deba@481
|
457 |
{
|
deba@481
|
458 |
LpSkeleton lp_skel;
|
deba@481
|
459 |
lpTest(lp_skel);
|
deba@481
|
460 |
|
alpar@1300
|
461 |
#ifdef LEMON_HAVE_LP
|
alpar@1300
|
462 |
{
|
alpar@1300
|
463 |
Lp lp,lp2;
|
alpar@1300
|
464 |
lpTest(lp);
|
alpar@1300
|
465 |
aTest(lp2);
|
alpar@1300
|
466 |
cloneTest<Lp>();
|
alpar@1300
|
467 |
}
|
alpar@1300
|
468 |
#endif
|
alpar@1300
|
469 |
|
ladanyi@674
|
470 |
#ifdef LEMON_HAVE_GLPK
|
deba@482
|
471 |
{
|
alpar@485
|
472 |
GlpkLp lp_glpk1,lp_glpk2;
|
deba@482
|
473 |
lpTest(lp_glpk1);
|
deba@482
|
474 |
aTest(lp_glpk2);
|
alpar@587
|
475 |
cloneTest<GlpkLp>();
|
alpar@1427
|
476 |
rangeConstraintTest<GlpkLp>();
|
deba@482
|
477 |
}
|
deba@481
|
478 |
#endif
|
deba@481
|
479 |
|
ladanyi@674
|
480 |
#ifdef LEMON_HAVE_CPLEX
|
deba@482
|
481 |
try {
|
alpar@485
|
482 |
CplexLp lp_cplex1,lp_cplex2;
|
deba@482
|
483 |
lpTest(lp_cplex1);
|
deba@482
|
484 |
aTest(lp_cplex2);
|
deba@598
|
485 |
cloneTest<CplexLp>();
|
alpar@1427
|
486 |
rangeConstraintTest<CplexLp>();
|
deba@482
|
487 |
} catch (CplexEnv::LicenseError& error) {
|
deba@482
|
488 |
check(false, error.what());
|
deba@482
|
489 |
}
|
deba@481
|
490 |
#endif
|
deba@481
|
491 |
|
ladanyi@674
|
492 |
#ifdef LEMON_HAVE_SOPLEX
|
deba@482
|
493 |
{
|
alpar@485
|
494 |
SoplexLp lp_soplex1,lp_soplex2;
|
deba@482
|
495 |
lpTest(lp_soplex1);
|
deba@482
|
496 |
aTest(lp_soplex2);
|
alpar@587
|
497 |
cloneTest<SoplexLp>();
|
alpar@1427
|
498 |
rangeConstraintTest<Soplex>();
|
deba@482
|
499 |
}
|
deba@482
|
500 |
#endif
|
deba@482
|
501 |
|
ladanyi@674
|
502 |
#ifdef LEMON_HAVE_CLP
|
deba@482
|
503 |
{
|
alpar@485
|
504 |
ClpLp lp_clp1,lp_clp2;
|
deba@482
|
505 |
lpTest(lp_clp1);
|
deba@482
|
506 |
aTest(lp_clp2);
|
alpar@587
|
507 |
cloneTest<ClpLp>();
|
alpar@1427
|
508 |
rangeConstraintTest<ClpLp>();
|
deba@482
|
509 |
}
|
deba@481
|
510 |
#endif
|
deba@481
|
511 |
|
deba@481
|
512 |
return 0;
|
deba@481
|
513 |
}
|