test/lp_test.cc
author alpar
Fri, 08 Feb 2008 11:04:37 +0000
changeset 2571 f5c6e63f51d1
parent 2553 bfced05fa852
child 2605 852361980706
permissions -rw-r--r--
Hopefully fix all compilation errors.
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>
athos@1542
    23
ladanyi@1387
    24
#ifdef HAVE_CONFIG_H
alpar@2571
    25
#include <lemon/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@2363
    36
#ifdef HAVE_SOPLEX
deba@2363
    37
#include <lemon/lp_soplex.h>
deba@2363
    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@2345
   197
    
athos@2264
   198
     
athos@2264
   199
athos@2264
   200
athos@2264
   201
    e=((p1+p2)+(p1-0.99*p2));
athos@2345
   202
    //e.prettyPrint(std::cout);
athos@2345
   203
    //(e<=2).prettyPrint(std::cout);
athos@2264
   204
    double tolerance=0.001;
athos@2264
   205
    e.simplify(tolerance);
athos@2264
   206
    buf << "Coeff. of p2 should be 0.01";
athos@2264
   207
    check(e[p2]>0, buf.str());
athos@2264
   208
    
athos@2264
   209
    tolerance=0.02;
athos@2264
   210
    e.simplify(tolerance);
athos@2264
   211
    buf << "Coeff. of p2 should be 0";
athos@2264
   212
    check(e.find(p2)==e.end(), buf.str());
athos@2264
   213
    
athos@2264
   214
alpar@1445
   215
  }
alpar@1272
   216
  
alpar@1445
   217
  {
alpar@1445
   218
    LP::DualExpr e,f,g;
alpar@1445
   219
    LP::Row p1,p2,p3,p4,p5;
alpar@1445
   220
    
alpar@1445
   221
    e[p1]=2;
alpar@1445
   222
    e[p1]+=2;
alpar@1445
   223
    e[p1]-=2;
alpar@1445
   224
    
alpar@1445
   225
    e=p1;
alpar@1445
   226
    e=f;
alpar@1445
   227
    
alpar@1445
   228
    e+=p1;
alpar@1445
   229
    e+=f;
alpar@1445
   230
    
alpar@1445
   231
    e-=p1;
alpar@1445
   232
    e-=f;
alpar@1445
   233
    
alpar@1445
   234
    e*=2;
alpar@1445
   235
    e*=2.2;
alpar@1445
   236
    e/=2;
alpar@1445
   237
    e/=2.2;
alpar@1445
   238
    
alpar@1493
   239
    e=((p1+p2)+(p1-p2)+
alpar@1445
   240
       (p1+f)+(f+p1)+(f+g)+
alpar@1445
   241
       (p1-f)+(f-p1)+(f-g)+
alpar@1445
   242
       2.2*f+f*2.2+f/2.2+
alpar@1445
   243
       2*f+f*2+f/2+
alpar@1445
   244
       2.2*p1+p1*2.2+p1/2.2+
alpar@1445
   245
       2*p1+p1*2+p1/2
alpar@1445
   246
       );
alpar@1445
   247
  }
alpar@1272
   248
  
athos@1508
   249
#endif
alpar@1264
   250
}
alpar@1264
   251
athos@1542
   252
void solveAndCheck(LpSolverBase& lp, LpSolverBase::SolutionStatus stat, 
athos@1543
   253
		   double exp_opt) {
athos@1543
   254
  using std::string;
athos@1542
   255
  lp.solve();
athos@1542
   256
  //int decimal,sign;
athos@1543
   257
  std::ostringstream buf;
athos@1543
   258
  buf << "Primalstatus should be: " << int(stat);
athos@1543
   259
athos@1542
   260
  //  itoa(stat,buf1, 10);
athos@1543
   261
  check(lp.primalStatus()==stat, buf.str());
athos@1543
   262
athos@1543
   263
  if (stat ==  LpSolverBase::OPTIMAL) {
deba@2386
   264
    std::ostringstream sbuf;
deba@2386
   265
    sbuf << "Wrong optimal value: the right optimum is " << exp_opt; 
deba@2386
   266
    check(std::abs(lp.primalValue()-exp_opt) < 1e-3, sbuf.str());
athos@1542
   267
    //+ecvt(exp_opt,2)
athos@1542
   268
  }
athos@1542
   269
}
athos@1542
   270
 
athos@1473
   271
