test/lp_test.cc
author athos
Mon, 30 Oct 2006 16:26:13 +0000
changeset 2275 ff46747676ed
parent 1956 a055123339d5
child 2314 dbbd5c514163
permissions -rw-r--r--
Small bugs in the documentation 2.
     1 /* -*- C++ -*-
     2  *
     3  * This file is a part of LEMON, a generic C++ optimization library
     4  *
     5  * Copyright (C) 2003-2006
     6  * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
     7  * (Egervary Research Group on Combinatorial Optimization, EGRES).
     8  *
     9  * Permission to use, modify and distribute this software is granted
    10  * provided that this copyright notice appears in all copies. For
    11  * precise terms see the accompanying LICENSE file.
    12  *
    13  * This software is provided "AS IS" with no warranty of any kind,
    14  * express or implied, and with no claim as to its suitability for any
    15  * purpose.
    16  *
    17  */
    18 
    19 #include <sstream>
    20 #include <lemon/lp_skeleton.h>
    21 #include "test_tools.h"
    22 #include <lemon/tolerance.h>
    23 
    24 #ifdef HAVE_CONFIG_H
    25 #include <config.h>
    26 #endif
    27 
    28 #ifdef HAVE_GLPK
    29 #include <lemon/lp_glpk.h>
    30 #endif
    31 
    32 #ifdef HAVE_CPLEX
    33 #include <lemon/lp_cplex.h>
    34 #endif
    35 
    36 using namespace lemon;
    37 
    38 void lpTest(LpSolverBase & lp)
    39 {
    40 
    41 
    42 
    43   typedef LpSolverBase LP;
    44 
    45   std::vector<LP::Col> x(10);
    46   //  for(int i=0;i<10;i++) x.push_back(lp.addCol());
    47   lp.addColSet(x);
    48   lp.colLowerBound(x,1);
    49   lp.colUpperBound(x,1);
    50   lp.colBounds(x,1,2);
    51 #ifndef GYORSITAS
    52 
    53   std::vector<LP::Col> y(10);
    54   lp.addColSet(y);
    55 
    56   lp.colLowerBound(y,1);
    57   lp.colUpperBound(y,1);
    58   lp.colBounds(y,1,2);
    59 
    60   std::map<int,LP::Col> z;
    61   
    62   z.insert(std::make_pair(12,INVALID));
    63   z.insert(std::make_pair(2,INVALID));
    64   z.insert(std::make_pair(7,INVALID));
    65   z.insert(std::make_pair(5,INVALID));
    66 
    67   lp.addColSet(z);
    68 
    69   lp.colLowerBound(z,1);
    70   lp.colUpperBound(z,1);
    71   lp.colBounds(z,1,2);
    72 
    73   {
    74     LP::Expr e,f,g;
    75     LP::Col p1,p2,p3,p4,p5;
    76     LP::Constr c;
    77     
    78     p1=lp.addCol();
    79     p2=lp.addCol();
    80     p3=lp.addCol();
    81     p4=lp.addCol();
    82     p5=lp.addCol();
    83     
    84     e[p1]=2;
    85     e.constComp()=12;
    86     e[p1]+=2;
    87     e.constComp()+=12;
    88     e[p1]-=2;
    89     e.constComp()-=12;
    90     
    91     e=2;
    92     e=2.2;
    93     e=p1;
    94     e=f;
    95     
    96     e+=2;
    97     e+=2.2;
    98     e+=p1;
    99     e+=f;
   100     
   101     e-=2;
   102     e-=2.2;
   103     e-=p1;
   104     e-=f;
   105     
   106     e*=2;
   107     e*=2.2;
   108     e/=2;
   109     e/=2.2;
   110     
   111     e=((p1+p2)+(p1-p2)+(p1+12)+(12+p1)+(p1-12)+(12-p1)+
   112        (f+12)+(12+f)+(p1+f)+(f+p1)+(f+g)+
   113        (f-12)+(12-f)+(p1-f)+(f-p1)+(f-g)+
   114        2.2*f+f*2.2+f/2.2+
   115        2*f+f*2+f/2+
   116        2.2*p1+p1*2.2+p1/2.2+
   117        2*p1+p1*2+p1/2
   118        );
   119 
   120 
   121     c = (e  <= f  );
   122     c = (e  <= 2.2);
   123     c = (e  <= 2  );
   124     c = (e  <= p1 );
   125     c = (2.2<= f  );
   126     c = (2  <= f  );
   127     c = (p1 <= f  );
   128     c = (p1 <= p2 );
   129     c = (p1 <= 2.2);
   130     c = (p1 <= 2  );
   131     c = (2.2<= p2 );
   132     c = (2  <= p2 );
   133     
   134     c = (e  >= f  );
   135     c = (e  >= 2.2);
   136     c = (e  >= 2  );
   137     c = (e  >= p1 );
   138     c = (2.2>= f  );
   139     c = (2  >= f  );
   140     c = (p1 >= f  );
   141     c = (p1 >= p2 );
   142     c = (p1 >= 2.2);
   143     c = (p1 >= 2  );
   144     c = (2.2>= p2 );
   145     c = (2  >= p2 );
   146     
   147     c = (e  == f  );
   148     c = (e  == 2.2);
   149     c = (e  == 2  );
   150     c = (e  == p1 );
   151     c = (2.2== f  );
   152     c = (2  == f  );
   153     c = (p1 == f  );
   154     //c = (p1 == p2 );
   155     c = (p1 == 2.2);
   156     c = (p1 == 2  );
   157     c = (2.2== p2 );
   158     c = (2  == p2 );
   159     
   160     c = (2 <= e <= 3);
   161     c = (2 <= p1<= 3);
   162     
   163     c = (2 >= e >= 3);
   164     c = (2 >= p1>= 3);
   165     
   166     e[x[3]]=2;
   167     e[x[3]]=4;
   168     e[x[3]]=1;
   169     e.constComp()=12;
   170     
   171     lp.addRow(LP::INF,e,23);
   172     lp.addRow(LP::INF,3.0*(x[1]+x[2]/2)-x[3],23);
   173     lp.addRow(LP::INF,3.0*(x[1]+x[2]*2-5*x[3]+12-x[4]/3)+2*x[4]-4,23);
   174     
   175     lp.addRow(x[1]+x[3]<=x[5]-3);
   176     lp.addRow(-7<=x[1]+x[3]-12<=3);
   177     lp.addRow(x[1]<=x[5]);
   178 
   179     std::ostringstream buf;
   180 
   181 
   182     //Checking the simplify function
   183 
   184 //     //How to check the simplify function? A map gives no information
   185 //     //on the question whether a given key is or is not stored in it, or
   186 //     //it does?
   187 //   Yes, it does, using the find() function.
   188     e=((p1+p2)+(p1-p2));
   189     e.simplify();
   190     buf << "Coeff. of p2 should be 0";
   191     //    std::cout<<e[p1]<<e[p2]<<e[p3]<<std::endl;
   192     check(e.find(p2)==e.end(), buf.str());
   193 
   194      
   195 
   196 
   197     e=((p1+p2)+(p1-0.99*p2));
   198     double tolerance=0.001;
   199     e.simplify(tolerance);
   200     buf << "Coeff. of p2 should be 0.01";
   201     check(e[p2]>0, buf.str());
   202     
   203     tolerance=0.02;
   204     e.simplify(tolerance);
   205     buf << "Coeff. of p2 should be 0";
   206     check(e.find(p2)==e.end(), buf.str());
   207     
   208 
   209   }
   210   
   211   {
   212     LP::DualExpr e,f,g;
   213     LP::Row p1,p2,p3,p4,p5;
   214     
   215     e[p1]=2;
   216     e[p1]+=2;
   217     e[p1]-=2;
   218     
   219     e=p1;
   220     e=f;
   221     
   222     e+=p1;
   223     e+=f;
   224     
   225     e-=p1;
   226     e-=f;
   227     
   228     e*=2;
   229     e*=2.2;
   230     e/=2;
   231     e/=2.2;
   232     
   233     e=((p1+p2)+(p1-p2)+
   234        (p1+f)+(f+p1)+(f+g)+
   235        (p1-f)+(f-p1)+(f-g)+
   236        2.2*f+f*2.2+f/2.2+
   237        2*f+f*2+f/2+
   238        2.2*p1+p1*2.2+p1/2.2+
   239        2*p1+p1*2+p1/2
   240        );
   241   }
   242   
   243 #endif
   244 }
   245 
   246 void solveAndCheck(LpSolverBase& lp, LpSolverBase::SolutionStatus stat, 
   247 		   double exp_opt) {
   248   using std::string;
   249   lp.solve();
   250   //int decimal,sign;
   251   std::ostringstream buf;
   252   buf << "Primalstatus should be: " << int(stat);
   253 
   254   //  itoa(stat,buf1, 10);
   255   check(lp.primalStatus()==stat, buf.str());
   256 
   257   if (stat ==  LpSolverBase::OPTIMAL) {
   258     std::ostringstream buf;
   259     buf << "Wrong optimal value: the right optimum is " << exp_opt; 
   260     check(std::abs(lp.primalValue()-exp_opt) < 1e-3, buf.str());
   261     //+ecvt(exp_opt,2)
   262   }
   263 }
   264  
   265 void aTest(LpSolverBase & lp)
   266 {
   267   typedef LpSolverBase LP;
   268 
   269  //The following example is very simple
   270 
   271   typedef LpSolverBase::Row Row;
   272   typedef LpSolverBase::Col Col;
   273 
   274 
   275   Col x1 = lp.addCol();
   276   Col x2 = lp.addCol();
   277 
   278 
   279   //Constraints
   280   Row upright=lp.addRow(x1+x2 <=1);  
   281   lp.addRow(x1+x2 >=-1);  
   282   lp.addRow(x1-x2 <=1);  
   283   lp.addRow(x1-x2 >=-1);  
   284   //Nonnegativity of the variables
   285   lp.colLowerBound(x1, 0);
   286   lp.colLowerBound(x2, 0);
   287   //Objective function
   288   lp.setObj(x1+x2);
   289 
   290   lp.max();
   291 
   292   //Maximization of x1+x2
   293   //over the triangle with vertices (0,0) (0,1) (1,0)
   294   double expected_opt=1;
   295   solveAndCheck(lp, LpSolverBase::OPTIMAL, expected_opt);
   296   
   297   //Minimization
   298   lp.min();
   299   expected_opt=0;
   300   solveAndCheck(lp, LpSolverBase::OPTIMAL, expected_opt);
   301   
   302   //Vertex (-1,0) instead of (0,0)
   303   lp.colLowerBound(x1, -LpSolverBase::INF);
   304   expected_opt=-1;
   305   solveAndCheck(lp, LpSolverBase::OPTIMAL, expected_opt);
   306 
   307   //Erase one constraint and return to maximization
   308   lp.eraseRow(upright);
   309   lp.max();
   310   expected_opt=LpSolverBase::INF;
   311   solveAndCheck(lp, LpSolverBase::INFINITE, expected_opt);
   312 
   313   //Infeasibilty
   314   lp.addRow(x1+x2 <=-2);  
   315   solveAndCheck(lp, LpSolverBase::INFEASIBLE, expected_opt);
   316 
   317   //Change problem and forget to solve
   318   lp.min();
   319   check(lp.primalStatus()==LpSolverBase::UNDEFINED,"Primalstatus should be UNDEFINED");
   320 
   321 //   lp.solve();
   322 //   if (lp.primalStatus()==LpSolverBase::OPTIMAL){
   323 //     std::cout<< "Z = "<<lp.primalValue()
   324 // 	     << " (error = " << lp.primalValue()-expected_opt
   325 // 	     << "); x1 = "<<lp.primal(x1)
   326 // 	     << "; x2 = "<<lp.primal(x2)
   327 // 	     <<std::endl;
   328     
   329 //   }
   330 //   else{
   331 //     std::cout<<lp.primalStatus()<<std::endl;
   332 //     std::cout<<"Optimal solution not found!"<<std::endl;
   333 //   }
   334 
   335  
   336 
   337 }
   338 
   339 
   340 int main() 
   341 {
   342   LpSkeleton lp_skel;
   343   lpTest(lp_skel);
   344 
   345 #ifdef HAVE_GLPK
   346   LpGlpk lp_glpk1,lp_glpk2;
   347   lpTest(lp_glpk1);
   348   aTest(lp_glpk2);
   349 #endif
   350 
   351 #ifdef HAVE_CPLEX
   352   LpCplex lp_cplex1,lp_cplex2;
   353   lpTest(lp_cplex1);
   354   aTest(lp_cplex2);
   355 #endif
   356 
   357   return 0;
   358 }