1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
2 | 2 |
* |
3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
4 | 4 |
* |
5 | 5 |
* Copyright (C) 2003-2008 |
6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
8 | 8 |
* |
9 | 9 |
* Permission to use, modify and distribute this software is granted |
10 | 10 |
* provided that this copyright notice appears in all copies. For |
11 | 11 |
* precise terms see the accompanying LICENSE file. |
12 | 12 |
* |
13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
14 | 14 |
* express or implied, and with no claim as to its suitability for any |
15 | 15 |
* purpose. |
16 | 16 |
* |
17 | 17 |
*/ |
18 | 18 |
|
19 | 19 |
#include <sstream> |
20 | 20 |
#include <lemon/lp_skeleton.h> |
21 | 21 |
#include "test_tools.h" |
22 | 22 |
#include <lemon/tolerance.h> |
23 | 23 |
|
24 | 24 |
#ifdef HAVE_CONFIG_H |
25 | 25 |
#include <lemon/config.h> |
26 | 26 |
#endif |
27 | 27 |
|
28 | 28 |
#ifdef HAVE_GLPK |
29 | 29 |
#include <lemon/lp_glpk.h> |
30 | 30 |
#endif |
31 | 31 |
|
32 | 32 |
#ifdef HAVE_CPLEX |
33 | 33 |
#include <lemon/lp_cplex.h> |
34 | 34 |
#endif |
35 | 35 |
|
36 | 36 |
#ifdef HAVE_SOPLEX |
37 | 37 |
#include <lemon/lp_soplex.h> |
38 | 38 |
#endif |
39 | 39 |
|
40 | 40 |
#ifdef HAVE_CLP |
41 | 41 |
#include <lemon/lp_clp.h> |
42 | 42 |
#endif |
43 | 43 |
|
44 | 44 |
using namespace lemon; |
45 | 45 |
|
46 | 46 |
void lpTest(LpSolver& lp) |
47 | 47 |
{ |
48 | 48 |
|
49 | 49 |
typedef LpSolver LP; |
50 | 50 |
|
51 | 51 |
std::vector<LP::Col> x(10); |
52 | 52 |
// for(int i=0;i<10;i++) x.push_back(lp.addCol()); |
53 | 53 |
lp.addColSet(x); |
54 | 54 |
lp.colLowerBound(x,1); |
55 | 55 |
lp.colUpperBound(x,1); |
56 | 56 |
lp.colBounds(x,1,2); |
57 | 57 |
|
58 | 58 |
std::vector<LP::Col> y(10); |
59 | 59 |
lp.addColSet(y); |
60 | 60 |
|
61 | 61 |
lp.colLowerBound(y,1); |
62 | 62 |
lp.colUpperBound(y,1); |
63 | 63 |
lp.colBounds(y,1,2); |
64 | 64 |
|
65 | 65 |
std::map<int,LP::Col> z; |
66 | 66 |
|
67 | 67 |
z.insert(std::make_pair(12,INVALID)); |
68 | 68 |
z.insert(std::make_pair(2,INVALID)); |
69 | 69 |
z.insert(std::make_pair(7,INVALID)); |
70 | 70 |
z.insert(std::make_pair(5,INVALID)); |
71 | 71 |
|
72 | 72 |
lp.addColSet(z); |
73 | 73 |
|
74 | 74 |
lp.colLowerBound(z,1); |
75 | 75 |
lp.colUpperBound(z,1); |
76 | 76 |
lp.colBounds(z,1,2); |
77 | 77 |
|
78 | 78 |
{ |
79 | 79 |
LP::Expr e,f,g; |
80 | 80 |
LP::Col p1,p2,p3,p4,p5; |
81 | 81 |
LP::Constr c; |
82 | 82 |
|
83 | 83 |
p1=lp.addCol(); |
84 | 84 |
p2=lp.addCol(); |
85 | 85 |
p3=lp.addCol(); |
86 | 86 |
p4=lp.addCol(); |
87 | 87 |
p5=lp.addCol(); |
88 | 88 |
|
89 | 89 |
e[p1]=2; |
90 | 90 |
*e=12; |
91 | 91 |
e[p1]+=2; |
92 | 92 |
*e+=12; |
93 | 93 |
e[p1]-=2; |
94 | 94 |
*e-=12; |
95 | 95 |
|
96 | 96 |
e=2; |
97 | 97 |
e=2.2; |
98 | 98 |
e=p1; |
99 | 99 |
e=f; |
100 | 100 |
|
101 | 101 |
e+=2; |
102 | 102 |
e+=2.2; |
103 | 103 |
e+=p1; |
104 | 104 |
e+=f; |
105 | 105 |
|
106 | 106 |
e-=2; |
107 | 107 |
e-=2.2; |
108 | 108 |
e-=p1; |
109 | 109 |
e-=f; |
110 | 110 |
|
111 | 111 |
e*=2; |
112 | 112 |
e*=2.2; |
113 | 113 |
e/=2; |
114 | 114 |
e/=2.2; |
115 | 115 |
|
116 | 116 |
e=((p1+p2)+(p1-p2)+(p1+12)+(12+p1)+(p1-12)+(12-p1)+ |
117 | 117 |
(f+12)+(12+f)+(p1+f)+(f+p1)+(f+g)+ |
118 | 118 |
(f-12)+(12-f)+(p1-f)+(f-p1)+(f-g)+ |
119 | 119 |
2.2*f+f*2.2+f/2.2+ |
120 | 120 |
2*f+f*2+f/2+ |
121 | 121 |
2.2*p1+p1*2.2+p1/2.2+ |
122 | 122 |
2*p1+p1*2+p1/2 |
123 | 123 |
); |
124 | 124 |
|
125 | 125 |
|
126 | 126 |
c = (e <= f ); |
127 | 127 |
c = (e <= 2.2); |
128 | 128 |
c = (e <= 2 ); |
129 | 129 |
c = (e <= p1 ); |
130 | 130 |
c = (2.2<= f ); |
131 | 131 |
c = (2 <= f ); |
132 | 132 |
c = (p1 <= f ); |
133 | 133 |
c = (p1 <= p2 ); |
134 | 134 |
c = (p1 <= 2.2); |
135 | 135 |
c = (p1 <= 2 ); |
136 | 136 |
c = (2.2<= p2 ); |
137 | 137 |
c = (2 <= p2 ); |
138 | 138 |
|
139 | 139 |
c = (e >= f ); |
140 | 140 |
c = (e >= 2.2); |
141 | 141 |
c = (e >= 2 ); |
142 | 142 |
c = (e >= p1 ); |
143 | 143 |
c = (2.2>= f ); |
144 | 144 |
c = (2 >= f ); |
145 | 145 |
c = (p1 >= f ); |
146 | 146 |
c = (p1 >= p2 ); |
147 | 147 |
c = (p1 >= 2.2); |
148 | 148 |
c = (p1 >= 2 ); |
149 | 149 |
c = (2.2>= p2 ); |
150 | 150 |
c = (2 >= p2 ); |
151 | 151 |
|
152 | 152 |
c = (e == f ); |
153 | 153 |
c = (e == 2.2); |
154 | 154 |
c = (e == 2 ); |
155 | 155 |
c = (e == p1 ); |
156 | 156 |
c = (2.2== f ); |
157 | 157 |
c = (2 == f ); |
158 | 158 |
c = (p1 == f ); |
159 | 159 |
//c = (p1 == p2 ); |
160 | 160 |
c = (p1 == 2.2); |
161 | 161 |
c = (p1 == 2 ); |
162 | 162 |
c = (2.2== p2 ); |
163 | 163 |
c = (2 == p2 ); |
164 | 164 |
|
165 |
c = (2 <= e <= 3); |
|
166 |
c = (2 <= p1<= 3); |
|
165 |
c = ((2 <= e) <= 3); |
|
166 |
c = ((2 <= p1) <= 3); |
|
167 | 167 |
|
168 |
c = (2 >= e >= 3); |
|
169 |
c = (2 >= p1>= 3); |
|
168 |
c = ((2 >= e) >= 3); |
|
169 |
c = ((2 >= p1) >= 3); |
|
170 | 170 |
|
171 | 171 |
e[x[3]]=2; |
172 | 172 |
e[x[3]]=4; |
173 | 173 |
e[x[3]]=1; |
174 | 174 |
*e=12; |
175 | 175 |
|
176 | 176 |
lp.addRow(-LP::INF,e,23); |
177 | 177 |
lp.addRow(-LP::INF,3.0*(x[1]+x[2]/2)-x[3],23); |
178 | 178 |
lp.addRow(-LP::INF,3.0*(x[1]+x[2]*2-5*x[3]+12-x[4]/3)+2*x[4]-4,23); |
179 | 179 |
|
180 | 180 |
lp.addRow(x[1]+x[3]<=x[5]-3); |
181 |
lp.addRow(-7<=x[1]+x[3]-12<=3); |
|
181 |
lp.addRow((-7<=x[1]+x[3]-12)<=3); |
|
182 | 182 |
lp.addRow(x[1]<=x[5]); |
183 | 183 |
|
184 | 184 |
std::ostringstream buf; |
185 | 185 |
|
186 | 186 |
|
187 | 187 |
e=((p1+p2)+(p1-0.99*p2)); |
188 | 188 |
//e.prettyPrint(std::cout); |
189 | 189 |
//(e<=2).prettyPrint(std::cout); |
190 | 190 |
double tolerance=0.001; |
191 | 191 |
e.simplify(tolerance); |
192 | 192 |
buf << "Coeff. of p2 should be 0.01"; |
193 | 193 |
check(e[p2]>0, buf.str()); |
194 | 194 |
|
195 | 195 |
tolerance=0.02; |
196 | 196 |
e.simplify(tolerance); |
197 | 197 |
buf << "Coeff. of p2 should be 0"; |
198 | 198 |
check(const_cast<const LpSolver::Expr&>(e)[p2]==0, buf.str()); |
199 | 199 |
|
200 | 200 |
|
201 | 201 |
} |
202 | 202 |
|
203 | 203 |
{ |
204 | 204 |
LP::DualExpr e,f,g; |
205 | 205 |
LP::Row p1 = INVALID, p2 = INVALID, p3 = INVALID, |
206 | 206 |
p4 = INVALID, p5 = INVALID; |
207 | 207 |
|
208 | 208 |
e[p1]=2; |
209 | 209 |
e[p1]+=2; |
210 | 210 |
e[p1]-=2; |
211 | 211 |
|
212 | 212 |
e=p1; |
213 | 213 |
e=f; |
214 | 214 |
|
215 | 215 |
e+=p1; |
216 | 216 |
e+=f; |
217 | 217 |
|
218 | 218 |
e-=p1; |
219 | 219 |
e-=f; |
220 | 220 |
|
221 | 221 |
e*=2; |
222 | 222 |
e*=2.2; |
223 | 223 |
e/=2; |
224 | 224 |
e/=2.2; |
225 | 225 |
|
226 | 226 |
e=((p1+p2)+(p1-p2)+ |
227 | 227 |
(p1+f)+(f+p1)+(f+g)+ |
228 | 228 |
(p1-f)+(f-p1)+(f-g)+ |
229 | 229 |
2.2*f+f*2.2+f/2.2+ |
230 | 230 |
2*f+f*2+f/2+ |
231 | 231 |
2.2*p1+p1*2.2+p1/2.2+ |
232 | 232 |
2*p1+p1*2+p1/2 |
233 | 233 |
); |
234 | 234 |
} |
235 | 235 |
|
236 | 236 |
} |
237 | 237 |
|
238 | 238 |
void solveAndCheck(LpSolver& lp, LpSolver::ProblemType stat, |
239 | 239 |
double exp_opt) { |
240 | 240 |
using std::string; |
241 | 241 |
lp.solve(); |
242 | 242 |
|
243 | 243 |
std::ostringstream buf; |
244 | 244 |
buf << "PrimalType should be: " << int(stat) << int(lp.primalType()); |
245 | 245 |
|
246 | 246 |
check(lp.primalType()==stat, buf.str()); |
247 | 247 |
|
248 | 248 |
if (stat == LpSolver::OPTIMAL) { |
249 | 249 |
std::ostringstream sbuf; |
250 | 250 |
sbuf << "Wrong optimal value: the right optimum is " << exp_opt; |
251 | 251 |
check(std::abs(lp.primal()-exp_opt) < 1e-3, sbuf.str()); |
252 | 252 |
} |
253 | 253 |
} |
254 | 254 |
|
255 | 255 |
void aTest(LpSolver & lp) |
256 | 256 |
{ |
257 | 257 |
typedef LpSolver LP; |
258 | 258 |
|
259 | 259 |
//The following example is very simple |
260 | 260 |
|
261 | 261 |
typedef LpSolver::Row Row; |
262 | 262 |
typedef LpSolver::Col Col; |
263 | 263 |
|
264 | 264 |
|
265 | 265 |
Col x1 = lp.addCol(); |
266 | 266 |
Col x2 = lp.addCol(); |
267 | 267 |
|
268 | 268 |
|
269 | 269 |
//Constraints |
270 | 270 |
Row upright=lp.addRow(x1+2*x2 <=1); |
271 | 271 |
lp.addRow(x1+x2 >=-1); |
272 | 272 |
lp.addRow(x1-x2 <=1); |
273 | 273 |
lp.addRow(x1-x2 >=-1); |
274 | 274 |
//Nonnegativity of the variables |
275 | 275 |
lp.colLowerBound(x1, 0); |
276 | 276 |
lp.colLowerBound(x2, 0); |
277 | 277 |
//Objective function |
278 | 278 |
lp.obj(x1+x2); |
279 | 279 |
|
280 | 280 |
lp.sense(lp.MAX); |
281 | 281 |
|
282 | 282 |
//Testing the problem retrieving routines |
283 | 283 |
check(lp.objCoeff(x1)==1,"First term should be 1 in the obj function!"); |
284 | 284 |
check(lp.sense() == lp.MAX,"This is a maximization!"); |
285 | 285 |
check(lp.coeff(upright,x1)==1,"The coefficient in question is 1!"); |
286 | 286 |
check(lp.colLowerBound(x1)==0, |
287 | 287 |
"The lower bound for variable x1 should be 0."); |
288 | 288 |
check(lp.colUpperBound(x1)==LpSolver::INF, |
289 | 289 |
"The upper bound for variable x1 should be infty."); |
290 | 290 |
check(lp.rowLowerBound(upright) == -LpSolver::INF, |
291 | 291 |
"The lower bound for the first row should be -infty."); |
292 | 292 |
check(lp.rowUpperBound(upright)==1, |
293 | 293 |
"The upper bound for the first row should be 1."); |
294 | 294 |
LpSolver::Expr e = lp.row(upright); |
295 | 295 |
check(e[x1] == 1, "The first coefficient should 1."); |
296 | 296 |
check(e[x2] == 2, "The second coefficient should 1."); |
297 | 297 |
|
298 | 298 |
lp.row(upright, x1+x2 <=1); |
299 | 299 |
e = lp.row(upright); |
300 | 300 |
check(e[x1] == 1, "The first coefficient should 1."); |
301 | 301 |
check(e[x2] == 1, "The second coefficient should 1."); |
302 | 302 |
|
303 | 303 |
LpSolver::DualExpr de = lp.col(x1); |
304 | 304 |
check( de[upright] == 1, "The first coefficient should 1."); |
305 | 305 |
|
306 | 306 |
LpSolver* clp = lp.cloneSolver(); |
307 | 307 |
|
308 | 308 |
//Testing the problem retrieving routines |
309 | 309 |
check(clp->objCoeff(x1)==1,"First term should be 1 in the obj function!"); |
310 | 310 |
check(clp->sense() == clp->MAX,"This is a maximization!"); |
311 | 311 |
check(clp->coeff(upright,x1)==1,"The coefficient in question is 1!"); |
312 | 312 |
// std::cout<<lp.colLowerBound(x1)<<std::endl; |
313 | 313 |
check(clp->colLowerBound(x1)==0, |
314 | 314 |
"The lower bound for variable x1 should be 0."); |
315 | 315 |
check(clp->colUpperBound(x1)==LpSolver::INF, |
316 | 316 |
"The upper bound for variable x1 should be infty."); |
317 | 317 |
|
318 | 318 |
check(lp.rowLowerBound(upright)==-LpSolver::INF, |
319 | 319 |
"The lower bound for the first row should be -infty."); |
320 | 320 |
check(lp.rowUpperBound(upright)==1, |
321 | 321 |
"The upper bound for the first row should be 1."); |
322 | 322 |
e = clp->row(upright); |
323 | 323 |
check(e[x1] == 1, "The first coefficient should 1."); |
324 | 324 |
check(e[x2] == 1, "The second coefficient should 1."); |
325 | 325 |
|
326 | 326 |
de = clp->col(x1); |
327 | 327 |
check(de[upright] == 1, "The first coefficient should 1."); |
328 | 328 |
|
329 | 329 |
delete clp; |
330 | 330 |
|
331 | 331 |
//Maximization of x1+x2 |
332 | 332 |
//over the triangle with vertices (0,0) (0,1) (1,0) |
333 | 333 |
double expected_opt=1; |
334 | 334 |
solveAndCheck(lp, LpSolver::OPTIMAL, expected_opt); |
335 | 335 |
|
336 | 336 |
//Minimization |
337 | 337 |
lp.sense(lp.MIN); |
338 | 338 |
expected_opt=0; |
339 | 339 |
solveAndCheck(lp, LpSolver::OPTIMAL, expected_opt); |
340 | 340 |
|
341 | 341 |
//Vertex (-1,0) instead of (0,0) |
342 | 342 |
lp.colLowerBound(x1, -LpSolver::INF); |
343 | 343 |
expected_opt=-1; |
344 | 344 |
solveAndCheck(lp, LpSolver::OPTIMAL, expected_opt); |
345 | 345 |
|
346 | 346 |
//Erase one constraint and return to maximization |
347 | 347 |
lp.erase(upright); |
348 | 348 |
lp.sense(lp.MAX); |
349 | 349 |
expected_opt=LpSolver::INF; |
350 | 350 |
solveAndCheck(lp, LpSolver::UNBOUNDED, expected_opt); |
351 | 351 |
|
352 | 352 |
//Infeasibilty |
353 | 353 |
lp.addRow(x1+x2 <=-2); |
354 | 354 |
solveAndCheck(lp, LpSolver::INFEASIBLE, expected_opt); |
355 | 355 |
|
356 | 356 |
} |
357 | 357 |
|
358 | 358 |
int main() |
359 | 359 |
{ |
360 | 360 |
LpSkeleton lp_skel; |
361 | 361 |
lpTest(lp_skel); |
362 | 362 |
|
363 | 363 |
#ifdef HAVE_GLPK |
364 | 364 |
{ |
365 | 365 |
LpGlpk lp_glpk1,lp_glpk2; |
366 | 366 |
lpTest(lp_glpk1); |
367 | 367 |
aTest(lp_glpk2); |
368 | 368 |
} |
369 | 369 |
#endif |
370 | 370 |
|
371 | 371 |
#ifdef HAVE_CPLEX |
372 | 372 |
try { |
373 | 373 |
LpCplex lp_cplex1,lp_cplex2; |
374 | 374 |
lpTest(lp_cplex1); |
375 | 375 |
aTest(lp_cplex2); |
376 | 376 |
} catch (CplexEnv::LicenseError& error) { |
377 | 377 |
#ifdef LEMON_FORCE_CPLEX_CHECK |
378 | 378 |
check(false, error.what()); |
379 | 379 |
#else |
380 | 380 |
std::cerr << error.what() << std::endl; |
381 | 381 |
std::cerr << "Cplex license check failed, lp check skipped" << std::endl; |
382 | 382 |
#endif |
383 | 383 |
} |
384 | 384 |
#endif |
385 | 385 |
|
386 | 386 |
#ifdef HAVE_SOPLEX |
387 | 387 |
{ |
388 | 388 |
LpSoplex lp_soplex1,lp_soplex2; |
389 | 389 |
lpTest(lp_soplex1); |
390 | 390 |
aTest(lp_soplex2); |
391 | 391 |
} |
392 | 392 |
#endif |
393 | 393 |
|
394 | 394 |
#ifdef HAVE_CLP |
395 | 395 |
{ |
396 | 396 |
LpClp lp_clp1,lp_clp2; |
397 | 397 |
lpTest(lp_clp1); |
398 | 398 |
aTest(lp_clp2); |
399 | 399 |
} |
400 | 400 |
#endif |
401 | 401 |
|
402 | 402 |
return 0; |
403 | 403 |
} |
0 comments (0 inline)