void aTest(LpSolverBase & lp)
athos@1473
   272
{
athos@1473
   273
  typedef LpSolverBase LP;
athos@1473
   274
athos@1542
   275
 //The following example is very simple
athos@1473
   276
athos@1473
   277
  typedef LpSolverBase::Row Row;
athos@1473
   278
  typedef LpSolverBase::Col Col;
athos@1473
   279
athos@1473
   280
athos@1473
   281
  Col x1 = lp.addCol();
athos@1473
   282
  Col x2 = lp.addCol();
athos@1473
   283
athos@1473
   284
athos@1473
   285
  //Constraints
athos@1542
   286
  Row upright=lp.addRow(x1+x2 <=1);  
athos@1542
   287
  lp.addRow(x1+x2 >=-1);  
athos@1542
   288
  lp.addRow(x1-x2 <=1);  
athos@1542
   289
  lp.addRow(x1-x2 >=-1);  
athos@1473
   290
  //Nonnegativity of the variables
athos@1473
   291
  lp.colLowerBound(x1, 0);
athos@1473
   292
  lp.colLowerBound(x2, 0);
athos@1473
   293
  //Objective function
deba@2369
   294
  lp.obj(x1+x2);
athos@1473
   295
athos@1473
   296
  lp.max();
athos@1473
   297
athos@2324
   298
athos@2324
   299
  //Testing the problem retrieving routines
athos@2324
   300
  check(lp.objCoeff(x1)==1,"First term should be 1 in the obj function!");
deba@2369
   301
  check(lp.isMax(),"This is a maximization!");
athos@2328
   302
  check(lp.coeff(upright,x1)==1,"The coefficient in question is 1!");
athos@2328
   303
  //  std::cout<<lp.colLowerBound(x1)<<std::endl;
athos@2328
   304
  check(  lp.colLowerBound(x1)==0,"The lower bound for variable x1 should be 0.");
athos@2328
   305
  check(  lp.colUpperBound(x1)==LpSolverBase::INF,"The upper bound for variable x1 should be infty.");
athos@2328
   306
  LpSolverBase::Value lb,ub;
athos@2328
   307
  lp.getRowBounds(upright,lb,ub);
athos@2328
   308
  check(  lb==-LpSolverBase::INF,"The lower bound for the first row should be -infty.");
athos@2328
   309
  check(  ub==1,"The upper bound for the first row should be 1.");
athos@2324
   310
athos@2324
   311
athos@1542
   312
  //Maximization of x1+x2
athos@1542
   313
  //over the triangle with vertices (0,0) (0,1) (1,0)
athos@1542
   314
  double expected_opt=1;
athos@1542
   315
  solveAndCheck(lp, LpSolverBase::OPTIMAL, expected_opt);
alpar@1484
   316
  
athos@1542
   317
  //Minimization
athos@1542
   318
  lp.min();
athos@1542
   319
  expected_opt=0;
athos@1542
   320
  solveAndCheck(lp, LpSolverBase::OPTIMAL, expected_opt);
athos@1542
   321
  
athos@1542
   322
  //Vertex (-1,0) instead of (0,0)
athos@1542
   323
  lp.colLowerBound(x1, -LpSolverBase::INF);
athos@1542
   324
  expected_opt=-1;
athos@1542
   325
  solveAndCheck(lp, LpSolverBase::OPTIMAL, expected_opt);
athos@1542
   326
athos@1542
   327
  //Erase one constraint and return to maximization
athos@1542
   328
  lp.eraseRow(upright);
athos@1542
   329
  lp.max();
athos@1542
   330
  expected_opt=LpSolverBase::INF;
athos@1542
   331
  solveAndCheck(lp, LpSolverBase::INFINITE, expected_opt);
athos@1542
   332
athos@1542
   333
  //Infeasibilty
athos@1542
   334
  lp.addRow(x1+x2 <=-2);  
athos@1542
   335
  solveAndCheck(lp, LpSolverBase::INFEASIBLE, expected_opt);
athos@1542
   336
athos@1542
   337
  //Change problem and forget to solve
athos@1542
   338
  lp.min();
athos@1542
   339
  check(lp.primalStatus()==LpSolverBase::UNDEFINED,"Primalstatus should be UNDEFINED");
athos@1542
   340
athos@1542
   341
//   lp.solve();
athos@1542
   342
//   if (lp.primalStatus()==LpSolverBase::OPTIMAL){
athos@1542
   343
//     std::cout<< "Z = "<<lp.primalValue()
athos@1542
   344
// 	     << " (error = " << lp.primalValue()-expected_opt
athos@1542
   345
// 	     << "); x1 = "<<lp.primal(x1)
athos@1542
   346
// 	     << "; x2 = "<<lp.primal(x2)
athos@1542
   347
// 	     <<std::endl;
alpar@1484
   348
    
athos@1542
   349
//   }
athos@1542
   350
//   else{
athos@1542
   351
//     std::cout<<lp.primalStatus()<<std::endl;
athos@1542
   352
//     std::cout<<"Optimal solution not found!"<<std::endl;
athos@1542
   353
//   }
athos@1473
   354
athos@1542
   355
 
athos@1473
   356
athos@1473
   357
}
athos@1473
   358
athos@1473
   359
alpar@1263
   360
int main() 
alpar@1263
   361
{
alpar@1390
   362
  LpSkeleton lp_skel;
alpar@1390
   363
  lpTest(lp_skel);
alpar@1390
   364
ladanyi@1437
   365
#ifdef HAVE_GLPK
alpar@1484
   366
  LpGlpk lp_glpk1,lp_glpk2;
alpar@1484
   367
  lpTest(lp_glpk1);
alpar@1484
   368
  aTest(lp_glpk2);
ladanyi@1437
   369
#endif
alpar@1263
   370
ladanyi@1437
   371
#ifdef HAVE_CPLEX
klao@1797
   372
  LpCplex lp_cplex1,lp_cplex2;
klao@1797
   373
  lpTest(lp_cplex1);
klao@1797
   374
  aTest(lp_cplex2);
ladanyi@1437
   375
#endif
alpar@1264
   376
deba@2363
   377
#ifdef HAVE_SOPLEX
deba@2363
   378
  LpSoplex lp_soplex1,lp_soplex2;
deba@2363
   379
  lpTest(lp_soplex1);
deba@2363
   380
  aTest(lp_soplex2);
deba@2363
   381
#endif
deba@2314
   382
alpar@1309
   383
  return 0;
alpar@1263
   384
}