test/lp_test.cc
author deba
Wed, 29 Nov 2006 17:32:03 +0000
changeset 2314 dbbd5c514163
parent 2264 90c66dc93ca4
child 2324 18fc834761d9
permissions -rw-r--r--
checking soplex
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@1956
     5
 * Copyright (C) 2003-2006
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>
athos@1542
    23
ladanyi@1387
    24
#ifdef HAVE_CONFIG_H
ladanyi@1387
    25
#include <config.h>
ladanyi@1387
    26
#endif
ladanyi@1387
    27
ladanyi@1387
    28
#ifdef HAVE_GLPK
ladanyi@1387
    29
#include <lemon/lp_glpk.h>
ladanyi@1437
    30
#endif
ladanyi@1437
    31
ladanyi@1437
    32
#ifdef HAVE_CPLEX
ladanyi@1387
    33
#include <lemon/lp_cplex.h>
ladanyi@1387
    34
#endif
alpar@1254
    35
deba@2314
    36
// #ifdef HAVE_SOPLEX
deba@2314
    37
// #include <lemon/lp_soplex.h>
deba@2314
    38
// #endif
deba@2314
    39
alpar@1254
    40
using namespace lemon;
alpar@1254
    41
alpar@1263
    42
void lpTest(LpSolverBase & lp)
alpar@1254
    43
{
athos@1508
    44
athos@1508
    45
athos@1508
    46
alpar@1263
    47
  typedef LpSolverBase LP;
alpar@1256
    48
alpar@1309
    49
  std::vector<LP::Col> x(10);
alpar@1309
    50
  //  for(int i=0;i<10;i++) x.push_back(lp.addCol());
alpar@1309
    51
  lp.addColSet(x);
alpar@1895
    52
  lp.colLowerBound(x,1);
alpar@1895
    53
  lp.colUpperBound(x,1);
alpar@1895
    54
  lp.colBounds(x,1,2);
athos@1508
    55
#ifndef GYORSITAS
athos@1508
    56
alpar@1256
    57
  std::vector<LP::Col> y(10);
alpar@1256
    58
  lp.addColSet(y);
alpar@1256
    59
alpar@1895
    60
  lp.colLowerBound(y,1);
alpar@1895
    61
  lp.colUpperBound(y,1);
alpar@1895
    62
  lp.colBounds(y,1,2);
alpar@1895
    63
alpar@1256
    64
  std::map<int,LP::Col> z;
alpar@1256
    65
  
alpar@1256
    66
  z.insert(std::make_pair(12,INVALID));
alpar@1256
    67
  z.insert(std::make_pair(2,INVALID));
alpar@1256
    68
  z.insert(std::make_pair(7,INVALID));
alpar@1256
    69
  z.insert(std::make_pair(5,INVALID));
alpar@1895
    70
alpar@1256
    71
  lp.addColSet(z);
alpar@1256
    72
alpar@1895
    73
  lp.colLowerBound(z,1);
alpar@1895
    74
  lp.colUpperBound(z,1);
alpar@1895
    75
  lp.colBounds(z,1,2);
alpar@1895
    76
alpar@1445
    77
  {
alpar@1445
    78
    LP::Expr e,f,g;
alpar@1445
    79
    LP::Col p1,p2,p3,p4,p5;
alpar@1445
    80
    LP::Constr c;
alpar@1445
    81
    
alpar@1484
    82
    p1=lp.addCol();
alpar@1484
    83
    p2=lp.addCol();
alpar@1484
    84
    p3=lp.addCol();
alpar@1484
    85
    p4=lp.addCol();
alpar@1484
    86
    p5=lp.addCol();
alpar@1484
    87
    
alpar@1445
    88
    e[p1]=2;
alpar@1445
    89
    e.constComp()=12;
alpar@1445
    90
    e[p1]+=2;
alpar@1445
    91
    e.constComp()+=12;
alpar@1445
    92
    e[p1]-=2;
alpar@1445
    93
    e.constComp()-=12;
alpar@1445
    94
    
alpar@1445
    95
    e=2;
alpar@1445
    96
    e=2.2;
alpar@1445
    97
    e=p1;
alpar@1445
    98
    e=f;
alpar@1445
    99
    
alpar@1445
   100
    e+=2;
alpar@1445
   101
    e+=2.2;
alpar@1445
   102
    e+=p1;
alpar@1445
   103
    e+=f;
alpar@1445
   104
    
alpar@1445
   105
    e-=2;
alpar@1445
   106
    e-=2.2;
alpar@1445
   107
    e-=p1;
alpar@1445
   108
    e-=f;
alpar@1445
   109
    
alpar@1445
   110
    e*=2;
alpar@1445
   111
    e*=2.2;
alpar@1445
   112
    e/=2;
alpar@1445
   113
    e/=2.2;
alpar@1445
   114
    
alpar@1445
   115
    e=((p1+p2)+(p1-p2)+(p1+12)+(12+p1)+(p1-12)+(12-p1)+
alpar@1445
   116
       (f+12)+(12+f)+(p1+f)+(f+p1)+(f+g)+
alpar@1445
   117
       (f-12)+(12-f)+(p1-f)+(f-p1)+(f-g)+
alpar@1445
   118
       2.2*f+f*2.2+f/2.2+
alpar@1445
   119
       2*f+f*2+f/2+
alpar@1445
   120
       2.2*p1+p1*2.2+p1/2.2+
alpar@1445
   121
       2*p1+p1*2+p1/2
alpar@1445
   122
       );
alpar@1256
   123
alpar@1445
   124
alpar@1445
   125
    c = (e  <= f  );
alpar@1445
   126
    c = (e  <= 2.2);
alpar@1445
   127
    c = (e  <= 2  );
alpar@1445
   128
    c = (e  <= p1 );
alpar@1445
   129
    c = (2.2<= f  );
alpar@1445
   130
    c = (2  <= f  );
alpar@1445
   131
    c = (p1 <= f  );
alpar@1445
   132
    c = (p1 <= p2 );
alpar@1445
   133
    c = (p1 <= 2.2);
alpar@1445
   134
    c = (p1 <= 2  );
alpar@1445
   135
    c = (2.2<= p2 );
alpar@1445
   136
    c = (2  <= p2 );
alpar@1445
   137
    
alpar@1445
   138
    c = (e  >= f  );
alpar@1445
   139
    c = (e  >= 2.2);
alpar@1445
   140
    c = (e  >= 2  );
alpar@1445
   141
    c = (e  >= p1 );
alpar@1445
   142
    c = (2.2>= f  );
alpar@1445
   143
    c = (2  >= f  );
alpar@1445
   144
    c = (p1 >= f  );
alpar@1445
   145
    c = (p1 >= p2 );
alpar@1445
   146
    c = (p1 >= 2.2);
alpar@1445
   147
    c = (p1 >= 2  );
alpar@1445
   148
    c = (2.2>= p2 );
alpar@1445
   149
    c = (2  >= p2 );
alpar@1445
   150
    
alpar@1445
   151
    c = (e  == f  );
alpar@1445
   152
    c = (e  == 2.2);
alpar@1445
   153
    c = (e  == 2  );
alpar@1445
   154
    c = (e  == p1 );
alpar@1445
   155
    c = (2.2== f  );
alpar@1445
   156
    c = (2  == f  );
alpar@1445
   157
    c = (p1 == f  );
alpar@1445
   158
    //c = (p1 == p2 );
alpar@1445
   159
    c = (p1 == 2.2);
alpar@1445
   160
    c = (p1 == 2  );
alpar@1445
   161
    c = (2.2== p2 );
alpar@1445
   162
    c = (2  == p2 );
alpar@1445
   163
    
alpar@1445
   164
    c = (2 <= e <= 3);
alpar@1445
   165
    c = (2 <= p1<= 3);
alpar@1445
   166
    
alpar@1445
   167
    c = (2 >= e >= 3);
alpar@1445
   168
    c = (2 >= p1>= 3);
alpar@1445
   169
    
alpar@1445
   170
    e[x[3]]=2;
alpar@1445
   171
    e[x[3]]=4;
alpar@1445
   172
    e[x[3]]=1;
alpar@1445
   173
    e.constComp()=12;
alpar@1445
   174
    
alpar@1445
   175
    lp.addRow(LP::INF,e,23);
alpar@1445
   176
    lp.addRow(LP::INF,3.0*(x[1]+x[2]/2)-x[3],23);
alpar@1445
   177
    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
   178
    
alpar@1445
   179
    lp.addRow(x[1]+x[3]<=x[5]-3);
alpar@1445
   180
    lp.addRow(-7<=x[1]+x[3]-12<=3);
alpar@1445
   181
    lp.addRow(x[1]<=x[5]);
athos@2264
   182
athos@2264
   183
    std::ostringstream buf;
athos@2264
   184
athos@2264
   185
athos@2264
   186
    //Checking the simplify function
athos@2264
   187
athos@2264
   188
//     //How to check the simplify function? A map gives no information
athos@2264
   189
//     //on the question whether a given key is or is not stored in it, or
athos@2264
   190
//     //it does?
athos@2264
   191
//   Yes, it does, using the find() function.
athos@2264
   192
    e=((p1+p2)+(p1-p2));
athos@2264
   193
    e.simplify();
athos@2264
   194
    buf << "Coeff. of p2 should be 0";
athos@2264
   195
    //    std::cout<<e[p1]<<e[p2]<<e[p3]<<std::endl;
athos@2264
   196
    check(e.find(p2)==e.end(), buf.str());
athos@2264
   197
athos@2264
   198
     
athos@2264
   199
athos@2264
   200
athos@2264
   201
    e=((p1+p2)+(p1-0.99*p2));
athos@2264
   202
    double tolerance=0.001;
athos@2264
   203
    e.simplify(tolerance);
athos@2264
   204
    buf << "Coeff. of p2 should be 0.01";
athos@2264
   205
    check(e[p2]>0, buf.str());
athos@2264
   206
    
athos@2264
   207
    tolerance=0.02;
athos@2264
   208
    e.simplify(tolerance);
athos@2264
   209
    buf << "Coeff. of p2 should be 0";
athos@2264
   210
    check(e.find(p2)==e.end(), buf.str());
athos@2264
   211
    
athos@2264
   212
alpar@1445
   213
  }
alpar@1272
   214
  
alpar@1445
   215
  {
alpar@1445
   216
    LP::DualExpr e,f,g;
alpar@1445
   217
    LP::Row p1,p2,p3,p4,p5;
alpar@1445
   218
    
alpar@1445
   219
    e[p1]=2;
alpar@1445
   220
    e[p1]+=2;
alpar@1445
   221
    e[p1]-=2;
alpar@1445
   222
    
alpar@1445
   223
    e=p1;
alpar@1445
   224
    e=f;
alpar@1445
   225
    
alpar@1445
   226
    e+=p1;
alpar@1445
   227
    e+=f;
alpar@1445
   228
    
alpar@1445
   229
    e-=p1;
alpar@1445
   230
    e-=f;
alpar@1445
   231
    
alpar@1445
   232
    e*=2;
alpar@1445
   233
    e*=2.2;
alpar@1445
   234
    e/=2;
alpar@1445
   235
    e/=2.2;
alpar@1445
   236
    
alpar@1493
   237
    e=((p1+p2)+(p1-p2)+
alpar@1445
   238
       (p1+f)+(f+p1)+(f+g)+
alpar@1445
   239
       (p1-f)+(f-p1)+(f-g)+
alpar@1445
   240
       2.2*f+f*2.2+f/2.2+
alpar@1445
   241
       2*f+f*2+f/2+
alpar@1445
   242
       2.2*p1+p1*2.2+p1/2.2+
alpar@1445
   243
       2*p1+p1*2+p1/2
alpar@1445
   244
       );
alpar@1445
   245
  }
alpar@1272
   246
  
athos@1508
   247
#endif
alpar@1264
   248
}
alpar@1264
   249
