[1956] | 1 | /* -*- C++ -*- |
---|
| 2 | * |
---|
| 3 | * This file is a part of LEMON, a generic C++ optimization library |
---|
| 4 | * |
---|
| 5 | * Copyright (C) 2003-2006 |
---|
| 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 | |
---|
[1543] | 19 | #include <sstream> |
---|
| 20 | #include <lemon/lp_skeleton.h> |
---|
[1473] | 21 | #include "test_tools.h" |
---|
[2264] | 22 | #include <lemon/tolerance.h> |
---|
[1542] | 23 | |
---|
[1387] | 24 | #ifdef HAVE_CONFIG_H |
---|
| 25 | #include <config.h> |
---|
| 26 | #endif |
---|
| 27 | |
---|
| 28 | #ifdef HAVE_GLPK |
---|
| 29 | #include <lemon/lp_glpk.h> |
---|
[1437] | 30 | #endif |
---|
| 31 | |
---|
| 32 | #ifdef HAVE_CPLEX |
---|
[1387] | 33 | #include <lemon/lp_cplex.h> |
---|
| 34 | #endif |
---|
[1254] | 35 | |
---|
[2314] | 36 | // #ifdef HAVE_SOPLEX |
---|
| 37 | // #include <lemon/lp_soplex.h> |
---|
| 38 | // #endif |
---|
| 39 | |
---|
[1254] | 40 | using namespace lemon; |
---|
| 41 | |
---|
[1263] | 42 | void lpTest(LpSolverBase & lp) |
---|
[1254] | 43 | { |
---|
[1508] | 44 | |
---|
| 45 | |
---|
| 46 | |
---|
[1263] | 47 | typedef LpSolverBase LP; |
---|
[1256] | 48 | |
---|
[1309] | 49 | std::vector<LP::Col> x(10); |
---|
| 50 | // for(int i=0;i<10;i++) x.push_back(lp.addCol()); |
---|
| 51 | lp.addColSet(x); |
---|
[1895] | 52 | lp.colLowerBound(x,1); |
---|
| 53 | lp.colUpperBound(x,1); |
---|
| 54 | lp.colBounds(x,1,2); |
---|
[1508] | 55 | #ifndef GYORSITAS |
---|
| 56 | |
---|
[1256] | 57 | std::vector<LP::Col> y(10); |
---|
| 58 | lp.addColSet(y); |
---|
| 59 | |
---|
[1895] | 60 | lp.colLowerBound(y,1); |
---|
| 61 | lp.colUpperBound(y,1); |
---|
| 62 | lp.colBounds(y,1,2); |
---|
| 63 | |
---|
[1256] | 64 | std::map<int,LP::Col> z; |
---|
| 65 | |
---|
| 66 | z.insert(std::make_pair(12,INVALID)); |
---|
| 67 | z.insert(std::make_pair(2,INVALID)); |
---|
| 68 | z.insert(std::make_pair(7,INVALID)); |
---|
| 69 | z.insert(std::make_pair(5,INVALID)); |
---|
[1895] | 70 | |
---|
[1256] | 71 | lp.addColSet(z); |
---|
| 72 | |
---|
[1895] | 73 | lp.colLowerBound(z,1); |
---|
| 74 | lp.colUpperBound(z,1); |
---|
| 75 | lp.colBounds(z,1,2); |
---|
| 76 | |
---|
[1445] | 77 | { |
---|
| 78 | LP::Expr e,f,g; |
---|
| 79 | LP::Col p1,p2,p3,p4,p5; |
---|
| 80 | LP::Constr c; |
---|
| 81 | |
---|
[1484] | 82 | p1=lp.addCol(); |
---|
| 83 | p2=lp.addCol(); |
---|
| 84 | p3=lp.addCol(); |
---|
| 85 | p4=lp.addCol(); |
---|
| 86 | p5=lp.addCol(); |
---|
| 87 | |
---|
[1445] | 88 | e[p1]=2; |
---|
| 89 | e.constComp()=12; |
---|
| 90 | e[p1]+=2; |
---|
| 91 | e.constComp()+=12; |
---|
| 92 | e[p1]-=2; |
---|
| 93 | e.constComp()-=12; |
---|
| 94 | |
---|
| 95 | e=2; |
---|
| 96 | e=2.2; |
---|
| 97 | e=p1; |
---|
| 98 | e=f; |
---|
| 99 | |
---|
| 100 | e+=2; |
---|
| 101 | e+=2.2; |
---|
| 102 | e+=p1; |
---|
| 103 | e+=f; |
---|
| 104 | |
---|
| 105 | e-=2; |
---|
| 106 | e-=2.2; |
---|
| 107 | e-=p1; |
---|
| 108 | e-=f; |
---|
| 109 | |
---|
| 110 | e*=2; |
---|
| 111 | e*=2.2; |
---|
| 112 | e/=2; |
---|
| 113 | e/=2.2; |
---|
| 114 | |
---|
| 115 | e=((p1+p2)+(p1-p2)+(p1+12)+(12+p1)+(p1-12)+(12-p1)+ |
---|
| 116 | (f+12)+(12+f)+(p1+f)+(f+p1)+(f+g)+ |
---|
| 117 | (f-12)+(12-f)+(p1-f)+(f-p1)+(f-g)+ |
---|
| 118 | 2.2*f+f*2.2+f/2.2+ |
---|
| 119 | 2*f+f*2+f/2+ |
---|
| 120 | 2.2*p1+p1*2.2+p1/2.2+ |
---|
| 121 | 2*p1+p1*2+p1/2 |
---|
| 122 | ); |
---|
[1256] | 123 | |
---|
[1445] | 124 | |
---|
| 125 | c = (e <= f ); |
---|
| 126 | c = (e <= 2.2); |
---|
| 127 | c = (e <= 2 ); |
---|
| 128 | c = (e <= p1 ); |
---|
| 129 | c = (2.2<= f ); |
---|
| 130 | c = (2 <= f ); |
---|
| 131 | c = (p1 <= f ); |
---|
| 132 | c = (p1 <= p2 ); |
---|
| 133 | c = (p1 <= 2.2); |
---|
| 134 | c = (p1 <= 2 ); |
---|
| 135 | c = (2.2<= p2 ); |
---|
| 136 | c = (2 <= p2 ); |
---|
| 137 | |
---|
| 138 | c = (e >= f ); |
---|
| 139 | c = (e >= 2.2); |
---|
| 140 | c = (e >= 2 ); |
---|
| 141 | c = (e >= p1 ); |
---|
| 142 | c = (2.2>= f ); |
---|
| 143 | c = (2 >= f ); |
---|
| 144 | c = (p1 >= f ); |
---|
| 145 | c = (p1 >= p2 ); |
---|
| 146 | c = (p1 >= 2.2); |
---|
| 147 | c = (p1 >= 2 ); |
---|
| 148 | c = (2.2>= p2 ); |
---|
| 149 | c = (2 >= p2 ); |
---|
| 150 | |
---|
| 151 | c = (e == f ); |
---|
| 152 | c = (e == 2.2); |
---|
| 153 | c = (e == 2 ); |
---|
| 154 | c = (e == p1 ); |
---|
| 155 | c = (2.2== f ); |
---|
| 156 | c = (2 == f ); |
---|
| 157 | c = (p1 == f ); |
---|
| 158 | //c = (p1 == p2 ); |
---|
| 159 | c = (p1 == 2.2); |
---|
| 160 | c = (p1 == 2 ); |
---|
| 161 | c = (2.2== p2 ); |
---|
| 162 | c = (2 == p2 ); |
---|
| 163 | |
---|
| 164 | c = (2 <= e <= 3); |
---|
| 165 | c = (2 <= p1<= 3); |
---|
| 166 | |
---|
| 167 | c = (2 >= e >= 3); |
---|
| 168 | c = (2 >= p1>= 3); |
---|
| 169 | |
---|
| 170 | e[x[3]]=2; |
---|
| 171 | e[x[3]]=4; |
---|
| 172 | e[x[3]]=1; |
---|
| 173 | e.constComp()=12; |
---|
| 174 | |
---|
| 175 | lp.addRow(LP::INF,e,23); |
---|
| 176 | lp.addRow(LP::INF,3.0*(x[1]+x[2]/2)-x[3],23); |
---|
| 177 | lp.addRow(LP::INF,3.0*(x[1]+x[2]*2-5*x[3]+12-x[4]/3)+2*x[4]-4,23); |
---|
| 178 | |
---|
| 179 | lp.addRow(x[1]+x[3]<=x[5]-3); |
---|
| 180 | lp.addRow(-7<=x[1]+x[3]-12<=3); |
---|
| 181 | lp.addRow(x[1]<=x[5]); |
---|
[2264] | 182 | |
---|
| 183 | std::ostringstream buf; |
---|
| 184 | |
---|
| 185 | |
---|
| 186 | //Checking the simplify function |
---|
| 187 | |
---|
| 188 | // //How to check the simplify function? A map gives no information |
---|
| 189 | // //on the question whether a given key is or is not stored in it, or |
---|
| 190 | // //it does? |
---|
| 191 | // Yes, it does, using the find() function. |
---|
| 192 | e=((p1+p2)+(p1-p2)); |
---|
| 193 | e.simplify(); |
---|
| 194 | buf << "Coeff. of p2 should be 0"; |
---|
| 195 | // std::cout<<e[p1]<<e[p2]<<e[p3]<<std::endl; |
---|
| 196 | check(e.find(p2)==e.end(), buf.str()); |
---|
| 197 | |
---|
| 198 | |
---|
| 199 | |
---|
| 200 | |
---|
| 201 | e=((p1+p2)+(p1-0.99*p2)); |
---|
| 202 | double tolerance=0.001; |
---|
| 203 | e.simplify(tolerance); |
---|
| 204 | buf << "Coeff. of p2 should be 0.01"; |
---|
| 205 | check(e[p2]>0, buf.str()); |
---|
| 206 | |
---|
| 207 | tolerance=0.02; |
---|
| 208 | e.simplify(tolerance); |
---|
| 209 | buf << "Coeff. of p2 should be 0"; |
---|
| 210 | check(e.find(p2)==e.end(), buf.str()); |
---|
| 211 | |
---|
| 212 | |
---|
[1445] | 213 | } |
---|
[1272] | 214 | |
---|
[1445] | 215 | { |
---|
| 216 | LP::DualExpr e,f,g; |
---|
| 217 | LP::Row p1,p2,p3,p4,p5; |
---|
| 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 | |
---|
[1493] | 237 | e=((p1+p2)+(p1-p2)+ |
---|
[1445] | 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 | } |
---|
[1272] | 246 | |
---|
[1508] | 247 | #endif |
---|
[1264] | 248 | } |
---|
| 249 | |
---|
[1542] | 250 | void solveAndCheck(LpSolverBase& lp, LpSolverBase::SolutionStatus stat, |
---|
[1543] | 251 | double exp_opt) { |
---|
| 252 | using std::string; |
---|
[1542] | 253 | lp.solve(); |
---|
| 254 | //int decimal,sign; |
---|
[1543] | 255 | std::ostringstream buf; |
---|
| 256 | buf << "Primalstatus should be: " << int(stat); |
---|
| 257 | |
---|
[1542] | 258 | // itoa(stat,buf1, 10); |
---|
[1543] | 259 | check(lp.primalStatus()==stat, buf.str()); |
---|
| 260 | |
---|
| 261 | if (stat == LpSolverBase::OPTIMAL) { |
---|
| 262 | std::ostringstream buf; |
---|
| 263 | buf << "Wrong optimal value: the right optimum is " << exp_opt; |
---|
| 264 | check(std::abs(lp.primalValue()-exp_opt) < 1e-3, buf.str()); |
---|
[1542] | 265 | //+ecvt(exp_opt,2) |
---|
| 266 | } |
---|
| 267 | } |
---|
| 268 | |
---|
[1473] | 269 | void aTest(LpSolverBase & lp) |
---|
| 270 | { |
---|
| 271 | typedef LpSolverBase LP; |
---|
| 272 | |
---|
[1542] | 273 | //The following example is very simple |
---|
[1473] | 274 | |
---|
| 275 | typedef LpSolverBase::Row Row; |
---|
| 276 | typedef LpSolverBase::Col Col; |
---|
| 277 | |
---|
| 278 | |
---|
| 279 | Col x1 = lp.addCol(); |
---|
| 280 | Col x2 = lp.addCol(); |
---|
| 281 | |
---|
| 282 | |
---|
| 283 | //Constraints |
---|
[1542] | 284 | Row upright=lp.addRow(x1+x2 <=1); |
---|
| 285 | lp.addRow(x1+x2 >=-1); |
---|
| 286 | lp.addRow(x1-x2 <=1); |
---|
| 287 | lp.addRow(x1-x2 >=-1); |
---|
[1473] | 288 | //Nonnegativity of the variables |
---|
| 289 | lp.colLowerBound(x1, 0); |
---|
| 290 | lp.colLowerBound(x2, 0); |
---|
| 291 | //Objective function |
---|
[1542] | 292 | lp.setObj(x1+x2); |
---|
[1473] | 293 | |
---|
| 294 | lp.max(); |
---|
| 295 | |
---|
[1542] | 296 | //Maximization of x1+x2 |
---|
| 297 | //over the triangle with vertices (0,0) (0,1) (1,0) |
---|
| 298 | double expected_opt=1; |
---|
| 299 | solveAndCheck(lp, LpSolverBase::OPTIMAL, expected_opt); |
---|
[1484] | 300 | |
---|
[1542] | 301 | //Minimization |
---|
| 302 | lp.min(); |
---|
| 303 | expected_opt=0; |
---|
| 304 | solveAndCheck(lp, LpSolverBase::OPTIMAL, expected_opt); |
---|
| 305 | |
---|
| 306 | //Vertex (-1,0) instead of (0,0) |
---|
| 307 | lp.colLowerBound(x1, -LpSolverBase::INF); |
---|
| 308 | expected_opt=-1; |
---|
| 309 | solveAndCheck(lp, LpSolverBase::OPTIMAL, expected_opt); |
---|
| 310 | |
---|
| 311 | //Erase one constraint and return to maximization |
---|
| 312 | lp.eraseRow(upright); |
---|
| 313 | lp.max(); |
---|
| 314 | expected_opt=LpSolverBase::INF; |
---|
| 315 | solveAndCheck(lp, LpSolverBase::INFINITE, expected_opt); |
---|
| 316 | |
---|
| 317 | //Infeasibilty |
---|
| 318 | lp.addRow(x1+x2 <=-2); |
---|
| 319 | solveAndCheck(lp, LpSolverBase::INFEASIBLE, expected_opt); |
---|
| 320 | |
---|
| 321 | //Change problem and forget to solve |
---|
| 322 | lp.min(); |
---|
| 323 | check(lp.primalStatus()==LpSolverBase::UNDEFINED,"Primalstatus should be UNDEFINED"); |
---|
| 324 | |
---|
| 325 | // lp.solve(); |
---|
| 326 | // if (lp.primalStatus()==LpSolverBase::OPTIMAL){ |
---|
| 327 | // std::cout<< "Z = "<<lp.primalValue() |
---|
| 328 | // << " (error = " << lp.primalValue()-expected_opt |
---|
| 329 | // << "); x1 = "<<lp.primal(x1) |
---|
| 330 | // << "; x2 = "<<lp.primal(x2) |
---|
| 331 | // <<std::endl; |
---|
[1484] | 332 | |
---|
[1542] | 333 | // } |
---|
| 334 | // else{ |
---|
| 335 | // std::cout<<lp.primalStatus()<<std::endl; |
---|
| 336 | // std::cout<<"Optimal solution not found!"<<std::endl; |
---|
| 337 | // } |
---|
[1473] | 338 | |
---|
[1542] | 339 | |
---|
[1473] | 340 | |
---|
| 341 | } |
---|
| 342 | |
---|
| 343 | |
---|
[1263] | 344 | int main() |
---|
| 345 | { |
---|
[1390] | 346 | LpSkeleton lp_skel; |
---|
| 347 | lpTest(lp_skel); |
---|
| 348 | |
---|
[1437] | 349 | #ifdef HAVE_GLPK |
---|
[1484] | 350 | LpGlpk lp_glpk1,lp_glpk2; |
---|
| 351 | lpTest(lp_glpk1); |
---|
| 352 | aTest(lp_glpk2); |
---|
[1437] | 353 | #endif |
---|
[1263] | 354 | |
---|
[1437] | 355 | #ifdef HAVE_CPLEX |
---|
[1797] | 356 | LpCplex lp_cplex1,lp_cplex2; |
---|
| 357 | lpTest(lp_cplex1); |
---|
| 358 | aTest(lp_cplex2); |
---|
[1437] | 359 | #endif |
---|
[1264] | 360 | |
---|
[2314] | 361 | // #ifdef HAVE_SOPLEX |
---|
| 362 | // LpSoplex lp_soplex1,lp_soplex2; |
---|
| 363 | // lpTest(lp_soplex1); |
---|
| 364 | // aTest(lp_soplex2); |
---|
| 365 | // #endif |
---|
| 366 | |
---|
[1309] | 367 | return 0; |
---|
[1263] | 368 | } |
---|