test/lp_test.cc
author alpar
Wed, 18 Jan 2006 09:40:53 +0000
changeset 1897 dd90f57b01d1
parent 1797 91b8b9cea2f7
child 1956 a055123339d5
permissions -rw-r--r--
Tolerance<long double> added
athos@1543
     1
#include <sstream>
athos@1543
     2
#include <lemon/lp_skeleton.h>
athos@1473
     3
#include "test_tools.h"
alpar@1390
     4
athos@1542
     5
ladanyi@1387
     6
#ifdef HAVE_CONFIG_H
ladanyi@1387
     7
#include <config.h>
ladanyi@1387
     8
#endif
ladanyi@1387
     9
ladanyi@1387
    10
#ifdef HAVE_GLPK
ladanyi@1387
    11
#include <lemon/lp_glpk.h>
ladanyi@1437
    12
#endif
ladanyi@1437
    13
ladanyi@1437
    14
#ifdef HAVE_CPLEX
ladanyi@1387
    15
#include <lemon/lp_cplex.h>
ladanyi@1387
    16
#endif
alpar@1254
    17
alpar@1254
    18
using namespace lemon;
alpar@1254
    19
alpar@1263
    20
void lpTest(LpSolverBase & lp)
alpar@1254
    21
{
athos@1508
    22
athos@1508
    23
athos@1508
    24
alpar@1263
    25
  typedef LpSolverBase LP;
alpar@1256
    26
alpar@1309
    27
  std::vector<LP::Col> x(10);
alpar@1309
    28
  //  for(int i=0;i<10;i++) x.push_back(lp.addCol());
alpar@1309
    29
  lp.addColSet(x);
alpar@1895
    30
  lp.colLowerBound(x,1);
alpar@1895
    31
  lp.colUpperBound(x,1);
alpar@1895
    32
  lp.colBounds(x,1,2);
athos@1508
    33
#ifndef GYORSITAS
athos@1508
    34
alpar@1256
    35
  std::vector<LP::Col> y(10);
alpar@1256
    36
  lp.addColSet(y);
alpar@1256
    37
alpar@1895
    38
  lp.colLowerBound(y,1);
alpar@1895
    39
  lp.colUpperBound(y,1);
alpar@1895
    40
  lp.colBounds(y,1,2);
alpar@1895
    41
alpar@1256
    42
  std::map<int,LP::Col> z;
alpar@1256
    43
  
alpar@1256
    44
  z.insert(std::make_pair(12,INVALID));
alpar@1256
    45
  z.insert(std::make_pair(2,INVALID));
alpar@1256
    46
  z.insert(std::make_pair(7,INVALID));
alpar@1256
    47
  z.insert(std::make_pair(5,INVALID));
alpar@1895
    48
alpar@1256
    49
  lp.addColSet(z);
alpar@1256
    50
alpar@1895
    51
  lp.colLowerBound(z,1);
alpar@1895
    52
  lp.colUpperBound(z,1);
alpar@1895
    53
  lp.colBounds(z,1,2);
alpar@1895
    54
alpar@1445
    55
  {
alpar@1445
    56
    LP::Expr e,f,g;
alpar@1445
    57
    LP::Col p1,p2,p3,p4,p5;
alpar@1445
    58
    LP::Constr c;
alpar@1445
    59
    
alpar@1484
    60
    p1=lp.addCol();
alpar@1484
    61
    p2=lp.addCol();
alpar@1484
    62
    p3=lp.addCol();
alpar@1484
    63
    p4=lp.addCol();
alpar@1484
    64
    p5=lp.addCol();
alpar@1484
    65
    
alpar@1445
    66
    e[p1]=2;
alpar@1445
    67
    e.constComp()=12;
alpar@1445
    68
    e[p1]+=2;
alpar@1445
    69
    e.constComp()+=12;
alpar@1445
    70
    e[p1]-=2;
alpar@1445
    71
    e.constComp()-=12;
alpar@1445
    72
    
alpar@1445
    73
    e=2;
alpar@1445
    74
    e=2.2;
alpar@1445
    75
    e=p1;
alpar@1445
    76
    e=f;
alpar@1445
    77
    
alpar@1445
    78
    e+=2;
alpar@1445
    79
    e+=2.2;
alpar@1445
    80
    e+=p1;
alpar@1445
    81
    e+=f;
alpar@1445
    82
    
alpar@1445
    83
    e-=2;
alpar@1445
    84
    e-=2.2;
alpar@1445
    85
    e-=p1;
alpar@1445
    86
    e-=f;
alpar@1445
    87
    
alpar@1445
    88
    e*=2;
alpar@1445
    89
    e*=2.2;
alpar@1445
    90
    e/=2;
alpar@1445
    91
    e/=2.2;
alpar@1445
    92
    
alpar@1445
    93
    e=((p1+p2)+(p1-p2)+(p1+12)+(12+p1)+(p1-12)+(12-p1)+
alpar@1445
    94
       (f+12)+(12+f)+(p1+f)+(f+p1)+(f+g)+
alpar@1445
    95
       (f-12)+(12-f)+(p1-f)+(f-p1)+(f-g)+
alpar@1445
    96
       2.2*f+f*2.2+f/2.2+
alpar@1445
    97
       2*f+f*2+f/2+
alpar@1445
    98
       2.2*p1+p1*2.2+p1/2.2+
alpar@1445
    99
       2*p1+p1*2+p1/2
alpar@1445
   100
       );
alpar@1256
   101
alpar@1445
   102
alpar@1445
   103
    c = (e  <= f  );
alpar@1445
   104
    c = (e  <= 2.2);
alpar@1445
   105
    c = (e  <= 2  );
alpar@1445
   106
    c = (e  <= p1 );
alpar@1445
   107
    c = (2.2<= f  );
alpar@1445
   108
    c = (2  <= f  );
alpar@1445
   109
    c = (p1 <= f  );
alpar@1445
   110
    c = (p1 <= p2 );
alpar@1445
   111
    c = (p1 <= 2.2);
alpar@1445
   112
    c = (p1 <= 2  );
alpar@1445
   113
    c = (2.2<= p2 );
alpar@1445
   114
    c = (2  <= p2 );
alpar@1445
   115
    
alpar@1445
   116
    c = (e  >= f  );
alpar@1445
   117
    c = (e  >= 2.2);
alpar@1445
   118
    c = (e  >= 2  );
alpar@1445
   119
    c = (e  >= p1 );
alpar@1445
   120
    c = (2.2>= f  );
alpar@1445
   121
    c = (2  >= f  );
alpar@1445
   122
    c = (p1 >= f  );
alpar@1445
   123
    c = (p1 >= p2 );
alpar@1445
   124
    c = (p1 >= 2.2);
alpar@1445
   125
    c = (p1 >= 2  );
alpar@1445
   126
    c = (2.2>= p2 );
alpar@1445
   127
    c = (2  >= p2 );
alpar@1445
   128
    
alpar@1445
   129
    c = (e  == f  );
alpar@1445
   130
    c = (e  == 2.2);
alpar@1445
   131
    c = (e  == 2  );
alpar@1445
   132
    c = (e  == p1 );
alpar@1445
   133
    c = (2.2== f  );
alpar@1445
   134
    c = (2  == f  );
alpar@1445
   135
    c = (p1 == f  );
alpar@1445
   136
    //c = (p1 == p2 );
alpar@1445
   137
    c = (p1 == 2.2);
alpar@1445
   138
    c = (p1 == 2  );
alpar@1445
   139
    c = (2.2== p2 );
alpar@1445
   140
    c = (2  == p2 );
alpar@1445
   141
    
alpar@1445
   142
    c = (2 <= e <= 3);
alpar@1445
   143
    c = (2 <= p1<= 3);
alpar@1445
   144
    
alpar@1445
   145
    c = (2 >= e >= 3);
alpar@1445
   146
    c = (2 >= p1>= 3);
alpar@1445
   147
    
alpar@1445
   148
    e[x[3]]=2;
alpar@1445
   149
    e[x[3]]=4;
alpar@1445
   150
    e[x[3]]=1;
alpar@1445
   151
    e.constComp()=12;
alpar@1445
   152
    
alpar@1445
   153
    lp.addRow(LP::INF,e,23);
alpar@1445
   154
    lp.addRow(LP::INF,3.0*(x[1]+x[2]/2)-x[3],23);
alpar@1445
   155
    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
   156
    
alpar@1445
   157
    lp.addRow(x[1]+x[3]<=x[5]-3);
alpar@1445
   158
    lp.addRow(-7<=x[1]+x[3]-12<=3);
alpar@1445
   159
    lp.addRow(x[1]<=x[5]);
alpar@1445
   160
  }
alpar@1272
   161
  
alpar@1445
   162
  {
alpar@1445
   163
    LP::DualExpr e,f,g;
alpar@1445
   164
    LP::Row p1,p2,p3,p4,p5;
alpar@1445
   165
    
alpar@1445
   166
    e[p1]=2;
alpar@1445
   167
    e[p1]+=2;
alpar@1445
   168
    e[p1]-=2;
alpar@1445
   169
    
alpar@1445
   170
    e=p1;
alpar@1445
   171
    e=f;
alpar@1445
   172
    
alpar@1445
   173
    e+=p1;
alpar@1445
   174
    e+=f;
alpar@1445
   175
    
alpar@1445
   176
    e-=p1;
alpar@1445
   177
    e-=f;
alpar@1445
   178
    
alpar@1445
   179
    e*=2;
alpar@1445
   180
    e*=2.2;
alpar@1445
   181
    e/=2;
alpar@1445
   182
    e/=2.2;
alpar@1445
   183
    
alpar@1493
   184
    e=((p1+p2)+(p1-p2)+
alpar@1445
   185
       (p1+f)+(f+p1)+(f+g)+
alpar@1445
   186
       (p1-f)+(f-p1)+(f-g)+
alpar@1445
   187
       2.2*f+f*2.2+f/2.2+
alpar@1445
   188
       2*f+f*2+f/2+
alpar@1445
   189
       2.2*p1+p1*2.2+p1/2.2+
alpar@1445
   190
       2*p1+p1*2+p1/2
alpar@1445
   191
       );
alpar@1445
   192
  }
alpar@1272
   193
  
athos@1508
   194
#endif
alpar@1264
   195
}
alpar@1264
   196
