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