test/lp_test.cc
author Alpar Juttner <alpar@cs.elte.hu>
Sun, 11 Aug 2013 15:28:12 +0200
changeset 1095 ad40f7d32846
parent 1064 fc3854d936f7
child 1092 dceba191c00d
permissions -rw-r--r--
Merge >=1.2 branch heads
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;
alpar@1078
   201
#if ( __GNUC__ == 4 ) && ( __GNUC_MINOR__ == 3 )
alpar@1078
   202
      c2 = ( -3 <= v ) <= 4;
alpar@1078
   203
#else
retvari@957
   204
      c2 = -3 <= v <= 4;
alpar@1078
   205
#endif
alpar@1078
   206
retvari@957
   207
    }
retvari@957
   208
deba@458
   209
    e[x[3]]=2;
deba@458
   210
    e[x[3]]=4;
deba@458
   211
    e[x[3]]=1;
deba@459
   212
    *e=12;
deba@458
   213
deba@459
   214
    lp.addRow(-LP::INF,e,23);
deba@459
   215
    lp.addRow(-LP::INF,3.0*(x[1]+x[2]/2)-x[3],23);
deba@459
   216
    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
   217
deba@458
   218
    lp.addRow(x[1]+x[3]<=x[5]-3);
alpar@460
   219
    lp.addRow((-7<=x[1]+x[3]-12)<=3);
deba@458
   220
    lp.addRow(x[1]<=x[5]);
deba@458
   221
deba@458
   222
    std::ostringstream buf;
deba@458
   223
deba@458
   224
deba@458
   225
    e=((p1+p2)+(p1-0.99*p2));
deba@458
   226
    //e.prettyPrint(std::cout);
deba@458
   227
    //(e<=2).prettyPrint(std::cout);
deba@458
   228
    double tolerance=0.001;
deba@458
   229
    e.simplify(tolerance);
deba@458
   230
    buf << "Coeff. of p2 should be 0.01";
deba@458
   231
    check(e[p2]>0, buf.str());
deba@458
   232
deba@458
   233
    tolerance=0.02;
deba@458
   234
    e.simplify(tolerance);
deba@458
   235
    buf << "Coeff. of p2 should be 0";
deba@459
   236
    check(const_cast<const LpSolver::Expr&>(e)[p2]==0, buf.str());
deba@458
   237
alpar@540
   238
    //Test for clone/new
alpar@540
   239
    LP* lpnew = lp.newSolver();
alpar@540
   240
    LP* lpclone = lp.cloneSolver();
alpar@540
   241
    delete lpnew;
alpar@540
   242
    delete lpclone;
deba@458
   243
deba@458
   244
  }
deba@458
   245
deba@458
   246
  {
deba@458
   247
    LP::DualExpr e,f,g;
alpar@1064
   248
    LP::Row p1 = INVALID, p2 = INVALID;
deba@458
   249
deba@458
   250
    e[p1]=2;
deba@458
   251
    e[p1]+=2;
deba@458
   252
    e[p1]-=2;
deba@458
   253
deba@458
   254
    e=p1;
deba@458
   255
    e=f;
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*=2;
deba@458
   264
    e*=2.2;
deba@458
   265
    e/=2;
deba@458
   266
    e/=2.2;
deba@458
   267
deba@458
   268
    e=((p1+p2)+(p1-p2)+
deba@458
   269
       (p1+f)+(f+p1)+(f+g)+
deba@458
   270
       (p1-f)+(f-p1)+(f-g)+
deba@458
   271
       2.2*f+f*2.2+f/2.2+
deba@458
   272
       2*f+f*2+f/2+
deba@458
   273
       2.2*p1+p1*2.2+p1/2.2+
deba@458
   274
       2*p1+p1*2+p1/2
deba@458
   275
       );
deba@458
   276
  }
deba@458
   277
deba@458
   278
}
deba@458
   279
deba@459
   280
void solveAndCheck(LpSolver& lp, LpSolver::ProblemType stat,
deba@458
   281
                   double exp_opt) {
deba@458
   282
  using std::string;
deba@458
   283
  lp.solve();
deba@459
   284
deba@458
   285
  std::ostringstream buf;
deba@459
   286
  buf << "PrimalType should be: " << int(stat) << int(lp.primalType());
deba@458
   287
deba@459
   288
  check(lp.primalType()==stat, buf.str());
deba@458
   289
deba@459
   290
  if (stat ==  LpSolver::OPTIMAL) {
deba@458
   291
    std::ostringstream sbuf;
alpar@540
   292
    sbuf << "Wrong optimal value (" << lp.primal() <<") with "
alpar@540
   293
         << lp.solverName() <<"\n     the right optimum is " << exp_opt;
deba@459
   294
    check(std::abs(lp.primal()-exp_opt) < 1e-3, sbuf.str());
deba@458
   295
  }
deba@458
   296
}
deba@458
   297
