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