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