3 * This file is a part of LEMON, a generic C++ optimization library
5 * Copyright (C) 2003-2006
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>
29 #include <lemon/lp_glpk.h>
33 #include <lemon/lp_cplex.h>
36 using namespace lemon;
38 void lpTest(LpSolverBase & lp)
43 typedef LpSolverBase LP;
45 std::vector<LP::Col> x(10);
46 // for(int i=0;i<10;i++) x.push_back(lp.addCol());
48 lp.colLowerBound(x,1);
49 lp.colUpperBound(x,1);
53 std::vector<LP::Col> y(10);
56 lp.colLowerBound(y,1);
57 lp.colUpperBound(y,1);
60 std::map<int,LP::Col> z;
62 z.insert(std::make_pair(12,INVALID));
63 z.insert(std::make_pair(2,INVALID));
64 z.insert(std::make_pair(7,INVALID));
65 z.insert(std::make_pair(5,INVALID));
69 lp.colLowerBound(z,1);
70 lp.colUpperBound(z,1);
75 LP::Col p1,p2,p3,p4,p5;
111 e=((p1+p2)+(p1-p2)+(p1+12)+(12+p1)+(p1-12)+(12-p1)+
112 (f+12)+(12+f)+(p1+f)+(f+p1)+(f+g)+
113 (f-12)+(12-f)+(p1-f)+(f-p1)+(f-g)+
116 2.2*p1+p1*2.2+p1/2.2+
171 lp.addRow(LP::INF,e,23);
172 lp.addRow(LP::INF,3.0*(x[1]+x[2]/2)-x[3],23);
173 lp.addRow(LP::INF,3.0*(x[1]+x[2]*2-5*x[3]+12-x[4]/3)+2*x[4]-4,23);
175 lp.addRow(x[1]+x[3]<=x[5]-3);
176 lp.addRow(-7<=x[1]+x[3]-12<=3);
177 lp.addRow(x[1]<=x[5]);
179 std::ostringstream buf;
182 //Checking the simplify function
184 // //How to check the simplify function? A map gives no information
185 // //on the question whether a given key is or is not stored in it, or
187 // Yes, it does, using the find() function.
190 buf << "Coeff. of p2 should be 0";
191 // std::cout<<e[p1]<<e[p2]<<e[p3]<<std::endl;
192 check(e.find(p2)==e.end(), buf.str());
197 e=((p1+p2)+(p1-0.99*p2));
198 double tolerance=0.001;
199 e.simplify(tolerance);
200 buf << "Coeff. of p2 should be 0.01";
201 check(e[p2]>0, buf.str());
204 e.simplify(tolerance);
205 buf << "Coeff. of p2 should be 0";
206 check(e.find(p2)==e.end(), buf.str());
213 LP::Row p1,p2,p3,p4,p5;
238 2.2*p1+p1*2.2+p1/2.2+
246 void solveAndCheck(LpSolverBase& lp, LpSolverBase::SolutionStatus stat,
251 std::ostringstream buf;
252 buf << "Primalstatus should be: " << int(stat);
254 // itoa(stat,buf1, 10);
255 check(lp.primalStatus()==stat, buf.str());
257 if (stat == LpSolverBase::OPTIMAL) {
258 std::ostringstream buf;
259 buf << "Wrong optimal value: the right optimum is " << exp_opt;
260 check(std::abs(lp.primalValue()-exp_opt) < 1e-3, buf.str());
265 void aTest(LpSolverBase & lp)
267 typedef LpSolverBase LP;
269 //The following example is very simple
271 typedef LpSolverBase::Row Row;
272 typedef LpSolverBase::Col Col;
275 Col x1 = lp.addCol();
276 Col x2 = lp.addCol();
280 Row upright=lp.addRow(x1+x2 <=1);
281 lp.addRow(x1+x2 >=-1);
282 lp.addRow(x1-x2 <=1);
283 lp.addRow(x1-x2 >=-1);
284 //Nonnegativity of the variables
285 lp.colLowerBound(x1, 0);
286 lp.colLowerBound(x2, 0);
292 //Maximization of x1+x2
293 //over the triangle with vertices (0,0) (0,1) (1,0)
294 double expected_opt=1;
295 solveAndCheck(lp, LpSolverBase::OPTIMAL, expected_opt);
300 solveAndCheck(lp, LpSolverBase::OPTIMAL, expected_opt);
302 //Vertex (-1,0) instead of (0,0)
303 lp.colLowerBound(x1, -LpSolverBase::INF);
305 solveAndCheck(lp, LpSolverBase::OPTIMAL, expected_opt);
307 //Erase one constraint and return to maximization
308 lp.eraseRow(upright);
310 expected_opt=LpSolverBase::INF;
311 solveAndCheck(lp, LpSolverBase::INFINITE, expected_opt);
314 lp.addRow(x1+x2 <=-2);
315 solveAndCheck(lp, LpSolverBase::INFEASIBLE, expected_opt);
317 //Change problem and forget to solve
319 check(lp.primalStatus()==LpSolverBase::UNDEFINED,"Primalstatus should be UNDEFINED");
322 // if (lp.primalStatus()==LpSolverBase::OPTIMAL){
323 // std::cout<< "Z = "<<lp.primalValue()
324 // << " (error = " << lp.primalValue()-expected_opt
325 // << "); x1 = "<<lp.primal(x1)
326 // << "; x2 = "<<lp.primal(x2)
331 // std::cout<<lp.primalStatus()<<std::endl;
332 // std::cout<<"Optimal solution not found!"<<std::endl;
346 LpGlpk lp_glpk1,lp_glpk2;
352 LpCplex lp_cplex1,lp_cplex2;