Changeset 482:ed54c0d13df0 in lemon for test/lp_test.cc
 Timestamp:
 12/02/08 22:48:28 (11 years ago)
 Branch:
 default
 Children:
 483:76ec7bd57026, 547:17cabb114d52
 Phase:
 public
 File:

 1 edited
Legend:
 Unmodified
 Added
 Removed

test/lp_test.cc
r481 r482 38 38 #endif 39 39 40 #ifdef HAVE_CLP 41 #include <lemon/lp_clp.h> 42 #endif 43 40 44 using namespace lemon; 41 45 42 void lpTest(LpSolver Base& lp)46 void lpTest(LpSolver& lp) 43 47 { 44 48 45 46 47 typedef LpSolverBase LP; 49 typedef LpSolver LP; 48 50 49 51 std::vector<LP::Col> x(10); … … 53 55 lp.colUpperBound(x,1); 54 56 lp.colBounds(x,1,2); 55 #ifndef GYORSITAS56 57 57 58 std::vector<LP::Col> y(10); … … 87 88 88 89 e[p1]=2; 89 e.constComp()=12;90 *e=12; 90 91 e[p1]+=2; 91 e.constComp()+=12;92 *e+=12; 92 93 e[p1]=2; 93 e.constComp()=12;94 *e=12; 94 95 95 96 e=2; … … 171 172 e[x[3]]=4; 172 173 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]*25*x[3]+12x[4]/3)+2*x[4]4,23);174 *e=12; 175 176 lp.addRow(LP::INF,e,23); 177 lp.addRow(LP::INF,3.0*(x[1]+x[2]/2)x[3],23); 178 lp.addRow(LP::INF,3.0*(x[1]+x[2]*25*x[3]+12x[4]/3)+2*x[4]4,23); 178 179 179 180 lp.addRow(x[1]+x[3]<=x[5]3); … … 182 183 183 184 std::ostringstream buf; 184 185 186 //Checking the simplify function187 188 // //How to check the simplify function? A map gives no information189 // //on the question whether a given key is or is not stored in it, or190 // //it does?191 // Yes, it does, using the find() function.192 e=((p1+p2)+(p1p2));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 185 200 186 … … 210 196 e.simplify(tolerance); 211 197 buf << "Coeff. of p2 should be 0"; 212 check( e.find(p2)==e.end(), buf.str());198 check(const_cast<const LpSolver::Expr&>(e)[p2]==0, buf.str()); 213 199 214 200 … … 248 234 } 249 235 250 #endif251 236 } 252 237 253 void solveAndCheck(LpSolver Base& lp, LpSolverBase::SolutionStatusstat,238 void solveAndCheck(LpSolver& lp, LpSolver::ProblemType stat, 254 239 double exp_opt) { 255 240 using std::string; 256 241 lp.solve(); 257 //int decimal,sign; 242 258 243 std::ostringstream buf; 259 buf << "Primalstatus should be: " << int(stat); 260 261 // itoa(stat,buf1, 10); 262 check(lp.primalStatus()==stat, buf.str()); 263 264 if (stat == LpSolverBase::OPTIMAL) { 244 buf << "PrimalType should be: " << int(stat) << int(lp.primalType()); 245 246 check(lp.primalType()==stat, buf.str()); 247 248 if (stat == LpSolver::OPTIMAL) { 265 249 std::ostringstream sbuf; 266 250 sbuf << "Wrong optimal value: the right optimum is " << exp_opt; 267 check(std::abs(lp.primalValue()exp_opt) < 1e3, sbuf.str()); 268 //+ecvt(exp_opt,2) 251 check(std::abs(lp.primal()exp_opt) < 1e3, sbuf.str()); 269 252 } 270 253 } 271 254 272 void aTest(LpSolver Base& lp)255 void aTest(LpSolver & lp) 273 256 { 274 typedef LpSolver BaseLP;257 typedef LpSolver LP; 275 258 276 259 //The following example is very simple 277 260 278 typedef LpSolver Base::Row Row;279 typedef LpSolver Base::Col Col;261 typedef LpSolver::Row Row; 262 typedef LpSolver::Col Col; 280 263 281 264 … … 285 268 286 269 //Constraints 287 Row upright=lp.addRow(x1+ x2 <=1);270 Row upright=lp.addRow(x1+2*x2 <=1); 288 271 lp.addRow(x1+x2 >=1); 289 272 lp.addRow(x1x2 <=1); … … 295 278 lp.obj(x1+x2); 296 279 297 lp. max();280 lp.sense(lp.MAX); 298 281 299 282 //Testing the problem retrieving routines 300 283 check(lp.objCoeff(x1)==1,"First term should be 1 in the obj function!"); 301 check(lp. isMax(),"This is a maximization!");284 check(lp.sense() == lp.MAX,"This is a maximization!"); 302 285 check(lp.coeff(upright,x1)==1,"The coefficient in question is 1!"); 303 // std::cout<<lp.colLowerBound(x1)<<std::endl; 304 check( lp.colLowerBound(x1)==0, 305 "The lower bound for variable x1 should be 0."); 306 check( lp.colUpperBound(x1)==LpSolverBase::INF, 307 "The upper bound for variable x1 should be infty."); 308 LpSolverBase::Value lb,ub; 309 lp.getRowBounds(upright,lb,ub); 310 check( lb==LpSolverBase::INF, 311 "The lower bound for the first row should be infty."); 312 check( ub==1,"The upper bound for the first row should be 1."); 313 LpSolverBase::Expr e = lp.row(upright); 314 check( e.size() == 2, "The row retrieval gives back wrong expression."); 315 check( e[x1] == 1, "The first coefficient should 1."); 316 check( e[x2] == 1, "The second coefficient should 1."); 317 318 LpSolverBase::DualExpr de = lp.col(x1); 319 check( de.size() == 4, "The col retrieval gives back wrong expression."); 286 check(lp.colLowerBound(x1)==0, 287 "The lower bound for variable x1 should be 0."); 288 check(lp.colUpperBound(x1)==LpSolver::INF, 289 "The upper bound for variable x1 should be infty."); 290 check(lp.rowLowerBound(upright) == LpSolver::INF, 291 "The lower bound for the first row should be infty."); 292 check(lp.rowUpperBound(upright)==1, 293 "The upper bound for the first row should be 1."); 294 LpSolver::Expr e = lp.row(upright); 295 check(e[x1] == 1, "The first coefficient should 1."); 296 check(e[x2] == 2, "The second coefficient should 1."); 297 298 lp.row(upright, x1+x2 <=1); 299 e = lp.row(upright); 300 check(e[x1] == 1, "The first coefficient should 1."); 301 check(e[x2] == 1, "The second coefficient should 1."); 302 303 LpSolver::DualExpr de = lp.col(x1); 320 304 check( de[upright] == 1, "The first coefficient should 1."); 321 305 322 LpSolver Base* clp = lp.copyLp();306 LpSolver* clp = lp.cloneSolver(); 323 307 324 308 //Testing the problem retrieving routines 325 309 check(clp>objCoeff(x1)==1,"First term should be 1 in the obj function!"); 326 check(clp> isMax(),"This is a maximization!");310 check(clp>sense() == clp>MAX,"This is a maximization!"); 327 311 check(clp>coeff(upright,x1)==1,"The coefficient in question is 1!"); 328 312 // std::cout<<lp.colLowerBound(x1)<<std::endl; 329 check( 330 331 check( clp>colUpperBound(x1)==LpSolverBase::INF,332 333 334 c lp>getRowBounds(upright,lb,ub);335 check( lb==LpSolverBase::INF,336 "The lower bound for the first row should be infty.");337 check( ub==1,"The upper bound for the first row should be 1.");313 check(clp>colLowerBound(x1)==0, 314 "The lower bound for variable x1 should be 0."); 315 check(clp>colUpperBound(x1)==LpSolver::INF, 316 "The upper bound for variable x1 should be infty."); 317 318 check(lp.rowLowerBound(upright)==LpSolver::INF, 319 "The lower bound for the first row should be infty."); 320 check(lp.rowUpperBound(upright)==1, 321 "The upper bound for the first row should be 1."); 338 322 e = clp>row(upright); 339 check( e.size() == 2, "The row retrieval gives back wrong expression."); 340 check( e[x1] == 1, "The first coefficient should 1."); 341 check( e[x2] == 1, "The second coefficient should 1."); 323 check(e[x1] == 1, "The first coefficient should 1."); 324 check(e[x2] == 1, "The second coefficient should 1."); 342 325 343 326 de = clp>col(x1); 344 check( de.size() == 4, "The col retrieval gives back wrong expression."); 345 check( de[upright] == 1, "The first coefficient should 1."); 327 check(de[upright] == 1, "The first coefficient should 1."); 346 328 347 329 delete clp; … … 350 332 //over the triangle with vertices (0,0) (0,1) (1,0) 351 333 double expected_opt=1; 352 solveAndCheck(lp, LpSolver Base::OPTIMAL, expected_opt);334 solveAndCheck(lp, LpSolver::OPTIMAL, expected_opt); 353 335 354 336 //Minimization 355 lp. min();337 lp.sense(lp.MIN); 356 338 expected_opt=0; 357 solveAndCheck(lp, LpSolver Base::OPTIMAL, expected_opt);339 solveAndCheck(lp, LpSolver::OPTIMAL, expected_opt); 358 340 359 341 //Vertex (1,0) instead of (0,0) 360 lp.colLowerBound(x1, LpSolver Base::INF);342 lp.colLowerBound(x1, LpSolver::INF); 361 343 expected_opt=1; 362 solveAndCheck(lp, LpSolver Base::OPTIMAL, expected_opt);344 solveAndCheck(lp, LpSolver::OPTIMAL, expected_opt); 363 345 364 346 //Erase one constraint and return to maximization 365 lp.erase Row(upright);366 lp. max();367 expected_opt=LpSolver Base::INF;368 solveAndCheck(lp, LpSolver Base::INFINITE, expected_opt);347 lp.erase(upright); 348 lp.sense(lp.MAX); 349 expected_opt=LpSolver::INF; 350 solveAndCheck(lp, LpSolver::UNBOUNDED, expected_opt); 369 351 370 352 //Infeasibilty 371 353 lp.addRow(x1+x2 <=2); 372 solveAndCheck(lp, LpSolverBase::INFEASIBLE, expected_opt); 373 374 //Change problem and forget to solve 375 lp.min(); 376 check(lp.primalStatus()==LpSolverBase::UNDEFINED, 377 "Primalstatus should be UNDEFINED"); 378 379 380 // lp.solve(); 381 // if (lp.primalStatus()==LpSolverBase::OPTIMAL){ 382 // std::cout<< "Z = "<<lp.primalValue() 383 // << " (error = " << lp.primalValue()expected_opt 384 // << "); x1 = "<<lp.primal(x1) 385 // << "; x2 = "<<lp.primal(x2) 386 // <<std::endl; 387 388 // } 389 // else{ 390 // std::cout<<lp.primalStatus()<<std::endl; 391 // std::cout<<"Optimal solution not found!"<<std::endl; 392 // } 393 394 354 solveAndCheck(lp, LpSolver::INFEASIBLE, expected_opt); 395 355 396 356 } 397 398 357 399 358 int main() … … 403 362 404 363 #ifdef HAVE_GLPK 405 LpGlpk lp_glpk1,lp_glpk2; 406 lpTest(lp_glpk1); 407 aTest(lp_glpk2); 364 { 365 LpGlpk lp_glpk1,lp_glpk2; 366 lpTest(lp_glpk1); 367 aTest(lp_glpk2); 368 } 408 369 #endif 409 370 410 371 #ifdef HAVE_CPLEX 411 LpCplex lp_cplex1,lp_cplex2; 412 lpTest(lp_cplex1); 413 aTest(lp_cplex2); 372 try { 373 LpCplex lp_cplex1,lp_cplex2; 374 lpTest(lp_cplex1); 375 aTest(lp_cplex2); 376 } catch (CplexEnv::LicenseError& error) { 377 #ifdef LEMON_FORCE_CPLEX_CHECK 378 check(false, error.what()); 379 #else 380 std::cerr << error.what() << std::endl; 381 std::cerr << "Cplex license check failed, lp check skipped" << std::endl; 382 #endif 383 } 414 384 #endif 415 385 416 386 #ifdef HAVE_SOPLEX 417 LpSoplex lp_soplex1,lp_soplex2; 418 lpTest(lp_soplex1); 419 aTest(lp_soplex2); 387 { 388 LpSoplex lp_soplex1,lp_soplex2; 389 lpTest(lp_soplex1); 390 aTest(lp_soplex2); 391 } 392 #endif 393 394 #ifdef HAVE_CLP 395 { 396 LpClp lp_clp1,lp_clp2; 397 lpTest(lp_clp1); 398 aTest(lp_clp2); 399 } 420 400 #endif 421 401
Note: See TracChangeset
for help on using the changeset viewer.