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