test/lp_test.cc
author ladanyi
Tue, 14 Jun 2005 20:01:41 +0000
changeset 1489 f7e9cc3bc2da
parent 1475 21aa0195aab9
child 1493 94535d1833b5
permissions -rw-r--r--
- separate file for each m4 script
- consistent naming
- removed cxxflags.m4
alpar@1390
     1
#include<lemon/lp_skeleton.h>
athos@1473
     2
#include "test_tools.h"
alpar@1390
     3
ladanyi@1387
     4
#ifdef HAVE_CONFIG_H
ladanyi@1387
     5
#include <config.h>
ladanyi@1387
     6
#endif
ladanyi@1387
     7
ladanyi@1387
     8
#ifdef HAVE_GLPK
ladanyi@1387
     9
#include <lemon/lp_glpk.h>
ladanyi@1437
    10
#endif
ladanyi@1437
    11
ladanyi@1437
    12
#ifdef HAVE_CPLEX
ladanyi@1387
    13
#include <lemon/lp_cplex.h>
ladanyi@1387
    14
#endif
alpar@1254
    15
alpar@1254
    16
using namespace lemon;
alpar@1254
    17
alpar@1263
    18
void lpTest(LpSolverBase & lp)
alpar@1254
    19
{
alpar@1263
    20
  typedef LpSolverBase LP;
alpar@1256
    21
alpar@1309
    22
  std::vector<LP::Col> x(10);
alpar@1309
    23
  //  for(int i=0;i<10;i++) x.push_back(lp.addCol());
alpar@1309
    24
  lp.addColSet(x);
alpar@1256
    25
alpar@1256
    26
  std::vector<LP::Col> y(10);
alpar@1256
    27
  lp.addColSet(y);
alpar@1256
    28
alpar@1256
    29
  std::map<int,LP::Col> z;
alpar@1256
    30
  
alpar@1256
    31
  z.insert(std::make_pair(12,INVALID));
alpar@1256
    32
  z.insert(std::make_pair(2,INVALID));
alpar@1256
    33
  z.insert(std::make_pair(7,INVALID));
alpar@1256
    34
  z.insert(std::make_pair(5,INVALID));
alpar@1256
    35
  
alpar@1256
    36
  lp.addColSet(z);
alpar@1256
    37
alpar@1445
    38
  {
alpar@1445
    39
    LP::Expr e,f,g;
alpar@1445
    40
    LP::Col p1,p2,p3,p4,p5;
alpar@1445
    41
    LP::Constr c;
alpar@1445
    42
    
alpar@1484
    43
    p1=lp.addCol();
alpar@1484
    44
    p2=lp.addCol();
alpar@1484
    45
    p3=lp.addCol();
alpar@1484
    46
    p4=lp.addCol();
alpar@1484
    47
    p5=lp.addCol();
alpar@1484
    48
    
alpar@1445
    49
    e[p1]=2;
alpar@1445
    50
    e.constComp()=12;
alpar@1445
    51
    e[p1]+=2;
alpar@1445
    52
    e.constComp()+=12;
alpar@1445
    53
    e[p1]-=2;
alpar@1445
    54
    e.constComp()-=12;
alpar@1445
    55
    
alpar@1445
    56
    e=2;
alpar@1445
    57
    e=2.2;
alpar@1445
    58
    e=p1;
alpar@1445
    59
    e=f;
alpar@1445
    60
    
alpar@1445
    61
    e+=2;
alpar@1445
    62
    e+=2.2;
alpar@1445
    63
    e+=p1;
alpar@1445
    64
    e+=f;
alpar@1445
    65
    
alpar@1445
    66
    e-=2;
alpar@1445
    67
    e-=2.2;
alpar@1445
    68
    e-=p1;
alpar@1445
    69
    e-=f;
alpar@1445
    70
    
alpar@1445
    71
    e*=2;
alpar@1445
    72
    e*=2.2;
alpar@1445
    73
    e/=2;
alpar@1445
    74
    e/=2.2;
alpar@1445
    75
    
alpar@1445
    76
    e=((p1+p2)+(p1-p2)+(p1+12)+(12+p1)+(p1-12)+(12-p1)+
alpar@1445
    77
       (f+12)+(12+f)+(p1+f)+(f+p1)+(f+g)+
alpar@1445
    78
       (f-12)+(12-f)+(p1-f)+(f-p1)+(f-g)+
alpar@1445
    79
       2.2*f+f*2.2+f/2.2+
alpar@1445
    80
       2*f+f*2+f/2+
alpar@1445
    81
       2.2*p1+p1*2.2+p1/2.2+
alpar@1445
    82
       2*p1+p1*2+p1/2
alpar@1445
    83
       );
alpar@1256
    84
alpar@1445
    85
alpar@1445
    86
    c = (e  <= f  );
alpar@1445
    87
    c = (e  <= 2.2);
alpar@1445
    88
    c = (e  <= 2  );
alpar@1445
    89
    c = (e  <= p1 );
alpar@1445
    90
    c = (2.2<= f  );
alpar@1445
    91
    c = (2  <= f  );
alpar@1445
    92
    c = (p1 <= f  );
alpar@1445
    93
    c = (p1 <= p2 );
alpar@1445
    94
    c = (p1 <= 2.2);
alpar@1445
    95
    c = (p1 <= 2  );
alpar@1445
    96
    c = (2.2<= p2 );
alpar@1445
    97
    c = (2  <= p2 );
alpar@1445
    98
    
alpar@1445
    99
    c = (e  >= f  );
alpar@1445
   100
    c = (e  >= 2.2);
alpar@1445
   101
    c = (e  >= 2  );
alpar@1445
   102
    c = (e  >= p1 );
alpar@1445
   103
    c = (2.2>= f  );
alpar@1445
   104
    c = (2  >= f  );
alpar@1445
   105
    c = (p1 >= f  );
alpar@1445
   106
    c = (p1 >= p2 );
alpar@1445
   107
    c = (p1 >= 2.2);
alpar@1445
   108
    c = (p1 >= 2  );
alpar@1445
   109
    c = (2.2>= p2 );
alpar@1445
   110
    c = (2  >= p2 );
alpar@1445
   111
    
alpar@1445
   112
    c = (e  == f  );
alpar@1445
   113
    c = (e  == 2.2);
alpar@1445
   114
    c = (e  == 2  );
alpar@1445
   115
    c = (e  == p1 );
alpar@1445
   116
    c = (2.2== f  );
alpar@1445
   117
    c = (2  == f  );
alpar@1445
   118
    c = (p1 == f  );
alpar@1445
   119
    //c = (p1 == p2 );
alpar@1445
   120
    c = (p1 == 2.2);
alpar@1445
   121
    c = (p1 == 2  );
alpar@1445
   122
    c = (2.2== p2 );
alpar@1445
   123
    c = (2  == p2 );
alpar@1445
   124
    
alpar@1445
   125
    c = (2 <= e <= 3);
alpar@1445
   126
    c = (2 <= p1<= 3);
alpar@1445
   127
    
alpar@1445
   128
    c = (2 >= e >= 3);
alpar@1445
   129
    c = (2 >= p1>= 3);
alpar@1445
   130
    
alpar@1445
   131
    e[x[3]]=2;
alpar@1445
   132
    e[x[3]]=4;
alpar@1445
   133
    e[x[3]]=1;
alpar@1445
   134
    e.constComp()=12;
alpar@1445
   135
    
alpar@1445
   136
    lp.addRow(LP::INF,e,23);
alpar@1445
   137
    lp.addRow(LP::INF,3.0*(x[1]+x[2]/2)-x[3],23);
alpar@1445
   138
    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
   139
    
alpar@1445
   140
    lp.addRow(x[1]+x[3]<=x[5]-3);
alpar@1445
   141
    lp.addRow(-7<=x[1]+x[3]-12<=3);
alpar@1445
   142
    lp.addRow(x[1]<=x[5]);
alpar@1445
   143
  }
alpar@1272
   144
  
alpar@1445
   145
  {
alpar@1445
   146
    LP::DualExpr e,f,g;
alpar@1445
   147
    LP::Row p1,p2,p3,p4,p5;
alpar@1445
   148
    
alpar@1445
   149
    e[p1]=2;
alpar@1445
   150
    e[p1]+=2;
alpar@1445
   151
    e[p1]-=2;
alpar@1445
   152
    
alpar@1445
   153
    e=p1;
alpar@1445
   154
    e=f;
alpar@1445
   155
    
alpar@1445
   156
    e+=p1;
alpar@1445
   157
    e+=f;
alpar@1445
   158
    
alpar@1445
   159
    e-=p1;
alpar@1445
   160
    e-=f;
alpar@1445
   161
    
alpar@1445
   162
    e*=2;
alpar@1445
   163
    e*=2.2;
alpar@1445
   164
    e/=2;
alpar@1445
   165
    e/=2.2;
alpar@1445
   166
    
alpar@1445
   167
    e=((p1+p2)+(p1-p2)+(p1+12)+(12+p1)+(p1-12)+(12-p1)+
alpar@1445
   168
       (p1+f)+(f+p1)+(f+g)+
alpar@1445
   169
       (p1-f)+(f-p1)+(f-g)+
alpar@1445
   170
       2.2*f+f*2.2+f/2.2+
alpar@1445
   171
       2*f+f*2+f/2+
alpar@1445
   172
       2.2*p1+p1*2.2+p1/2.2+
alpar@1445
   173
       2*p1+p1*2+p1/2
alpar@1445
   174
       );
alpar@1445
   175
  }
alpar@1272
   176
  
alpar@1272
   177
alpar@1264
   178
}
alpar@1264
   179
