test/lp_test.cc
author Alpar Juttner <alpar@cs.elte.hu>
Tue, 30 Jul 2013 15:03:53 +0200
changeset 975 756022ac1674
parent 955 8d281761dea4
child 976 736436e516d3
child 980 115031ac8001
permissions -rw-r--r--
Suppress 'unused local typedefs' warnings, and resolve others (#470)
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@955
    44
int countCols(LpBase & lp) {
alpar@955
    45
  int count=0;
alpar@955
    46
  for (LpBase::ColIt c(lp); c!=INVALID; ++c) ++count;
alpar@955
    47
  return count;
alpar@955
    48
}
alpar@955
    49
alpar@955
    50
int countRows(LpBase & lp) {
alpar@955
    51
  int count=0;
alpar@955
    52
  for (LpBase::RowIt r(lp); r!=INVALID; ++r) ++count;
alpar@955
    53
  return count;
alpar@955
    54
}
alpar@955
    55
alpar@955
    56
deba@459
    57
void lpTest(LpSolver& lp)
deba@458
    58
{
deba@458
    59
deba@459
    60
  typedef LpSolver LP;
deba@458
    61
alpar@955
    62
  // Test LpBase::clear()
alpar@955
    63
  check(countRows(lp)==0, "Wrong number of rows");
alpar@955
    64
  check(countCols(lp)==0, "Wrong number of cols");
alpar@955
    65
  lp.addCol(); lp.addRow(); lp.addRow();
alpar@955
    66
  check(countRows(lp)==2, "Wrong number of rows");
alpar@955
    67
  check(countCols(lp)==1, "Wrong number of cols");
alpar@955
    68
  lp.clear();
alpar@955
    69
  check(countRows(lp)==0, "Wrong number of rows");
alpar@955
    70
  check(countCols(lp)==0, "Wrong number of cols");
alpar@955
    71
  lp.addCol(); lp.addCol(); lp.addCol(); lp.addRow();
alpar@955
    72
  check(countRows(lp)==1, "Wrong number of rows");
alpar@955
    73
  check(countCols(lp)==3, "Wrong number of cols");
alpar@955
    74
  lp.clear();
alpar@955
    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@932
   196
    { //Tests for #430
retvari@932
   197
      LP::Col v=lp.addCol();
retvari@932
   198
      LP::Constr c = v >= -3;
retvari@932
   199
      c = c <= 4;
retvari@932
   200
      LP::Constr c2;
retvari@932
   201
      c2 = -3 <= v <= 4;
retvari@932
   202
    }
retvari@932
   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;
alpar@975
   243
    LP::Row p1 = INVALID, p2 = INVALID;
deba@458
   244
deba@458
   245
    e[p1]=2;
deba@458
   246
    e[p1]+=2;
deba@458
   247
    e[p1]-=2;
deba@458
   248
deba@458
   249
    e=p1;
deba@458
   250
    e=f;
deba@458
   251
deba@458
   252
    e+=p1;
deba@458
   253
    e+=f;
deba@458
   254
deba@458
   255
    e-=p1;
deba@458
   256
    e-=f;
deba@458
   257
deba@458
   258
    e*=2;
deba@458
   259
    e*=2.2;
deba@458
   260
    e/=2;
deba@458
   261
    e/=2.2;
deba@458
   262
deba@458
   263
    e=((p1+p2)+(p1-p2)+
deba@458
   264
       (p1+f)+(f+p1)+(f+g)+
deba@458
   265
       (p1-f)+(f-p1)+(f-g)+
deba@458
   266
       2.2*f+f*2.2+f/2.2+
deba@458
   267
       2*f+f*2+f/2+
deba@458
   268
       2.2*p1+p1*2.2+p1/2.2+
deba@458
   269
       2*p1+p1*2+p1/2
deba@458
   270
       );
deba@458
   271
  }
deba@458
   272
deba@458
   273
}
deba@458
   274
deba@459
   275
void solveAndCheck(LpSolver& lp, LpSolver::ProblemType stat,
deba@458
   276
                   double exp_opt) {
deba@458
   277
  using std::string;
deba@458
   278
  lp.solve();
deba@459
   279
deba@458
   280
  std::ostringstream buf;
deba@459
   281
  buf << "PrimalType should be: " << int(stat) << int(lp.primalType());
deba@458
   282
deba@459
   283
  check(lp.primalType()==stat, buf.str());
deba@458
   284
deba@459
   285
  if (stat ==  LpSolver::OPTIMAL) {
deba@458
   286
    std::ostringstream sbuf;
alpar@540
   287
    sbuf << "Wrong optimal value (" << lp.primal() <<") with "
alpar@540
   288
         << lp.solverName() <<"\n     the right optimum is " << exp_opt;
deba@459
   289
    check(std::abs(lp.primal()-exp_opt) < 1e-3, sbuf.str());
deba@458
   290
  }
deba@458
   291
}
deba@458
   292
