test/lp_test.cc
author Balazs Dezso <deba@inf.elte.hu>
Tue, 02 Dec 2008 21:40:33 +0100
changeset 458 7afc121e0689
child 459 ed54c0d13df0
permissions -rw-r--r--
Port LP and MIP solvers from SVN -r3509 (#44)
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@458
     5
 * Copyright (C) 2003-2008
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
#ifdef HAVE_CONFIG_H
deba@458
    25
#include <lemon/config.h>
deba@458
    26
#endif
deba@458
    27
deba@458
    28
#ifdef HAVE_GLPK
deba@458
    29
#include <lemon/lp_glpk.h>
deba@458
    30
#endif
deba@458
    31
deba@458
    32
#ifdef HAVE_CPLEX
deba@458
    33
#include <lemon/lp_cplex.h>
deba@458
    34
#endif
deba@458
    35
deba@458
    36
#ifdef HAVE_SOPLEX
deba@458
    37
#include <lemon/lp_soplex.h>
deba@458
    38
#endif
deba@458
    39
deba@458
    40
using namespace lemon;
deba@458
    41
deba@458
    42
void lpTest(LpSolverBase & lp)
deba@458
    43
{
deba@458
    44
deba@458
    45
deba@458
    46
deba@458
    47
  typedef LpSolverBase 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
#ifndef GYORSITAS
deba@458
    56
deba@458
    57
  std::vector<LP::Col> y(10);
deba@458
    58
  lp.addColSet(y);
deba@458
    59
deba@458
    60
  lp.colLowerBound(y,1);
deba@458
    61
  lp.colUpperBound(y,1);
deba@458
    62
  lp.colBounds(y,1,2);
deba@458
    63
deba@458
    64
  std::map<int,LP::Col> z;
deba@458
    65
deba@458
    66
  z.insert(std::make_pair(12,INVALID));
deba@458
    67
  z.insert(std::make_pair(2,INVALID));
deba@458
    68
  z.insert(std::make_pair(7,INVALID));
deba@458
    69
  z.insert(std::make_pair(5,INVALID));
deba@458
    70
deba@458
    71
  lp.addColSet(z);
deba@458
    72
deba@458
    73
  lp.colLowerBound(z,1);
deba@458
    74
  lp.colUpperBound(z,1);
deba@458
    75
  lp.colBounds(z,1,2);
deba@458
    76
deba@458
    77
  {
deba@458
    78
    LP::Expr e,f,g;
deba@458
    79
    LP::Col p1,p2,p3,p4,p5;
deba@458
    80
    LP::Constr c;
deba@458
    81
deba@458
    82
    p1=lp.addCol();
deba@458
    83
    p2=lp.addCol();
deba@458
    84
    p3=lp.addCol();
deba@458
    85
    p4=lp.addCol();
deba@458
    86
    p5=lp.addCol();
deba@458
    87
deba@458
    88
    e[p1]=2;
deba@458
    89
    e.constComp()=12;
deba@458
    90
    e[p1]+=2;
deba@458
    91
    e.constComp()+=12;
deba@458
    92
    e[p1]-=2;
deba@458
    93
    e.constComp()-=12;
deba@458
    94
deba@458
    95
    e=2;
deba@458
    96
    e=2.2;
deba@458
    97
    e=p1;
deba@458
    98
    e=f;
deba@458
    99
deba@458
   100
    e+=2;
deba@458
   101
    e+=2.2;
deba@458
   102
    e+=p1;
deba@458
   103
    e+=f;
deba@458
   104
deba@458
   105
    e-=2;
deba@458
   106
    e-=2.2;
deba@458
   107
    e-=p1;
deba@458
   108
    e-=f;
deba@458
   109
deba@458
   110
    e*=2;
deba@458
   111
    e*=2.2;
deba@458
   112
    e/=2;
deba@458
   113
    e/=2.2;
deba@458
   114
deba@458
   115
    e=((p1+p2)+(p1-p2)+(p1+12)+(12+p1)+(p1-12)+(12-p1)+
deba@458
   116
       (f+12)+(12+f)+(p1+f)+(f+p1)+(f+g)+
deba@458
   117
       (f-12)+(12-f)+(p1-f)+(f-p1)+(f-g)+
deba@458
   118
       2.2*f+f*2.2+f/2.2+
deba@458
   119
       2*f+f*2+f/2+
deba@458
   120
       2.2*p1+p1*2.2+p1/2.2+
deba@458
   121
       2*p1+p1*2+p1/2
deba@458
   122
       );
deba@458
   123
deba@458
   124
deba@458
   125
    c = (e  <= f  );
deba@458
   126
    c = (e  <= 2.2);
deba@458
   127
    c = (e  <= 2  );
deba@458
   128
    c = (e  <= p1 );
deba@458
   129
    c = (2.2<= f  );
deba@458
   130
    c = (2  <= f  );
deba@458
   131
    c = (p1 <= f  );
deba@458
   132
    c = (p1 <= p2 );
deba@458
   133
    c = (p1 <= 2.2);
deba@458
   134
    c = (p1 <= 2  );
deba@458
   135
    c = (2.2<= p2 );
deba@458
   136
    c = (2  <= p2 );
deba@458
   137
deba@458
   138
    c = (e  >= f  );
deba@458
   139
    c = (e  >= 2.2);
deba@458
   140
    c = (e  >= 2  );
deba@458
   141
    c = (e  >= p1 );
deba@458
   142
    c = (2.2>= f  );
deba@458
   143
    c = (2  >= f  );
deba@458
   144
    c = (p1 >= f  );
deba@458
   145
    c = (p1 >= p2 );
deba@458
   146
    c = (p1 >= 2.2);
deba@458
   147
    c = (p1 >= 2  );
deba@458
   148
    c = (2.2>= p2 );
deba@458
   149
    c = (2  >= p2 );
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 = (2 <= e <= 3);
deba@458
   165
    c = (2 <= p1<= 3);
deba@458
   166
deba@458
   167
    c = (2 >= e >= 3);
deba@458
   168
    c = (2 >= p1>= 3);
deba@458
   169
deba@458
   170
    e[x[3]]=2;
deba@458
   171
    e[x[3]]=4;
deba@458
   172
    e[x[3]]=1;
deba@458
   173
    e.constComp()=12;
deba@458
   174
deba@458
   175
    lp.addRow(LP::INF,e,23);
deba@458
   176
    lp.addRow(LP::INF,3.0*(x[1]+x[2]/2)-x[3],23);
deba@458
   177
    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
   178
deba@458
   179
    lp.addRow(x[1]+x[3]<=x[5]-3);
deba@458
   180
    lp.addRow(-7<=x[1]+x[3]-12<=3);
deba@458
   181
    lp.addRow(x[1]<=x[5]);
deba@458
   182
deba@458
   183
    std::ostringstream buf;
deba@458
   184
deba@458
   185
deba@458
   186
    //Checking the simplify function
deba@458
   187
deba@458
   188
//     //How to check the simplify function? A map gives no information
deba@458
   189
//     //on the question whether a given key is or is not stored in it, or
deba@458
   190
//     //it does?
deba@458
   191
//   Yes, it does, using the find() function.
deba@458
   192
    e=((p1+p2)+(p1-p2));
deba@458
   193
    e.simplify();
deba@458
   194
    buf << "Coeff. of p2 should be 0";
deba@458
   195
    //    std::cout<<e[p1]<<e[p2]<<e[p3]<<std::endl;
deba@458
   196
    check(e.find(p2)==e.end(), buf.str());
deba@458
   197
deba@458
   198
deba@458
   199
deba@458
   200
deba@458
   201
    e=((p1+p2)+(p1-0.99*p2));
deba@458
   202
    //e.prettyPrint(std::cout);
deba@458
   203
    //(e<=2).prettyPrint(std::cout);
deba@458
   204
    double tolerance=0.001;
deba@458
   205
    e.simplify(tolerance);
deba@458
   206
    buf << "Coeff. of p2 should be 0.01";
deba@458
   207
    check(e[p2]>0, buf.str());
deba@458
   208
deba@458
   209
    tolerance=0.02;
deba@458
   210
    e.simplify(tolerance);
deba@458
   211
    buf << "Coeff. of p2 should be 0";
deba@458
   212
    check(e.find(p2)==e.end(), buf.str());
deba@458
   213
deba@458
   214
deba@458
   215
  }
deba@458
   216
deba@458
   217
  {
deba@458
   218
    LP::DualExpr e,f,g;
deba@458
   219
    LP::Row p1 = INVALID, p2 = INVALID, p3 = INVALID,
deba@458
   220
      p4 = INVALID, p5 = INVALID;
deba@458
   221
deba@458
   222
    e[p1]=2;
deba@458
   223
    e[p1]+=2;
deba@458
   224
    e[p1]-=2;
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-=p1;
deba@458
   233
    e-=f;
deba@458
   234
deba@458
   235
    e*=2;
deba@458
   236
    e*=2.2;
deba@458
   237
    e/=2;
deba@458
   238
    e/=2.2;
deba@458
   239
deba@458
   240
    e=((p1+p2)+(p1-p2)+
deba@458
   241
       (p1+f)+(f+p1)+(f+g)+
deba@458
   242
       (p1-f)+(f-p1)+(f-g)+
deba@458
   243
       2.2*f+f*2.2+f/2.2+
deba@458
   244
       2*f+f*2+f/2+
deba@458
   245
       2.2*p1+p1*2.2+p1/2.2+
deba@458
   246
       2*p1+p1*2+p1/2
deba@458
   247
       );
deba@458
   248
  }
deba@458
   249
deba@458
   250
#endif
deba@458
   251
}
deba@458
   252
deba@458
   253
void solveAndCheck(LpSolverBase& lp, LpSolverBase::SolutionStatus stat,
deba@458
   254
                   double exp_opt) {
deba@458
   255
  using std::string;
deba@458
   256
  lp.solve();
deba@458
   257
  //int decimal,sign;
deba@458
   258
  std::ostringstream buf;
deba@458
   259
  buf << "Primalstatus should be: " << int(stat);
deba@458
   260
deba@458
   261
  //  itoa(stat,buf1, 10);
deba@458
   262
  check(lp.primalStatus()==stat, buf.str());
deba@458
   263
deba@458
   264
  if (stat ==  LpSolverBase::OPTIMAL) {
deba@458
   265
    std::ostringstream sbuf;
deba@458
   266
    sbuf << "Wrong optimal value: the right optimum is " << exp_opt;
deba@458
   267
    check(std::abs(lp.primalValue()-exp_opt) < 1e-3, sbuf.str());
deba@458
   268
    //+ecvt(exp_opt,2)
deba@458
   269
  }
deba@458
   270
}
deba@458
   271
deba@458
   272
void aTest(LpSolverBase & lp)
deba@458
   273
{
deba@458
   274
  typedef LpSolverBase LP;
deba@458
   275
deba@458
   276
 //The following example is very simple
deba@458
   277
deba@458
   278
  typedef LpSolverBase::Row Row;
deba@458
   279
  typedef LpSolverBase::Col Col;
deba@458
   280
deba@458
   281
deba@458
   282
  Col x1 = lp.addCol();
deba@458
   283
  Col x2 = lp.addCol();
deba@458
   284
deba@458
   285
deba@458
   286
  //Constraints
deba@458
   287
  Row upright=lp.addRow(x1+x2 <=1);
deba@458
   288
  lp.addRow(x1+x2 >=-1);
deba@458
   289
  lp.addRow(x1-x2 <=1);
deba@458
   290
  lp.addRow(x1-x2 >=-1);
deba@458
   291
  //Nonnegativity of the variables
deba@458
   292
  lp.colLowerBound(x1, 0);
deba@458
   293
  lp.colLowerBound(x2, 0);
deba@458
   294
  //Objective function
deba@458
   295
  lp.obj(x1+x2);
deba@458
   296
deba@458
   297
  lp.max();
deba@458
   298
deba@458
   299
  //Testing the problem retrieving routines
deba@458
   300
  check(lp.objCoeff(x1)==1,"First term should be 1 in the obj function!");
deba@458
   301
  check(lp.isMax(),"This is a maximization!");
deba@458
   302
  check(lp.coeff(upright,x1)==1,"The coefficient in question is 1!");
deba@458
   303
  //  std::cout<<lp.colLowerBound(x1)<<std::endl;
deba@458
   304
  check(  lp.colLowerBound(x1)==0,
deba@458
   305
          "The lower bound for variable x1 should be 0.");
deba@458
   306
  check(  lp.colUpperBound(x1)==LpSolverBase::INF,
deba@458
   307
          "The upper bound for variable x1 should be infty.");
deba@458
   308
  LpSolverBase::Value lb,ub;
deba@458
   309
  lp.getRowBounds(upright,lb,ub);
deba@458
   310
  check(  lb==-LpSolverBase::INF,
deba@458
   311
          "The lower bound for the first row should be -infty.");
deba@458
   312
  check(  ub==1,"The upper bound for the first row should be 1.");
deba@458
   313
  LpSolverBase::Expr e = lp.row(upright);
deba@458
   314
  check(  e.size() == 2, "The row retrieval gives back wrong expression.");
deba@458
   315
  check(  e[x1] == 1, "The first coefficient should 1.");
deba@458
   316
  check(  e[x2] == 1, "The second coefficient should 1.");
deba@458
   317
deba@458
   318
  LpSolverBase::DualExpr de = lp.col(x1);
deba@458
   319
  check(  de.size() == 4, "The col retrieval gives back wrong expression.");
deba@458
   320
  check(  de[upright] == 1, "The first coefficient should 1.");
deba@458
   321
deba@458
   322
  LpSolverBase* clp = lp.copyLp();
deba@458
   323
deba@458
   324
  //Testing the problem retrieving routines
deba@458
   325
  check(clp->objCoeff(x1)==1,"First term should be 1 in the obj function!");
deba@458
   326
  check(clp->isMax(),"This is a maximization!");
deba@458
   327
  check(clp->coeff(upright,x1)==1,"The coefficient in question is 1!");
deba@458
   328
  //  std::cout<<lp.colLowerBound(x1)<<std::endl;
deba@458
   329
  check(  clp->colLowerBound(x1)==0,
deba@458
   330
          "The lower bound for variable x1 should be 0.");
deba@458
   331
  check(  clp->colUpperBound(x1)==LpSolverBase::INF,
deba@458
   332
          "The upper bound for variable x1 should be infty.");
deba@458
   333
deba@458
   334
  clp->getRowBounds(upright,lb,ub);
deba@458
   335
  check(  lb==-LpSolverBase::INF,
deba@458
   336
          "The lower bound for the first row should be -infty.");
deba@458
   337
  check(  ub==1,"The upper bound for the first row should be 1.");
deba@458
   338
  e = clp->row(upright);
deba@458
   339
  check(  e.size() == 2, "The row retrieval gives back wrong expression.");
deba@458
   340
  check(  e[x1] == 1, "The first coefficient should 1.");
deba@458
   341
  check(  e[x2] == 1, "The second coefficient should 1.");
deba@458
   342
deba@458
   343
  de = clp->col(x1);
deba@458
   344
  check(  de.size() == 4, "The col retrieval gives back wrong expression.");
deba@458
   345
  check(  de[upright] == 1, "The first coefficient should 1.");
deba@458
   346
deba@458
   347
  delete clp;
deba@458
   348
deba@458
   349
  //Maximization of x1+x2
deba@458
   350
  //over the triangle with vertices (0,0) (0,1) (1,0)
deba@458
   351
  double expected_opt=1;
deba@458
   352
  solveAndCheck(lp, LpSolverBase::OPTIMAL, expected_opt);
deba@458
   353
deba@458
   354
  //Minimization
deba@458
   355
  lp.min();
deba@458
   356
  expected_opt=0;
deba@458
   357
  solveAndCheck(lp, LpSolverBase::OPTIMAL, expected_opt);
deba@458
   358
deba@458
   359
  //Vertex (-1,0) instead of (0,0)
deba@458
   360
  lp.colLowerBound(x1, -LpSolverBase::INF);
deba@458
   361
  expected_opt=-1;
deba@458
   362
  solveAndCheck(lp, LpSolverBase::OPTIMAL, expected_opt);
deba@458
   363
deba@458
   364
  //Erase one constraint and return to maximization
deba@458
   365
  lp.eraseRow(upright);
deba@458
   366
  lp.max();
deba@458
   367
  expected_opt=LpSolverBase::INF;
deba@458
   368
  solveAndCheck(lp, LpSolverBase::INFINITE, expected_opt);
deba@458
   369
deba@458
   370
  //Infeasibilty
deba@458
   371
  lp.addRow(x1+x2 <=-2);
deba@458
   372
  solveAndCheck(lp, LpSolverBase::INFEASIBLE, expected_opt);
deba@458
   373
deba@458
   374
  //Change problem and forget to solve
deba@458
   375
  lp.min();
deba@458
   376
  check(lp.primalStatus()==LpSolverBase::UNDEFINED,
deba@458
   377
        "Primalstatus should be UNDEFINED");
deba@458
   378
deba@458
   379
deba@458
   380
//   lp.solve();
deba@458
   381
//   if (lp.primalStatus()==LpSolverBase::OPTIMAL){
deba@458
   382
//     std::cout<< "Z = "<<lp.primalValue()
deba@458
   383
//              << " (error = " << lp.primalValue()-expected_opt
deba@458
   384
//              << "); x1 = "<<lp.primal(x1)
deba@458
   385
//              << "; x2 = "<<lp.primal(x2)
deba@458
   386
//              <<std::endl;
deba@458
   387
deba@458
   388
//   }
deba@458
   389
//   else{
deba@458
   390
//     std::cout<<lp.primalStatus()<<std::endl;
deba@458
   391
//     std::cout<<"Optimal solution not found!"<<std::endl;
deba@458
   392
//   }
deba@458
   393
deba@458
   394
deba@458
   395
deba@458
   396
}
deba@458
   397
deba@458
   398
deba@458
   399
int main()
deba@458
   400
{
deba@458
   401
  LpSkeleton lp_skel;
deba@458
   402
  lpTest(lp_skel);
deba@458
   403
deba@458
   404
#ifdef HAVE_GLPK
deba@458
   405
  LpGlpk lp_glpk1,lp_glpk2;
deba@458
   406
  lpTest(lp_glpk1);
deba@458
   407
  aTest(lp_glpk2);
deba@458
   408
#endif
deba@458
   409
deba@458
   410
#ifdef HAVE_CPLEX
deba@458
   411
  LpCplex lp_cplex1,lp_cplex2;
deba@458
   412
  lpTest(lp_cplex1);
deba@458
   413
  aTest(lp_cplex2);
deba@458
   414
#endif
deba@458
   415
deba@458
   416
#ifdef HAVE_SOPLEX
deba@458
   417
  LpSoplex lp_soplex1,lp_soplex2;
deba@458
   418
  lpTest(lp_soplex1);
deba@458
   419
  aTest(lp_soplex2);
deba@458
   420
#endif
deba@458
   421
deba@458
   422
  return 0;
deba@458
   423
}