test/lp_test.cc
author kpeter
Sun, 05 Oct 2008 13:46:07 +0000
changeset 2621 814ba94d9989
parent 2571 f5c6e63f51d1
child 2637 bafe730864da
permissions -rw-r--r--
Bug fix in min_cost_flow_test.cc
alpar@1956
     1
/* -*- C++ -*-
alpar@1956
     2
 *
alpar@1956
     3
 * This file is a part of LEMON, a generic C++ optimization library
alpar@1956
     4
 *
alpar@2553
     5
 * Copyright (C) 2003-2008
alpar@1956
     6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
alpar@1956
     7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
alpar@1956
     8
 *
alpar@1956
     9
 * Permission to use, modify and distribute this software is granted
alpar@1956
    10
 * provided that this copyright notice appears in all copies. For
alpar@1956
    11
 * precise terms see the accompanying LICENSE file.
alpar@1956
    12
 *
alpar@1956
    13
 * This software is provided "AS IS" with no warranty of any kind,
alpar@1956
    14
 * express or implied, and with no claim as to its suitability for any
alpar@1956
    15
 * purpose.
alpar@1956
    16
 *
alpar@1956
    17
 */
alpar@1956
    18
athos@1543
    19
#include <sstream>
athos@1543
    20
#include <lemon/lp_skeleton.h>
athos@1473
    21
#include "test_tools.h"
athos@2264
    22
#include <lemon/tolerance.h>
deba@2605
    23
#include <lemon/lp_utils.h>
athos@1542
    24
ladanyi@1387
    25
#ifdef HAVE_CONFIG_H
alpar@2571
    26
#include <lemon/config.h>
ladanyi@1387
    27
#endif
ladanyi@1387
    28
ladanyi@1387
    29
#ifdef HAVE_GLPK
ladanyi@1387
    30
#include <lemon/lp_glpk.h>
ladanyi@1437
    31
#endif
ladanyi@1437
    32
ladanyi@1437
    33
#ifdef HAVE_CPLEX
ladanyi@1387
    34
#include <lemon/lp_cplex.h>
ladanyi@1387
    35
#endif
alpar@1254
    36
deba@2363
    37
#ifdef HAVE_SOPLEX
deba@2363
    38
#include <lemon/lp_soplex.h>
deba@2363
    39
#endif
deba@2314
    40
alpar@1254
    41
using namespace lemon;
alpar@1254
    42
alpar@1263
    43
