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