test/lp_test.cc
author deba
Fri, 14 Apr 2006 18:05:02 +0000
changeset 2050 d9a221218ea4
parent 1895 5b01801efbc0
child 2264 90c66dc93ca4
permissions -rw-r--r--
Changing the mining of the clear in heaps
It does not touch the heap cross ref. It is
sometimes more clean useable and more efficient
     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 
    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   
   180   {
   181     LP::DualExpr e,f,g;
   182     LP::Row p1,p2,p3,p4,p5;
   183     
   184     e[p1]=2;
   185     e[p1]+=2;
   186     e[p1]-=2;
   187     
   188     e=p1;
   189     e=f;
   190     
   191     e+=p1;
   192     e+=f;
   193     
   194     e-=p1;
   195     e-=f;
   196     
   197     e*=2;
   198     e*=2.2;
   199     e/=2;
   200     e/=2.2;
   201     
   202     e=((p1+p2)+(p1-p2)+
   203        (p1+f)+(f+p1)+(f+g)+
   204        (p1-f)+(f-p1)+(f-g)+
   205        2.2*f+f*2.2+f/2.2+
   206        2*f+f*2+f/2+
   207        2.2*p1+p1*2.2+p1/2.2+
   208        2*p1+p1*2+p1/2
   209        );
   210   }
   211   
   212 #endif
   213 }
   214 
   215 void solveAndCheck(LpSolverBase& lp, LpSolverBase::SolutionStatus stat, 
   216 		   double exp_opt) {
   217   using std::string;
   218   lp.solve();
   219   //int decimal,sign;
   220   std::ostringstream buf;
   221   buf << "Primalstatus should be: " << int(stat);
   222 
   223   //  itoa(stat,buf1, 10);
   224   check(lp.primalStatus()==stat, buf.str());
   225 
   226   if (stat ==  LpSolverBase::OPTIMAL) {
   227     std::ostringstream buf;
   228     buf << "Wrong optimal value: the right optimum is " << exp_opt; 
   229     check(std::abs(lp.primalValue()-exp_opt) < 1e-3, buf.str());
   230     //+ecvt(exp_opt,2)
   231   }
   232 }
   233  
   234 void aTest(LpSolverBase & lp)
   235 {
   236   typedef LpSolverBase LP;
   237 
   238  //The following example is very simple
   239 
   240   typedef LpSolverBase::Row Row;
   241   typedef LpSolverBase::Col Col;
   242 
   243 
   244   Col x1 = lp.addCol();
   245   Col x2 = lp.addCol();
   246 
   247 
   248   //Constraints
   249   Row upright=lp.addRow(x1+x2 <=1);  
   250   lp.addRow(x1+x2 >=-1);  
   251   lp.addRow(x1-x2 <=1);  
   252   lp.addRow(x1-x2 >=-1);  
   253   //Nonnegativity of the variables
   254   lp.colLowerBound(x1, 0);
   255   lp.colLowerBound(x2, 0);
   256   //Objective function
   257   lp.setObj(x1+x2);
   258 
   259   lp.max();
   260 
   261   //Maximization of x1+x2
   262   //over the triangle with vertices (0,0) (0,1) (1,0)
   263   double expected_opt=1;
   264   solveAndCheck(lp, LpSolverBase::OPTIMAL, expected_opt);
   265   
   266   //Minimization
   267   lp.min();
   268   expected_opt=0;
   269   solveAndCheck(lp, LpSolverBase::OPTIMAL, expected_opt);
   270   
   271   //Vertex (-1,0) instead of (0,0)
   272   lp.colLowerBound(x1, -LpSolverBase::INF);
   273   expected_opt=-1;
   274   solveAndCheck(lp, LpSolverBase::OPTIMAL, expected_opt);
   275 
   276   //Erase one constraint and return to maximization
   277   lp.eraseRow(upright);
   278   lp.max();
   279   expected_opt=LpSolverBase::INF;
   280   solveAndCheck(lp, LpSolverBase::INFINITE, expected_opt);
   281 
   282   //Infeasibilty
   283   lp.addRow(x1+x2 <=-2);  
   284   solveAndCheck(lp, LpSolverBase::INFEASIBLE, expected_opt);
   285 
   286   //Change problem and forget to solve
   287   lp.min();
   288   check(lp.primalStatus()==LpSolverBase::UNDEFINED,"Primalstatus should be UNDEFINED");
   289 
   290 //   lp.solve();
   291 //   if (lp.primalStatus()==LpSolverBase::OPTIMAL){
   292 //     std::cout<< "Z = "<<lp.primalValue()
   293 // 	     << " (error = " << lp.primalValue()-expected_opt
   294 // 	     << "); x1 = "<<lp.primal(x1)
   295 // 	     << "; x2 = "<<lp.primal(x2)
   296 // 	     <<std::endl;
   297     
   298 //   }
   299 //   else{
   300 //     std::cout<<lp.primalStatus()<<std::endl;
   301 //     std::cout<<"Optimal solution not found!"<<std::endl;
   302 //   }
   303 
   304  
   305 
   306 }
   307 
   308 
   309 int main() 
   310 {
   311   LpSkeleton lp_skel;
   312   lpTest(lp_skel);
   313 
   314 #ifdef HAVE_GLPK
   315   LpGlpk lp_glpk1,lp_glpk2;
   316   lpTest(lp_glpk1);
   317   aTest(lp_glpk2);
   318 #endif
   319 
   320 #ifdef HAVE_CPLEX
   321   LpCplex lp_cplex1,lp_cplex2;
   322   lpTest(lp_cplex1);
   323   aTest(lp_cplex2);
   324 #endif
   325 
   326   return 0;
   327 }