COIN-OR::LEMON - Graph Library

source: lemon-main/test/lp_test.cc @ 907:1937b6455b7d

Last change on this file since 907:1937b6455b7d was 631:d21b38647e53, checked in by Akos Ladanyi <ladanyi@…>, 16 years ago

Remove superfluous HAVE_CONFIG_H (#278)

File size: 9.0 KB
RevLine 
[458]1/* -*- mode: C++; indent-tabs-mode: nil; -*-
2 *
3 * This file is a part of LEMON, a generic C++ optimization library.
4 *
[551]5 * Copyright (C) 2003-2009
[458]6 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7 * (Egervary Research Group on Combinatorial Optimization, EGRES).
8 *
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.
12 *
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
15 * purpose.
16 *
17 */
18
19#include <sstream>
20#include <lemon/lp_skeleton.h>
21#include "test_tools.h"
22#include <lemon/tolerance.h>
23
24#include <lemon/config.h>
25
[627]26#ifdef LEMON_HAVE_GLPK
[461]27#include <lemon/glpk.h>
[458]28#endif
29
[627]30#ifdef LEMON_HAVE_CPLEX
[461]31#include <lemon/cplex.h>
[458]32#endif
33
[627]34#ifdef LEMON_HAVE_SOPLEX
[461]35#include <lemon/soplex.h>
[458]36#endif
37
[627]38#ifdef LEMON_HAVE_CLP
[461]39#include <lemon/clp.h>
[459]40#endif
41
[458]42using namespace lemon;
43
[459]44void lpTest(LpSolver& lp)
[458]45{
46
[459]47  typedef LpSolver LP;
[458]48
49  std::vector<LP::Col> x(10);
50  //  for(int i=0;i<10;i++) x.push_back(lp.addCol());
51  lp.addColSet(x);
52  lp.colLowerBound(x,1);
53  lp.colUpperBound(x,1);
54  lp.colBounds(x,1,2);
55
56  std::vector<LP::Col> y(10);
57  lp.addColSet(y);
58
59  lp.colLowerBound(y,1);
60  lp.colUpperBound(y,1);
61  lp.colBounds(y,1,2);
62
63  std::map<int,LP::Col> z;
64
65  z.insert(std::make_pair(12,INVALID));
66  z.insert(std::make_pair(2,INVALID));
67  z.insert(std::make_pair(7,INVALID));
68  z.insert(std::make_pair(5,INVALID));
69
70  lp.addColSet(z);
71
72  lp.colLowerBound(z,1);
73  lp.colUpperBound(z,1);
74  lp.colBounds(z,1,2);
75
76  {
77    LP::Expr e,f,g;
78    LP::Col p1,p2,p3,p4,p5;
79    LP::Constr c;
80
81    p1=lp.addCol();
82    p2=lp.addCol();
83    p3=lp.addCol();
84    p4=lp.addCol();
85    p5=lp.addCol();
86
87    e[p1]=2;
[459]88    *e=12;
[458]89    e[p1]+=2;
[459]90    *e+=12;
[458]91    e[p1]-=2;
[459]92    *e-=12;
[458]93
94    e=2;
95    e=2.2;
96    e=p1;
97    e=f;
98
99    e+=2;
100    e+=2.2;
101    e+=p1;
102    e+=f;
103
104    e-=2;
105    e-=2.2;
106    e-=p1;
107    e-=f;
108
109    e*=2;
110    e*=2.2;
111    e/=2;
112    e/=2.2;
113
114    e=((p1+p2)+(p1-p2)+(p1+12)+(12+p1)+(p1-12)+(12-p1)+
115       (f+12)+(12+f)+(p1+f)+(f+p1)+(f+g)+
116       (f-12)+(12-f)+(p1-f)+(f-p1)+(f-g)+
117       2.2*f+f*2.2+f/2.2+
118       2*f+f*2+f/2+
119       2.2*p1+p1*2.2+p1/2.2+
120       2*p1+p1*2+p1/2
121       );
122
123
124    c = (e  <= f  );
125    c = (e  <= 2.2);
126    c = (e  <= 2  );
127    c = (e  <= p1 );
128    c = (2.2<= f  );
129    c = (2  <= f  );
130    c = (p1 <= f  );
131    c = (p1 <= p2 );
132    c = (p1 <= 2.2);
133    c = (p1 <= 2  );
134    c = (2.2<= p2 );
135    c = (2  <= p2 );
136
137    c = (e  >= f  );
138    c = (e  >= 2.2);
139    c = (e  >= 2  );
140    c = (e  >= p1 );
141    c = (2.2>= f  );
142    c = (2  >= f  );
143    c = (p1 >= f  );
144    c = (p1 >= p2 );
145    c = (p1 >= 2.2);
146    c = (p1 >= 2  );
147    c = (2.2>= p2 );
148    c = (2  >= p2 );
149
150    c = (e  == f  );
151    c = (e  == 2.2);
152    c = (e  == 2  );
153    c = (e  == p1 );
154    c = (2.2== f  );
155    c = (2  == f  );
156    c = (p1 == f  );
157    //c = (p1 == p2 );
158    c = (p1 == 2.2);
159    c = (p1 == 2  );
160    c = (2.2== p2 );
161    c = (2  == p2 );
162
[460]163    c = ((2 <= e) <= 3);
164    c = ((2 <= p1) <= 3);
[458]165
[460]166    c = ((2 >= e) >= 3);
167    c = ((2 >= p1) >= 3);
[458]168
169    e[x[3]]=2;
170    e[x[3]]=4;
171    e[x[3]]=1;
[459]172    *e=12;
[458]173
[459]174    lp.addRow(-LP::INF,e,23);
175    lp.addRow(-LP::INF,3.0*(x[1]+x[2]/2)-x[3],23);
176    lp.addRow(-LP::INF,3.0*(x[1]+x[2]*2-5*x[3]+12-x[4]/3)+2*x[4]-4,23);
[458]177
178    lp.addRow(x[1]+x[3]<=x[5]-3);
[460]179    lp.addRow((-7<=x[1]+x[3]-12)<=3);
[458]180    lp.addRow(x[1]<=x[5]);
181
182    std::ostringstream buf;
183
184
185    e=((p1+p2)+(p1-0.99*p2));
186    //e.prettyPrint(std::cout);
187    //(e<=2).prettyPrint(std::cout);
188    double tolerance=0.001;
189    e.simplify(tolerance);
190    buf << "Coeff. of p2 should be 0.01";
191    check(e[p2]>0, buf.str());
192
193    tolerance=0.02;
194    e.simplify(tolerance);
195    buf << "Coeff. of p2 should be 0";
[459]196    check(const_cast<const LpSolver::Expr&>(e)[p2]==0, buf.str());
[458]197
[540]198    //Test for clone/new
199    LP* lpnew = lp.newSolver();
200    LP* lpclone = lp.cloneSolver();
201    delete lpnew;
202    delete lpclone;
[458]203
204  }
205
206  {
207    LP::DualExpr e,f,g;
208    LP::Row p1 = INVALID, p2 = INVALID, p3 = INVALID,
209      p4 = INVALID, p5 = INVALID;
210
211    e[p1]=2;
212    e[p1]+=2;
213    e[p1]-=2;
214
215    e=p1;
216    e=f;
217
218    e+=p1;
219    e+=f;
220
221    e-=p1;
222    e-=f;
223
224    e*=2;
225    e*=2.2;
226    e/=2;
227    e/=2.2;
228
229    e=((p1+p2)+(p1-p2)+
230       (p1+f)+(f+p1)+(f+g)+
231       (p1-f)+(f-p1)+(f-g)+
232       2.2*f+f*2.2+f/2.2+
233       2*f+f*2+f/2+
234       2.2*p1+p1*2.2+p1/2.2+
235       2*p1+p1*2+p1/2
236       );
237  }
238
239}
240
[459]241void solveAndCheck(LpSolver& lp, LpSolver::ProblemType stat,
[458]242                   double exp_opt) {
243  using std::string;
244  lp.solve();
[459]245
[458]246  std::ostringstream buf;
[459]247  buf << "PrimalType should be: " << int(stat) << int(lp.primalType());
[458]248
[459]249  check(lp.primalType()==stat, buf.str());
[458]250
[459]251  if (stat ==  LpSolver::OPTIMAL) {
[458]252    std::ostringstream sbuf;
[540]253    sbuf << "Wrong optimal value (" << lp.primal() <<") with "
254         << lp.solverName() <<"\n     the right optimum is " << exp_opt;
[459]255    check(std::abs(lp.primal()-exp_opt) < 1e-3, sbuf.str());
[458]256  }
257}
258
[459]259void aTest(LpSolver & lp)
[458]260{
[459]261  typedef LpSolver LP;
[458]262
263 //The following example is very simple
264
[459]265  typedef LpSolver::Row Row;
266  typedef LpSolver::Col Col;
[458]267
268
269  Col x1 = lp.addCol();
270  Col x2 = lp.addCol();
271
272
273  //Constraints
[459]274  Row upright=lp.addRow(x1+2*x2 <=1);
[458]275  lp.addRow(x1+x2 >=-1);
276  lp.addRow(x1-x2 <=1);
277  lp.addRow(x1-x2 >=-1);
278  //Nonnegativity of the variables
279  lp.colLowerBound(x1, 0);
280  lp.colLowerBound(x2, 0);
281  //Objective function
282  lp.obj(x1+x2);
283
[459]284  lp.sense(lp.MAX);
[458]285
286  //Testing the problem retrieving routines
287  check(lp.objCoeff(x1)==1,"First term should be 1 in the obj function!");
[459]288  check(lp.sense() == lp.MAX,"This is a maximization!");
[458]289  check(lp.coeff(upright,x1)==1,"The coefficient in question is 1!");
[459]290  check(lp.colLowerBound(x1)==0,
291        "The lower bound for variable x1 should be 0.");
292  check(lp.colUpperBound(x1)==LpSolver::INF,
293        "The upper bound for variable x1 should be infty.");
294  check(lp.rowLowerBound(upright) == -LpSolver::INF,
295        "The lower bound for the first row should be -infty.");
296  check(lp.rowUpperBound(upright)==1,
297        "The upper bound for the first row should be 1.");
298  LpSolver::Expr e = lp.row(upright);
299  check(e[x1] == 1, "The first coefficient should 1.");
300  check(e[x2] == 2, "The second coefficient should 1.");
[458]301
[459]302  lp.row(upright, x1+x2 <=1);
303  e = lp.row(upright);
304  check(e[x1] == 1, "The first coefficient should 1.");
305  check(e[x2] == 1, "The second coefficient should 1.");
306
307  LpSolver::DualExpr de = lp.col(x1);
[458]308  check(  de[upright] == 1, "The first coefficient should 1.");
309
[459]310  LpSolver* clp = lp.cloneSolver();
[458]311
312  //Testing the problem retrieving routines
313  check(clp->objCoeff(x1)==1,"First term should be 1 in the obj function!");
[459]314  check(clp->sense() == clp->MAX,"This is a maximization!");
[458]315  check(clp->coeff(upright,x1)==1,"The coefficient in question is 1!");
316  //  std::cout<<lp.colLowerBound(x1)<<std::endl;
[459]317  check(clp->colLowerBound(x1)==0,
318        "The lower bound for variable x1 should be 0.");
319  check(clp->colUpperBound(x1)==LpSolver::INF,
320        "The upper bound for variable x1 should be infty.");
[458]321
[459]322  check(lp.rowLowerBound(upright)==-LpSolver::INF,
323        "The lower bound for the first row should be -infty.");
324  check(lp.rowUpperBound(upright)==1,
325        "The upper bound for the first row should be 1.");
[458]326  e = clp->row(upright);
[459]327  check(e[x1] == 1, "The first coefficient should 1.");
328  check(e[x2] == 1, "The second coefficient should 1.");
[458]329
330  de = clp->col(x1);
[459]331  check(de[upright] == 1, "The first coefficient should 1.");
[458]332
333  delete clp;
334
335  //Maximization of x1+x2
336  //over the triangle with vertices (0,0) (0,1) (1,0)
337  double expected_opt=1;
[459]338  solveAndCheck(lp, LpSolver::OPTIMAL, expected_opt);
[458]339
340  //Minimization
[459]341  lp.sense(lp.MIN);
[458]342  expected_opt=0;
[459]343  solveAndCheck(lp, LpSolver::OPTIMAL, expected_opt);
[458]344
345  //Vertex (-1,0) instead of (0,0)
[459]346  lp.colLowerBound(x1, -LpSolver::INF);
[458]347  expected_opt=-1;
[459]348  solveAndCheck(lp, LpSolver::OPTIMAL, expected_opt);
[458]349
350  //Erase one constraint and return to maximization
[459]351  lp.erase(upright);
352  lp.sense(lp.MAX);
353  expected_opt=LpSolver::INF;
354  solveAndCheck(lp, LpSolver::UNBOUNDED, expected_opt);
[458]355
356  //Infeasibilty
357  lp.addRow(x1+x2 <=-2);
[459]358  solveAndCheck(lp, LpSolver::INFEASIBLE, expected_opt);
[458]359
360}
361
[540]362template<class LP>
363void cloneTest()
364{
365  //Test for clone/new
[551]366
[540]367  LP* lp = new LP();
368  LP* lpnew = lp->newSolver();
369  LP* lpclone = lp->cloneSolver();
370  delete lp;
371  delete lpnew;
372  delete lpclone;
373}
374
[458]375int main()
376{
377  LpSkeleton lp_skel;
378  lpTest(lp_skel);
379
[627]380#ifdef LEMON_HAVE_GLPK
[459]381  {
[462]382    GlpkLp lp_glpk1,lp_glpk2;
[459]383    lpTest(lp_glpk1);
384    aTest(lp_glpk2);
[540]385    cloneTest<GlpkLp>();
[459]386  }
[458]387#endif
388
[627]389#ifdef LEMON_HAVE_CPLEX
[459]390  try {
[462]391    CplexLp lp_cplex1,lp_cplex2;
[459]392    lpTest(lp_cplex1);
393    aTest(lp_cplex2);
[551]394    cloneTest<CplexLp>();
[459]395  } catch (CplexEnv::LicenseError& error) {
396    check(false, error.what());
397  }
[458]398#endif
399
[627]400#ifdef LEMON_HAVE_SOPLEX
[459]401  {
[462]402    SoplexLp lp_soplex1,lp_soplex2;
[459]403    lpTest(lp_soplex1);
404    aTest(lp_soplex2);
[540]405    cloneTest<SoplexLp>();
[459]406  }
407#endif
408
[627]409#ifdef LEMON_HAVE_CLP
[459]410  {
[462]411    ClpLp lp_clp1,lp_clp2;
[459]412    lpTest(lp_clp1);
413    aTest(lp_clp2);
[540]414    cloneTest<ClpLp>();
[459]415  }
[458]416#endif
417
418  return 0;
419}
Note: See TracBrowser for help on using the repository browser.