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