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