deba@459
   298
void aTest(LpSolver & lp)
deba@458
   299
{
deba@459
   300
  typedef LpSolver LP;
deba@458
   301
deba@458
   302
 //The following example is very simple
deba@458
   303
deba@459
   304
  typedef LpSolver::Row Row;
deba@459
   305
  typedef LpSolver::Col Col;
deba@458
   306
deba@458
   307
deba@458
   308
  Col x1 = lp.addCol();
deba@458
   309
  Col x2 = lp.addCol();
deba@458
   310
deba@458
   311
deba@458
   312
  //Constraints
deba@459
   313
  Row upright=lp.addRow(x1+2*x2 <=1);
deba@458
   314
  lp.addRow(x1+x2 >=-1);
deba@458
   315
  lp.addRow(x1-x2 <=1);
deba@458
   316
  lp.addRow(x1-x2 >=-1);
deba@458
   317
  //Nonnegativity of the variables
deba@458
   318
  lp.colLowerBound(x1, 0);
deba@458
   319
  lp.colLowerBound(x2, 0);
deba@458
   320
  //Objective function
deba@458
   321
  lp.obj(x1+x2);
deba@458
   322
deba@459
   323
  lp.sense(lp.MAX);
deba@458
   324
deba@458
   325
  //Testing the problem retrieving routines
deba@458
   326
  check(lp.objCoeff(x1)==1,"First term should be 1 in the obj function!");
deba@459
   327
  check(lp.sense() == lp.MAX,"This is a maximization!");
deba@458
   328
  check(lp.coeff(upright,x1)==1,"The coefficient in question is 1!");
deba@459
   329
  check(lp.colLowerBound(x1)==0,
deba@459
   330
        "The lower bound for variable x1 should be 0.");
deba@459
   331
  check(lp.colUpperBound(x1)==LpSolver::INF,
deba@459
   332
        "The upper bound for variable x1 should be infty.");
deba@459
   333
  check(lp.rowLowerBound(upright) == -LpSolver::INF,
deba@459
   334
        "The lower bound for the first row should be -infty.");
deba@459
   335
  check(lp.rowUpperBound(upright)==1,
deba@459
   336
        "The upper bound for the first row should be 1.");
deba@459
   337
  LpSolver::Expr e = lp.row(upright);
deba@459
   338
  check(e[x1] == 1, "The first coefficient should 1.");
deba@459
   339
  check(e[x2] == 2, "The second coefficient should 1.");
deba@458
   340
deba@459
   341
  lp.row(upright, x1+x2 <=1);
deba@459
   342
  e = lp.row(upright);
deba@459
   343
  check(e[x1] == 1, "The first coefficient should 1.");
deba@459
   344
  check(e[x2] == 1, "The second coefficient should 1.");
deba@459
   345
deba@459
   346
  LpSolver::DualExpr de = lp.col(x1);
deba@458
   347
  check(  de[upright] == 1, "The first coefficient should 1.");
deba@458
   348
deba@459
   349
  LpSolver* clp = lp.cloneSolver();
deba@458
   350
deba@458
   351
  //Testing the problem retrieving routines
deba@458
   352
  check(clp->objCoeff(x1)==1,"First term should be 1 in the obj function!");
deba@459
   353
  check(clp->sense() == clp->MAX,"This is a maximization!");
deba@458
   354
  check(clp->coeff(upright,x1)==1,"The coefficient in question is 1!");
deba@458
   355
  //  std::cout<<lp.colLowerBound(x1)<<std::endl;
deba@459
   356
  check(clp->colLowerBound(x1)==0,
deba@459
   357
        "The lower bound for variable x1 should be 0.");
deba@459
   358
  check(clp->colUpperBound(x1)==LpSolver::INF,
deba@459
   359
        "The upper bound for variable x1 should be infty.");
deba@458
   360
deba@459
   361
  check(lp.rowLowerBound(upright)==-LpSolver::INF,
deba@459
   362
        "The lower bound for the first row should be -infty.");
deba@459
   363
  check(lp.rowUpperBound(upright)==1,
deba@459
   364
        "The upper bound for the first row should be 1.");
deba@458
   365
  e = clp->row(upright);
deba@459
   366
  check(e[x1] == 1, "The first coefficient should 1.");
deba@459
   367
  check(e[x2] == 1, "The second coefficient should 1.");
deba@458
   368
deba@458
   369
  de = clp->col(x1);
deba@459
   370
  check(de[upright] == 1, "The first coefficient should 1.");
deba@458
   371
deba@458
   372
  delete clp;
deba@458
   373
deba@458
   374
  //Maximization of x1+x2
deba@458
   375
  //over the triangle with vertices (0,0) (0,1) (1,0)
deba@458
   376
  double expected_opt=1;
deba@459
   377
  solveAndCheck(lp, LpSolver::OPTIMAL, expected_opt);
deba@458
   378
deba@458
   379
  //Minimization
deba@459
   380
  lp.sense(lp.MIN);
deba@458
   381
  expected_opt=0;
deba@459
   382
  solveAndCheck(lp, LpSolver::OPTIMAL, expected_opt);
deba@458
   383
deba@458
   384
  //Vertex (-1,0) instead of (0,0)
deba@459
   385
  lp.colLowerBound(x1, -LpSolver::INF);
deba@458
   386
  expected_opt=-1;
deba@459
   387
  solveAndCheck(lp, LpSolver::OPTIMAL, expected_opt);
deba@458
   388
deba@458
   389
  //Erase one constraint and return to maximization
deba@459
   390
  lp.erase(upright);
deba@459
   391
  lp.sense(lp.MAX);
deba@459
   392
  expected_opt=LpSolver::INF;
deba@459
   393
  solveAndCheck(lp, LpSolver::UNBOUNDED, expected_opt);
deba@458
   394
deba@458
   395
  //Infeasibilty
deba@458
   396
  lp.addRow(x1+x2 <=-2);
deba@459
   397
  solveAndCheck(lp, LpSolver::INFEASIBLE, expected_opt);
deba@458
   398
deba@458
   399
}
deba@458
   400
