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);
170 LP::Col v=lp.addCol();
171 LP::Constr c = v >= -3;
182 lp.addRow(-LP::INF,e,23);
183 lp.addRow(-LP::INF,3.0*(x[1]+x[2]/2)-x[3],23);
184 lp.addRow(-LP::INF,3.0*(x[1]+x[2]*2-5*x[3]+12-x[4]/3)+2*x[4]-4,23);
186 lp.addRow(x[1]+x[3]<=x[5]-3);
187 lp.addRow((-7<=x[1]+x[3]-12)<=3);
188 lp.addRow(x[1]<=x[5]);
190 std::ostringstream buf;
193 e=((p1+p2)+(p1-0.99*p2));
194 //e.prettyPrint(std::cout);
195 //(e<=2).prettyPrint(std::cout);
196 double tolerance=0.001;
197 e.simplify(tolerance);
198 buf << "Coeff. of p2 should be 0.01";
199 check(e[p2]>0, buf.str());
202 e.simplify(tolerance);
203 buf << "Coeff. of p2 should be 0";
204 check(const_cast<const LpSolver::Expr&>(e)[p2]==0, buf.str());
207 LP* lpnew = lp.newSolver();
208 LP* lpclone = lp.cloneSolver();
216 LP::Row p1 = INVALID, p2 = INVALID, p3 = INVALID,
217 p4 = INVALID, p5 = INVALID;
242 2.2*p1+p1*2.2+p1/2.2+
249 void solveAndCheck(LpSolver& lp, LpSolver::ProblemType stat,
254 std::ostringstream buf;
255 buf << "PrimalType should be: " << int(stat) << int(lp.primalType());
257 check(lp.primalType()==stat, buf.str());
259 if (stat == LpSolver::OPTIMAL) {
260 std::ostringstream sbuf;
261 sbuf << "Wrong optimal value (" << lp.primal() <<") with "
262 << lp.solverName() <<"\n the right optimum is " << exp_opt;
263 check(std::abs(lp.primal()-exp_opt) < 1e-3, sbuf.str());
267 void aTest(LpSolver & lp)
271 //The following example is very simple
273 typedef LpSolver::Row Row;
274 typedef LpSolver::Col Col;
277 Col x1 = lp.addCol();
278 Col x2 = lp.addCol();
282 Row upright=lp.addRow(x1+2*x2 <=1);
283 lp.addRow(x1+x2 >=-1);
284 lp.addRow(x1-x2 <=1);
285 lp.addRow(x1-x2 >=-1);
286 //Nonnegativity of the variables
287 lp.colLowerBound(x1, 0);
288 lp.colLowerBound(x2, 0);
294 //Testing the problem retrieving routines
295 check(lp.objCoeff(x1)==1,"First term should be 1 in the obj function!");
296 check(lp.sense() == lp.MAX,"This is a maximization!");
297 check(lp.coeff(upright,x1)==1,"The coefficient in question is 1!");
298 check(lp.colLowerBound(x1)==0,
299 "The lower bound for variable x1 should be 0.");
300 check(lp.colUpperBound(x1)==LpSolver::INF,
301 "The upper bound for variable x1 should be infty.");
302 check(lp.rowLowerBound(upright) == -LpSolver::INF,
303 "The lower bound for the first row should be -infty.");
304 check(lp.rowUpperBound(upright)==1,
305 "The upper bound for the first row should be 1.");
306 LpSolver::Expr e = lp.row(upright);
307 check(e[x1] == 1, "The first coefficient should 1.");
308 check(e[x2] == 2, "The second coefficient should 1.");
310 lp.row(upright, x1+x2 <=1);
312 check(e[x1] == 1, "The first coefficient should 1.");
313 check(e[x2] == 1, "The second coefficient should 1.");
315 LpSolver::DualExpr de = lp.col(x1);
316 check( de[upright] == 1, "The first coefficient should 1.");
318 LpSolver* clp = lp.cloneSolver();
320 //Testing the problem retrieving routines
321 check(clp->objCoeff(x1)==1,"First term should be 1 in the obj function!");
322 check(clp->sense() == clp->MAX,"This is a maximization!");
323 check(clp->coeff(upright,x1)==1,"The coefficient in question is 1!");
324 // std::cout<<lp.colLowerBound(x1)<<std::endl;
325 check(clp->colLowerBound(x1)==0,
326 "The lower bound for variable x1 should be 0.");
327 check(clp->colUpperBound(x1)==LpSolver::INF,
328 "The upper bound for variable x1 should be infty.");
330 check(lp.rowLowerBound(upright)==-LpSolver::INF,
331 "The lower bound for the first row should be -infty.");
332 check(lp.rowUpperBound(upright)==1,
333 "The upper bound for the first row should be 1.");
334 e = clp->row(upright);
335 check(e[x1] == 1, "The first coefficient should 1.");
336 check(e[x2] == 1, "The second coefficient should 1.");
339 check(de[upright] == 1, "The first coefficient should 1.");
343 //Maximization of x1+x2
344 //over the triangle with vertices (0,0) (0,1) (1,0)
345 double expected_opt=1;
346 solveAndCheck(lp, LpSolver::OPTIMAL, expected_opt);
351 solveAndCheck(lp, LpSolver::OPTIMAL, expected_opt);
353 //Vertex (-1,0) instead of (0,0)
354 lp.colLowerBound(x1, -LpSolver::INF);
356 solveAndCheck(lp, LpSolver::OPTIMAL, expected_opt);
358 //Erase one constraint and return to maximization
361 expected_opt=LpSolver::INF;
362 solveAndCheck(lp, LpSolver::UNBOUNDED, expected_opt);
365 lp.addRow(x1+x2 <=-2);
366 solveAndCheck(lp, LpSolver::INFEASIBLE, expected_opt);
376 LP* lpnew = lp->newSolver();
377 LP* lpclone = lp->cloneSolver();
388 #ifdef LEMON_HAVE_GLPK
390 GlpkLp lp_glpk1,lp_glpk2;
397 #ifdef LEMON_HAVE_CPLEX
399 CplexLp lp_cplex1,lp_cplex2;
402 cloneTest<CplexLp>();
403 } catch (CplexEnv::LicenseError& error) {
404 check(false, error.what());
408 #ifdef LEMON_HAVE_SOPLEX
410 SoplexLp lp_soplex1,lp_soplex2;
413 cloneTest<SoplexLp>();
417 #ifdef LEMON_HAVE_CLP
419 ClpLp lp_clp1,lp_clp2;