athos@1542
   250
void solveAndCheck(LpSolverBase& lp, LpSolverBase::SolutionStatus stat, 
athos@1543
   251
		   double exp_opt) {
athos@1543
   252
  using std::string;
athos@1542
   253
  lp.solve();
athos@1542
   254
  //int decimal,sign;
athos@1543
   255
  std::ostringstream buf;
athos@1543
   256
  buf << "Primalstatus should be: " << int(stat);
athos@1543
   257
athos@1542
   258
  //  itoa(stat,buf1, 10);
athos@1543
   259
  check(lp.primalStatus()==stat, buf.str());
athos@1543
   260
athos@1543
   261
  if (stat ==  LpSolverBase::OPTIMAL) {
athos@1543
   262
    std::ostringstream buf;
athos@1543
   263
    buf << "Wrong optimal value: the right optimum is " << exp_opt; 
athos@1543
   264
    check(std::abs(lp.primalValue()-exp_opt) < 1e-3, buf.str());
athos@1542
   265
    //+ecvt(exp_opt,2)
athos@1542
   266
  }
athos@1542
   267
}
athos@1542
   268
 
athos@1473
   269
void aTest(LpSolverBase & lp)
athos@1473
   270
{
athos@1473
   271
  typedef LpSolverBase LP;
athos@1473
   272
athos@1542
   273
 //The following example is very simple
athos@1473
   274
athos@1473
   275
  typedef LpSolverBase::Row Row;
athos@1473
   276
  typedef LpSolverBase::Col Col;
athos@1473
   277
athos@1473
   278
athos@1473
   279
  Col x1 = lp.addCol();
athos@1473
   280
  Col x2 = lp.addCol();
athos@1473
   281
athos@1473
   282
athos@1473
   283
  //Constraints
athos@1542
   284
  Row upright=lp.addRow(x1+x2 <=1);  
athos@1542
   285
  lp.addRow(x1+x2 >=-1);  
athos@1542
   286
  lp.addRow(x1-x2 <=1);  
athos@1542
   287
  lp.addRow(x1-x2 >=-1);  
athos@1473
   288
  //Nonnegativity of the variables
athos@1473
   289
  lp.colLowerBound(x1, 0);
athos@1473
   290
  lp.colLowerBound(x2, 0);
athos@1473
   291
  //Objective function
athos@1542
   292
  lp.setObj(x1+x2);
athos@1473
   293
athos@1473
   294
  lp.max();
athos@1473
   295
athos@1542
   296
  //Maximization of x1+x2
athos@1542
   297
  //over the triangle with vertices (0,0) (0,1) (1,0)
athos@1542
   298
  double expected_opt=1;
athos@1542
   299
  solveAndCheck(lp, LpSolverBase::OPTIMAL, expected_opt);
alpar@1484
   300
  
athos@1542
   301
  //Minimization
athos@1542
   302
  lp.min();
athos@1542
   303
  expected_opt=0;
athos@1542
   304
  solveAndCheck(lp, LpSolverBase::OPTIMAL, expected_opt);
athos@1542
   305
  
athos@1542
   306
  //Vertex (-1,0) instead of (0,0)
athos@1542
   307
  lp.colLowerBound(x1, -LpSolverBase::INF);
athos@1542
   308
  expected_opt=-1;
athos@1542
   309
  solveAndCheck(lp, LpSolverBase::OPTIMAL, expected_opt);
athos@1542
   310
athos@1542
   311
  //Erase one constraint and return to maximization
athos@1542
   312
  lp.eraseRow(upright);
athos@1542
   313
  lp.max();
athos@1542
   314
  expected_opt=LpSolverBase::INF;
athos@1542
   315
  solveAndCheck(lp, LpSolverBase::INFINITE, expected_opt);
athos@1542
   316
athos@1542
   317
  //Infeasibilty
athos@1542
   318
  lp.addRow(x1+x2 <=-2);  
athos@1542
   319
  solveAndCheck(lp, LpSolverBase::INFEASIBLE, expected_opt);
athos@1542
   320
athos@1542
   321
  //Change problem and forget to solve
athos@1542
   322
  lp.min();
athos@1542
   323
  check(lp.primalStatus()==LpSolverBase::UNDEFINED,"Primalstatus should be UNDEFINED");
athos@1542
   324
athos@1542
   325
//   lp.solve();
athos@1542
   326
//   if (lp.primalStatus()==LpSolverBase::OPTIMAL){
athos@1542
   327
//     std::cout<< "Z = "<<lp.primalValue()
athos@1542
   328
// 	     << " (error = " << lp.primalValue()-expected_opt
athos@1542
   329
// 	     << "); x1 = "<<lp.primal(x1)
athos@1542
   330
// 	     << "; x2 = "<<lp.primal(x2)
athos@1542
   331
// 	     <<std::endl;
alpar@1484
   332
    
athos@1542
   333
//   }
athos@1542
   334
//   else{
athos@1542
   335
//     std::cout<<lp.primalStatus()<<std::endl;
athos@1542
   336
//     std::cout<<"Optimal solution not found!"<<std::endl;
athos@1542
   337
//   }
athos@1473
   338
athos@1542
   339
 
athos@1473
   340
athos@1473
   341
}
athos@1473
   342
