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());
205 LP::Row p1 = INVALID, p2 = INVALID, p3 = INVALID,
206 p4 = INVALID, p5 = INVALID;
231 2.2*p1+p1*2.2+p1/2.2+
238 void solveAndCheck(LpSolver& lp, LpSolver::ProblemType stat,
243 std::ostringstream buf;
244 buf << "PrimalType should be: " << int(stat) << int(lp.primalType());
246 check(lp.primalType()==stat, buf.str());
248 if (stat == LpSolver::OPTIMAL) {
249 std::ostringstream sbuf;
250 sbuf << "Wrong optimal value: the right optimum is " << exp_opt;
251 check(std::abs(lp.primal()-exp_opt) < 1e-3, sbuf.str());
255 void aTest(LpSolver & lp)
259 //The following example is very simple
261 typedef LpSolver::Row Row;
262 typedef LpSolver::Col Col;
265 Col x1 = lp.addCol();
266 Col x2 = lp.addCol();
270 Row upright=lp.addRow(x1+2*x2 <=1);
271 lp.addRow(x1+x2 >=-1);
272 lp.addRow(x1-x2 <=1);
273 lp.addRow(x1-x2 >=-1);
274 //Nonnegativity of the variables
275 lp.colLowerBound(x1, 0);
276 lp.colLowerBound(x2, 0);
282 //Testing the problem retrieving routines
283 check(lp.objCoeff(x1)==1,"First term should be 1 in the obj function!");
284 check(lp.sense() == lp.MAX,"This is a maximization!");
285 check(lp.coeff(upright,x1)==1,"The coefficient in question is 1!");
286 check(lp.colLowerBound(x1)==0,
287 "The lower bound for variable x1 should be 0.");
288 check(lp.colUpperBound(x1)==LpSolver::INF,
289 "The upper bound for variable x1 should be infty.");
290 check(lp.rowLowerBound(upright) == -LpSolver::INF,
291 "The lower bound for the first row should be -infty.");
292 check(lp.rowUpperBound(upright)==1,
293 "The upper bound for the first row should be 1.");
294 LpSolver::Expr e = lp.row(upright);
295 check(e[x1] == 1, "The first coefficient should 1.");
296 check(e[x2] == 2, "The second coefficient should 1.");
298 lp.row(upright, x1+x2 <=1);
300 check(e[x1] == 1, "The first coefficient should 1.");
301 check(e[x2] == 1, "The second coefficient should 1.");
303 LpSolver::DualExpr de = lp.col(x1);
304 check( de[upright] == 1, "The first coefficient should 1.");
306 LpSolver* clp = lp.cloneSolver();
308 //Testing the problem retrieving routines
309 check(clp->objCoeff(x1)==1,"First term should be 1 in the obj function!");
310 check(clp->sense() == clp->MAX,"This is a maximization!");
311 check(clp->coeff(upright,x1)==1,"The coefficient in question is 1!");
312 // std::cout<<lp.colLowerBound(x1)<<std::endl;
313 check(clp->colLowerBound(x1)==0,
314 "The lower bound for variable x1 should be 0.");
315 check(clp->colUpperBound(x1)==LpSolver::INF,
316 "The upper bound for variable x1 should be infty.");
318 check(lp.rowLowerBound(upright)==-LpSolver::INF,
319 "The lower bound for the first row should be -infty.");
320 check(lp.rowUpperBound(upright)==1,
321 "The upper bound for the first row should be 1.");
322 e = clp->row(upright);
323 check(e[x1] == 1, "The first coefficient should 1.");
324 check(e[x2] == 1, "The second coefficient should 1.");
327 check(de[upright] == 1, "The first coefficient should 1.");
331 //Maximization of x1+x2
332 //over the triangle with vertices (0,0) (0,1) (1,0)
333 double expected_opt=1;
334 solveAndCheck(lp, LpSolver::OPTIMAL, expected_opt);
339 solveAndCheck(lp, LpSolver::OPTIMAL, expected_opt);
341 //Vertex (-1,0) instead of (0,0)
342 lp.colLowerBound(x1, -LpSolver::INF);
344 solveAndCheck(lp, LpSolver::OPTIMAL, expected_opt);
346 //Erase one constraint and return to maximization
349 expected_opt=LpSolver::INF;
350 solveAndCheck(lp, LpSolver::UNBOUNDED, expected_opt);
353 lp.addRow(x1+x2 <=-2);
354 solveAndCheck(lp, LpSolver::INFEASIBLE, expected_opt);
365 GlpkLp lp_glpk1,lp_glpk2;
373 CplexLp lp_cplex1,lp_cplex2;
376 } catch (CplexEnv::LicenseError& error) {
377 #ifdef LEMON_FORCE_CPLEX_CHECK
378 check(false, error.what());
380 std::cerr << error.what() << std::endl;
381 std::cerr << "Cplex license check failed, lp check skipped" << std::endl;
388 SoplexLp lp_soplex1,lp_soplex2;
396 ClpLp lp_clp1,lp_clp2;