1 /* -*- mode: C++; indent-tabs-mode: nil; -*-
3 * This file is a part of LEMON, a generic C++ optimization library.
5 * Copyright (C) 2003-2013
6 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7 * (Egervary Research Group on Combinatorial Optimization, EGRES).
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.
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
20 #include <lemon/lp_skeleton.h>
21 #include "test_tools.h"
22 #include <lemon/tolerance.h>
23 #include <lemon/concept_check.h>
24 #include <lemon/config.h>
26 #ifdef LEMON_HAVE_GLPK
27 #include <lemon/glpk.h>
30 #ifdef LEMON_HAVE_CPLEX
31 #include <lemon/cplex.h>
34 #ifdef LEMON_HAVE_SOPLEX
35 #include <lemon/soplex.h>
39 #include <lemon/clp.h>
45 using namespace lemon;
47 int countCols(LpBase & lp) {
49 for (LpBase::ColIt c(lp); c!=INVALID; ++c) ++count;
52 for(auto c: lp.cols()) { cnt++; ::lemon::ignore_unused_variable_warning(c); }
53 check(count == cnt, "Wrong STL iterator");
58 int countRows(LpBase & lp) {
60 for (LpBase::RowIt r(lp); r!=INVALID; ++r) ++count;
63 for(auto r: lp.rows()) { cnt++; ::lemon::ignore_unused_variable_warning(r); }
64 check(count == cnt, "Wrong STL iterator");
70 void lpTest(LpSolver& lp)
75 // Test LpBase::clear()
76 check(countRows(lp)==0, "Wrong number of rows");
77 check(countCols(lp)==0, "Wrong number of cols");
78 lp.addCol(); lp.addRow(); lp.addRow();
79 check(countRows(lp)==2, "Wrong number of rows");
80 check(countCols(lp)==1, "Wrong number of cols");
82 check(countRows(lp)==0, "Wrong number of rows");
83 check(countCols(lp)==0, "Wrong number of cols");
84 lp.addCol(); lp.addCol(); lp.addCol(); lp.addRow();
85 check(countRows(lp)==1, "Wrong number of rows");
86 check(countCols(lp)==3, "Wrong number of cols");
89 std::vector<LP::Col> x(10);
90 // for(int i=0;i<10;i++) x.push_back(lp.addCol());
92 lp.colLowerBound(x,1);
93 lp.colUpperBound(x,1);
96 std::vector<LP::Col> y(10);
99 lp.colLowerBound(y,1);
100 lp.colUpperBound(y,1);
103 std::map<int,LP::Col> z;
105 z.insert(std::make_pair(12,INVALID));
106 z.insert(std::make_pair(2,INVALID));
107 z.insert(std::make_pair(7,INVALID));
108 z.insert(std::make_pair(5,INVALID));
112 lp.colLowerBound(z,1);
113 lp.colUpperBound(z,1);
118 LP::Col p1,p2,p3,p4,p5;
154 e=((p1+p2)+(p1-p2)+(p1+12)+(12+p1)+(p1-12)+(12-p1)+
155 (f+12)+(12+f)+(p1+f)+(f+p1)+(f+g)+
156 (f-12)+(12-f)+(p1-f)+(f-p1)+(f-g)+
159 2.2*p1+p1*2.2+p1/2.2+
204 c = ((2 <= p1) <= 3);
207 c = ((2 >= p1) >= 3);
210 LP::Col v=lp.addCol();
211 LP::Constr c = v >= -3;
214 #if ( __GNUC__ == 4 ) && ( __GNUC_MINOR__ == 3 )
215 c2 = ( -3 <= v ) <= 4;
227 lp.addRow(-LP::INF,e,23);
228 lp.addRow(-LP::INF,3.0*(x[1]+x[2]/2)-x[3],23);
229 lp.addRow(-LP::INF,3.0*(x[1]+x[2]*2-5*x[3]+12-x[4]/3)+2*x[4]-4,23);
231 lp.addRow(x[1]+x[3]<=x[5]-3);
232 lp.addRow((-7<=x[1]+x[3]-12)<=3);
233 lp.addRow(x[1]<=x[5]);
235 std::ostringstream buf;
238 e=((p1+p2)+(p1-0.99*p2));
239 //e.prettyPrint(std::cout);
240 //(e<=2).prettyPrint(std::cout);
241 double tolerance=0.001;
242 e.simplify(tolerance);
243 buf << "Coeff. of p2 should be 0.01";
244 check(e[p2]>0, buf.str());
247 e.simplify(tolerance);
248 buf << "Coeff. of p2 should be 0";
249 check(const_cast<const LpSolver::Expr&>(e)[p2]==0, buf.str());
252 LP* lpnew = lp.newSolver();
253 LP* lpclone = lp.cloneSolver();
261 LP::Row p1 = INVALID, p2 = INVALID;
286 2.2*p1+p1*2.2+p1/2.2+
293 void solveAndCheck(LpSolver& lp, LpSolver::ProblemType stat,
298 std::ostringstream buf;
299 buf << "PrimalType should be: " << int(stat) << int(lp.primalType());
301 check(lp.primalType()==stat, buf.str());
303 if (stat == LpSolver::OPTIMAL) {
304 std::ostringstream sbuf;
305 sbuf << "Wrong optimal value (" << lp.primal() <<") with "
306 << lp.solverName() <<"\n the right optimum is " << exp_opt;
307 check(std::abs(lp.primal()-exp_opt) < 1e-3, sbuf.str());
311 void aTest(LpSolver & lp)
315 //The following example is very simple
317 typedef LpSolver::Row Row;
318 typedef LpSolver::Col Col;
321 Col x1 = lp.addCol();
322 Col x2 = lp.addCol();
326 Row upright=lp.addRow(x1+2*x2 <=1);
327 lp.addRow(x1+x2 >=-1);
328 lp.addRow(x1-x2 <=1);
329 lp.addRow(x1-x2 >=-1);
330 //Nonnegativity of the variables
331 lp.colLowerBound(x1, 0);
332 lp.colLowerBound(x2, 0);
338 //Testing the problem retrieving routines
339 check(lp.objCoeff(x1)==1,"First term should be 1 in the obj function!");
340 check(lp.sense() == lp.MAX,"This is a maximization!");
341 check(lp.coeff(upright,x1)==1,"The coefficient in question is 1!");
342 check(lp.coeff(upright,x2)==2,"The coefficient in question is 1!");
343 check(lp.colLowerBound(x1)==0,
344 "The lower bound for variable x1 should be 0.");
345 check(lp.colUpperBound(x1)==LpSolver::INF,
346 "The upper bound for variable x1 should be infty.");
347 check(lp.rowLowerBound(upright) == -LpSolver::INF,
348 "The lower bound for the first row should be -infty.");
349 check(lp.rowUpperBound(upright)==1,
350 "The upper bound for the first row should be 1.");
351 LpSolver::Expr e = lp.row(upright);
352 check(e[x1] == 1, "The first coefficient should 1.");
353 check(e[x2] == 2, "The second coefficient should 1.");
355 lp.row(upright, x1+x2 <=1);
357 check(e[x1] == 1, "The first coefficient should 1.");
358 check(e[x2] == 1, "The second coefficient should 1.");
360 LpSolver::DualExpr de = lp.col(x1);
361 check( de[upright] == 1, "The first coefficient should 1.");
363 LpSolver* clp = lp.cloneSolver();
365 //Testing the problem retrieving routines
366 check(clp->objCoeff(x1)==1,"First term should be 1 in the obj function!");
367 check(clp->sense() == clp->MAX,"This is a maximization!");
368 check(clp->coeff(upright,x1)==1,"The coefficient in question is 1!");
369 // std::cout<<lp.colLowerBound(x1)<<std::endl;
370 check(clp->colLowerBound(x1)==0,
371 "The lower bound for variable x1 should be 0.");
372 check(clp->colUpperBound(x1)==LpSolver::INF,
373 "The upper bound for variable x1 should be infty.");
375 check(lp.rowLowerBound(upright)==-LpSolver::INF,
376 "The lower bound for the first row should be -infty.");
377 check(lp.rowUpperBound(upright)==1,
378 "The upper bound for the first row should be 1.");
379 e = clp->row(upright);
380 check(e[x1] == 1, "The first coefficient should 1.");
381 check(e[x2] == 1, "The second coefficient should 1.");
384 check(de[upright] == 1, "The first coefficient should 1.");
388 //Maximization of x1+x2
389 //over the triangle with vertices (0,0) (0,1) (1,0)
390 double expected_opt=1;
391 solveAndCheck(lp, LpSolver::OPTIMAL, expected_opt);
396 solveAndCheck(lp, LpSolver::OPTIMAL, expected_opt);
398 //Vertex (-1,0) instead of (0,0)
399 lp.colLowerBound(x1, -LpSolver::INF);
401 solveAndCheck(lp, LpSolver::OPTIMAL, expected_opt);
403 //Erase one constraint and return to maximization
406 expected_opt=LpSolver::INF;
407 solveAndCheck(lp, LpSolver::UNBOUNDED, expected_opt);
410 lp.addRow(x1+x2 <=-2);
411 solveAndCheck(lp, LpSolver::INFEASIBLE, expected_opt);
421 LP* lpnew = lp->newSolver();
422 LP* lpclone = lp->cloneSolver();
429 void rangeConstraintTest()
432 // Add two columns (variables) to the problem
433 typename LP::Col x1 = lp.addCol();
434 typename LP::Col x2 = lp.addCol();
435 // Add rows (constraints) to the problem
436 lp.addRow(x1 - 5 <= x2);
437 lp.addRow(0 <= 2 * x1 + x2 <= 25);
439 // Set lower and upper bounds for the columns (variables)
440 lp.colLowerBound(x1, 0);
441 lp.colUpperBound(x2, 10);
443 // Specify the objective function
445 lp.obj(5 * x1 + 3 * x2);
447 // Solve the problem using the underlying LP solver
450 check(lp.primalType() == LP::OPTIMAL, "Optimal solution is not found");
451 check(lp.primal() <= 67.501 && lp.primal() >= 67.499, "Wrong objective value");
452 check(lp.primal(x1) <= 7.501 && lp.primal(x1) >= 7.499, "Wrong value for x1");
453 check(lp.primal(x2) <= 10.001 && lp.primal(x2) >= 9.999, "Wrong value for x2");
470 #ifdef LEMON_HAVE_GLPK
472 GlpkLp lp_glpk1,lp_glpk2;
476 rangeConstraintTest<GlpkLp>();
480 #ifdef LEMON_HAVE_CPLEX
482 CplexLp lp_cplex1,lp_cplex2;
485 cloneTest<CplexLp>();
486 rangeConstraintTest<CplexLp>();
487 } catch (CplexEnv::LicenseError& error) {
488 check(false, error.what());
492 #ifdef LEMON_HAVE_SOPLEX
494 SoplexLp lp_soplex1,lp_soplex2;
497 cloneTest<SoplexLp>();
498 rangeConstraintTest<Soplex>();
502 #ifdef LEMON_HAVE_CLP
504 ClpLp lp_clp1,lp_clp2;
508 rangeConstraintTest<ClpLp>();