athos@1473
   343
alpar@1263
   344
int main() 
alpar@1263
   345
{
alpar@1390
   346
  LpSkeleton lp_skel;
alpar@1390
   347
  lpTest(lp_skel);
alpar@1390
   348
ladanyi@1437
   349
#ifdef HAVE_GLPK
alpar@1484
   350
  LpGlpk lp_glpk1,lp_glpk2;
alpar@1484
   351
  lpTest(lp_glpk1);
alpar@1484
   352
  aTest(lp_glpk2);
ladanyi@1437
   353
#endif
alpar@1263
   354
ladanyi@1437
   355
#ifdef HAVE_CPLEX
klao@1797
   356
  LpCplex lp_cplex1,lp_cplex2;
klao@1797
   357
  lpTest(lp_cplex1);
klao@1797
   358
  aTest(lp_cplex2);
ladanyi@1437
   359
#endif
alpar@1264
   360
deba@2314
   361
// #ifdef HAVE_SOPLEX
deba@2314
   362
//   LpSoplex lp_soplex1,lp_soplex2;
deba@2314
   363
//   lpTest(lp_soplex1);
deba@2314
   364
//   aTest(lp_soplex2);
deba@2314
   365
// #endif
deba@2314
   366
alpar@1309
   367
  return 0;
alpar@1263
   368
}