void lpTest(LpSolverBase & lp)
alpar@1254
    44
{
athos@1508
    45
athos@1508
    46
athos@1508
    47
alpar@1263
    48
  typedef LpSolverBase LP;
alpar@1256
    49
alpar@1309
    50
  std::vector<LP::Col> x(10);
alpar@1309
    51
  //  for(int i=0;i<10;i++) x.push_back(lp.addCol());
alpar@1309
    52
  lp.addColSet(x);
alpar@1895
    53
  lp.colLowerBound(x,1);
alpar@1895
    54
  lp.colUpperBound(x,1);
alpar@1895
    55
  lp.colBounds(x,1,2);
athos@1508
    56
#ifndef GYORSITAS
athos@1508
    57
alpar@1256
    58
  std::vector<LP::Col> y(10);
alpar@1256
    59
  lp.addColSet(y);
alpar@1256
    60
alpar@1895
    61
  lp.colLowerBound(y,1);
alpar@1895
    62
  lp.colUpperBound(y,1);
alpar@1895
    63
  lp.colBounds(y,1,2);
alpar@1895
    64
alpar@1256
    65
  std::map<int,LP::Col> z;
alpar@1256
    66
  
alpar@1256
    67
  z.insert(std::make_pair(12,INVALID));
alpar@1256
    68
  z.insert(std::make_pair(2,INVALID));
alpar@1256
    69
  z.insert(std::make_pair(7,INVALID));
alpar@1256
    70
  z.insert(std::make_pair(5,INVALID));
alpar@1895
    71
alpar@1256
    72
  lp.addColSet(z);
alpar@1256
    73
alpar@1895
    74
  lp.colLowerBound(z,1);
alpar@1895
    75
  lp.colUpperBound(z,1);
alpar@1895
    76
  lp.colBounds(z,1,2);
alpar@1895
    77
alpar@1445
    78
  {
alpar@1445
    79
    LP::Expr e,f,g;
alpar@1445
    80
    LP::Col p1,p2,p3,p4,p5;
alpar@1445
    81
    LP::Constr c;
alpar@1445
    82
    
alpar@1484
    83
    p1=lp.addCol();
alpar@1484
    84
    p2=lp.addCol();
alpar@1484
    85
    p3=lp.addCol();
alpar@1484
    86
    p4=lp.addCol();
alpar@1484
    87
    p5=lp.addCol();
alpar@1484
    88
    
alpar@1445
    89
    e[p1]=2;
alpar@1445
    90
    e.constComp()=12;
alpar@1445
    91
    e[p1]+=2;
alpar@1445
    92
    e.constComp()+=12;
alpar@1445
    93
    e[p1]-=2;
alpar@1445
    94
    e.constComp()-=12;
alpar@1445
    95
    
alpar@1445
    96
    e=2;
alpar@1445
    97
    e=2.2;
alpar@1445
    98
    e=p1;
alpar@1445
    99
    e=f;
alpar@1445
   100
    
alpar@1445
   101
    e+=2;
alpar@1445
   102
    e+=2.2;
alpar@1445
   103
    e+=p1;
alpar@1445
   104
    e+=f;
alpar@1445
   105
    
alpar@1445
   106
    e-=2;
alpar@1445
   107
    e-=2.2;
alpar@1445
   108
    e-=p1;
alpar@1445
   109
    e-=f;
alpar@1445
   110
    
alpar@1445
   111
    e*=2;
alpar@1445
   112
    e*=2.2;
alpar@1445
   113
    e/=2;
alpar@1445
   114
    e/=2.2;
alpar@1445
   115
    
alpar@1445
   116
    e=((p1+p2)+(p1-p2)+(p1+12)+(12+p1)+(p1-12)+(12-p1)+
alpar@1445
   117
       (f+12)+(12+f)+(p1+f)+(f+p1)+(f+g)+
alpar@1445
   118
       (f-12)+(12-f)+(p1-f)+(f-p1)+(f-g)+
alpar@1445
   119
       2.2*f+f*2.2+f/2.2+
alpar@1445
   120
       2*f+f*2+f/2+
alpar@1445
   121
       2.2*p1+p1*2.2+p1/2.2+
alpar@1445
   122
       2*p1+p1*2+p1/2
alpar@1445
   123
       );
alpar@1256
   124
alpar@1445
   125
alpar@1445
   126
    c = (e  <= f  );
alpar@1445
   127
    c = (e  <= 2.2);
alpar@1445
   128
    c = (e  <= 2  );
alpar@1445
   129
    c = (e  <= p1 );
alpar@1445
   130
    c = (2.2<= f  );
alpar@1445
   131
    c = (2  <= f  );
alpar@1445
   132
    c = (p1 <= f  );
alpar@1445
   133
    c = (p1 <= p2 );
alpar@1445
   134
    c = (p1 <= 2.2);
alpar@1445
   135
    c = (p1 <= 2  );
alpar@1445
   136
    c = (2.2<= p2 );
alpar@1445
   137
    c = (2  <= p2 );
alpar@1445
   138
    
alpar@1445
   139
    c = (e  >= f  );
alpar@1445
   140
    c = (e  >= 2.2);
alpar@1445
   141
    c = (e  >= 2  );
alpar@1445
   142
    c = (e  >= p1 );
alpar@1445
   143
    c = (2.2>= f  );
alpar@1445
   144
    c = (2  >= f  );
alpar@1445
   145
    c = (p1 >= f  );
alpar@1445
   146
    c = (p1 >= p2 );
alpar@1445
   147
    c = (p1 >= 2.2);
alpar@1445
   148
    c = (p1 >= 2  );
alpar@1445
   149
    c = (2.2>= p2 );
alpar@1445
   150
    c = (2  >= p2 );
alpar@1445
   151
    
alpar@1445
   152
    c = (e  == f  );
alpar@1445
   153
    c = (e  == 2.2);
alpar@1445
   154
    c = (e  == 2  );
alpar@1445
   155
    c = (e  == p1 );
alpar@1445
   156
    c = (2.2== f  );
alpar@1445
   157
    c = (2  == f  );
alpar@1445
   158
    c = (p1 == f  );
alpar@1445
   159
    //c = (p1 == p2 );
alpar@1445
   160
    c = (p1 == 2.2);
alpar@1445
   161
    c = (p1 == 2  );
alpar@1445
   162
    c = (2.2== p2 );
alpar@1445
   163
    c = (2  == p2 );
alpar@1445
   164
    
alpar@1445
   165
    c = (2 <= e <= 3);
alpar@1445
   166
    c = (2 <= p1<= 3);
alpar@1445
   167
    
alpar@1445
   168
    c = (2 >= e >= 3);
alpar@1445
   169
    c = (2 >= p1>= 3);
alpar@1445
   170
    
alpar@1445
   171
    e[x[3]]=2;
alpar@1445
   172
    e[x[3]]=4;
alpar@1445
   173
    e[x[3]]=1;
alpar@1445
   174
    e.constComp()=12;
alpar@1445
   175
    
alpar@1445
   176
    lp.addRow(LP::INF,e,23);
alpar@1445
   177
    lp.addRow(LP::INF,3.0*(x[1]+x[2]/2)-x[3],23);
alpar@1445
   178
    lp.addRow(LP::INF,3.0*(x[1]+x[2]*2-5*x[3]+12-x[4]/3)+2*x[4]-4,23);
alpar@1445
   179
    
alpar@1445
   180
    lp.addRow(x[1]+x[3]<=x[5]-3);
alpar@1445
   181
    lp.addRow(-7<=x[1]+x[3]-12<=3);
alpar@1445
   182
    lp.addRow(x[1]<=x[5]);
athos@2264
   183
athos@2264
   184
    std::ostringstream buf;
athos@2264
   185
athos@2264
   186
athos@2264
   187
    //Checking the simplify function
athos@2264
   188
athos@2264
   189
//     //How to check the simplify function? A map gives no information
athos@2264
   190
//     //on the question whether a given key is or is not stored in it, or
athos@2264
   191
//     //it does?
athos@2264
   192
//   Yes, it does, using the find() function.
athos@2264
   193
    e=((p1+p2)+(p1-p2));
athos@2264
   194
    e.simplify();
athos@2264
   195
    buf << "Coeff. of p2 should be 0";
athos@2264
   196
    //    std::cout<<e[p1]<<e[p2]<<e[p3]<<std::endl;
athos@2264
   197
    check(e.find(p2)==e.end(), buf.str());
athos@2345
   198
    
athos@2264
   199
     
athos@2264
   200
athos@2264
   201
athos@2264
   202
    e=((p1+p2)+(p1-0.99*p2));
athos@2345
   203
    //e.prettyPrint(std::cout);
athos@2345
   204
    //(e<=2).prettyPrint(std::cout);
athos@2264
   205
    double tolerance=0.001;
athos@2264
   206
    e.simplify(tolerance);
athos@2264
   207
    buf << "Coeff. of p2 should be 0.01";
athos@2264
   208
    check(e[p2]>0, buf.str());
athos@2264
   209
    
athos@2264
   210
    tolerance=0.02;
athos@2264
   211
    e.simplify(tolerance);
athos@2264
   212
    buf << "Coeff. of p2 should be 0";
athos@2264
   213
    check(e.find(p2)==e.end(), buf.str());
athos@2264
   214
    
athos@2264
   215
alpar@1445
   216
  }
alpar@1272
   217
  
alpar@1445
   218
  {
alpar@1445
   219
    LP::DualExpr e,f,g;
deba@2605
   220
    LP::Row p1 = INVALID, p2 = INVALID, p3 = INVALID, 
deba@2605
   221
      p4 = INVALID, p5 = INVALID;
alpar@1445
   222
    
alpar@1445
   223
    e[p1]=2;
alpar@1445
   224
    e[p1]+=2;
alpar@1445
   225
    e[p1]-=2;
alpar@1445
   226
    
alpar@1445
   227
    e=p1;
alpar@1445
   228
    e=f;
alpar@1445
   229
    
alpar@1445
   230
    e+=p1;
alpar@1445
   231
    e+=f;
alpar@1445
   232
    
alpar@1445
   233
    e-=p1;
alpar@1445
   234
    e-=f;
alpar@1445
   235
    
alpar@1445
   236
    e*=2;
alpar@1445
   237
    e*=2.2;
alpar@1445
   238
    e/=2;
alpar@1445
   239
    e/=2.2;
alpar@1445
   240
    
alpar@1493
   241
    e=((p1+p2)+(p1-p2)+
alpar@1445
   242
       (p1+f)+(f+p1)+(f+g)+
alpar@1445
   243
       (p1-f)+(f-p1)+(f-g)+
alpar@1445
   244
       2.2*f+f*2.2+f/2.2+
alpar@1445
   245
       2*f+f*2+f/2+
alpar@1445
   246
       2.2*p1+p1*2.2+p1/2.2+
alpar@1445
   247
       2*p1+p1*2+p1/2
alpar@1445
   248
       );
alpar@1445
   249
  }
alpar@1272
   250
  
athos@1508
   251
#endif
alpar@1264
   252
}
alpar@1264
   253
