Various improvements in NetworkSimplex.
- Faster variant of "Altering Candidate List" pivot rule using make_heap
instead of partial_sort.
- Doc improvements.
- Removing unecessary inline keywords.
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>
23 #include <lemon/lp_utils.h>
26 #include <lemon/config.h>
30 #include <lemon/lp_glpk.h>
34 #include <lemon/lp_cplex.h>
38 #include <lemon/lp_soplex.h>
41 using namespace lemon;
43 void lpTest(LpSolverBase & lp)
48 typedef LpSolverBase LP;
50 std::vector<LP::Col> x(10);
51 // for(int i=0;i<10;i++) x.push_back(lp.addCol());
53 lp.colLowerBound(x,1);
54 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+
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 //Checking the simplify function
189 // //How to check the simplify function? A map gives no information
190 // //on the question whether a given key is or is not stored in it, or
192 // Yes, it does, using the find() function.
195 buf << "Coeff. of p2 should be 0";
196 // std::cout<<e[p1]<<e[p2]<<e[p3]<<std::endl;
197 check(e.find(p2)==e.end(), buf.str());
202 e=((p1+p2)+(p1-0.99*p2));
203 //e.prettyPrint(std::cout);
204 //(e<=2).prettyPrint(std::cout);
205 double tolerance=0.001;
206 e.simplify(tolerance);
207 buf << "Coeff. of p2 should be 0.01";
208 check(e[p2]>0, buf.str());
211 e.simplify(tolerance);
212 buf << "Coeff. of p2 should be 0";
213 check(e.find(p2)==e.end(), buf.str());
220 LP::Row p1 = INVALID, p2 = INVALID, p3 = INVALID,
221 p4 = INVALID, p5 = INVALID;
246 2.2*p1+p1*2.2+p1/2.2+
254 void solveAndCheck(LpSolverBase& lp, LpSolverBase::SolutionStatus stat,
259 std::ostringstream buf;
260 buf << "Primalstatus should be: " << int(stat);
262 // itoa(stat,buf1, 10);
263 check(lp.primalStatus()==stat, buf.str());
265 if (stat == LpSolverBase::OPTIMAL) {
266 std::ostringstream sbuf;
267 sbuf << "Wrong optimal value: the right optimum is " << exp_opt;
268 check(std::abs(lp.primalValue()-exp_opt) < 1e-3, sbuf.str());
273 void aTest(LpSolverBase & lp)
275 typedef LpSolverBase LP;
277 //The following example is very simple
279 typedef LpSolverBase::Row Row;
280 typedef LpSolverBase::Col Col;
283 Col x1 = lp.addCol();
284 Col x2 = lp.addCol();
288 Row upright=lp.addRow(x1+x2 <=1);
289 lp.addRow(x1+x2 >=-1);
290 lp.addRow(x1-x2 <=1);
291 lp.addRow(x1-x2 >=-1);
292 //Nonnegativity of the variables
293 lp.colLowerBound(x1, 0);
294 lp.colLowerBound(x2, 0);
300 //Testing the problem retrieving routines
301 check(lp.objCoeff(x1)==1,"First term should be 1 in the obj function!");
302 check(lp.isMax(),"This is a maximization!");
303 check(lp.coeff(upright,x1)==1,"The coefficient in question is 1!");
304 // std::cout<<lp.colLowerBound(x1)<<std::endl;
305 check( lp.colLowerBound(x1)==0,"The lower bound for variable x1 should be 0.");
306 check( lp.colUpperBound(x1)==LpSolverBase::INF,"The upper bound for variable x1 should be infty.");
307 LpSolverBase::Value lb,ub;
308 lp.getRowBounds(upright,lb,ub);
309 check( lb==-LpSolverBase::INF,"The lower bound for the first row should be -infty.");
310 check( ub==1,"The upper bound for the first row should be 1.");
311 LpSolverBase::Expr e = lp.row(upright);
312 check( e.size() == 2, "The row retrieval gives back wrong expression.");
313 check( e[x1] == 1, "The first coefficient should 1.");
314 check( e[x2] == 1, "The second coefficient should 1.");
316 LpSolverBase::DualExpr de = lp.col(x1);
317 check( de.size() == 4, "The col retrieval gives back wrong expression.");
318 check( de[upright] == 1, "The first coefficient should 1.");
320 LpSolverBase* clp = lp.copyLp();
322 //Testing the problem retrieving routines
323 check(clp->objCoeff(x1)==1,"First term should be 1 in the obj function!");
324 check(clp->isMax(),"This is a maximization!");
325 check(clp->coeff(upright,x1)==1,"The coefficient in question is 1!");
326 // std::cout<<lp.colLowerBound(x1)<<std::endl;
327 check( clp->colLowerBound(x1)==0,"The lower bound for variable x1 should be 0.");
328 std::cerr << clp->colUpperBound(x1) << std::endl;
329 check( clp->colUpperBound(x1)==LpSolverBase::INF,"The upper bound for variable x1 should be infty.");
331 clp->getRowBounds(upright,lb,ub);
332 check( lb==-LpSolverBase::INF,"The lower bound for the first row should be -infty.");
333 check( ub==1,"The upper bound for the first row should be 1.");
334 e = clp->row(upright);
335 check( e.size() == 2, "The row retrieval gives back wrong expression.");
336 check( e[x1] == 1, "The first coefficient should 1.");
337 check( e[x2] == 1, "The second coefficient should 1.");
340 check( de.size() == 4, "The col retrieval gives back wrong expression.");
341 check( de[upright] == 1, "The first coefficient should 1.");
345 //Maximization of x1+x2
346 //over the triangle with vertices (0,0) (0,1) (1,0)
347 double expected_opt=1;
348 solveAndCheck(lp, LpSolverBase::OPTIMAL, expected_opt);
353 solveAndCheck(lp, LpSolverBase::OPTIMAL, expected_opt);
355 //Vertex (-1,0) instead of (0,0)
356 lp.colLowerBound(x1, -LpSolverBase::INF);
358 solveAndCheck(lp, LpSolverBase::OPTIMAL, expected_opt);
360 //Erase one constraint and return to maximization
361 lp.eraseRow(upright);
363 expected_opt=LpSolverBase::INF;
364 solveAndCheck(lp, LpSolverBase::INFINITE, expected_opt);
367 lp.addRow(x1+x2 <=-2);
368 solveAndCheck(lp, LpSolverBase::INFEASIBLE, expected_opt);
370 //Change problem and forget to solve
372 check(lp.primalStatus()==LpSolverBase::UNDEFINED,"Primalstatus should be UNDEFINED");
376 // if (lp.primalStatus()==LpSolverBase::OPTIMAL){
377 // std::cout<< "Z = "<<lp.primalValue()
378 // << " (error = " << lp.primalValue()-expected_opt
379 // << "); x1 = "<<lp.primal(x1)
380 // << "; x2 = "<<lp.primal(x2)
385 // std::cout<<lp.primalStatus()<<std::endl;
386 // std::cout<<"Optimal solution not found!"<<std::endl;
400 LpGlpk lp_glpk1,lp_glpk2;
406 LpCplex lp_cplex1,lp_cplex2;
412 LpSoplex lp_soplex1,lp_soplex2;