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