athos@1473
   180
void aTest(LpSolverBase & lp)
athos@1473
   181
{
athos@1473
   182
  typedef LpSolverBase LP;
athos@1473
   183
athos@1473
   184
 //The following example is taken from the book by Gáspár and Temesi, page 39.
athos@1473
   185
athos@1473
   186
  typedef LpSolverBase::Row Row;
athos@1473
   187
  typedef LpSolverBase::Col Col;
athos@1473
   188
athos@1473
   189
athos@1473
   190
  Col x1 = lp.addCol();
athos@1473
   191
  Col x2 = lp.addCol();
athos@1473
   192
athos@1473
   193
athos@1473
   194
  //Constraints
athos@1473
   195
  lp.addRow(3*x1+2*x2 >=6);  
athos@1473
   196
  lp.addRow(-1*x1+x2<=4);  
athos@1473
   197
  lp.addRow(5*x1+8*x2<=40);  
athos@1473
   198
  lp.addRow(x1-2*x2<=4);  
athos@1473
   199
  //Nonnegativity of the variables
athos@1473
   200
  lp.colLowerBound(x1, 0);
athos@1473
   201
  lp.colLowerBound(x2, 0);
athos@1473
   202
  //Objective function
athos@1473
   203
  lp.setObj(2*x1+x2);
athos@1473
   204
athos@1473
   205
  lp.max();
athos@1473
   206
  lp.solve();
athos@1473
   207
alpar@1484
   208
  double opt=122.0/9.0;
alpar@1484
   209
  
athos@1473
   210
  if (lp.primalStatus()==LpSolverBase::OPTIMAL){
alpar@1484
   211
    std::cout<< "Z = "<<lp.primalValue()
alpar@1484
   212
	     << " (error = " << lp.primalValue()-opt
alpar@1484
   213
	     << "); x1 = "<<lp.primal(x1)
alpar@1484
   214
	     << "; x2 = "<<lp.primal(x2)
alpar@1484
   215
	     <<std::endl;
alpar@1484
   216
    
athos@1473
   217
  }
athos@1473
   218
  else{
athos@1473
   219
    std::cout<<"Optimal solution not found!"<<std::endl;
athos@1473
   220
  }
athos@1473
   221
athos@1473
   222
  check(lp.primalStatus()==LpSolverBase::OPTIMAL,"Primalstatus should be OPTIMAL");
athos@1473
   223
alpar@1484
   224
  check(std::abs(lp.primalValue()-opt)<1e-3,
alpar@1484
   225
	"Wrong optimal value: the right optimum is 122/9 (13.555555...)");
athos@1473
   226
athos@1473
   227
athos@1473
   228
}
athos@1473
   229
athos@1473
   230
alpar@1263
   231
int main() 
alpar@1263
   232
{
alpar@1390
   233
  LpSkeleton lp_skel;
alpar@1390
   234
  lpTest(lp_skel);
alpar@1390
   235
ladanyi@1437
   236
#ifdef HAVE_GLPK
alpar@1484
   237
  LpGlpk lp_glpk1,lp_glpk2;
alpar@1484
   238
  lpTest(lp_glpk1);
alpar@1484
   239
  aTest(lp_glpk2);
ladanyi@1437
   240
#endif
alpar@1263
   241
ladanyi@1437
   242
#ifdef HAVE_CPLEX
athos@1475
   243
//  LpCplex lp_cplex;
athos@1475
   244
//  lpTest(lp_cplex);
ladanyi@1437
   245
#endif
alpar@1264
   246
alpar@1309
   247
  return 0;
alpar@1263
   248
}