deba@459
   293
void aTest(LpSolver & lp)
deba@458
   294
{
deba@459
   295
  typedef LpSolver LP;
deba@458
   296
deba@458
   297
 //The following example is very simple
deba@458
   298
deba@459
   299
  typedef LpSolver::Row Row;
deba@459
   300
  typedef LpSolver::Col Col;
deba@458
   301
deba@458
   302
deba@458
   303
  Col x1 = lp.addCol();
deba@458
   304
  Col x2 = lp.addCol();
deba@458
   305
deba@458
   306
deba@458
   307
  //Constraints
deba@459
   308
  Row upright=lp.addRow(x1+2*x2 <=1);
deba@458
   309
  lp.addRow(x1+x2 >=-1);
deba@458
   310
  lp.addRow(x1-x2 <=1);
deba@458
   311
  lp.addRow(x1-x2 >=-1);
deba@458
   312
  //Nonnegativity of the variables
deba@458
   313
  lp.colLowerBound(x1, 0);
deba@458
   314
  lp.colLowerBound(x2, 0);
deba@458
   315
  //Objective function
deba@458
   316
  lp.obj(x1+x2);
deba@458
   317
deba@459
   318
  lp.sense(lp.MAX);
deba@458
   319
deba@458
   320
  //Testing the problem retrieving routines
deba@458
   321
  check(lp.objCoeff(x1)==1,"First term should be 1 in the obj function!");
deba@459
   322
  check(lp.sense() == lp.MAX,"This is a maximization!");
deba@458
   323
  check(lp.coeff(upright,x1)==1,"The coefficient in question is 1!");
deba@459
   324
  check(lp.colLowerBound(x1)==0,
deba@459
   325
        "The lower bound for variable x1 should be 0.");
deba@459
   326
  check(lp.colUpperBound(x1)==LpSolver::INF,
deba@459
   327
        "The upper bound for variable x1 should be infty.");
deba@459
   328
  check(lp.rowLowerBound(upright) == -LpSolver::INF,
deba@459
   329
        "The lower bound for the first row should be -infty.");
deba@459
   330
  check(lp.rowUpperBound(upright)==1,
deba@459
   331
        "The upper bound for the first row should be 1.");
deba@459
   332
  LpSolver::Expr e = lp.row(upright);
deba@459
   333
  check(e[x1] == 1, "The first coefficient should 1.");
deba@459
   334
  check(e[x2] == 2, "The second coefficient should 1.");
deba@458
   335
deba@459
   336
  lp.row(upright, x1+x2 <=1);
deba@459
   337
  e = lp.row(upright);
deba@459
   338
  check(e[x1] == 1, "The first coefficient should 1.");
deba@459
   339
  check(e[x2] == 1, "The second coefficient should 1.");
deba@459
   340
deba@459
   341
  LpSolver::DualExpr de = lp.col(x1);
deba@458
   342
  check(  de[upright] == 1, "The first coefficient should 1.");
deba@458
   343
deba@459
   344
  LpSolver* clp = lp.cloneSolver();
deba@458
   345
deba@458
   346
  //Testing the problem retrieving routines
deba@458
   347
  check(clp->objCoeff(x1)==1,"First term should be 1 in the obj function!");
deba@459
   348
  check(clp->sense() == clp->MAX,"This is a maximization!");
deba@458
   349
  check(clp->coeff(upright,x1)==1,"The coefficient in question is 1!");
deba@458
   350
  //  std::cout<<lp.colLowerBound(x1)<<std::endl;
deba@459
   351
  check(clp->colLowerBound(x1)==0,
deba@459
   352
        "The lower bound for variable x1 should be 0.");
deba@459
   353
  check(clp->colUpperBound(x1)==LpSolver::INF,
deba@459
   354
        "The upper bound for variable x1 should be infty.");
deba@458
   355
deba@459
   356
  check(lp.rowLowerBound(upright)==-LpSolver::INF,
deba@459
   357
        "The lower bound for the first row should be -infty.");
deba@459
   358
  check(lp.rowUpperBound(upright)==1,
deba@459
   359
        "The upper bound for the first row should be 1.");
deba@458
   360
  e = clp->row(upright);
deba@459
   361
  check(e[x1] == 1, "The first coefficient should 1.");
deba@459
   362
  check(e[x2] == 1, "The second coefficient should 1.");
deba@458
   363
deba@458
   364
  de = clp->col(x1);
deba@459
   365
  check(de[upright] == 1, "The first coefficient should 1.");
deba@458
   366
deba@458
   367
  delete clp;
deba@458
   368
deba@458
   369
  //Maximization of x1+x2
deba@458
   370
  //over the triangle with vertices (0,0) (0,1) (1,0)
deba@458
   371
  double expected_opt=1;
deba@459
   372
  solveAndCheck(lp, LpSolver::OPTIMAL, expected_opt);
deba@458
   373
deba@458
   374
  //Minimization
deba@459
   375
  lp.sense(lp.MIN);
deba@458
   376
  expected_opt=0;
deba@459
   377
  solveAndCheck(lp, LpSolver::OPTIMAL, expected_opt);
deba@458
   378
deba@458
   379
  //Vertex (-1,0) instead of (0,0)
deba@459
   380
  lp.colLowerBound(x1, -LpSolver::INF);
deba@458
   381
  expected_opt=-1;
deba@459
   382
  solveAndCheck(lp, LpSolver::OPTIMAL, expected_opt);
deba@458
   383
deba@458
   384
  //Erase one constraint and return to maximization
deba@459
   385
  lp.erase(upright);
deba@459
   386
  lp.sense(lp.MAX);
deba@459
   387
  expected_opt=LpSolver::INF;
deba@459
   388
  solveAndCheck(lp, LpSolver::UNBOUNDED, expected_opt);
deba@458
   389
deba@458
   390
  //Infeasibilty
deba@458
   391
  lp.addRow(x1+x2 <=-2);
deba@459
   392
  solveAndCheck(lp, LpSolver::INFEASIBLE, expected_opt);
deba@458
   393
deba@458
   394
}
deba@458
   395
