test/lp_test.cc
author marci
Sun, 13 Nov 2005 20:01:17 +0000
changeset 1787 932b8490caf0
parent 1542 0219ee65ffcc
child 1797 91b8b9cea2f7
permissions -rw-r--r--
bugfix in setCol, getting dual values
     1 #include <sstream>
     2 #include <lemon/lp_skeleton.h>
     3 #include "test_tools.h"
     4 
     5 
     6 #ifdef HAVE_CONFIG_H
     7 #include <config.h>
     8 #endif
     9 
    10 #ifdef HAVE_GLPK
    11 #include <lemon/lp_glpk.h>
    12 #endif
    13 
    14 #ifdef HAVE_CPLEX
    15 #include <lemon/lp_cplex.h>
    16 #endif
    17 
    18 using namespace lemon;
    19 
    20 void lpTest(LpSolverBase & lp)
    21 {
    22 
    23 
    24 
    25   typedef LpSolverBase LP;
    26 
    27   std::vector<LP::Col> x(10);
    28   //  for(int i=0;i<10;i++) x.push_back(lp.addCol());
    29   lp.addColSet(x);
    30 
    31 #ifndef GYORSITAS
    32 
    33   std::vector<LP::Col> y(10);
    34   lp.addColSet(y);
    35 
    36   std::map<int,LP::Col> z;
    37   
    38   z.insert(std::make_pair(12,INVALID));
    39   z.insert(std::make_pair(2,INVALID));
    40   z.insert(std::make_pair(7,INVALID));
    41   z.insert(std::make_pair(5,INVALID));
    42   
    43   lp.addColSet(z);
    44 
    45   {
    46     LP::Expr e,f,g;
    47     LP::Col p1,p2,p3,p4,p5;
    48     LP::Constr c;
    49     
    50     p1=lp.addCol();
    51     p2=lp.addCol();
    52     p3=lp.addCol();
    53     p4=lp.addCol();
    54     p5=lp.addCol();
    55     
    56     e[p1]=2;
    57     e.constComp()=12;
    58     e[p1]+=2;
    59     e.constComp()+=12;
    60     e[p1]-=2;
    61     e.constComp()-=12;
    62     
    63     e=2;
    64     e=2.2;
    65     e=p1;
    66     e=f;
    67     
    68     e+=2;
    69     e+=2.2;
    70     e+=p1;
    71     e+=f;
    72     
    73     e-=2;
    74     e-=2.2;
    75     e-=p1;
    76     e-=f;
    77     
    78     e*=2;
    79     e*=2.2;
    80     e/=2;
    81     e/=2.2;
    82     
    83     e=((p1+p2)+(p1-p2)+(p1+12)+(12+p1)+(p1-12)+(12-p1)+
    84        (f+12)+(12+f)+(p1+f)+(f+p1)+(f+g)+
    85        (f-12)+(12-f)+(p1-f)+(f-p1)+(f-g)+
    86        2.2*f+f*2.2+f/2.2+
    87        2*f+f*2+f/2+
    88        2.2*p1+p1*2.2+p1/2.2+
    89        2*p1+p1*2+p1/2
    90        );
    91 
    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 = (e  == f  );
   120     c = (e  == 2.2);
   121     c = (e  == 2  );
   122     c = (e  == p1 );
   123     c = (2.2== f  );
   124     c = (2  == f  );
   125     c = (p1 == f  );
   126     //c = (p1 == p2 );
   127     c = (p1 == 2.2);
   128     c = (p1 == 2  );
   129     c = (2.2== p2 );
   130     c = (2  == p2 );
   131     
   132     c = (2 <= e <= 3);
   133     c = (2 <= p1<= 3);
   134     
   135     c = (2 >= e >= 3);
   136     c = (2 >= p1>= 3);
   137     
   138     e[x[3]]=2;
   139     e[x[3]]=4;
   140     e[x[3]]=1;
   141     e.constComp()=12;
   142     
   143     lp.addRow(LP::INF,e,23);
   144     lp.addRow(LP::INF,3.0*(x[1]+x[2]/2)-x[3],23);
   145     lp.addRow(LP::INF,3.0*(x[1]+x[2]*2-5*x[3]+12-x[4]/3)+2*x[4]-4,23);
   146     
   147     lp.addRow(x[1]+x[3]<=x[5]-3);
   148     lp.addRow(-7<=x[1]+x[3]-12<=3);
   149     lp.addRow(x[1]<=x[5]);
   150   }
   151   
   152   {
   153     LP::DualExpr e,f,g;
   154     LP::Row p1,p2,p3,p4,p5;
   155     
   156     e[p1]=2;
   157     e[p1]+=2;
   158     e[p1]-=2;
   159     
   160     e=p1;
   161     e=f;
   162     
   163     e+=p1;
   164     e+=f;
   165     
   166     e-=p1;
   167     e-=f;
   168     
   169     e*=2;
   170     e*=2.2;
   171     e/=2;
   172     e/=2.2;
   173     
   174     e=((p1+p2)+(p1-p2)+
   175        (p1+f)+(f+p1)+(f+g)+
   176        (p1-f)+(f-p1)+(f-g)+
   177        2.2*f+f*2.2+f/2.2+
   178        2*f+f*2+f/2+
   179        2.2*p1+p1*2.2+p1/2.2+
   180        2*p1+p1*2+p1/2
   181        );
   182   }
   183   
   184 #endif
   185 }
   186 
   187 void solveAndCheck(LpSolverBase& lp, LpSolverBase::SolutionStatus stat, 
   188 		   double exp_opt) {
   189   using std::string;
   190   lp.solve();
   191   //int decimal,sign;
   192   std::ostringstream buf;
   193   buf << "Primalstatus should be: " << int(stat);
   194 
   195   //  itoa(stat,buf1, 10);
   196   check(lp.primalStatus()==stat, buf.str());
   197 
   198   if (stat ==  LpSolverBase::OPTIMAL) {
   199     std::ostringstream buf;
   200     buf << "Wrong optimal value: the right optimum is " << exp_opt; 
   201     check(std::abs(lp.primalValue()-exp_opt) < 1e-3, buf.str());
   202     //+ecvt(exp_opt,2)
   203   }
   204 }
   205  
   206 void aTest(LpSolverBase & lp)
   207 {
   208   typedef LpSolverBase LP;
   209 
   210  //The following example is very simple
   211 
   212   typedef LpSolverBase::Row Row;
   213   typedef LpSolverBase::Col Col;
   214 
   215 
   216   Col x1 = lp.addCol();
   217   Col x2 = lp.addCol();
   218 
   219 
   220   //Constraints
   221   Row upright=lp.addRow(x1+x2 <=1);  
   222   lp.addRow(x1+x2 >=-1);  
   223   lp.addRow(x1-x2 <=1);  
   224   lp.addRow(x1-x2 >=-1);  
   225   //Nonnegativity of the variables
   226   lp.colLowerBound(x1, 0);
   227   lp.colLowerBound(x2, 0);
   228   //Objective function
   229   lp.setObj(x1+x2);
   230 
   231   lp.max();
   232 
   233   //Maximization of x1+x2
   234   //over the triangle with vertices (0,0) (0,1) (1,0)
   235   double expected_opt=1;
   236   solveAndCheck(lp, LpSolverBase::OPTIMAL, expected_opt);
   237   
   238   //Minimization
   239   lp.min();
   240   expected_opt=0;
   241   solveAndCheck(lp, LpSolverBase::OPTIMAL, expected_opt);
   242   
   243   //Vertex (-1,0) instead of (0,0)
   244   lp.colLowerBound(x1, -LpSolverBase::INF);
   245   expected_opt=-1;
   246   solveAndCheck(lp, LpSolverBase::OPTIMAL, expected_opt);
   247 
   248   //Erase one constraint and return to maximization
   249   lp.eraseRow(upright);
   250   lp.max();
   251   expected_opt=LpSolverBase::INF;
   252   solveAndCheck(lp, LpSolverBase::INFINITE, expected_opt);
   253 
   254   //Infeasibilty
   255   lp.addRow(x1+x2 <=-2);  
   256   solveAndCheck(lp, LpSolverBase::INFEASIBLE, expected_opt);
   257 
   258   //Change problem and forget to solve
   259   lp.min();
   260   check(lp.primalStatus()==LpSolverBase::UNDEFINED,"Primalstatus should be UNDEFINED");
   261 
   262 //   lp.solve();
   263 //   if (lp.primalStatus()==LpSolverBase::OPTIMAL){
   264 //     std::cout<< "Z = "<<lp.primalValue()
   265 // 	     << " (error = " << lp.primalValue()-expected_opt
   266 // 	     << "); x1 = "<<lp.primal(x1)
   267 // 	     << "; x2 = "<<lp.primal(x2)
   268 // 	     <<std::endl;
   269     
   270 //   }
   271 //   else{
   272 //     std::cout<<lp.primalStatus()<<std::endl;
   273 //     std::cout<<"Optimal solution not found!"<<std::endl;
   274 //   }
   275 
   276  
   277 
   278 }
   279 
   280 
   281 int main() 
   282 {
   283   LpSkeleton lp_skel;
   284   lpTest(lp_skel);
   285 
   286 #ifdef HAVE_GLPK
   287   LpGlpk lp_glpk1,lp_glpk2;
   288   lpTest(lp_glpk1);
   289   aTest(lp_glpk2);
   290 #endif
   291 
   292 #ifdef HAVE_CPLEX
   293   LpCplex lp_cplex;
   294   lpTest(lp_cplex);
   295   aTest(lp_cplex);
   296 #endif
   297 
   298   return 0;
   299 }