alpar@540
   401
template<class LP>
alpar@540
   402
void cloneTest()
alpar@540
   403
{
alpar@540
   404
  //Test for clone/new
deba@551
   405
alpar@540
   406
  LP* lp = new LP();
alpar@540
   407
  LP* lpnew = lp->newSolver();
alpar@540
   408
  LP* lpclone = lp->cloneSolver();
alpar@540
   409
  delete lp;
alpar@540
   410
  delete lpnew;
alpar@540
   411
  delete lpclone;
alpar@540
   412
}
alpar@540
   413
deba@458
   414
int main()
deba@458
   415
{
deba@458
   416
  LpSkeleton lp_skel;
deba@458
   417
  lpTest(lp_skel);
deba@458
   418
ladanyi@627
   419
#ifdef LEMON_HAVE_GLPK
deba@459
   420
  {
alpar@462
   421
    GlpkLp lp_glpk1,lp_glpk2;
deba@459
   422
    lpTest(lp_glpk1);
deba@459
   423
    aTest(lp_glpk2);
alpar@540
   424
    cloneTest<GlpkLp>();
deba@459
   425
  }
deba@458
   426
#endif
deba@458
   427
ladanyi@627
   428
#ifdef LEMON_HAVE_CPLEX
deba@459
   429
  try {
alpar@462
   430
    CplexLp lp_cplex1,lp_cplex2;
deba@459
   431
    lpTest(lp_cplex1);
deba@459
   432
    aTest(lp_cplex2);
deba@551
   433
    cloneTest<CplexLp>();
deba@459
   434
  } catch (CplexEnv::LicenseError& error) {
deba@459
   435
    check(false, error.what());
deba@459
   436
  }
deba@458
   437
#endif
deba@458
   438
ladanyi@627
   439
#ifdef LEMON_HAVE_SOPLEX
deba@459
   440
  {
alpar@462
   441
    SoplexLp lp_soplex1,lp_soplex2;
deba@459
   442
    lpTest(lp_soplex1);
deba@459
   443
    aTest(lp_soplex2);
alpar@540
   444
    cloneTest<SoplexLp>();
deba@459
   445
  }
deba@459
   446
#endif
deba@459
   447
ladanyi@627
   448
#ifdef LEMON_HAVE_CLP
deba@459
   449
  {
alpar@462
   450
    ClpLp lp_clp1,lp_clp2;
deba@459
   451
    lpTest(lp_clp1);
deba@459
   452
    aTest(lp_clp2);
alpar@540
   453
    cloneTest<ClpLp>();
deba@459
   454
  }
deba@458
   455
#endif
deba@458
   456
deba@458
   457
  return 0;
deba@458
   458
}