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>
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;
53 int countRows(LpBase & lp) {
55 for (LpBase::RowIt r(lp); r!=INVALID; ++r) ++count;
60 void lpTest(LpSolver& lp)
65 // Test LpBase::clear()
66 check(countRows(lp)==0, "Wrong number of rows");
67 check(countCols(lp)==0, "Wrong number of cols");
68 lp.addCol(); lp.addRow(); lp.addRow();
69 check(countRows(lp)==2, "Wrong number of rows");
70 check(countCols(lp)==1, "Wrong number of cols");
72 check(countRows(lp)==0, "Wrong number of rows");
73 check(countCols(lp)==0, "Wrong number of cols");
74 lp.addCol(); lp.addCol(); lp.addCol(); lp.addRow();
75 check(countRows(lp)==1, "Wrong number of rows");
76 check(countCols(lp)==3, "Wrong number of cols");
79 std::vector<LP::Col> x(10);
80 // for(int i=0;i<10;i++) x.push_back(lp.addCol());
82 lp.colLowerBound(x,1);
83 lp.colUpperBound(x,1);
86 std::vector<LP::Col> y(10);
89 lp.colLowerBound(y,1);
90 lp.colUpperBound(y,1);
93 std::map<int,LP::Col> z;
95 z.insert(std::make_pair(12,INVALID));
96 z.insert(std::make_pair(2,INVALID));
97 z.insert(std::make_pair(7,INVALID));
98 z.insert(std::make_pair(5,INVALID));
102 lp.colLowerBound(z,1);
103 lp.colUpperBound(z,1);
108 LP::Col p1,p2,p3,p4,p5;
144 e=((p1+p2)+(p1-p2)+(p1+12)+(12+p1)+(p1-12)+(12-p1)+
145 (f+12)+(12+f)+(p1+f)+(f+p1)+(f+g)+
146 (f-12)+(12-f)+(p1-f)+(f-p1)+(f-g)+
149 2.2*p1+p1*2.2+p1/2.2+
194 c = ((2 <= p1) <= 3);
197 c = ((2 >= p1) >= 3);
200 LP::Col v=lp.addCol();
201 LP::Constr c = v >= -3;
204 #if ( __GNUC__ == 4 ) && ( __GNUC_MINOR__ == 3 )
205 c2 = ( -3 <= v ) <= 4;
217 lp.addRow(-LP::INF,e,23);
218 lp.addRow(-LP::INF,3.0*(x[1]+x[2]/2)-x[3],23);
219 lp.addRow(-LP::INF,3.0*(x[1]+x[2]*2-5*x[3]+12-x[4]/3)+2*x[4]-4,23);
221 lp.addRow(x[1]+x[3]<=x[5]-3);
222 lp.addRow((-7<=x[1]+x[3]-12)<=3);
223 lp.addRow(x[1]<=x[5]);
225 std::ostringstream buf;
228 e=((p1+p2)+(p1-0.99*p2));
229 //e.prettyPrint(std::cout);
230 //(e<=2).prettyPrint(std::cout);
231 double tolerance=0.001;
232 e.simplify(tolerance);
233 buf << "Coeff. of p2 should be 0.01";
234 check(e[p2]>0, buf.str());
237 e.simplify(tolerance);
238 buf << "Coeff. of p2 should be 0";
239 check(const_cast<const LpSolver::Expr&>(e)[p2]==0, buf.str());
242 LP* lpnew = lp.newSolver();
243 LP* lpclone = lp.cloneSolver();
251 LP::Row p1 = INVALID, p2 = INVALID;
276 2.2*p1+p1*2.2+p1/2.2+
283 void solveAndCheck(LpSolver& lp, LpSolver::ProblemType stat,
288 std::ostringstream buf;
289 buf << "PrimalType should be: " << int(stat) << int(lp.primalType());
291 check(lp.primalType()==stat, buf.str());
293 if (stat == LpSolver::OPTIMAL) {
294 std::ostringstream sbuf;
295 sbuf << "Wrong optimal value (" << lp.primal() <<") with "
296 << lp.solverName() <<"\n the right optimum is " << exp_opt;
297 check(std::abs(lp.primal()-exp_opt) < 1e-3, sbuf.str());
301 void aTest(LpSolver & lp)
305 //The following example is very simple
307 typedef LpSolver::Row Row;
308 typedef LpSolver::Col Col;
311 Col x1 = lp.addCol();
312 Col x2 = lp.addCol();
316 Row upright=lp.addRow(x1+2*x2 <=1);
317 lp.addRow(x1+x2 >=-1);
318 lp.addRow(x1-x2 <=1);
319 lp.addRow(x1-x2 >=-1);
320 //Nonnegativity of the variables
321 lp.colLowerBound(x1, 0);
322 lp.colLowerBound(x2, 0);
328 //Testing the problem retrieving routines
329 check(lp.objCoeff(x1)==1,"First term should be 1 in the obj function!");
330 check(lp.sense() == lp.MAX,"This is a maximization!");
331 check(lp.coeff(upright,x1)==1,"The coefficient in question is 1!");
332 check(lp.colLowerBound(x1)==0,
333 "The lower bound for variable x1 should be 0.");
334 check(lp.colUpperBound(x1)==LpSolver::INF,
335 "The upper bound for variable x1 should be infty.");
336 check(lp.rowLowerBound(upright) == -LpSolver::INF,
337 "The lower bound for the first row should be -infty.");
338 check(lp.rowUpperBound(upright)==1,
339 "The upper bound for the first row should be 1.");
340 LpSolver::Expr e = lp.row(upright);
341 check(e[x1] == 1, "The first coefficient should 1.");
342 check(e[x2] == 2, "The second coefficient should 1.");
344 lp.row(upright, x1+x2 <=1);
346 check(e[x1] == 1, "The first coefficient should 1.");
347 check(e[x2] == 1, "The second coefficient should 1.");
349 LpSolver::DualExpr de = lp.col(x1);
350 check( de[upright] == 1, "The first coefficient should 1.");
352 LpSolver* clp = lp.cloneSolver();
354 //Testing the problem retrieving routines
355 check(clp->objCoeff(x1)==1,"First term should be 1 in the obj function!");
356 check(clp->sense() == clp->MAX,"This is a maximization!");
357 check(clp->coeff(upright,x1)==1,"The coefficient in question is 1!");
358 // std::cout<<lp.colLowerBound(x1)<<std::endl;
359 check(clp->colLowerBound(x1)==0,
360 "The lower bound for variable x1 should be 0.");
361 check(clp->colUpperBound(x1)==LpSolver::INF,
362 "The upper bound for variable x1 should be infty.");
364 check(lp.rowLowerBound(upright)==-LpSolver::INF,
365 "The lower bound for the first row should be -infty.");
366 check(lp.rowUpperBound(upright)==1,
367 "The upper bound for the first row should be 1.");
368 e = clp->row(upright);
369 check(e[x1] == 1, "The first coefficient should 1.");
370 check(e[x2] == 1, "The second coefficient should 1.");
373 check(de[upright] == 1, "The first coefficient should 1.");
377 //Maximization of x1+x2
378 //over the triangle with vertices (0,0) (0,1) (1,0)
379 double expected_opt=1;
380 solveAndCheck(lp, LpSolver::OPTIMAL, expected_opt);
385 solveAndCheck(lp, LpSolver::OPTIMAL, expected_opt);
387 //Vertex (-1,0) instead of (0,0)
388 lp.colLowerBound(x1, -LpSolver::INF);
390 solveAndCheck(lp, LpSolver::OPTIMAL, expected_opt);
392 //Erase one constraint and return to maximization
395 expected_opt=LpSolver::INF;
396 solveAndCheck(lp, LpSolver::UNBOUNDED, expected_opt);
399 lp.addRow(x1+x2 <=-2);
400 solveAndCheck(lp, LpSolver::INFEASIBLE, expected_opt);
410 LP* lpnew = lp->newSolver();
411 LP* lpclone = lp->cloneSolver();
431 #ifdef LEMON_HAVE_GLPK
433 GlpkLp lp_glpk1,lp_glpk2;
440 #ifdef LEMON_HAVE_CPLEX
442 CplexLp lp_cplex1,lp_cplex2;
445 cloneTest<CplexLp>();
446 } catch (CplexEnv::LicenseError& error) {
447 check(false, error.what());
451 #ifdef LEMON_HAVE_SOPLEX
453 SoplexLp lp_soplex1,lp_soplex2;
456 cloneTest<SoplexLp>();
460 #ifdef LEMON_HAVE_CLP
462 ClpLp lp_clp1,lp_clp2;