athos@1542
   254
void solveAndCheck(LpSolverBase& lp, LpSolverBase::SolutionStatus stat, 
athos@1543
   255
		   double exp_opt) {
athos@1543
   256
  using std::string;
athos@1542
   257
  lp.solve();
athos@1542
   258
  //int decimal,sign;
athos@1543
   259
  std::ostringstream buf;
athos@1543
   260
  buf << "Primalstatus should be: " << int(stat);
athos@1543
   261
athos@1542
   262
  //  itoa(stat,buf1, 10);
athos@1543
   263
  check(lp.primalStatus()==stat, buf.str());
athos@1543
   264
athos@1543
   265
  if (stat ==  LpSolverBase::OPTIMAL) {
deba@2386
   266
    std::ostringstream sbuf;
deba@2386
   267
    sbuf << "Wrong optimal value: the right optimum is " << exp_opt; 
deba@2386
   268
    check(std::abs(lp.primalValue()-exp_opt) < 1e-3, sbuf.str());
athos@1542
   269
    //+ecvt(exp_opt,2)
athos@1542
   270
  }
athos@1542
   271
}
athos@1542
   272
 
athos@1473
   273
void aTest(LpSolverBase & lp)
athos@1473
   274
{
athos@1473
   275
  typedef LpSolverBase LP;
athos@1473
   276
athos@1542
   277
 //The following example is very simple
athos@1473
   278
athos@1473
   279
  typedef LpSolverBase::Row Row;
athos@1473
   280
  typedef LpSolverBase::Col Col;
athos@1473
   281
athos@1473
   282
athos@1473
   283
  Col x1 = lp.addCol();
athos@1473
   284
  Col x2 = lp.addCol();
athos@1473
   285
athos@1473
   286
athos@1473
   287
  //Constraints
athos@1542
   288
  Row upright=lp.addRow(x1+x2 <=1);  
athos@1542
   289
  lp.addRow(x1+x2 >=-1);  
athos@1542
   290
  lp.addRow(x1-x2 <=1);  
athos@1542
   291
  lp.addRow(x1-x2 >=-1);  
athos@1473
   292
  //Nonnegativity of the variables
athos@1473
   293
  lp.colLowerBound(x1, 0);
athos@1473
   294
  lp.colLowerBound(x2, 0);
athos@1473
   295
  //Objective function
deba@2369
   296
  lp.obj(x1+x2);
athos@1473
   297
athos@1473
   298
  lp.max();
athos@1473
   299
athos@2324
   300
  //Testing the problem retrieving routines
athos@2324
   301
  check(lp.objCoeff(x1)==1,"First term should be 1 in the obj function!");
deba@2369
   302
  check(lp.isMax(),"This is a maximization!");
athos@2328
   303
  check(lp.coeff(upright,x1)==1,"The coefficient in question is 1!");
athos@2328
   304
  //  std::cout<<lp.colLowerBound(x1)<<std::endl;
athos@2328
   305
  check(  lp.colLowerBound(x1)==0,"The lower bound for variable x1 should be 0.");
athos@2328
   306
  check(  lp.colUpperBound(x1)==LpSolverBase::INF,"The upper bound for variable x1 should be infty.");
athos@2328
   307
  LpSolverBase::Value lb,ub;
athos@2328
   308
  lp.getRowBounds(upright,lb,ub);
athos@2328
   309
  check(  lb==-LpSolverBase::INF,"The lower bound for the first row should be -infty.");
athos@2328
   310
  check(  ub==1,"The upper bound for the first row should be 1.");
deba@2605
   311
  LpSolverBase::Expr e = lp.row(upright);
deba@2605
   312
  check(  e.size() == 2, "The row retrieval gives back wrong expression.");
deba@2605
   313
  check(  e[x1] == 1, "The first coefficient should 1.");
deba@2605
   314
  check(  e[x2] == 1, "The second coefficient should 1.");
athos@2324
   315
deba@2605
   316
  LpSolverBase::DualExpr de = lp.col(x1);
deba@2605
   317
  check(  de.size() == 4, "The col retrieval gives back wrong expression.");
deba@2605
   318
  check(  de[upright] == 1, "The first coefficient should 1.");
deba@2605
   319
deba@2605
   320
  LpSolverBase* clp = lp.copyLp();
deba@2605
   321
deba@2605
   322
  //Testing the problem retrieving routines
deba@2605
   323
  check(clp->objCoeff(x1)==1,"First term should be 1 in the obj function!");
deba@2605
   324
  check(clp->isMax(),"This is a maximization!");
deba@2605
   325
  check(clp->coeff(upright,x1)==1,"The coefficient in question is 1!");
deba@2605
   326
  //  std::cout<<lp.colLowerBound(x1)<<std::endl;
deba@2605
   327
  check(  clp->colLowerBound(x1)==0,"The lower bound for variable x1 should be 0.");
deba@2605
   328
  std::cerr << clp->colUpperBound(x1) << std::endl;
deba@2605
   329
  check(  clp->colUpperBound(x1)==LpSolverBase::INF,"The upper bound for variable x1 should be infty.");
deba@2605
   330
deba@2605
   331
  clp->getRowBounds(upright,lb,ub);
deba@2605
   332
  check(  lb==-LpSolverBase::INF,"The lower bound for the first row should be -infty.");
deba@2605
   333
  check(  ub==1,"The upper bound for the first row should be 1.");
deba@2605
   334
  e = clp->row(upright);
deba@2605
   335
  check(  e.size() == 2, "The row retrieval gives back wrong expression.");
deba@2605
   336
  check(  e[x1] == 1, "The first coefficient should 1.");
deba@2605
   337
  check(  e[x2] == 1, "The second coefficient should 1.");
deba@2605
   338
deba@2605
   339
  de = clp->col(x1);
deba@2605
   340
  check(  de.size() == 4, "The col retrieval gives back wrong expression.");
deba@2605
   341
  check(  de[upright] == 1, "The first coefficient should 1.");
deba@2605
   342
  
deba@2605
   343
  delete clp;
athos@2324
   344
athos@1542
   345
  //Maximization of x1+x2
athos@1542
   346
  //over the triangle with vertices (0,0) (0,1) (1,0)
athos@1542
   347
  double expected_opt=1;
athos@1542
   348
  solveAndCheck(lp, LpSolverBase::OPTIMAL, expected_opt);
alpar@1484
   349
  
athos@1542
   350
  //Minimization
athos@1542
   351
  lp.min();
athos@1542
   352
  expected_opt=0;
athos@1542
   353
  solveAndCheck(lp, LpSolverBase::OPTIMAL, expected_opt);
athos@1542
   354
  
athos@1542
   355
  //Vertex (-1,0) instead of (0,0)
athos@1542
   356
  lp.colLowerBound(x1, -LpSolverBase::INF);
athos@1542
   357
  expected_opt=-1;
athos@1542
   358
  solveAndCheck(lp, LpSolverBase::OPTIMAL, expected_opt);
athos@1542
   359
athos@1542
   360
  //Erase one constraint and return to maximization
athos@1542
   361
  lp.eraseRow(upright);
athos@1542
   362
  lp.max();
athos@1542
   363
  expected_opt=LpSolverBase::INF;
athos@1542
   364
  solveAndCheck(lp, LpSolverBase::INFINITE, expected_opt);
athos@1542
   365
athos@1542
   366
  //Infeasibilty
athos@1542
   367
  lp.addRow(x1+x2 <=-2);  
athos@1542
   368
  solveAndCheck(lp, LpSolverBase::INFEASIBLE, expected_opt);
athos@1542
   369
athos@1542
   370
  //Change problem and forget to solve
athos@1542
   371
  lp.min();
athos@1542
   372
  check(lp.primalStatus()==LpSolverBase::UNDEFINED,"Primalstatus should be UNDEFINED");
athos@1542
   373
deba@2605
   374
athos@1542
   375
//   lp.solve();
athos@1542
   376
//   if (lp.primalStatus()==LpSolverBase::OPTIMAL){
athos@1542
   377
//     std::cout<< "Z = "<<lp.primalValue()
athos@1542
   378
// 	     << " (error = " << lp.primalValue()-expected_opt
athos@1542
   379
// 	     << "); x1 = "<<lp.primal(x1)
athos@1542
   380
// 	     << "; x2 = "<<lp.primal(x2)
athos@1542
   381
// 	     <<std::endl;
alpar@1484
   382
    
athos@1542
   383
//   }
athos@1542
   384
//   else{
athos@1542
   385
//     std::cout<<lp.primalStatus()<<std::endl;
athos@1542
   386
//     std::cout<<"Optimal solution not found!"<<std::endl;
athos@1542
   387
//   }
athos@1473
   388
athos@1542
   389
 
athos@1473
   390
athos@1473
   391
}
athos@1473
   392
