1 /* -*- mode: C++; indent-tabs-mode: nil; -*-
3 * This file is a part of LEMON, a generic C++ optimization library.
5 * Copyright (C) 2003-2008
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>
25 #include <lemon/config.h>
29 #include <lemon/glpk.h>
33 #include <lemon/cplex.h>
37 #include <lemon/soplex.h>
41 #include <lemon/clp.h>
44 using namespace lemon;
46 void lpTest(LpSolver& lp)
51 std::vector<LP::Col> x(10);
52 // for(int i=0;i<10;i++) x.push_back(lp.addCol());
54 lp.colLowerBound(x,1);
55 lp.colUpperBound(x,1);
58 std::vector<LP::Col> y(10);
61 lp.colLowerBound(y,1);
62 lp.colUpperBound(y,1);
65 std::map<int,LP::Col> z;
67 z.insert(std::make_pair(12,INVALID));
68 z.insert(std::make_pair(2,INVALID));
69 z.insert(std::make_pair(7,INVALID));
70 z.insert(std::make_pair(5,INVALID));
74 lp.colLowerBound(z,1);
75 lp.colUpperBound(z,1);
80 LP::Col p1,p2,p3,p4,p5;
116 e=((p1+p2)+(p1-p2)+(p1+12)+(12+p1)+(p1-12)+(12-p1)+
117 (f+12)+(12+f)+(p1+f)+(f+p1)+(f+g)+
118 (f-12)+(12-f)+(p1-f)+(f-p1)+(f-g)+
121 2.2*p1+p1*2.2+p1/2.2+
166 c = ((2 <= p1) <= 3);
169 c = ((2 >= p1) >= 3);
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]*2-5*x[3]+12-x[4]/3)+2*x[4]-4,23);
180 lp.addRow(x[1]+x[3]<=x[5]-3);
181 lp.addRow((-7<=x[1]+x[3]-12)<=3);
182 lp.addRow(x[1]<=x[5]);
184 std::ostringstream buf;
187 e=((p1+p2)+(p1-0.99*p2));
188 //e.prettyPrint(std::cout);
189 //(e<=2).prettyPrint(std::cout);
190 double tolerance=0.001;
191 e.simplify(tolerance);
192 buf << "Coeff. of p2 should be 0.01";
193 check(e[p2]>0, buf.str());
196 e.simplify(tolerance);
197 buf << "Coeff. of p2 should be 0";
198 check(const_cast<const LpSolver::Expr&>(e)[p2]==0, buf.str());
201 LP* lpnew = lp.newSolver();
202 LP* lpclone = lp.cloneSolver();
210 LP::Row p1 = INVALID, p2 = INVALID, p3 = INVALID,
211 p4 = INVALID, p5 = INVALID;
236 2.2*p1+p1*2.2+p1/2.2+
243 void solveAndCheck(LpSolver& lp, LpSolver::ProblemType stat,
248 std::ostringstream buf;
249 buf << "PrimalType should be: " << int(stat) << int(lp.primalType());
251 check(lp.primalType()==stat, buf.str());
253 if (stat == LpSolver::OPTIMAL) {
254 std::ostringstream sbuf;
255 sbuf << "Wrong optimal value (" << lp.primal() <<") with "
256 << lp.solverName() <<"\n the right optimum is " << exp_opt;
257 check(std::abs(lp.primal()-exp_opt) < 1e-3, sbuf.str());
261 void aTest(LpSolver & lp)
265 //The following example is very simple
267 typedef LpSolver::Row Row;
268 typedef LpSolver::Col Col;
271 Col x1 = lp.addCol();
272 Col x2 = lp.addCol();
276 Row upright=lp.addRow(x1+2*x2 <=1);
277 lp.addRow(x1+x2 >=-1);
278 lp.addRow(x1-x2 <=1);
279 lp.addRow(x1-x2 >=-1);
280 //Nonnegativity of the variables
281 lp.colLowerBound(x1, 0);
282 lp.colLowerBound(x2, 0);
288 //Testing the problem retrieving routines
289 check(lp.objCoeff(x1)==1,"First term should be 1 in the obj function!");
290 check(lp.sense() == lp.MAX,"This is a maximization!");
291 check(lp.coeff(upright,x1)==1,"The coefficient in question is 1!");
292 check(lp.colLowerBound(x1)==0,
293 "The lower bound for variable x1 should be 0.");
294 check(lp.colUpperBound(x1)==LpSolver::INF,
295 "The upper bound for variable x1 should be infty.");
296 check(lp.rowLowerBound(upright) == -LpSolver::INF,
297 "The lower bound for the first row should be -infty.");
298 check(lp.rowUpperBound(upright)==1,
299 "The upper bound for the first row should be 1.");
300 LpSolver::Expr e = lp.row(upright);
301 check(e[x1] == 1, "The first coefficient should 1.");
302 check(e[x2] == 2, "The second coefficient should 1.");
304 lp.row(upright, x1+x2 <=1);
306 check(e[x1] == 1, "The first coefficient should 1.");
307 check(e[x2] == 1, "The second coefficient should 1.");
309 LpSolver::DualExpr de = lp.col(x1);
310 check( de[upright] == 1, "The first coefficient should 1.");
312 LpSolver* clp = lp.cloneSolver();
314 //Testing the problem retrieving routines
315 check(clp->objCoeff(x1)==1,"First term should be 1 in the obj function!");
316 check(clp->sense() == clp->MAX,"This is a maximization!");
317 check(clp->coeff(upright,x1)==1,"The coefficient in question is 1!");
318 // std::cout<<lp.colLowerBound(x1)<<std::endl;
319 check(clp->colLowerBound(x1)==0,
320 "The lower bound for variable x1 should be 0.");
321 check(clp->colUpperBound(x1)==LpSolver::INF,
322 "The upper bound for variable x1 should be infty.");
324 check(lp.rowLowerBound(upright)==-LpSolver::INF,
325 "The lower bound for the first row should be -infty.");
326 check(lp.rowUpperBound(upright)==1,
327 "The upper bound for the first row should be 1.");
328 e = clp->row(upright);
329 check(e[x1] == 1, "The first coefficient should 1.");
330 check(e[x2] == 1, "The second coefficient should 1.");
333 check(de[upright] == 1, "The first coefficient should 1.");
337 //Maximization of x1+x2
338 //over the triangle with vertices (0,0) (0,1) (1,0)
339 double expected_opt=1;
340 solveAndCheck(lp, LpSolver::OPTIMAL, expected_opt);
345 solveAndCheck(lp, LpSolver::OPTIMAL, expected_opt);
347 //Vertex (-1,0) instead of (0,0)
348 lp.colLowerBound(x1, -LpSolver::INF);
350 solveAndCheck(lp, LpSolver::OPTIMAL, expected_opt);
352 //Erase one constraint and return to maximization
355 expected_opt=LpSolver::INF;
356 solveAndCheck(lp, LpSolver::UNBOUNDED, expected_opt);
359 lp.addRow(x1+x2 <=-2);
360 solveAndCheck(lp, LpSolver::INFEASIBLE, expected_opt);
370 LP* lpnew = lp->newSolver();
371 LP* lpclone = lp->cloneSolver();
384 GlpkLp lp_glpk1,lp_glpk2;
393 CplexLp lp_cplex1,lp_cplex2;
396 } catch (CplexEnv::LicenseError& error) {
397 #ifdef LEMON_FORCE_CPLEX_CHECK
398 check(false, error.what());
400 std::cerr << error.what() << std::endl;
401 std::cerr << "Cplex license check failed, lp check skipped" << std::endl;
404 cloneTest<CplexLp>();
409 SoplexLp lp_soplex1,lp_soplex2;
412 cloneTest<SoplexLp>();
418 ClpLp lp_clp1,lp_clp2;