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