Configurable glpk prefix in ./scripts/bootstrap.sh and ...
unneeded solver backends are explicitely switched off with --without-*
1 /* -*- mode: C++; indent-tabs-mode: nil; -*-
3 * This file is a part of LEMON, a generic C++ optimization library.
5 * Copyright (C) 2003-2009
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>
42 using namespace lemon;
44 void lpTest(LpSolver& lp)
49 std::vector<LP::Col> x(10);
50 // for(int i=0;i<10;i++) x.push_back(lp.addCol());
52 lp.colLowerBound(x,1);
53 lp.colUpperBound(x,1);
56 std::vector<LP::Col> y(10);
59 lp.colLowerBound(y,1);
60 lp.colUpperBound(y,1);
63 std::map<int,LP::Col> z;
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));
72 lp.colLowerBound(z,1);
73 lp.colUpperBound(z,1);
78 LP::Col p1,p2,p3,p4,p5;
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)+
119 2.2*p1+p1*2.2+p1/2.2+
164 c = ((2 <= p1) <= 3);
167 c = ((2 >= p1) >= 3);
174 lp.addRow(-LP::INF,e,23);
175 lp.addRow(-LP::INF,3.0*(x[1]+x[2]/2)-x[3],23);
176 lp.addRow(-LP::INF,3.0*(x[1]+x[2]*2-5*x[3]+12-x[4]/3)+2*x[4]-4,23);
178 lp.addRow(x[1]+x[3]<=x[5]-3);
179 lp.addRow((-7<=x[1]+x[3]-12)<=3);
180 lp.addRow(x[1]<=x[5]);
182 std::ostringstream buf;
185 e=((p1+p2)+(p1-0.99*p2));
186 //e.prettyPrint(std::cout);
187 //(e<=2).prettyPrint(std::cout);
188 double tolerance=0.001;
189 e.simplify(tolerance);
190 buf << "Coeff. of p2 should be 0.01";
191 check(e[p2]>0, buf.str());
194 e.simplify(tolerance);
195 buf << "Coeff. of p2 should be 0";
196 check(const_cast<const LpSolver::Expr&>(e)[p2]==0, buf.str());
199 LP* lpnew = lp.newSolver();
200 LP* lpclone = lp.cloneSolver();
208 LP::Row p1 = INVALID, p2 = INVALID, p3 = INVALID,
209 p4 = INVALID, p5 = INVALID;
234 2.2*p1+p1*2.2+p1/2.2+
241 void solveAndCheck(LpSolver& lp, LpSolver::ProblemType stat,
246 std::ostringstream buf;
247 buf << "PrimalType should be: " << int(stat) << int(lp.primalType());
249 check(lp.primalType()==stat, buf.str());
251 if (stat == LpSolver::OPTIMAL) {
252 std::ostringstream sbuf;
253 sbuf << "Wrong optimal value (" << lp.primal() <<") with "
254 << lp.solverName() <<"\n the right optimum is " << exp_opt;
255 check(std::abs(lp.primal()-exp_opt) < 1e-3, sbuf.str());
259 void aTest(LpSolver & lp)
263 //The following example is very simple
265 typedef LpSolver::Row Row;
266 typedef LpSolver::Col Col;
269 Col x1 = lp.addCol();
270 Col x2 = lp.addCol();
274 Row upright=lp.addRow(x1+2*x2 <=1);
275 lp.addRow(x1+x2 >=-1);
276 lp.addRow(x1-x2 <=1);
277 lp.addRow(x1-x2 >=-1);
278 //Nonnegativity of the variables
279 lp.colLowerBound(x1, 0);
280 lp.colLowerBound(x2, 0);
286 //Testing the problem retrieving routines
287 check(lp.objCoeff(x1)==1,"First term should be 1 in the obj function!");
288 check(lp.sense() == lp.MAX,"This is a maximization!");
289 check(lp.coeff(upright,x1)==1,"The coefficient in question is 1!");
290 check(lp.colLowerBound(x1)==0,
291 "The lower bound for variable x1 should be 0.");
292 check(lp.colUpperBound(x1)==LpSolver::INF,
293 "The upper bound for variable x1 should be infty.");
294 check(lp.rowLowerBound(upright) == -LpSolver::INF,
295 "The lower bound for the first row should be -infty.");
296 check(lp.rowUpperBound(upright)==1,
297 "The upper bound for the first row should be 1.");
298 LpSolver::Expr e = lp.row(upright);
299 check(e[x1] == 1, "The first coefficient should 1.");
300 check(e[x2] == 2, "The second coefficient should 1.");
302 lp.row(upright, x1+x2 <=1);
304 check(e[x1] == 1, "The first coefficient should 1.");
305 check(e[x2] == 1, "The second coefficient should 1.");
307 LpSolver::DualExpr de = lp.col(x1);
308 check( de[upright] == 1, "The first coefficient should 1.");
310 LpSolver* clp = lp.cloneSolver();
312 //Testing the problem retrieving routines
313 check(clp->objCoeff(x1)==1,"First term should be 1 in the obj function!");
314 check(clp->sense() == clp->MAX,"This is a maximization!");
315 check(clp->coeff(upright,x1)==1,"The coefficient in question is 1!");
316 // std::cout<<lp.colLowerBound(x1)<<std::endl;
317 check(clp->colLowerBound(x1)==0,
318 "The lower bound for variable x1 should be 0.");
319 check(clp->colUpperBound(x1)==LpSolver::INF,
320 "The upper bound for variable x1 should be infty.");
322 check(lp.rowLowerBound(upright)==-LpSolver::INF,
323 "The lower bound for the first row should be -infty.");
324 check(lp.rowUpperBound(upright)==1,
325 "The upper bound for the first row should be 1.");
326 e = clp->row(upright);
327 check(e[x1] == 1, "The first coefficient should 1.");
328 check(e[x2] == 1, "The second coefficient should 1.");
331 check(de[upright] == 1, "The first coefficient should 1.");
335 //Maximization of x1+x2
336 //over the triangle with vertices (0,0) (0,1) (1,0)
337 double expected_opt=1;
338 solveAndCheck(lp, LpSolver::OPTIMAL, expected_opt);
343 solveAndCheck(lp, LpSolver::OPTIMAL, expected_opt);
345 //Vertex (-1,0) instead of (0,0)
346 lp.colLowerBound(x1, -LpSolver::INF);
348 solveAndCheck(lp, LpSolver::OPTIMAL, expected_opt);
350 //Erase one constraint and return to maximization
353 expected_opt=LpSolver::INF;
354 solveAndCheck(lp, LpSolver::UNBOUNDED, expected_opt);
357 lp.addRow(x1+x2 <=-2);
358 solveAndCheck(lp, LpSolver::INFEASIBLE, expected_opt);
368 LP* lpnew = lp->newSolver();
369 LP* lpclone = lp->cloneSolver();
380 #ifdef LEMON_HAVE_GLPK
382 GlpkLp lp_glpk1,lp_glpk2;
389 #ifdef LEMON_HAVE_CPLEX
391 CplexLp lp_cplex1,lp_cplex2;
394 cloneTest<CplexLp>();
395 } catch (CplexEnv::LicenseError& error) {
396 check(false, error.what());
400 #ifdef LEMON_HAVE_SOPLEX
402 SoplexLp lp_soplex1,lp_soplex2;
405 cloneTest<SoplexLp>();
409 #ifdef LEMON_HAVE_CLP
411 ClpLp lp_clp1,lp_clp2;