athos@1473
   393
alpar@1263
   394
int main() 
alpar@1263
   395
{
alpar@1390
   396
  LpSkeleton lp_skel;
alpar@1390
   397
  lpTest(lp_skel);
alpar@1390
   398
ladanyi@1437
   399
#ifdef HAVE_GLPK
alpar@1484
   400
  LpGlpk lp_glpk1,lp_glpk2;
alpar@1484
   401
  lpTest(lp_glpk1);
alpar@1484
   402
  aTest(lp_glpk2);
ladanyi@1437
   403
#endif
alpar@1263
   404
ladanyi@1437
   405
#ifdef HAVE_CPLEX
klao@1797
   406
  LpCplex lp_cplex1,lp_cplex2;
klao@1797
   407
  lpTest(lp_cplex1);
klao@1797
   408
  aTest(lp_cplex2);
ladanyi@1437
   409
#endif
alpar@1264
   410
deba@2363
   411
#ifdef HAVE_SOPLEX
deba@2363
   412
  LpSoplex lp_soplex1,lp_soplex2;
deba@2363
   413
  lpTest(lp_soplex1);
deba@2363
   414
  aTest(lp_soplex2);
deba@2363
   415
#endif
deba@2314
   416
alpar@1309
   417
  return 0;
alpar@1263
   418
}