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