athos@1542
   197
void solveAndCheck(LpSolverBase& lp, LpSolverBase::SolutionStatus stat, 
athos@1543
   198
		   double exp_opt) {
athos@1543
   199
  using std::string;
athos@1542
   200
  lp.solve();
athos@1542
   201
  //int decimal,sign;
athos@1543
   202
  std::ostringstream buf;
athos@1543
   203
  buf << "Primalstatus should be: " << int(stat);
athos@1543
   204
athos@1542
   205
  //  itoa(stat,buf1, 10);
athos@1543
   206
  check(lp.primalStatus()==stat, buf.str());
athos@1543
   207
athos@1543
   208
  if (stat ==  LpSolverBase::OPTIMAL) {
athos@1543
   209
    std::ostringstream buf;
athos@1543
   210
    buf << "Wrong optimal value: the right optimum is " << exp_opt; 
athos@1543
   211
    check(std::abs(lp.primalValue()-exp_opt) < 1e-3, buf.str());
athos@1542
   212
    //+ecvt(exp_opt,2)
athos@1542
   213
  }
athos@1542
   214
}
athos@1542
   215
 
athos@1473
   216
void aTest(LpSolverBase & lp)
athos@1473
   217
{
athos@1473
   218
  typedef LpSolverBase LP;
athos@1473
   219
athos@1542
   220
 //The following example is very simple
athos@1473
   221
athos@1473
   222
  typedef LpSolverBase::Row Row;
athos@1473
   223
  typedef LpSolverBase::Col Col;
athos@1473
   224
athos@1473
   225
athos@1473
   226
  Col x1 = lp.addCol();
athos@1473
   227
  Col x2 = lp.addCol();
athos@1473
   228
athos@1473
   229
athos@1473
   230
  //Constraints
athos@1542
   231
  Row upright=lp.addRow(x1+x2 <=1);  
athos@1542
   232
  lp.addRow(x1+x2 >=-1);  
athos@1542
   233
  lp.addRow(x1-x2 <=1);  
athos@1542
   234
  lp.addRow(x1-x2 >=-1);  
athos@1473
   235
  //Nonnegativity of the variables
athos@1473
   236
  lp.colLowerBound(x1, 0);
athos@1473
   237
  lp.colLowerBound(x2, 0);
athos@1473
   238
  //Objective function
athos@1542
   239
  lp.setObj(x1+x2);
athos@1473
   240
athos@1473
   241
  lp.max();
athos@1473
   242
athos@1542
   243
  //Maximization of x1+x2
athos@1542
   244
  //over the triangle with vertices (0,0) (0,1) (1,0)
athos@1542
   245
  double expected_opt=1;
athos@1542
   246
  solveAndCheck(lp, LpSolverBase::OPTIMAL, expected_opt);
alpar@1484
   247
  
athos@1542
   248
  //Minimization
athos@1542
   249
  lp.min();
athos@1542
   250
  expected_opt=0;
athos@1542
   251
  solveAndCheck(lp, LpSolverBase::OPTIMAL, expected_opt);
athos@1542
   252
  
athos@1542
   253
  //Vertex (-1,0) instead of (0,0)
athos@1542
   254
  lp.colLowerBound(x1, -LpSolverBase::INF);
athos@1542
   255
  expected_opt=-1;
athos@1542
   256
  solveAndCheck(lp, LpSolverBase::OPTIMAL, expected_opt);
athos@1542
   257
athos@1542
   258
  //Erase one constraint and return to maximization
athos@1542
   259
  lp.eraseRow(upright);
athos@1542
   260
  lp.max();
athos@1542
   261
  expected_opt=LpSolverBase::INF;
athos@1542
   262
  solveAndCheck(lp, LpSolverBase::INFINITE, expected_opt);
athos@1542
   263
athos@1542
   264
  //Infeasibilty
athos@1542
   265
  lp.addRow(x1+x2 <=-2);  
athos@1542
   266
  solveAndCheck(lp, LpSolverBase::INFEASIBLE, expected_opt);
athos@1542
   267
athos@1542
   268
  //Change problem and forget to solve
athos@1542
   269
  lp.min();
athos@1542
   270
  check(lp.primalStatus()==LpSolverBase::UNDEFINED,"Primalstatus should be UNDEFINED");
athos@1542
   271
athos@1542
   272
//   lp.solve();
athos@1542
   273
//   if (lp.primalStatus()==LpSolverBase::OPTIMAL){
athos@1542
   274
//     std::cout<< "Z = "<<lp.primalValue()
athos@1542
   275
// 	     << " (error = " << lp.primalValue()-expected_opt
athos@1542
   276
// 	     << "); x1 = "<<lp.primal(x1)
athos@1542
   277
// 	     << "; x2 = "<<lp.primal(x2)
athos@1542
   278
// 	     <<std::endl;
alpar@1484
   279
    
athos@1542
   280
//   }
athos@1542
   281
//   else{
athos@1542
   282
//     std::cout<<lp.primalStatus()<<std::endl;
athos@1542
   283
//     std::cout<<"Optimal solution not found!"<<std::endl;
athos@1542
   284
//   }
athos@1473
   285
athos@1542
   286
 
athos@1473
   287
athos@1473
   288
}
athos@1473
   289
athos@1473
   290
alpar@1263
   291
int main() 
alpar@1263
   292
{
alpar@1390
   293
  LpSkeleton lp_skel;
alpar@1390
   294
  lpTest(lp_skel);
alpar@1390
   295
ladanyi@1437
   296
#ifdef HAVE_GLPK
alpar@1484
   297
  LpGlpk lp_glpk1,lp_glpk2;
alpar@1484
   298
  lpTest(lp_glpk1);
alpar@1484
   299
  aTest(lp_glpk2);
ladanyi@1437
   300
#endif
alpar@1263
   301
ladanyi@1437
   302
#ifdef HAVE_CPLEX
klao@1797
   303
  LpCplex lp_cplex1,lp_cplex2;
klao@1797
   304
  lpTest(lp_cplex1);
klao@1797
   305
  aTest(lp_cplex2);
ladanyi@1437
   306
#endif
alpar@1264
   307
alpar@1309
   308
  return 0;
alpar@1263
   309
}