alpar@540
   396
template<class LP>
alpar@540
   397
void cloneTest()
alpar@540
   398
{
alpar@540
   399
  //Test for clone/new
deba@551
   400
alpar@540
   401
  LP* lp = new LP();
alpar@540
   402
  LP* lpnew = lp->newSolver();
alpar@540
   403
  LP* lpclone = lp->cloneSolver();
alpar@540
   404
  delete lp;
alpar@540
   405
  delete lpnew;
alpar@540
   406
  delete lpclone;
alpar@540
   407
}
alpar@540
   408
deba@458
   409
int main()
deba@458
   410
{
deba@458
   411
  LpSkeleton lp_skel;
deba@458
   412
  lpTest(lp_skel);
deba@458
   413
ladanyi@627
   414
#ifdef LEMON_HAVE_GLPK
deba@459
   415
  {
alpar@462
   416
    GlpkLp lp_glpk1,lp_glpk2;
deba@459
   417
    lpTest(lp_glpk1);
deba@459
   418
    aTest(lp_glpk2);
alpar@540
   419
    cloneTest<GlpkLp>();
deba@459
   420
  }
deba@458
   421
#endif
deba@458
   422
ladanyi@627
   423
#ifdef LEMON_HAVE_CPLEX
deba@459
   424
  try {
alpar@462
   425
    CplexLp lp_cplex1,lp_cplex2;
deba@459
   426
    lpTest(lp_cplex1);
deba@459
   427
    aTest(lp_cplex2);
deba@551
   428
    cloneTest<CplexLp>();
deba@459
   429
  } catch (CplexEnv::LicenseError& error) {
deba@459
   430
    check(false, error.what());
deba@459
   431
  }
deba@458
   432
#endif
deba@458
   433
ladanyi@627
   434
#ifdef LEMON_HAVE_SOPLEX
deba@459
   435
  {
alpar@462
   436
    SoplexLp lp_soplex1,lp_soplex2;
deba@459
   437
    lpTest(lp_soplex1);
deba@459
   438
    aTest(lp_soplex2);
alpar@540
   439
    cloneTest<SoplexLp>();
deba@459
   440
  }
deba@459
   441
#endif
deba@459
   442
ladanyi@627
   443
#ifdef LEMON_HAVE_CLP
deba@459
   444
  {
alpar@462
   445
    ClpLp lp_clp1,lp_clp2;
deba@459
   446
    lpTest(lp_clp1);
deba@459
   447
    aTest(lp_clp2);
alpar@540
   448
    cloneTest<ClpLp>();
deba@459
   449
  }
deba@458
   450
#endif
deba@458
   451
deba@458
   452
  return 0;
deba@458
   453
}