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