Some testing of the LP interface: bugs got fixed.
1 #include<lemon/lp_skeleton.h>
2 #include "test_tools.h"
10 #include <lemon/lp_glpk.h>
14 #include <lemon/lp_cplex.h>
17 using namespace lemon;
19 void lpTest(LpSolverBase & lp)
24 typedef LpSolverBase LP;
26 std::vector<LP::Col> x(10);
27 // for(int i=0;i<10;i++) x.push_back(lp.addCol());
32 std::vector<LP::Col> y(10);
35 std::map<int,LP::Col> z;
37 z.insert(std::make_pair(12,INVALID));
38 z.insert(std::make_pair(2,INVALID));
39 z.insert(std::make_pair(7,INVALID));
40 z.insert(std::make_pair(5,INVALID));
46 LP::Col p1,p2,p3,p4,p5;
82 e=((p1+p2)+(p1-p2)+(p1+12)+(12+p1)+(p1-12)+(12-p1)+
83 (f+12)+(12+f)+(p1+f)+(f+p1)+(f+g)+
84 (f-12)+(12-f)+(p1-f)+(f-p1)+(f-g)+
142 lp.addRow(LP::INF,e,23);
143 lp.addRow(LP::INF,3.0*(x[1]+x[2]/2)-x[3],23);
144 lp.addRow(LP::INF,3.0*(x[1]+x[2]*2-5*x[3]+12-x[4]/3)+2*x[4]-4,23);
146 lp.addRow(x[1]+x[3]<=x[5]-3);
147 lp.addRow(-7<=x[1]+x[3]-12<=3);
148 lp.addRow(x[1]<=x[5]);
153 LP::Row p1,p2,p3,p4,p5;
178 2.2*p1+p1*2.2+p1/2.2+
186 void solveAndCheck(LpSolverBase& lp, LpSolverBase::SolutionStatus stat,
191 // itoa(stat,buf1, 10);
192 check(lp.primalStatus()==stat,"Primalstatus should be "+buf1);
194 if (stat == LpSolverBase::OPTIMAL){
195 check(std::abs(lp.primalValue()-exp_opt)<1e-3,
196 "Wrong optimal value: the right optimum is ");
201 void aTest(LpSolverBase & lp)
203 typedef LpSolverBase LP;
205 //The following example is very simple
207 typedef LpSolverBase::Row Row;
208 typedef LpSolverBase::Col Col;
211 Col x1 = lp.addCol();
212 Col x2 = lp.addCol();
216 Row upright=lp.addRow(x1+x2 <=1);
217 lp.addRow(x1+x2 >=-1);
218 lp.addRow(x1-x2 <=1);
219 lp.addRow(x1-x2 >=-1);
220 //Nonnegativity of the variables
221 lp.colLowerBound(x1, 0);
222 lp.colLowerBound(x2, 0);
228 //Maximization of x1+x2
229 //over the triangle with vertices (0,0) (0,1) (1,0)
230 double expected_opt=1;
231 solveAndCheck(lp, LpSolverBase::OPTIMAL, expected_opt);
236 solveAndCheck(lp, LpSolverBase::OPTIMAL, expected_opt);
238 //Vertex (-1,0) instead of (0,0)
239 lp.colLowerBound(x1, -LpSolverBase::INF);
241 solveAndCheck(lp, LpSolverBase::OPTIMAL, expected_opt);
243 //Erase one constraint and return to maximization
244 lp.eraseRow(upright);
246 expected_opt=LpSolverBase::INF;
247 solveAndCheck(lp, LpSolverBase::INFINITE, expected_opt);
250 lp.addRow(x1+x2 <=-2);
251 solveAndCheck(lp, LpSolverBase::INFEASIBLE, expected_opt);
253 //Change problem and forget to solve
255 check(lp.primalStatus()==LpSolverBase::UNDEFINED,"Primalstatus should be UNDEFINED");
258 // if (lp.primalStatus()==LpSolverBase::OPTIMAL){
259 // std::cout<< "Z = "<<lp.primalValue()
260 // << " (error = " << lp.primalValue()-expected_opt
261 // << "); x1 = "<<lp.primal(x1)
262 // << "; x2 = "<<lp.primal(x2)
267 // std::cout<<lp.primalStatus()<<std::endl;
268 // std::cout<<"Optimal solution not found!"<<std::endl;
282 LpGlpk lp_glpk1,lp_glpk2;